@clonegod/ttd-sol-common 2.0.51 → 2.0.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StandardPoolInfoType, StandardTokenInfoType } from "@clonegod/ttd-core";
|
|
2
2
|
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
|
|
3
3
|
export declare const get_token_program_id: (token: StandardTokenInfoType) => PublicKey;
|
|
4
|
-
export declare function get_wallet_token_account(wallet_pubkey: string, pool_list: StandardPoolInfoType[]):
|
|
4
|
+
export declare function get_wallet_token_account(wallet_pubkey: string, pool_list: StandardPoolInfoType[]): Map<string, string>;
|
|
5
5
|
export declare const create_token_account_if_not_exist: (connection: Connection, owner: Keypair, token_list: StandardTokenInfoType[]) => Promise<void>;
|
|
@@ -14,11 +14,26 @@ const get_token_program_id = (token) => {
|
|
|
14
14
|
return programId;
|
|
15
15
|
};
|
|
16
16
|
exports.get_token_program_id = get_token_program_id;
|
|
17
|
-
|
|
17
|
+
const wallet_token_accounts_cache = new Map();
|
|
18
|
+
function getWalletTokenAccountCacheKey(wallet_pubkey, token_address) {
|
|
19
|
+
return `${wallet_pubkey}_${token_address}}`;
|
|
20
|
+
}
|
|
21
|
+
function getCachedAtaAddress(wallet_pubkey, token_address, programId) {
|
|
22
|
+
const cacheKey = getWalletTokenAccountCacheKey(wallet_pubkey, token_address);
|
|
23
|
+
if (wallet_token_accounts_cache.has(cacheKey)) {
|
|
24
|
+
return wallet_token_accounts_cache.get(cacheKey);
|
|
25
|
+
}
|
|
26
|
+
const ataAddress = (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(token_address), new web3_js_1.PublicKey(wallet_pubkey), false, programId).toBase58();
|
|
27
|
+
wallet_token_accounts_cache.set(cacheKey, ataAddress);
|
|
28
|
+
return ataAddress;
|
|
29
|
+
}
|
|
30
|
+
function get_wallet_token_account(wallet_pubkey, pool_list) {
|
|
18
31
|
let account_map = new Map();
|
|
19
32
|
for (let { tokenA, tokenB } of pool_list) {
|
|
20
|
-
|
|
21
|
-
|
|
33
|
+
const programIdA = (0, exports.get_token_program_id)(tokenA);
|
|
34
|
+
const programIdB = (0, exports.get_token_program_id)(tokenB);
|
|
35
|
+
account_map.set(tokenA.symbol, getCachedAtaAddress(wallet_pubkey, tokenA.address, programIdA));
|
|
36
|
+
account_map.set(tokenB.symbol, getCachedAtaAddress(wallet_pubkey, tokenB.address, programIdB));
|
|
22
37
|
}
|
|
23
38
|
return account_map;
|
|
24
39
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { StandardPoolInfoType, StandardTokenInfoType } from "@clonegod/ttd-core";
|
|
2
2
|
import { log_info, log_error } from "@clonegod/ttd-core/dist";
|
|
3
3
|
import { COMMITMENT_LEVEL } from "./constants";
|
|
4
|
-
import { ASSOCIATED_TOKEN_PROGRAM_ID, createAssociatedTokenAccountIdempotent, getAssociatedTokenAddress, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token";
|
|
4
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, createAssociatedTokenAccountIdempotent, getAssociatedTokenAddress, getAssociatedTokenAddressSync, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token";
|
|
5
5
|
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
|
|
6
6
|
|
|
7
7
|
|
|
@@ -13,13 +13,51 @@ export const get_token_program_id = (token: StandardTokenInfoType) => {
|
|
|
13
13
|
return programId
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
// 缓存 ATA 地址:key = `${wallet_pubkey}_${token_address}`
|
|
17
|
+
const wallet_token_accounts_cache = new Map<string, string>()
|
|
18
|
+
|
|
19
|
+
function getWalletTokenAccountCacheKey(wallet_pubkey: string, token_address: string): string {
|
|
20
|
+
return `${wallet_pubkey}_${token_address}}`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function getCachedAtaAddress(
|
|
24
|
+
wallet_pubkey: string,
|
|
25
|
+
token_address: string,
|
|
26
|
+
programId: PublicKey
|
|
27
|
+
): string {
|
|
28
|
+
const cacheKey = getWalletTokenAccountCacheKey(wallet_pubkey, token_address)
|
|
29
|
+
|
|
30
|
+
if (wallet_token_accounts_cache.has(cacheKey)) {
|
|
31
|
+
return wallet_token_accounts_cache.get(cacheKey)!
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ataAddress = getAssociatedTokenAddressSync(
|
|
35
|
+
new PublicKey(token_address),
|
|
36
|
+
new PublicKey(wallet_pubkey),
|
|
37
|
+
false,
|
|
38
|
+
programId
|
|
39
|
+
).toBase58()
|
|
40
|
+
|
|
41
|
+
wallet_token_accounts_cache.set(cacheKey, ataAddress)
|
|
42
|
+
return ataAddress
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function get_wallet_token_account(wallet_pubkey:string, pool_list:StandardPoolInfoType[]
|
|
17
46
|
) {
|
|
18
47
|
let account_map = new Map<string,string>()
|
|
19
48
|
|
|
20
49
|
for(let {tokenA, tokenB} of pool_list) {
|
|
21
|
-
|
|
22
|
-
|
|
50
|
+
const programIdA = get_token_program_id(tokenA)
|
|
51
|
+
const programIdB = get_token_program_id(tokenB)
|
|
52
|
+
|
|
53
|
+
account_map.set(
|
|
54
|
+
tokenA.symbol,
|
|
55
|
+
getCachedAtaAddress(wallet_pubkey, tokenA.address, programIdA)
|
|
56
|
+
)
|
|
57
|
+
account_map.set(
|
|
58
|
+
tokenB.symbol,
|
|
59
|
+
getCachedAtaAddress(wallet_pubkey, tokenB.address, programIdB)
|
|
60
|
+
)
|
|
23
61
|
}
|
|
24
62
|
return account_map
|
|
25
63
|
}
|