-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathplatform.ts
285 lines (246 loc) · 7.93 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
import {
Balances,
Chain,
ChainsConfig,
Network,
PlatformContext,
SignedTx,
TokenId,
TxHash,
Wormhole,
chainToPlatform,
decimals,
nativeChainIds,
networkPlatformConfigs,
} from '@wormhole-foundation/connect-sdk';
import { SolanaChain } from './chain';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import {
Commitment,
Connection,
ParsedAccountData,
PublicKey,
SendOptions,
SendTransactionError,
TransactionExpiredBlockheightExceededError,
} from '@solana/web3.js';
import { SolanaAddress, SolanaZeroAddress } from './address';
import {
AnySolanaAddress,
SolanaChains,
SolanaPlatformType,
_platform,
} from './types';
/**
* @category Solana
*/
export class SolanaPlatform<N extends Network> extends PlatformContext<
N,
SolanaPlatformType
> {
static _platform = _platform;
constructor(network: N, config?: ChainsConfig<N, SolanaPlatformType>) {
super(
network,
config ?? networkPlatformConfigs(network, SolanaPlatform._platform),
);
}
getRpc<C extends SolanaChains>(
chain: C,
commitment: Commitment = 'confirmed',
): Connection {
if (chain in this.config)
return new Connection(this.config[chain]!.rpc, commitment);
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 | 'native',
): Promise<bigint> {
if (token === 'native')
return BigInt(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 BigInt(numDecimals);
}
static async getBalance(
chain: Chain,
rpc: Connection,
walletAddress: string,
token: AnySolanaAddress | 'native',
): Promise<bigint | null> {
if (token === 'native')
return BigInt(await rpc.getBalance(new PublicKey(walletAddress)));
const splToken = await rpc.getTokenAccountsByOwner(
new PublicKey(walletAddress),
{ mint: new SolanaAddress(token).unwrap() },
);
if (!splToken.value[0]) return null;
const balance = await rpc.getTokenAccountBalance(splToken.value[0].pubkey);
return BigInt(balance.value.amount);
}
static async getBalances(
chain: Chain,
rpc: Connection,
walletAddress: string,
tokens: (AnySolanaAddress | 'native')[],
): Promise<Balances> {
let native: bigint;
if (tokens.includes('native')) {
native = BigInt(await rpc.getBalance(new PublicKey(walletAddress)));
}
const splParsedTokenAccounts = await rpc.getParsedTokenAccountsByOwner(
new PublicKey(walletAddress),
{
programId: new PublicKey(TOKEN_PROGRAM_ID),
},
);
const balancesArr = tokens.map((token) => {
if (token === 'native') {
return { ['native']: native };
}
const addrString = new SolanaAddress(token).toString();
const amount = splParsedTokenAccounts.value.find(
(v) => v?.account.data.parsed?.info?.mint === token,
)?.account.data.parsed?.info?.tokenAmount?.amount;
if (!amount) return { [addrString]: null };
return { [addrString]: BigInt(amount) };
});
return balancesArr.reduce((obj, item) => Object.assign(obj, item), {});
}
// Handles retrying a Transaction if the error is deemed to be
// recoverable. Currently handles:
// - Blockhash not found (blockhash too new for the node we submitted to)
// - Not enough bytes (storage account not seen yet)
private static async sendWithRetry(
rpc: Connection,
stxns: SignedTx,
opts: SendOptions,
retries: number = 3,
): Promise<string> {
// Shouldnt get hit but just in case
if (!retries) throw new Error('Too many retries');
try {
const txid = await rpc.sendRawTransaction(stxns.tx, opts);
return txid;
} catch (e) {
retries -= 1;
if (!retries) throw e;
// Would require re-signing, for now bail
if (e instanceof TransactionExpiredBlockheightExceededError) throw e;
// Only handle SendTransactionError
if (!(e instanceof SendTransactionError)) throw e;
const emsg = e.message;
// Only handle simulation errors
if (!emsg.includes('Transaction simulation failed')) throw e;
// Blockhash not found _yet_
if (emsg.includes('Blockhash not found'))
return this.sendWithRetry(rpc, stxns, opts, retries);
// Find the log message with the error details
const loggedErr = e.logs.find((log) =>
log.startsWith('Program log: Error: '),
);
// Probably caused by storage account not seen yet
if (loggedErr && loggedErr.includes('Not enough bytes'))
return this.sendWithRetry(rpc, stxns, opts, retries);
}
}
static async sendWait(
chain: Chain,
rpc: Connection,
stxns: SignedTx[],
opts?: SendOptions,
): Promise<TxHash[]> {
const { blockhash, lastValidBlockHeight } = await this.latestBlock(rpc);
const txhashes = await Promise.all(
stxns.map((stxn) =>
this.sendWithRetry(
rpc,
stxn,
// Set the commitment level to match the rpc commitment level
// otherwise, it defaults to finalized
opts ?? { preflightCommitment: rpc.commitment },
),
),
);
await Promise.all(
txhashes.map((signature) => {
return rpc.confirmTransaction(
{
signature,
blockhash,
lastValidBlockHeight,
},
rpc.commitment,
);
}),
);
return txhashes;
}
static async latestBlock(
rpc: Connection,
commitment?: Commitment,
): Promise<{ blockhash: string; lastValidBlockHeight: number }> {
// Use finalized to prevent blockhash not found errors
// Note: this may mean we have less time to submit transactions?
return rpc.getLatestBlockhash(commitment ?? 'finalized');
}
static async getLatestBlock(rpc: Connection): Promise<number> {
const { lastValidBlockHeight } = await this.latestBlock(rpc, 'confirmed');
return lastValidBlockHeight;
}
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]> {
const gh = await rpc.getGenesisHash();
return SolanaPlatform.chainFromChainId(gh);
}
}