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

Commit 5493842

Browse files
token-js: Bindings For Transfer Fee Extension IXs (#3102)
* JS Bindings For Transfer Fee Extensions IXs * Add tests and actions Co-authored-by: Jon Cinque <[email protected]>
1 parent ad97543 commit 5493842

File tree

7 files changed

+1307
-2
lines changed

7 files changed

+1307
-2
lines changed

token/js/src/extensions/extensionType.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { MULTISIG_SIZE } from '../state/multisig';
44
import { ACCOUNT_TYPE_SIZE } from './accountType';
55
import { MINT_CLOSE_AUTHORITY_SIZE } from './mintCloseAuthority';
66
import { IMMUTABLE_OWNER_SIZE } from './immutableOwner';
7+
import { TRANSFER_FEE_CONFIG_SIZE, TRANSFER_FEE_AMOUNT_SIZE } from './transferFee';
78

89
export enum ExtensionType {
910
Uninitialized,
@@ -27,9 +28,9 @@ export function getTypeLen(e: ExtensionType): number {
2728
case ExtensionType.Uninitialized:
2829
return 0;
2930
case ExtensionType.TransferFeeConfig:
30-
return 108;
31+
return TRANSFER_FEE_CONFIG_SIZE;
3132
case ExtensionType.TransferFeeAmount:
32-
return 8;
33+
return TRANSFER_FEE_AMOUNT_SIZE;
3334
case ExtensionType.MintCloseAuthority:
3435
return MINT_CLOSE_AUTHORITY_SIZE;
3536
case ExtensionType.ConfidentialTransferMint:

token/js/src/extensions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './accountType';
22
export * from './extensionType';
33
export * from './mintCloseAuthority';
44
export * from './immutableOwner';
5+
export * from './transferFee/index';
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import {
2+
ConfirmOptions,
3+
Connection,
4+
PublicKey,
5+
sendAndConfirmTransaction,
6+
Signer,
7+
Transaction,
8+
TransactionSignature,
9+
} from '@solana/web3.js';
10+
import { TOKEN_2022_PROGRAM_ID } from '../../constants';
11+
import {
12+
createTransferCheckedWithFeeInstruction,
13+
createHarvestWithheldTokensToMintInstruction,
14+
createWithdrawWithheldTokensFromAccountsInstruction,
15+
createWithdrawWithheldTokensFromMintInstruction,
16+
} from './instructions';
17+
import { getSigners } from '../../actions/internal';
18+
19+
/**
20+
* Transfer tokens from one account to another, asserting the transfer fee, token mint, and decimals
21+
*
22+
* @param connection Connection to use
23+
* @param payer Payer of the transaction fees
24+
* @param source Source account
25+
* @param mint Mint for the account
26+
* @param destination Destination account
27+
* @param owner Owner of the source account
28+
* @param amount Number of tokens to transfer
29+
* @param decimals Number of decimals in transfer amount
30+
* @param multiSigners Signing accounts if `owner` is a multisig
31+
* @param confirmOptions Options for confirming the transaction
32+
* @param programId SPL Token program account
33+
*
34+
* @return Signature of the confirmed transaction
35+
*/
36+
export async function transferCheckedWithFee(
37+
connection: Connection,
38+
payer: Signer,
39+
source: PublicKey,
40+
mint: PublicKey,
41+
destination: PublicKey,
42+
owner: Signer | PublicKey,
43+
amount: bigint,
44+
decimals: number,
45+
fee: bigint,
46+
multiSigners: Signer[] = [],
47+
confirmOptions?: ConfirmOptions,
48+
programId = TOKEN_2022_PROGRAM_ID
49+
): Promise<TransactionSignature> {
50+
const [ownerPublicKey, signers] = getSigners(owner, multiSigners);
51+
52+
const transaction = new Transaction().add(
53+
createTransferCheckedWithFeeInstruction(
54+
source,
55+
mint,
56+
destination,
57+
ownerPublicKey,
58+
amount,
59+
decimals,
60+
fee,
61+
multiSigners,
62+
programId
63+
)
64+
);
65+
66+
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions);
67+
}
68+
69+
/**
70+
* Withdraw withheld tokens from mint
71+
*
72+
* @param connection Connection to use
73+
* @param payer Payer of the transaction fees
74+
* @param mint The token mint
75+
* @param destination The destination account
76+
* @param authority The mint's withdraw withheld tokens authority
77+
* @param multiSigners Signing accounts if `owner` is a multisig
78+
* @param confirmOptions Options for confirming the transaction
79+
* @param programId SPL Token program account
80+
*
81+
* @return Signature of the confirmed transaction
82+
*/
83+
export async function withdrawWithheldTokensFromMint(
84+
connection: Connection,
85+
payer: Signer,
86+
mint: PublicKey,
87+
destination: PublicKey,
88+
authority: Signer | PublicKey,
89+
multiSigners: Signer[] = [],
90+
confirmOptions?: ConfirmOptions,
91+
programId = TOKEN_2022_PROGRAM_ID
92+
): Promise<TransactionSignature> {
93+
const [authorityPublicKey, signers] = getSigners(authority, multiSigners);
94+
95+
const transaction = new Transaction().add(
96+
createWithdrawWithheldTokensFromMintInstruction(mint, destination, authorityPublicKey, signers, programId)
97+
);
98+
99+
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions);
100+
}
101+
102+
/**
103+
* Withdraw withheld tokens from accounts
104+
*
105+
* @param connection Connection to use
106+
* @param payer Payer of the transaction fees
107+
* @param mint The token mint
108+
* @param destination The destination account
109+
* @param authority The mint's withdraw withheld tokens authority
110+
* @param multiSigners Signing accounts if `owner` is a multisig
111+
* @param sources Source accounts from which to withdraw withheld fees
112+
* @param confirmOptions Options for confirming the transaction
113+
* @param programId SPL Token program account
114+
*
115+
* @return Signature of the confirmed transaction
116+
*/
117+
export async function withdrawWithheldTokensFromAccounts(
118+
connection: Connection,
119+
payer: Signer,
120+
mint: PublicKey,
121+
destination: PublicKey,
122+
authority: Signer | PublicKey,
123+
multiSigners: Signer[],
124+
sources: PublicKey[],
125+
confirmOptions?: ConfirmOptions,
126+
programId = TOKEN_2022_PROGRAM_ID
127+
): Promise<TransactionSignature> {
128+
const [authorityPublicKey, signers] = getSigners(authority, multiSigners);
129+
130+
const transaction = new Transaction().add(
131+
createWithdrawWithheldTokensFromAccountsInstruction(
132+
mint,
133+
destination,
134+
authorityPublicKey,
135+
signers,
136+
sources,
137+
programId
138+
)
139+
);
140+
141+
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions);
142+
}
143+
144+
/**
145+
* Harvest withheld tokens from accounts to the mint
146+
*
147+
* @param connection Connection to use
148+
* @param payer Payer of the transaction fees
149+
* @param mint The token mint
150+
* @param sources Source accounts from which to withdraw withheld fees
151+
* @param confirmOptions Options for confirming the transaction
152+
* @param programId SPL Token program account
153+
*
154+
* @return Signature of the confirmed transaction
155+
*/
156+
export async function harvestWithheldTokensToMint(
157+
connection: Connection,
158+
payer: Signer,
159+
mint: PublicKey,
160+
sources: PublicKey[],
161+
confirmOptions?: ConfirmOptions,
162+
programId = TOKEN_2022_PROGRAM_ID
163+
): Promise<TransactionSignature> {
164+
const transaction = new Transaction().add(createHarvestWithheldTokensToMintInstruction(mint, sources, programId));
165+
166+
return await sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions);
167+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './actions';
2+
export * from './instructions';
3+
export * from './state';

0 commit comments

Comments
 (0)