-
Notifications
You must be signed in to change notification settings - Fork 59
/
platform.ts
302 lines (269 loc) · 8.78 KB
/
platform.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import type {
Balances,
Chain,
ChainsConfig,
Network,
SignedTx,
StaticPlatformMethods,
TokenId,
TxHash,
} from '@wormhole-foundation/sdk-connect';
import {
PlatformContext,
Wormhole,
chainToPlatform,
decimals,
nativeChainIds,
networkPlatformConfigs,
isNative,
} from '@wormhole-foundation/sdk-connect';
import { SolanaChain } from './chain.js';
import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
import type {
AccountInfo,
Commitment,
ConnectionConfig,
ParsedAccountData,
RpcResponseAndContext,
SendOptions,
SignatureResult,
} from '@solana/web3.js';
import { Connection, PublicKey } from '@solana/web3.js';
import { SolanaAddress, SolanaZeroAddress } from './address.js';
import type {
AnySolanaAddress,
SolanaChains,
SolanaPlatformType,
} from './types.js';
import { _platform } from './types.js';
/**
* @category Solana
*/
export class SolanaPlatform<N extends Network>
extends PlatformContext<N, SolanaPlatformType>
implements StaticPlatformMethods<SolanaPlatformType, typeof SolanaPlatform>
{
static _platform = _platform;
constructor(network: N, config?: ChainsConfig<N, SolanaPlatformType>) {
super(
network,
config ?? networkPlatformConfigs(network, SolanaPlatform._platform),
);
}
getRpc<C extends SolanaChains>(
chain: C,
config: ConnectionConfig = {
commitment: 'confirmed',
disableRetryOnRateLimit: true,
},
): Connection {
if (chain in this.config)
return new Connection(this.config[chain]!.rpc, config);
throw new Error('No configuration available for chain: ' + chain);
}
getChain<C extends SolanaChains>(
chain: C,
rpc?: Connection,
): SolanaChain<N, C> {
if (chain in this.config) return new SolanaChain<N, C>(chain, this, rpc);
throw new Error('No configuration available for chain: ' + chain);
}
static nativeTokenId<N extends Network, C extends SolanaChains>(
network: N,
chain: C,
): TokenId<C> {
if (!SolanaPlatform.isSupportedChain(chain))
throw new Error(`invalid chain: ${chain}`);
return Wormhole.chainAddress(chain, SolanaZeroAddress);
}
static isNativeTokenId<N extends Network, C extends SolanaChains>(
network: N,
chain: C,
tokenId: TokenId,
): boolean {
if (!this.isSupportedChain(chain)) return false;
if (tokenId.chain !== chain) return false;
const native = this.nativeTokenId(network, chain);
return native == tokenId;
}
static isSupportedChain(chain: Chain): boolean {
const platform = chainToPlatform(chain);
return platform === SolanaPlatform._platform;
}
static async getDecimals(
chain: Chain,
rpc: Connection,
token: AnySolanaAddress,
): Promise<number> {
if (isNative(token))
return decimals.nativeDecimals(SolanaPlatform._platform);
let mint = await rpc.getParsedAccountInfo(
new SolanaAddress(token).unwrap(),
);
if (!mint || !mint.value) throw new Error('could not fetch token details');
const { decimals: numDecimals } = (mint.value.data as ParsedAccountData)
.parsed.info;
return numDecimals;
}
static async getBalance(
chain: Chain,
rpc: Connection,
walletAddress: string,
token: AnySolanaAddress,
): Promise<bigint | null> {
const address = new PublicKey(walletAddress);
if (isNative(token)) return BigInt(await rpc.getBalance(address));
// Check to see if we were passed wallet address or token account
const splToken = await rpc.getTokenAccountsByOwner(address, {
mint: new SolanaAddress(token).unwrap(),
});
// Use the first token account if it exists, otherwise fall back to wallet address
const checkAddress =
splToken.value.length > 0 ? splToken.value[0]!.pubkey : address;
const balance = await rpc.getTokenAccountBalance(checkAddress);
return BigInt(balance.value.amount);
}
static async getBalances(
chain: Chain,
rpc: Connection,
walletAddress: string,
tokens: AnySolanaAddress[],
): Promise<Balances> {
let native: bigint;
if (tokens.includes('native')) {
native = BigInt(await rpc.getBalance(new PublicKey(walletAddress)));
}
const splParsedTokenAccounts = (await Promise.all(
[TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID]
.map(pid => new PublicKey(pid))
.map(programId => rpc.getParsedTokenAccountsByOwner(new PublicKey(walletAddress), { programId })
))).reduce<{
pubkey: PublicKey;
account: AccountInfo<ParsedAccountData>;
}[]
>((acc, val) => {
return acc.concat(val.value);
}, []);
const balancesArr = tokens.map((token) => {
if (isNative(token)) {
return { ['native']: native };
}
const addrString = new SolanaAddress(token).toString();
const amount = splParsedTokenAccounts.find(
(v) => v?.account.data.parsed?.info?.mint === token.toString(),
)?.account.data.parsed?.info?.tokenAmount?.amount;
if (!amount) return { [addrString]: null };
return { [addrString]: BigInt(amount) };
});
return balancesArr.reduce((obj, item) => Object.assign(obj, item), {});
}
static async sendWait(
chain: Chain,
rpc: Connection,
stxns: SignedTx[],
opts?: SendOptions,
): Promise<TxHash[]> {
const results = await Promise.all(
stxns.map((stxn) => this.sendTxWithRetry(rpc, stxn, opts)),
);
const txhashes = results.map((r) => r.signature);
const erroredTxs = results
.filter((r) => r.response.value.err)
.map((r) => r.response.value.err);
if (erroredTxs.length > 0)
throw new Error(`Failed to confirm transaction: ${erroredTxs}`);
return txhashes;
}
static async sendTxWithRetry(
rpc: Connection,
tx: SignedTx,
sendOpts: SendOptions = {},
retryInterval = 5000,
): Promise<{
signature: string;
response: RpcResponseAndContext<SignatureResult>;
}> {
const commitment = sendOpts.preflightCommitment ?? rpc.commitment;
const signature = await rpc.sendRawTransaction(tx, {
...sendOpts,
skipPreflight: false, // The first send should not skip preflight to catch any errors
maxRetries: 0,
preflightCommitment: commitment,
});
// TODO: Use the lastValidBlockHeight that corresponds to the blockhash used in the transaction.
const { blockhash, lastValidBlockHeight } = await rpc.getLatestBlockhash();
const confirmTransactionPromise = rpc.confirmTransaction(
{
signature,
blockhash,
lastValidBlockHeight,
},
commitment,
);
// This loop will break once the transaction has been confirmed or the block height is exceeded.
// An exception will be thrown if the block height is exceeded by the confirmTransactionPromise.
// The transaction will be resent if it hasn't been confirmed after the interval.
let confirmedTx: RpcResponseAndContext<SignatureResult> | null = null;
while (!confirmedTx) {
confirmedTx = await Promise.race([
confirmTransactionPromise,
new Promise<null>((resolve) =>
setTimeout(() => {
resolve(null);
}, retryInterval),
),
]);
if (confirmedTx) {
break;
}
await rpc.sendRawTransaction(tx, {
...sendOpts,
skipPreflight: true,
maxRetries: 0,
preflightCommitment: commitment,
});
}
return { signature, response: confirmedTx };
}
static async latestBlock(
rpc: Connection,
commitment?: Commitment,
): Promise<{ blockhash: string; lastValidBlockHeight: number }> {
return rpc.getLatestBlockhash(commitment ?? rpc.commitment);
}
static async getLatestBlock(rpc: Connection): Promise<number> {
return await rpc.getSlot();
}
static async getLatestFinalizedBlock(rpc: Connection): Promise<number> {
const { lastValidBlockHeight } = await this.latestBlock(rpc, 'finalized');
return lastValidBlockHeight;
}
static chainFromChainId(genesisHash: string): [Network, SolanaChains] {
const netChain = nativeChainIds.platformNativeChainIdToNetworkChain(
SolanaPlatform._platform,
genesisHash,
);
if (!netChain)
throw new Error(
`No matching genesis hash to determine network and chain: ${genesisHash}`,
);
const [network, chain] = netChain;
return [network, chain];
}
static async chainFromRpc(rpc: Connection): Promise<[Network, SolanaChains]> {
try {
const gh = await rpc.getGenesisHash();
return SolanaPlatform.chainFromChainId(gh);
} catch (e) {
// Override for devnet which will often have a new Genesis hash
if (
rpc.rpcEndpoint.includes('http://127') ||
rpc.rpcEndpoint.includes('http://localhost') ||
rpc.rpcEndpoint === 'http://solana-devnet:8899'
) {
return ['Devnet', 'Solana'];
}
throw e;
}
}
}