Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External deposit program #1

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions programs/governance-registry/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct Registrar {
pub rates: [ExchangeRateEntry; 2],
// The decimals to use when converting deposits into a common currency.
pub rate_decimals: u8,
// Empty bytes for future upgrades.
pub padding: [u8; 1024],
}

impl Registrar {
Expand All @@ -35,6 +37,9 @@ impl Registrar {
/// The "common regsitrar currency" is the unit used to calculate voting
/// weight.
pub fn convert(&self, er: &ExchangeRateEntry, amount: u64) -> Result<u64> {
// TODO: Truncate the amount if the exchange rate decimals are greater
// than the common currency decimals.
//
require!(self.rate_decimals >= er.decimals, InvalidDecimals);
let decimal_diff = self.rate_decimals.checked_sub(er.decimals).unwrap();
let convert = amount
Expand Down Expand Up @@ -330,6 +335,16 @@ pub enum LockupKind {
Cliff,
}

/// Auth account. The existance of this account means the registrar authority
/// approved some action.
#[account]
pub struct Auth {
granted_ts: i64,
revoked_ts: i64,
revoked: bool,
bump: u8,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
24 changes: 24 additions & 0 deletions programs/governance-registry/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ pub struct CreateDeposit<'info> {
pub deposit: UpdateDeposit<'info>,
}

#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct CreateAuthExternal<'info> {
#[account(init, seeds = [], bump = bump)]
pub auth: Account<'info, Auth>,
#[account(executable)]
pub external_program: UncheckedAccount<'info>,
}

// TODO.
#[derive(Accounts)]
pub struct RevokeAuthExternal {}

// TODO: Initialize an "auth account" which is used to ensure this instruction
// can only be called once. If called more than once, the account
// will already exist and so it will fail to init the account.
// This will allow us to use accounts from external programs.
#[derive(Accounts)]
pub struct CreateDepositExternal {}

// TODO.
#[derive(Accounts)]
pub struct UpdateDepositExternal {}

#[derive(Accounts)]
pub struct UpdateDeposit<'info> {
pub registrar: AccountLoader<'info, Registrar>,
Expand Down
33 changes: 32 additions & 1 deletion programs/governance-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
/// exchange rate, which must be fixed.
///
/// Note that the above also implies that the `max_vote_weight` must fit into
/// a u64.
/// a u64. Assuming all tokens are using 6 decimals with a 10B supply,
/// that's ~1844 mints that can be used for voting.
#[program]
pub mod governance_registry {
use super::*;
Expand Down Expand Up @@ -177,6 +178,36 @@ pub mod governance_registry {
Ok(())
}

/// Creates an auth account which allows the associated program to be used
/// as a source of deposits.
pub fn create_auth_external(ctx: Context<CreateAuthExternal>, bump: u8) -> Result<()> {
let auth = &mut ctx.accounts.auth;
auth.bump = bump;
Ok(())
}

/// Revokes the auth account so that the associated program can no longer be
/// used as a source of deposits.
pub fn revoke_auth_external(ctx: Context<RevokeAuthExternal>) -> Result<()> {
// TODO.
Ok(())
}

/// Creates a new deposit entry using an external, trusted compensation
/// program, which holds and stores the locked tokens.
pub fn create_deposit_external(ctx: Context<CreateDepositExternal>) -> Result<()> {
// TODO.
Ok(())
}

/// Updates a deposit entry using an external, trusted compensation program.
/// This method should be called by a crank whenever an account in the
/// external compensation program changes.
pub fn update_deposit_external(ctx: Context<UpdateDepositExternal>) -> Result<()> {
// TODO.
Ok(())
}

/// Updates a deposit entry by depositing tokens into the registrar in
/// exchange for *frozen* voting tokens. These tokens are not used for
/// anything other than displaying the amount in wallets.
Expand Down