Skip to content

Commit

Permalink
Pass TokenSwap state into the constructor to simplify function calls (s…
Browse files Browse the repository at this point in the history
…olana-labs#586)

* Pass TokenSwap state into the constructor to simplify function calls

* WIP: Update program to return token_program_id

* Add tokenProgramId to js TokenSwapInfo object

* Address pr comments

* Remove TokenSwapInfo and use loadTokenSwap as the primary way to fetch TokenSwap metadata

* Fix module.d.ts

* Address pr comments

* Clarification on comments

Co-authored-by: Yutaro Mori <[email protected]>
  • Loading branch information
Yutaro Mori and Yutaro Mori authored Oct 14, 2020
1 parent b45e3bf commit 5f56635
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 255 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 16 additions & 24 deletions token-swap/js/cli/token-swap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,31 @@ export async function createTokenSwap(): Promise<void> {
swapPayer,
tokenSwapAccount,
authority,
nonce,
tokenAccountA,
tokenAccountB,
tokenPool.publicKey,
tokenAccountPool,
tokenSwapProgramId,
tokenProgramId,
nonce,
1,
4,
);

console.log('loading token swap');
const fetchedTokenSwap = await TokenSwap.loadTokenSwap(
connection,
tokenSwapAccount.publicKey,
tokenSwapProgramId,
swapPayer,
);

console.log('getting token swap');
const swapInfo = await tokenSwap.getInfo();
assert(swapInfo.tokenAccountA.equals(tokenAccountA));
assert(swapInfo.tokenAccountB.equals(tokenAccountB));
assert(swapInfo.tokenPool.equals(tokenPool.publicKey));
assert(1 == swapInfo.feesNumerator.toNumber());
assert(4 == swapInfo.feesDenominator.toNumber());
assert(fetchedTokenSwap.tokenProgramId.equals(tokenProgramId));
assert(fetchedTokenSwap.tokenAccountA.equals(tokenAccountA));
assert(fetchedTokenSwap.tokenAccountB.equals(tokenAccountB));
assert(fetchedTokenSwap.poolToken.equals(tokenPool.publicKey));
assert(1 == fetchedTokenSwap.feeNumerator.toNumber());
assert(4 == fetchedTokenSwap.feeDenominator.toNumber());
}

export async function deposit(): Promise<void> {
Expand All @@ -232,18 +239,12 @@ export async function deposit(): Promise<void> {
await mintB.approve(userAccountB, authority, owner, [], tokenB);
console.log('Creating depositor pool token account');
const newAccountPool = await tokenPool.createAccount(owner.publicKey);
const [tokenProgramId] = await GetPrograms(connection);

console.log('Depositing into swap');
await tokenSwap.deposit(
authority,
userAccountA,
userAccountB,
tokenAccountA,
tokenAccountB,
tokenPool.publicKey,
newAccountPool,
tokenProgramId,
POOL_TOKEN_AMOUNT,
tokenA,
tokenB,
Expand Down Expand Up @@ -283,18 +284,12 @@ export async function withdraw(): Promise<void> {
[],
POOL_TOKEN_AMOUNT,
);
const [tokenProgramId] = await GetPrograms(connection);

console.log('Withdrawing pool tokens for A and B tokens');
await tokenSwap.withdraw(
authority,
tokenPool.publicKey,
tokenAccountPool,
tokenAccountA,
tokenAccountB,
userAccountA,
userAccountB,
tokenProgramId,
tokenAccountPool,
POOL_TOKEN_AMOUNT,
tokenA,
tokenB,
Expand Down Expand Up @@ -323,16 +318,13 @@ export async function swap(): Promise<void> {
await mintA.approve(userAccountA, authority, owner, [], SWAP_AMOUNT_IN);
console.log('Creating swap token b account');
let userAccountB = await mintB.createAccount(owner.publicKey);
const [tokenProgramId] = await GetPrograms(connection);

console.log('Swapping');
await tokenSwap.swap(
authority,
userAccountA,
tokenAccountA,
tokenAccountB,
userAccountB,
tokenProgramId,
SWAP_AMOUNT_IN,
SWAP_AMOUNT_OUT,
);
Expand Down
Loading

0 comments on commit 5f56635

Please sign in to comment.