Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit af98c77

Browse files
authored
Expose Layouts and instruction creation functions to allow more flexible use of the Token and TokenSwap clients. (#517)
1 parent 4c93e07 commit af98c77

File tree

6 files changed

+107
-56
lines changed

6 files changed

+107
-56
lines changed

token-swap/js/client/token-swap.js

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@
55
import assert from 'assert';
66
import BN from 'bn.js';
77
import * as BufferLayout from 'buffer-layout';
8-
import {
9-
Account,
10-
PublicKey,
11-
SystemProgram,
12-
Transaction,
13-
TransactionInstruction,
14-
} from '@solana/web3.js';
15-
import type {Connection, TransactionSignature} from '@solana/web3.js';
8+
import type { Connection, TransactionSignature } from '@solana/web3.js';
9+
import { Account, PublicKey, SystemProgram, Transaction, TransactionInstruction, } from '@solana/web3.js';
1610

1711
import * as Layout from './layout';
18-
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
12+
import { sendAndConfirmTransaction } from './util/send-and-confirm-transaction';
1913

2014
/**
2115
* Some amount of tokens
@@ -95,7 +89,7 @@ type TokenSwapInfo = {|
9589
/**
9690
* @private
9791
*/
98-
const TokenSwapLayout = BufferLayout.struct([
92+
export const TokenSwapLayout = BufferLayout.struct([
9993
BufferLayout.u8('isInitialized'),
10094
BufferLayout.u8('nonce'),
10195
Layout.publicKey('tokenAccountA'),
@@ -159,6 +153,55 @@ export class TokenSwap {
159153
);
160154
}
161155

156+
157+
static createInitSwapInstruction(
158+
tokenSwapAccount: Account,
159+
authority: PublicKey,
160+
nonce: number,
161+
tokenAccountA: PublicKey,
162+
tokenAccountB: PublicKey,
163+
tokenPool: PublicKey,
164+
tokenAccountPool: PublicKey,
165+
tokenProgramId: PublicKey,
166+
swapProgramId: PublicKey,
167+
feeNumerator: number,
168+
feeDenominator: number
169+
):TransactionInstruction {
170+
const keys = [
171+
{ pubkey: tokenSwapAccount.publicKey, isSigner: false, isWritable: true },
172+
{ pubkey: authority, isSigner: false, isWritable: false },
173+
{ pubkey: tokenAccountA, isSigner: false, isWritable: false },
174+
{ pubkey: tokenAccountB, isSigner: false, isWritable: false },
175+
{ pubkey: tokenPool, isSigner: false, isWritable: true },
176+
{ pubkey: tokenAccountPool, isSigner: false, isWritable: true },
177+
{ pubkey: tokenProgramId, isSigner: false, isWritable: false },
178+
];
179+
const commandDataLayout = BufferLayout.struct([
180+
BufferLayout.u8('instruction'),
181+
BufferLayout.nu64('feeNumerator'),
182+
BufferLayout.nu64('feeDenominator'),
183+
BufferLayout.u8('nonce'),
184+
]);
185+
let data = Buffer.alloc(1024);
186+
{
187+
const encodeLength = commandDataLayout.encode(
188+
{
189+
instruction: 0, // InitializeSwap instruction
190+
feeNumerator,
191+
feeDenominator,
192+
nonce,
193+
},
194+
data,
195+
);
196+
data = data.slice(0, encodeLength);
197+
}
198+
return new TransactionInstruction({
199+
keys,
200+
programId: swapProgramId,
201+
data,
202+
});
203+
}
204+
162205
/**
163206
* Create a new Token Swap
164207
*
@@ -173,7 +216,7 @@ export class TokenSwap {
173216
* @param tokenProgramId The program id of the token program
174217
* @param feeNumerator Numerator of the fee ratio
175218
* @param feeDenominator Denominator of the fee ratio
176-
* @param programId Program ID of the token-swap program
219+
* @param swapProgramId Program ID of the token-swap program
177220
* @return Token object for the newly minted token, Public key of the account holding the total supply of new tokens
178221
*/
179222
static async createTokenSwap(
@@ -189,13 +232,13 @@ export class TokenSwap {
189232
nonce: number,
190233
feeNumerator: number,
191234
feeDenominator: number,
192-
programId: PublicKey,
235+
swapProgramId: PublicKey,
193236
): Promise<TokenSwap> {
194237
let transaction;
195238
const tokenSwap = new TokenSwap(
196239
connection,
197240
tokenSwapAccount.publicKey,
198-
programId,
241+
swapProgramId,
199242
payer,
200243
);
201244

@@ -210,43 +253,13 @@ export class TokenSwap {
210253
newAccountPubkey: tokenSwapAccount.publicKey,
211254
lamports: balanceNeeded,
212255
space: TokenSwapLayout.span,
213-
programId,
256+
programId: swapProgramId,
214257
}),
215258
);
216259

217-
let keys = [
218-
{pubkey: tokenSwapAccount.publicKey, isSigner: false, isWritable: true},
219-
{pubkey: authority, isSigner: false, isWritable: false},
220-
{pubkey: tokenAccountA, isSigner: false, isWritable: false},
221-
{pubkey: tokenAccountB, isSigner: false, isWritable: false},
222-
{pubkey: tokenPool, isSigner: false, isWritable: true},
223-
{pubkey: tokenAccountPool, isSigner: false, isWritable: true},
224-
{pubkey: tokenProgramId, isSigner: false, isWritable: false},
225-
];
226-
const commandDataLayout = BufferLayout.struct([
227-
BufferLayout.u8('instruction'),
228-
BufferLayout.nu64('feeNumerator'),
229-
BufferLayout.nu64('feeDenominator'),
230-
BufferLayout.u8('nonce'),
231-
]);
232-
let data = Buffer.alloc(1024);
233-
{
234-
const encodeLength = commandDataLayout.encode(
235-
{
236-
instruction: 0, // InitializeSwap instruction
237-
feeNumerator,
238-
feeDenominator,
239-
nonce,
240-
},
241-
data,
242-
);
243-
data = data.slice(0, encodeLength);
244-
}
245-
transaction.add({
246-
keys,
247-
programId,
248-
data,
249-
});
260+
const instruction = TokenSwap.createInitSwapInstruction(tokenSwapAccount, authority, nonce, tokenAccountA, tokenAccountB, tokenPool, tokenAccountPool, tokenProgramId, swapProgramId, feeNumerator, feeDenominator);
261+
262+
transaction.add(instruction);
250263
await sendAndConfirmTransaction(
251264
'createAccount and InitializeSwap',
252265
connection,
@@ -258,6 +271,7 @@ export class TokenSwap {
258271
return tokenSwap;
259272
}
260273

274+
261275
/**
262276
* Retrieve tokenSwap information
263277
*/

token-swap/js/module.d.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module '@solana/spl-token-swap' {
22
import { Buffer } from 'buffer';
3+
import { Layout } from 'buffer-layout';
34
import { PublicKey, TransactionInstruction, TransactionSignature, Connection, Account } from "@solana/web3.js";
45
import BN from 'bn.js';
56

@@ -19,13 +20,29 @@ declare module '@solana/spl-token-swap' {
1920
feeRatio: number,
2021
};
2122

23+
export const TokenSwapLayout: Layout;
24+
2225
export class TokenSwap {
2326
constructor(connection: Connection, tokenSwap: PublicKey, programId: PublicKey, payer: Account);
2427

2528
static getMinBalanceRentForExemptTokenSwap(
2629
connection: Connection,
2730
): Promise<number>;
2831

32+
static createInitSwapInstruction(
33+
tokenSwapAccount: Account,
34+
authority: PublicKey,
35+
nonce: number,
36+
tokenAccountA: PublicKey,
37+
tokenAccountB: PublicKey,
38+
tokenPool: PublicKey,
39+
tokenAccountPool: PublicKey,
40+
tokenProgramId: PublicKey,
41+
swapProgramId: PublicKey,
42+
feeNumerator: number,
43+
feeDenominator: number
44+
): TransactionInstruction;
45+
2946
static createTokenSwap(
3047
connection: Connection,
3148
payer: Account,
@@ -39,7 +56,7 @@ declare module '@solana/spl-token-swap' {
3956
nonce: number,
4057
feeNumerator: number,
4158
feeDenominator: number,
42-
programId: PublicKey,
59+
swapProgramId: PublicKey,
4360
): Promise<TokenSwap>
4461

4562
getInfo(): Promise<TokenSwapInfo>

token-swap/js/module.flow.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ declare module '@solana/spl-token-swap' {
2222
feeRatio: number,
2323
|};
2424

25+
declare export var TokenSwapLayout: Layout;
26+
2527
declare export class TokenSwap {
2628
constructor(
2729
connection: Connection,
@@ -34,6 +36,20 @@ declare module '@solana/spl-token-swap' {
3436
connection: Connection,
3537
): Promise<number>;
3638

39+
static createInitSwapInstruction(
40+
programId: PublicKey,
41+
tokenSwapAccount: Account,
42+
authority: PublicKey,
43+
nonce: number,
44+
tokenAccountA: PublicKey,
45+
tokenAccountB: PublicKey,
46+
tokenPool: PublicKey,
47+
tokenAccountPool: PublicKey,
48+
tokenProgramId: PublicKey,
49+
feeNumerator: number,
50+
feeDenominator: number
51+
): TransactionInstruction;
52+
3753
static createTokenSwap(
3854
connection: Connection,
3955
payer: Account,

token/js/client/token.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
TransactionInstruction,
1414
SYSVAR_RENT_PUBKEY,
1515
} from '@solana/web3.js';
16-
import type {Connection, TransactionSignature} from '@solana/web3.js';
16+
import type {Connection, Commitment, TransactionSignature} from '@solana/web3.js';
1717

1818
import * as Layout from './layout';
1919
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
@@ -107,7 +107,7 @@ type MintInfo = {|
107107
freezeAuthority: null | PublicKey,
108108
|};
109109

110-
const MintLayout = BufferLayout.struct([
110+
export const MintLayout = BufferLayout.struct([
111111
BufferLayout.u32('mintAuthorityOption'),
112112
Layout.publicKey('mintAuthority'),
113113
Layout.uint64('supply'),
@@ -177,7 +177,7 @@ type AccountInfo = {|
177177
/**
178178
* @private
179179
*/
180-
const AccountLayout = BufferLayout.struct([
180+
export const AccountLayout = BufferLayout.struct([
181181
Layout.publicKey('mint'),
182182
Layout.publicKey('owner'),
183183
Layout.uint64('amount'),
@@ -619,8 +619,8 @@ export class Token {
619619
*
620620
* @param account Public key of the account
621621
*/
622-
async getAccountInfo(account: PublicKey): Promise<AccountInfo> {
623-
const info = await this.connection.getAccountInfo(account);
622+
async getAccountInfo(account: PublicKey, commitment?: Commitment): Promise<AccountInfo> {
623+
const info = await this.connection.getAccountInfo(account, commitment);
624624
if (info === null) {
625625
throw new Error('Failed to find account');
626626
}

token/js/module.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare module '@solana/spl-token' {
22
import { Buffer } from 'buffer';
3-
import { PublicKey, TransactionInstruction, TransactionSignature, Connection, Account } from "@solana/web3.js";
3+
import { Layout } from 'buffer-layout';
4+
import { PublicKey, TransactionInstruction, TransactionSignature, Connection, Account } from '@solana/web3.js';
45
import BN from 'bn.js';
56

67
// === client/token.js ===
@@ -15,14 +16,16 @@ declare module '@solana/spl-token' {
1516
| 'CloseAccount';
1617

1718
export const NATIVE_MINT: PublicKey;
18-
19+
export const MintLayout: Layout;
1920
export type MintInfo = {
2021
mintAuthority: null | PublicKey,
2122
supply: u64,
2223
decimals: number,
2324
isInitialized: boolean,
2425
freezeAuthority: null | PublicKey,
2526
};
27+
28+
export const AccountLayout: Layout;
2629
export type AccountInfo = {
2730
mint: PublicKey,
2831
owner: PublicKey,

token/js/module.flow.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
declare module '@solana/spl-token' {
9-
// === client/token.js ===
109
declare export class u64 extends BN {
1110
toBuffer(): Buffer;
1211
static fromBuffer(buffer: Buffer): u64;
@@ -17,13 +16,15 @@ declare module '@solana/spl-token' {
1716
| 'AccountOwner'
1817
| 'CloseAccount';
1918
declare export var NATIVE_MINT: PublicKey;
19+
declare export var MintLayout: Layout;
2020
declare export type MintInfo = {|
2121
mintAuthority: null | PublicKey,
2222
supply: u64,
2323
decimals: number,
2424
isInitialized: boolean,
2525
freezeAuthority: null | PublicKey,
2626
|};
27+
declare export var AccountLayout: Layout;
2728
declare export type AccountInfo = {|
2829
mint: PublicKey,
2930
owner: PublicKey,

0 commit comments

Comments
 (0)