-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathplatform.ts
166 lines (147 loc) · 5.07 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
import type {
Balances,
Chain,
ChainsConfig,
Network,
SignedTx,
StaticPlatformMethods,
TokenId,
TxHash,
} from "@wormhole-foundation/sdk-connect";
import {
PlatformContext,
Wormhole,
chainToPlatform,
isNative,
nativeChainIds,
decimals as nativeDecimals,
networkPlatformConfigs,
} from "@wormhole-foundation/sdk-connect";
import { Aptos, AptosConfig, Network as AptosNetwork } from "@aptos-labs/ts-sdk";
import { AptosChain } from "./chain.js";
import type { AptosChains, AptosPlatformType } from "./types.js";
import { _platform } from "./types.js";
import { AptosAddress } from "./address.js";
import { APTOS_COIN } from "./constants.js";
import type { AnyAptosAddress } from "./types.js";
/**
* @category Aptos
*/
export class AptosPlatform<N extends Network>
extends PlatformContext<N, AptosPlatformType>
implements StaticPlatformMethods<AptosPlatformType, typeof AptosPlatform>
{
static _platform = _platform;
constructor(network: N, config?: ChainsConfig<N, AptosPlatformType>) {
super(network, config ?? networkPlatformConfigs(network, AptosPlatform._platform));
}
getRpc<C extends AptosChains>(chain: C): Aptos {
if (chain in this.config) {
const network = this.network === "Mainnet" ? AptosNetwork.MAINNET : AptosNetwork.TESTNET;
const config = new AptosConfig({ fullnode: this.config[chain]!.rpc, network });
return new Aptos(config);
}
throw new Error("No configuration available for chain: " + chain);
}
getChain<C extends AptosChains>(chain: C, rpc?: Aptos): AptosChain<N, C> {
if (chain in this.config) return new AptosChain(chain, this);
throw new Error("No configuration available for chain: " + chain);
}
static nativeTokenId<N extends Network, C extends AptosChains>(network: N, chain: C): TokenId<C> {
if (!this.isSupportedChain(chain)) throw new Error(`invalid chain: ${chain}`);
return Wormhole.tokenId(chain, APTOS_COIN);
}
static isNativeTokenId<N extends Network, C extends AptosChains>(
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 === AptosPlatform._platform;
}
static async getDecimals(chain: Chain, rpc: Aptos, token: AnyAptosAddress): Promise<number> {
if (isNative(token) || token === APTOS_COIN)
return nativeDecimals.nativeDecimals(AptosPlatform._platform);
const assetType = token.toString();
const data = await rpc.getFungibleAssetMetadataByAssetType({ assetType });
return data.decimals;
}
static async getBalance(
chain: Chain,
rpc: Aptos,
walletAddress: string,
token: AnyAptosAddress,
): Promise<bigint | null> {
const assetType = isNative(token) ? APTOS_COIN : token.toString();
try {
const data = await rpc.getCurrentFungibleAssetBalances({
options: {
where: {
owner_address: { _eq: walletAddress },
asset_type: { _eq: assetType },
},
},
});
return data[0]?.amount ?? null;
} catch (e: any) {
if (e.status === 404) {
return null;
}
throw e;
}
}
static async getBalances(
chain: Chain,
rpc: Aptos,
walletAddress: string,
tokens: AnyAptosAddress[],
): Promise<Balances> {
const balancesArr = await Promise.all(
tokens.map(async (token) => {
const balance = await this.getBalance(chain, rpc, walletAddress, token);
const address = isNative(token) ? "native" : new AptosAddress(token).toString();
return { [address]: balance };
}),
);
return balancesArr.reduce((obj, item) => Object.assign(obj, item), {});
}
static async sendWait(chain: Chain, rpc: Aptos, stxns: SignedTx[]): Promise<TxHash[]> {
const txhashes = [];
for (const stxn of stxns) {
const pendingTx = await rpc.transaction.submit.simple(stxn.transaction);
const res = await rpc.waitForTransaction({
transactionHash: pendingTx.hash,
});
txhashes.push(res.hash);
}
return txhashes;
}
static async getLatestBlock(rpc: Aptos): Promise<number> {
const li = await rpc.getLedgerInfo();
return Number(li.block_height);
}
static async getLatestFinalizedBlock(rpc: Aptos): Promise<number> {
const li = await rpc.getLedgerInfo();
return Number(li.block_height);
}
static chainFromChainId(chainId: string | bigint): [Network, AptosChains] {
const netChain = nativeChainIds.platformNativeChainIdToNetworkChain(
AptosPlatform._platform,
BigInt(chainId),
);
if (!netChain)
throw new Error(`No matching chainId to determine network and chain: ${chainId}`);
const [network, chain] = netChain;
return [network, chain];
}
static async chainFromRpc(rpc: Aptos): Promise<[Network, AptosChains]> {
const li = await rpc.getLedgerInfo();
return this.chainFromChainId(li.chain_id.toString());
}
}