Skip to content

Commit

Permalink
token-swap: Swap instruction fix and update libs for CI (solana-labs#408
Browse files Browse the repository at this point in the history
)

* token-swap: Fix swap instruction

* Update SDK libs and fix outstanding issues

* Run lint in token/js

* Disable CI token-swap js tests

* Update code for web3.js 0.75.0

* Update output -> destination verbiage for clarity / consistency
  • Loading branch information
joncinque authored Sep 10, 2020
1 parent 7141298 commit f5e0d79
Show file tree
Hide file tree
Showing 12 changed files with 380 additions and 140 deletions.
3 changes: 2 additions & 1 deletion ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ js_token_swap() {
# TODO: Restore flow
# time npm run flow || exit $?

# TODO reenable once all issues are fixed
# TODO re-enable after investigating CI issues
# https://github.com/solana-labs/solana-program-library/pull/408
# npm run cluster:localnet || exit $?
# npm run localnet:down
# npm run localnet:update || exit $?
Expand Down
8 changes: 3 additions & 5 deletions token-swap/js/cli/token-swap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export async function withdraw(): Promise<void> {
}

export async function swap(): Promise<void> {
console.log('Creating swap token a account');
let userAccountA = await mintA.createAccount(owner.publicKey);
await mintA.mintTo(userAccountA, owner, [], USER_AMOUNT);
await mintA.approve(
Expand All @@ -290,9 +291,11 @@ export async function swap(): Promise<void> {
[],
USER_AMOUNT,
);
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,
Expand All @@ -305,18 +308,13 @@ export async function swap(): Promise<void> {
await sleep(500);
let info;
info = await mintA.getAccountInfo(userAccountA);
console.log('userAccountA', info.amount.toNumber());
assert(info.amount.toNumber() == 0);
info = await mintA.getAccountInfo(tokenAccountA);
console.log('tokenAccountA', info.amount.toNumber());
assert(info.amount.toNumber() == BASE_AMOUNT + USER_AMOUNT);
info = await mintB.getAccountInfo(tokenAccountB);
console.log('tokenAccountB', info.amount.toNumber());
assert(info.amount.toNumber() == 931);
info = await mintB.getAccountInfo(userAccountB);
console.log('userAccountB', info.amount.toNumber());
assert(info.amount.toNumber() == 69);
info = await tokenPool.getAccountInfo(tokenAccountPool);
console.log('tokenAccountPool', info.amount.toNumber());
assert(info.amount.toNumber() == BASE_AMOUNT - USER_AMOUNT);
}
30 changes: 15 additions & 15 deletions token-swap/js/client/token-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,14 @@ export class TokenSwap {
const balanceNeeded = await TokenSwap.getMinBalanceRentForExemptTokenSwap(
connection,
);
transaction = SystemProgram.createAccount({
transaction = new Transaction();
transaction.add(SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: tokenSwapAccount.publicKey,
lamports: balanceNeeded,
space: TokenSwapLayout.span,
programId,
});
}));
await sendAndConfirmTransaction(
'createAccount',
connection,
Expand Down Expand Up @@ -292,17 +293,17 @@ export class TokenSwap {
*
* @param authority Authority
* @param source Source account
* @param into Base account to swap into, must be a source token
* @param from Base account to swap from, must be a destination token
* @param dest Destination token
* @param swap_source Base account to swap into, must be a source token
* @param swap_destination Base account to swap from, must be a destination token
* @param destination Destination token account
* @param tokenProgramId Token program id
* @param amount Amount to transfer from source account
*/
async swap(
authority: PublicKey,
source: PublicKey,
into: PublicKey,
from: PublicKey,
swap_source: PublicKey,
swap_destination: PublicKey,
destination: PublicKey,
tokenProgramId: PublicKey,
amount: number | Numberu64,
Expand All @@ -314,8 +315,8 @@ export class TokenSwap {
this.swapInstruction(
authority,
source,
into,
from,
swap_source,
swap_destination,
destination,
tokenProgramId,
amount,
Expand All @@ -324,11 +325,12 @@ export class TokenSwap {
this.payer,
);
}

swapInstruction(
authority: PublicKey,
source: PublicKey,
into: PublicKey,
from: PublicKey,
swap_source: PublicKey,
swap_destination: PublicKey,
destination: PublicKey,
tokenProgramId: PublicKey,
amount: number | Numberu64,
Expand All @@ -351,11 +353,10 @@ export class TokenSwap {
{pubkey: this.tokenSwap, isSigner: false, isWritable: false},
{pubkey: authority, isSigner: false, isWritable: false},
{pubkey: source, isSigner: false, isWritable: true},
{pubkey: into, isSigner: false, isWritable: true},
{pubkey: from, isSigner: false, isWritable: true},
{pubkey: swap_source, isSigner: false, isWritable: true},
{pubkey: swap_destination, isSigner: false, isWritable: true},
{pubkey: destination, isSigner: false, isWritable: true},
{pubkey: tokenProgramId, isSigner: false, isWritable: false},

];
return new TransactionInstruction({
keys,
Expand Down Expand Up @@ -443,7 +444,6 @@ export class TokenSwap {
{pubkey: poolToken, isSigner: false, isWritable: true},
{pubkey: poolAccount, isSigner: false, isWritable: true},
{pubkey: tokenProgramId, isSigner: false, isWritable: false},

];
return new TransactionInstruction({
keys,
Expand Down
2 changes: 1 addition & 1 deletion token-swap/js/client/util/send-and-confirm-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function sendAndConfirmTransaction(
...signers: Array<Account>
): Promise<TransactionSignature> {
return realSendAndConfirmTransaction(connection, transaction, signers, {
confirmations: 1,
skipPreflight: true,
commitment: 'recent',
});
}
57 changes: 44 additions & 13 deletions token-swap/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ pub enum SwapInstruction {
/// 0. `[]` Token-swap
/// 1. `[]` $authority
/// 2. `[writable]` token_(A|B) SOURCE Account, amount is transferable by $authority,
/// 4. `[writable]` token_(A|B) Base Account to swap INTO. Must be the SOURCE token.
/// 5. `[writable]` token_(A|B) Base Account to swap FROM. Must be the DEST token.
/// 6. `[writable]` token_(A|B) DEST Account assigned to USER as the owner.
/// 7. '[]` Token program id
/// userdata: SOURCE amount to transfer, output to DEST is based on the exchange rate
/// 3. `[writable]` token_(A|B) Base Account to swap INTO. Must be the SOURCE token.
/// 4. `[writable]` token_(A|B) Base Account to swap FROM. Must be the DESTINATION token.
/// 5. `[writable]` token_(A|B) DESTINATION Account assigned to USER as the owner.
/// 6. '[]` Token program id
/// userdata: SOURCE amount to transfer, output to DESTINATION is based on the exchange rate
Swap(u64),

/// Deposit some tokens into the pool. The output is a "pool" token representing ownership
Expand Down Expand Up @@ -160,7 +160,7 @@ pub fn initialize(
token_a_pubkey: &Pubkey,
token_b_pubkey: &Pubkey,
pool_pubkey: &Pubkey,
user_output_pubkey: &Pubkey,
destination_pubkey: &Pubkey,
nonce: u8,
fee: Fee,
) -> Result<Instruction, ProgramError> {
Expand All @@ -177,7 +177,7 @@ pub fn initialize(
AccountMeta::new(*token_a_pubkey, false),
AccountMeta::new(*token_b_pubkey, false),
AccountMeta::new(*pool_pubkey, false),
AccountMeta::new(*user_output_pubkey, false),
AccountMeta::new(*destination_pubkey, false),
AccountMeta::new(*token_program_id, false),
];

Expand All @@ -199,7 +199,7 @@ pub fn deposit(
swap_token_a_pubkey: &Pubkey,
swap_token_b_pubkey: &Pubkey,
pool_mint_pubkey: &Pubkey,
user_output_pubkey: &Pubkey,
destination_pubkey: &Pubkey,
amount: u64,
) -> Result<Instruction, ProgramError> {
let data = SwapInstruction::Deposit(amount).serialize()?;
Expand All @@ -212,7 +212,7 @@ pub fn deposit(
AccountMeta::new(*swap_token_a_pubkey, false),
AccountMeta::new(*swap_token_b_pubkey, false),
AccountMeta::new(*pool_mint_pubkey, false),
AccountMeta::new(*user_output_pubkey, false),
AccountMeta::new(*destination_pubkey, false),
AccountMeta::new(*token_program_id, false),
];

Expand All @@ -233,8 +233,8 @@ pub fn withdraw(
source_pubkey: &Pubkey,
swap_token_a_pubkey: &Pubkey,
swap_token_b_pubkey: &Pubkey,
output_token_a_pubkey: &Pubkey,
output_token_b_pubkey: &Pubkey,
destination_token_a_pubkey: &Pubkey,
destination_token_b_pubkey: &Pubkey,
amount: u64,
) -> Result<Instruction, ProgramError> {
let data = SwapInstruction::Withdraw(amount).serialize()?;
Expand All @@ -246,8 +246,39 @@ pub fn withdraw(
AccountMeta::new(*source_pubkey, false),
AccountMeta::new(*swap_token_a_pubkey, false),
AccountMeta::new(*swap_token_b_pubkey, false),
AccountMeta::new(*output_token_a_pubkey, false),
AccountMeta::new(*output_token_b_pubkey, false),
AccountMeta::new(*destination_token_a_pubkey, false),
AccountMeta::new(*destination_token_b_pubkey, false),
AccountMeta::new(*token_program_id, false),
];

Ok(Instruction {
program_id: *program_id,
accounts,
data,
})
}

/// Creates a 'swap' instruction.
pub fn swap(
program_id: &Pubkey,
token_program_id: &Pubkey,
swap_pubkey: &Pubkey,
authority_pubkey: &Pubkey,
source_pubkey: &Pubkey,
swap_source_pubkey: &Pubkey,
swap_destination_pubkey: &Pubkey,
destination_pubkey: &Pubkey,
amount: u64,
) -> Result<Instruction, ProgramError> {
let data = SwapInstruction::Swap(amount).serialize()?;

let accounts = vec![
AccountMeta::new(*swap_pubkey, false),
AccountMeta::new(*authority_pubkey, false),
AccountMeta::new(*source_pubkey, false),
AccountMeta::new(*swap_source_pubkey, false),
AccountMeta::new(*swap_destination_pubkey, false),
AccountMeta::new(*destination_pubkey, false),
AccountMeta::new(*token_program_id, false),
];

Expand Down
Loading

0 comments on commit f5e0d79

Please sign in to comment.