/
spl.ts
133 lines (121 loc) · 5.19 KB
/
spl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, createAssociatedTokenAccountInstruction, createCloseAccountInstruction, createTransferCheckedInstruction, getAccount, getAssociatedTokenAddress, getMint } from "@solana/spl-token";
import { Connection, PublicKey, Transaction } from "@solana/web3.js";
import { BridgeTokenConfig, Routing, RoutingHelper } from "../../../common";
/**
* Sends a token transaction using the provided routing information.
*
* @param {Routing} routing - The routing information for the transaction.
* @param {PublicKey} senderTokenAccount - The sender's token account public key.
* @param {PublicKey} recipientTokenAccount - The recipient's token account public key.
* @param {BridgeTokenConfig} token - The configuration for the token.
* @returns {Promise<Transaction>} - A Promise that resolves to the sent token transaction.
*/
export async function sendTokenTransaction(
routing: Routing,
senderTokenAccount: PublicKey,
recipientTokenAccount: PublicKey,
token: BridgeTokenConfig
): Promise<Transaction> {
if (!routing.amount) throw new Error("Routing Amount not found");
if (!token) throw new Error("Token not found");
const txn = new Transaction().add(
createTransferCheckedInstruction(
senderTokenAccount,
new PublicKey(token.address),
recipientTokenAccount,
new PublicKey(routing.from.address),
RoutingHelper.BaseUnits_FromReadableValue(routing.amount, token.decimals).toNumber(),
token.decimals
)
);
return txn;
}
/**
* Creates a transaction to close a token account.
*
* @param {PublicKey} senderAccount - The public key of the sender's account.
* @param {PublicKey} senderTokenAccount - The public key of the sender's token account to be closed.
* @returns {Promise<Transaction>} - A Promise that resolves to the transaction to close the token account.
*/
export async function closeTokenAccountTransaction(senderAccount: PublicKey, senderTokenAccount: PublicKey): Promise<Transaction> {
const tx = new Transaction();
tx.add(
createCloseAccountInstruction(
senderTokenAccount,
senderAccount,
senderAccount,
[]
)
);
tx.feePayer = senderAccount;
return tx
}
/**
* Generates the associated token account address for a given owner.
*
* @param {string} owner - The owner's address for which to generate the associated token account address.
* @param {BridgeTokenConfig} solanaTokenConfig - The configuration for the Solana token.
* @param {Connection} connection - The Solana connection object.
* @returns {Promise<PublicKey>} - A Promise that resolves to the generated associated token account address.
*/
export async function generateAssociatedTokenAccountAddress(
owner: string,
solanaTokenConfig: BridgeTokenConfig,
connection: Connection
): Promise<PublicKey> {
const mintAddress = await getMint(connection, new PublicKey(solanaTokenConfig.address));
const tokenAccountAddress = await getAssociatedTokenAddress(new PublicKey(mintAddress.address), new PublicKey(owner))
return tokenAccountAddress
}
/**
* Retrieves the associated token account for a given owner.
*
* @param {string} owner - The owner's address for which to retrieve the associated token account.
* @param {BridgeTokenConfig} solanaTokenConfig - The configuration for the Solana token.
* @param {Connection} connection - The Solana connection object.
* @returns {Promise<PublicKey>} - A Promise that resolves to the associated token account's PublicKey.
*/
export async function getAssociatedTokenAccount(
owner: string,
solanaTokenConfig: BridgeTokenConfig,
connection: Connection
) {
const associatedTokenAccountAddress = await generateAssociatedTokenAccountAddress(
owner,
solanaTokenConfig,
connection
)
const tokenAccount = await getAccount(
connection,
associatedTokenAccountAddress,
"processed",
TOKEN_PROGRAM_ID
)
return tokenAccount
}
/**
* Creates a transaction to create an associated token account for a given owner.
*
* @param {string} owner - The owner's address for which to create the associated token account.
* @param {BridgeTokenConfig} tokenConfig - The configuration for the token.
* @param {Connection} connection - The Solana connection object.
* @returns {Promise<Transaction>} - A Promise that resolves to the transaction to create the associated token account.
*/
export async function createAssociatedTokenAccountTransaction(owner: string, tokenConfig: BridgeTokenConfig, connection: Connection): Promise<Transaction> {
const ownerPk = new PublicKey(owner)
const mintAddress = new PublicKey(tokenConfig.address);
const address = await generateAssociatedTokenAccountAddress(owner, tokenConfig, connection);
const programId = TOKEN_PROGRAM_ID;
const associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID;
const transaction = new Transaction().add(
createAssociatedTokenAccountInstruction(
ownerPk,
address,
ownerPk,
mintAddress,
programId,
associatedTokenProgramId
)
);
return transaction
}