Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante committed Sep 16, 2021
1 parent 7ad3dc0 commit 3ab92db
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 43 deletions.
74 changes: 33 additions & 41 deletions tests/cfo/programs/cfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,14 @@ pub struct Distribute<'info> {
has_one = stake,
)]
officer: Account<'info, Officer>,
#[account(mut)]
treasury: Account<'info, TokenAccount>,
#[account(mut)]
stake: Account<'info, TokenAccount>,
#[account(mut)]
srm_vault: Account<'info, TokenAccount>,
mint: Account<'info, Mint>,
#[account(mut)]
srm_mint: Account<'info, Mint>,
token_program: Program<'info, Token>,
dex_program: Program<'info, Dex>,
}
Expand Down Expand Up @@ -688,7 +692,7 @@ pub struct Officer {
/// authority. This is used as an authorization token which allows one to
/// permissionlessly invoke the swap instructions on the market. Without this
/// one would be able to create their own market with prices unfavorable
/// to the smart contract.
/// to the smart contract (and subsequently swap on it).
///
/// Because a PDA is used here, the account existing (without a tombstone) is
/// proof of the validity of a given market, which means that anyone can use
Expand Down Expand Up @@ -804,15 +808,35 @@ impl<'info> From<&SwapToUsdc<'info>> for CpiContext<'_, '_, '_, 'info, swap::Swa
}
}

impl<'info> From<&Distribute<'info>> for CpiContext<'_, '_, '_, 'info, token::Burn<'info>> {
fn from(accs: &Distribute<'info>) -> Self {
let program = accs.token_program.to_account_info();
impl<'info> Distribute<'info> {
fn into_burn(&self) -> CpiContext<'_, '_, '_, 'info, token::Burn<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Burn {
mint: accs.mint.to_account_info(),
to: accs.srm_vault.to_account_info(),
authority: accs.officer.to_account_info(),
mint: self.srm_mint.to_account_info(),
to: self.srm_vault.to_account_info(),
authority: self.officer.to_account_info(),
};
CpiContext::new(program.to_account_info(), accounts)
CpiContext::new(program, accounts)
}

fn into_stake_transfer(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.srm_vault.to_account_info(),
to: self.stake.to_account_info(),
authority: self.officer.to_account_info(),
};
CpiContext::new(program, accounts)
}

fn into_treasury_transfer(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.srm_vault.to_account_info(),
to: self.treasury.to_account_info(),
authority: self.officer.to_account_info(),
};
CpiContext::new(program, accounts)
}
}

Expand Down Expand Up @@ -854,38 +878,6 @@ impl<'info> DropStakeReward<'info> {
}
}

impl<'info> Distribute<'info> {
fn into_burn(&self) -> CpiContext<'_, '_, '_, 'info, token::Burn<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Burn {
mint: self.mint.to_account_info(),
to: self.srm_vault.to_account_info(),
authority: self.officer.to_account_info(),
};
CpiContext::new(program.to_account_info(), accounts)
}

fn into_stake_transfer(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.srm_vault.to_account_info(),
to: self.stake.to_account_info(),
authority: self.officer.to_account_info(),
};
CpiContext::new(program.to_account_info(), accounts)
}

fn into_treasury_transfer(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.srm_vault.to_account_info(),
to: self.treasury.to_account_info(),
authority: self.officer.to_account_info(),
};
CpiContext::new(program.to_account_info(), accounts)
}
}

// Events.

#[event]
Expand Down
45 changes: 43 additions & 2 deletions tests/cfo/tests/cfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe("cfo", () => {
let marketAClient, marketBClient;
let marketAuth, marketAuthBump;
let marketAuthB, marketAuthBumpB;
let distribution;

// Accounts used to setup the orderbook.
let ORDERBOOK_ENV,
Expand Down Expand Up @@ -212,7 +213,7 @@ describe("cfo", () => {
});

it("Creates a CFO!", async () => {
let distribution = {
distribution = {
burn: 80,
stake: 20,
treasury: 0,
Expand Down Expand Up @@ -460,5 +461,45 @@ describe("cfo", () => {
assert.ok(usdcVaultAfter.amount.toNumber() === 530863);
});

it("Distributes the tokens to categories", async () => {});
it("Distributes the tokens to categories", async () => {
const srmVaultBefore = await SRM_TOKEN_CLIENT.getAccountInfo(srmVault);
const treasuryBefore = await SRM_TOKEN_CLIENT.getAccountInfo(treasury);
const stakeBefore = await SRM_TOKEN_CLIENT.getAccountInfo(stake);
const mintInfoBefore = await SRM_TOKEN_CLIENT.getMintInfo();

await program.rpc.distribute({
accounts: {
officer,
treasury,
stake,
srmVault,
srmMint: ORDERBOOK_ENV.mintA,
tokenProgram: TOKEN_PID,
dexProgram: DEX_PID,
},
});

const srmVaultAfter = await SRM_TOKEN_CLIENT.getAccountInfo(srmVault);
const treasuryAfter = await SRM_TOKEN_CLIENT.getAccountInfo(treasury);
const stakeAfter = await SRM_TOKEN_CLIENT.getAccountInfo(stake);
const mintInfoAfter = await SRM_TOKEN_CLIENT.getMintInfo();

const beforeAmount = 1152000000;
assert.ok(srmVaultBefore.amount.toNumber() === beforeAmount);
assert.ok(srmVaultAfter.amount.toNumber() === 0); // Fully distributed.
assert.ok(
stakeAfter.amount.toNumber() ===
beforeAmount * (distribution.stake / 100.0)
);
assert.ok(
treasuryAfter.amount.toNumber() ===
beforeAmount * (distribution.treasury / 100.0)
);
// Check burn amount.
assert.ok(mintInfoBefore.supply.toString() === "1000000000000000000");
assert.ok(
mintInfoBefore.supply.sub(mintInfoAfter.supply).toNumber() ===
beforeAmount * (distribution.burn / 100.0)
);
});
});

0 comments on commit 3ab92db

Please sign in to comment.