Skip to content

Commit

Permalink
admin: More logging and test set_fee_account (#107)
Browse files Browse the repository at this point in the history
* Add more loggin to admin instructions:

* Test AdminSetFeeAccount

* Clean up

* Admin: Admin fee B account set to -> Admin: Fee account B set to

* Clean up
  • Loading branch information
michaelhly authored Jan 2, 2022
1 parent 1efe2e2 commit 91c6203
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
15 changes: 12 additions & 3 deletions stable-swap-program/program/src/processor/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ fn ramp_a(token_swap: &mut SwapInfo, target_amp: u64, stop_ramp_ts: i64) -> Prog
return Err(SwapError::InvalidInput.into());
}

msg!("Admin: Current A {}", current_amp);
token_swap.initial_amp_factor = current_amp;
token_swap.target_amp_factor = target_amp;
token_swap.start_ramp_ts = clock.unix_timestamp;
Expand Down Expand Up @@ -183,16 +184,22 @@ fn set_fee_account<'a, 'b: 'a, I: Iterator<Item = &'a AccountInfo<'b>>>(

let new_admin_fee_account =
utils::unpack_token_account(&new_fee_account_info.data.borrow_mut())?;
msg!(
"Admin: New fee account owner {}",
new_admin_fee_account.owner
);
if new_admin_fee_account.mint == token_swap.token_a.mint {
msg!("Admin: Old fee account A {}", token_swap.token_a.admin_fees);
token_swap.token_a.admin_fees = *new_fee_account_info.key;
msg!(
"Admin: Setting admin fee A account to {}",
"Admin: Fee account A set to {}",
token_swap.token_a.admin_fees
);
} else if new_admin_fee_account.mint == token_swap.token_b.mint {
msg!("Admin: Old fee account B {}", token_swap.token_b.admin_fees);
token_swap.token_b.admin_fees = *new_fee_account_info.key;
msg!(
"Admin: Setting admin fee B account to {}",
"Admin: Fee account B set to {}",
token_swap.token_b.admin_fees
);
} else {
Expand All @@ -212,6 +219,7 @@ fn apply_new_admin(token_swap: &mut SwapInfo) -> ProgramResult {
return Err(SwapError::AdminDeadlineExceeded.into());
}

msg!("Admin: old admin {}", token_swap.admin_key);
token_swap.admin_key = token_swap.future_admin_key;
token_swap.future_admin_key = Pubkey::default();
token_swap.future_admin_deadline = ZERO_TS;
Expand Down Expand Up @@ -246,8 +254,9 @@ fn commit_new_admin<'a, 'b: 'a, I: Iterator<Item = &'a AccountInfo<'b>>>(

/// Set new fees
fn set_new_fees(token_swap: &mut SwapInfo, new_fees: &Fees) -> ProgramResult {
msg!("Admin: Old fees {:?}", token_swap.fees);
token_swap.fees = *new_fees;
msg!("Admin: New fees set");
msg!("Admin: New fees {:?}", token_swap.fees);
Ok(())
}

Expand Down
46 changes: 43 additions & 3 deletions stable-swap-program/sdk/test/admin.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { SignerWallet } from "@saberhq/solana-contrib";
import type { Provider } from "@saberhq/solana-contrib";
import { SignerWallet, TransactionEnvelope } from "@saberhq/solana-contrib";
import {
createAdminApplyNewAdminInstruction,
createAdminCommitNewAdminInstruction,
createAdminSetFeeAccountInstruction,
deployNewSwap,
StableSwap,
SWAP_PROGRAM_ID,
ZERO_TS,
} from "@saberhq/stableswap-sdk";
import { u64 } from "@saberhq/token-utils";
import type { Signer } from "@solana/web3.js";
import { getOrCreateATA, u64 } from "@saberhq/token-utils";
import type { Signer, TransactionInstruction } from "@solana/web3.js";
import {
Connection,
Keypair,
Expand Down Expand Up @@ -77,6 +79,44 @@ describe("admin test", () => {
stableSwap = newSwap;
}, BOOTSTRAP_TIMEOUT);

it("Set fee account", async () => {
const fetchedStableSwap = await StableSwap.load(
connection,
stableSwapAccount.publicKey,
stableSwapProgramId
);

const provider = new SignerWallet(owner).createProvider(connection);
const tokenOwner = Keypair.generate();
const { address: expectedFeeAccount, instruction } = await getOrCreateATA({
provider,
mint: fetchedStableSwap.state.tokenA.mint,
owner: tokenOwner.publicKey,
});

const instructions: TransactionInstruction[] = [];
if (instruction) {
instructions.push(instruction);
}
instructions.push(
createAdminSetFeeAccountInstruction({
config: fetchedStableSwap.config,
state: fetchedStableSwap.state,
tokenAccount: expectedFeeAccount,
})
);
const txEnv = new TransactionEnvelope(provider, instructions);
const pendingTx = await txEnv.send();
await pendingTx.wait();

const newSwap = await StableSwap.load(
connection,
stableSwap.config.swapAccount,
stableSwap.config.swapProgramID
);
expect(newSwap.state.tokenA.adminFeeAccount).toEqual(expectedFeeAccount);
});

it("Commit new admin", async () => {
const fetchedStableSwap = await StableSwap.load(
connection,
Expand Down

0 comments on commit 91c6203

Please sign in to comment.