Skip to content

Commit 5943b6b

Browse files
authored
aptos: migrate sdk to latest version (#766)
* aptos: migrate sdk to latest version use @aptos-labs/ts-sdk instead of deprecated aptos package * fix tests
1 parent 641242c commit 5943b6b

File tree

10 files changed

+373
-224
lines changed

10 files changed

+373
-224
lines changed

package-lock.json

+219-89
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platforms/aptos/__tests__/integration/tokenBridge.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe("TokenBridge Tests", () => {
163163
expect(attestTx!.chain).toEqual(chain);
164164

165165
const { transaction } = attestTx!;
166-
expect(transaction.arguments).toHaveLength(0);
166+
expect(transaction.functionArguments).toHaveLength(0);
167167
});
168168

169169
test("Submit Attestation", async () => {
@@ -223,7 +223,7 @@ describe("TokenBridge Tests", () => {
223223
expect(xferTx!.chain).toEqual(chain);
224224

225225
const { transaction } = xferTx!;
226-
expect(transaction.arguments).toHaveLength(5);
226+
expect(transaction.functionArguments).toHaveLength(5);
227227
// ...
228228
});
229229

@@ -242,8 +242,8 @@ describe("TokenBridge Tests", () => {
242242
expect(xferTx!.chain).toEqual(chain);
243243

244244
const { transaction } = xferTx!;
245-
expect(transaction.type_arguments).toHaveLength(1);
246-
expect(transaction.arguments).toHaveLength(5);
245+
expect(transaction.typeArguments).toHaveLength(1);
246+
expect(transaction.functionArguments).toHaveLength(5);
247247
});
248248
});
249249
});

platforms/aptos/__tests__/unit/platform.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AptosChains, AptosPlatform } from "./../../src/index.js";
66

77
import "@wormhole-foundation/sdk-aptos-core";
88
import "@wormhole-foundation/sdk-aptos-tokenbridge";
9-
import { AptosClient } from "aptos";
9+
import { Aptos, AptosConfig, Network as AptosNetwork } from "@aptos-labs/ts-sdk";
1010

1111
const network = DEFAULT_NETWORK;
1212

@@ -22,7 +22,11 @@ describe("Aptos Platform Tests", () => {
2222
[APTOS_CHAINS[0]!]: configs[APTOS_CHAINS[0]!],
2323
});
2424

25-
const client = new AptosClient(configs[APTOS_CHAINS[0]!]!.rpc);
25+
const config = new AptosConfig({
26+
fullnode: configs[APTOS_CHAINS[0]!]!.rpc,
27+
network: AptosNetwork.MAINNET,
28+
});
29+
const client = new Aptos(config);
2630
const tb = await p.getProtocol("TokenBridge", client);
2731
expect(tb).toBeTruthy();
2832
});

platforms/aptos/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"prettier": "prettier --write ./src"
4747
},
4848
"dependencies": {
49-
"@wormhole-foundation/sdk-connect": "1.0.3",
50-
"aptos": "1.21.0"
49+
"@aptos-labs/ts-sdk": "^1.33.1",
50+
"@wormhole-foundation/sdk-connect": "1.0.3"
5151
},
5252
"type": "module",
5353
"typesVersions": {
@@ -99,4 +99,4 @@
9999
}
100100
}
101101
}
102-
}
102+
}

platforms/aptos/protocols/core/src/core.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
AptosPlatformType,
1616
} from "@wormhole-foundation/sdk-aptos";
1717
import { AptosPlatform } from "@wormhole-foundation/sdk-aptos";
18-
import type { AptosClient, Types } from "aptos";
18+
import { Aptos } from "@aptos-labs/ts-sdk";
1919

2020
export class AptosWormholeCore<N extends Network, C extends AptosChains>
2121
implements WormholeCore<N, C>
@@ -26,7 +26,7 @@ export class AptosWormholeCore<N extends Network, C extends AptosChains>
2626
constructor(
2727
readonly network: N,
2828
readonly chain: C,
29-
readonly connection: AptosClient,
29+
readonly connection: Aptos,
3030
readonly contracts: Contracts,
3131
) {
3232
this.chainId = toChainId(chain);
@@ -46,7 +46,7 @@ export class AptosWormholeCore<N extends Network, C extends AptosChains>
4646
}
4747

4848
static async fromRpc<N extends Network>(
49-
connection: AptosClient,
49+
connection: Aptos,
5050
config: ChainsConfig<N, AptosPlatformType>,
5151
): Promise<AptosWormholeCore<N, AptosChains>> {
5252
const [network, chain] = await AptosPlatform.chainFromRpc(connection);
@@ -78,14 +78,11 @@ export class AptosWormholeCore<N extends Network, C extends AptosChains>
7878
});
7979
}
8080
async parseMessages(txid: string) {
81-
const transaction = await this.connection.getTransactionByHash(txid);
81+
const transaction = await this.connection.getTransactionByHash({ transactionHash: txid });
8282
if (transaction.type !== "user_transaction")
8383
throw new Error(`${txid} is not a user_transaction`);
8484

85-
const userTransaction = transaction as Types.UserTransaction;
86-
const messages = userTransaction.events.filter((event) =>
87-
event.type.endsWith("WormholeMessage"),
88-
);
85+
const messages = transaction.events.filter((event) => event.type.endsWith("WormholeMessage"));
8986
if (!messages || messages.length === 0)
9087
throw new Error(`WormholeMessage not found for ${txid}`);
9188

platforms/aptos/protocols/tokenBridge/src/tokenBridge.ts

+46-47
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ import {
3535
coalesceModuleAddress,
3636
isValidAptosType,
3737
} from "@wormhole-foundation/sdk-aptos";
38-
import type { AptosClient, Types } from "aptos";
3938
import { serializeForeignAddressSeeds } from "./foreignAddress.js";
4039
import type { OriginInfo, TokenBridgeState } from "./types.js";
40+
import { Aptos, InputGenerateTransactionPayloadData } from "@aptos-labs/ts-sdk";
4141

4242
export class AptosTokenBridge<N extends Network, C extends AptosChains>
4343
implements TokenBridge<N, C>
@@ -48,7 +48,7 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
4848
constructor(
4949
readonly network: N,
5050
readonly chain: C,
51-
readonly connection: AptosClient,
51+
readonly connection: Aptos,
5252
readonly contracts: Contracts,
5353
) {
5454
this.chainId = toChainId(chain);
@@ -60,7 +60,7 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
6060
}
6161

6262
static async fromRpc<N extends Network>(
63-
connection: AptosClient,
63+
connection: Aptos,
6464
config: ChainsConfig<N, AptosPlatformType>,
6565
): Promise<AptosTokenBridge<N, AptosChains>> {
6666
const [network, chain] = await AptosPlatform.chainFromRpc(connection);
@@ -81,14 +81,11 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
8181

8282
async getOriginalAsset(token: AnyAptosAddress): Promise<TokenId> {
8383
const fqt = token.toString().split(APTOS_SEPARATOR);
84-
let originInfo: OriginInfo | undefined;
8584

86-
originInfo = (
87-
await this.connection.getAccountResource(
88-
fqt[0]!,
89-
`${this.tokenBridgeAddress}::state::OriginInfo`,
90-
)
91-
).data as OriginInfo;
85+
const originInfo = await this.connection.getAccountResource<OriginInfo>({
86+
accountAddress: fqt[0]!,
87+
resourceType: `${this.tokenBridgeAddress}::state::OriginInfo`,
88+
});
9289

9390
if (!originInfo) throw ErrNotWrapped(token.toString());
9491

@@ -131,32 +128,33 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
131128
if (!assetFullyQualifiedType) throw new Error("Invalid asset address.");
132129

133130
// check to see if we can get origin info from asset address
134-
await this.connection.getAccountResource(
135-
coalesceModuleAddress(assetFullyQualifiedType),
136-
`${this.tokenBridgeAddress}::state::OriginInfo`,
137-
);
131+
await this.connection.getAccountResource({
132+
accountAddress: coalesceModuleAddress(assetFullyQualifiedType),
133+
resourceType: `${this.tokenBridgeAddress}::state::OriginInfo`,
134+
});
138135

139136
// if successful, we can just return the computed address
140137
return toNative(this.chain, assetFullyQualifiedType);
141138
}
142139

143140
async isTransferCompleted(vaa: TokenBridge.TransferVAA): Promise<boolean> {
144-
const state = (
145-
await this.connection.getAccountResource(
146-
this.tokenBridgeAddress,
147-
`${this.tokenBridgeAddress}::state::State`,
148-
)
149-
).data as TokenBridgeState;
141+
const state = await this.connection.getAccountResource<TokenBridgeState>({
142+
accountAddress: this.tokenBridgeAddress,
143+
resourceType: `${this.tokenBridgeAddress}::state::State`,
144+
});
150145

151146
const handle = state.consumed_vaas.elems.handle;
152147

153148
// check if vaa hash is in consumed_vaas
154149
try {
155150
// when accessing Set<T>, key is type T and value is 0
156-
await this.connection.getTableItem(handle, {
157-
key_type: "vector<u8>",
158-
value_type: "u8",
159-
key: `0x${Buffer.from(keccak256(vaa.hash)).toString("hex")}`,
151+
await this.connection.getTableItem({
152+
handle,
153+
data: {
154+
key_type: "vector<u8>",
155+
value_type: "u8",
156+
key: `0x${Buffer.from(keccak256(vaa.hash)).toString("hex")}`,
157+
},
160158
});
161159
return true;
162160
} catch {
@@ -179,8 +177,8 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
179177
yield this.createUnsignedTx(
180178
{
181179
function: `${this.tokenBridgeAddress}::attest_token::attest_token_entry`,
182-
type_arguments: [assetType],
183-
arguments: [],
180+
typeArguments: [assetType],
181+
functionArguments: [],
184182
},
185183
"Aptos.AttestToken",
186184
);
@@ -193,8 +191,8 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
193191
yield this.createUnsignedTx(
194192
{
195193
function: `${this.tokenBridgeAddress}::wrapped::create_wrapped_coin_type`,
196-
type_arguments: [],
197-
arguments: [serialize(vaa)],
194+
typeArguments: [],
195+
functionArguments: [serialize(vaa)],
198196
},
199197
"Aptos.CreateWrappedCoinType",
200198
);
@@ -205,8 +203,8 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
205203
yield this.createUnsignedTx(
206204
{
207205
function: `${this.tokenBridgeAddress}::wrapped::create_wrapped_coin`,
208-
type_arguments: [assetType],
209-
arguments: [serialize(vaa)],
206+
typeArguments: [assetType],
207+
functionArguments: [serialize(vaa)],
210208
},
211209
"Aptos.CreateWrappedCoin",
212210
);
@@ -230,17 +228,17 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
230228
yield this.createUnsignedTx(
231229
{
232230
function: `${this.tokenBridgeAddress}::transfer_tokens::transfer_tokens_with_payload_entry`,
233-
type_arguments: [fullyQualifiedType],
234-
arguments: [amount, dstChain, dstAddress, nonce, payload],
231+
typeArguments: [fullyQualifiedType],
232+
functionArguments: [amount, dstChain, dstAddress, nonce, payload],
235233
},
236234
"Aptos.TransferTokensWithPayload",
237235
);
238236
} else {
239237
yield this.createUnsignedTx(
240238
{
241239
function: `${this.tokenBridgeAddress}::transfer_tokens::transfer_tokens_entry`,
242-
type_arguments: [fullyQualifiedType],
243-
arguments: [amount, dstChain, dstAddress, fee, nonce],
240+
typeArguments: [fullyQualifiedType],
241+
functionArguments: [amount, dstChain, dstAddress, fee, nonce],
244242
},
245243
"Aptos.TransferTokens",
246244
);
@@ -262,8 +260,8 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
262260
yield this.createUnsignedTx(
263261
{
264262
function: `${this.tokenBridgeAddress}::complete_transfer::submit_vaa_and_register_entry`,
265-
type_arguments: [assetType],
266-
arguments: [serialize(vaa)],
263+
typeArguments: [assetType],
264+
functionArguments: [serialize(vaa)],
267265
},
268266
"Aptos.CompleteTransfer",
269267
);
@@ -296,19 +294,20 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
296294
async getTypeFromExternalAddress(address: string): Promise<string | null> {
297295
try {
298296
// get handle
299-
const state = (
300-
await this.connection.getAccountResource(
301-
this.tokenBridgeAddress,
302-
`${this.tokenBridgeAddress}::state::State`,
303-
)
304-
).data as TokenBridgeState;
297+
const state = await this.connection.getAccountResource<TokenBridgeState>({
298+
accountAddress: this.tokenBridgeAddress,
299+
resourceType: `${this.tokenBridgeAddress}::state::State`,
300+
});
305301
const { handle } = state.native_infos;
306302

307303
// get type info
308-
const typeInfo = await this.connection.getTableItem(handle, {
309-
key_type: `${this.tokenBridgeAddress}::token_hash::TokenHash`,
310-
value_type: "0x1::type_info::TypeInfo",
311-
key: { hash: address },
304+
const typeInfo: any = await this.connection.getTableItem({
305+
handle,
306+
data: {
307+
key_type: `${this.tokenBridgeAddress}::token_hash::TokenHash`,
308+
value_type: "0x1::type_info::TypeInfo",
309+
key: { hash: address },
310+
},
312311
});
313312

314313
return typeInfo
@@ -346,7 +345,7 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
346345
}
347346

348347
private createUnsignedTx(
349-
txReq: Types.EntryFunctionPayload,
348+
txReq: InputGenerateTransactionPayloadData,
350349
description: string,
351350
parallelizable: boolean = false,
352351
): AptosUnsignedTransaction<N, C> {

platforms/aptos/protocols/tokenBridge/src/types.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
import type { TokenTypes } from "aptos";
1+
export interface TokenDataId {
2+
/** Token creator address */
3+
creator: string;
4+
5+
/** Unique name within this creator's account for this Token's collection */
6+
collection: string;
7+
8+
/** Name of Token */
9+
name: string;
10+
}
11+
12+
export interface TokenId {
13+
token_data_id: TokenDataId;
14+
15+
/** version number of the property map */
16+
property_version: string;
17+
}
218

319
export type TokenBridgeState = {
420
consumed_vaas: {
@@ -49,7 +65,7 @@ export type CreateTokenDataEvent = {
4965
type: "0x3::token::CreateTokenDataEvent";
5066
data: {
5167
description: string;
52-
id: TokenTypes.TokenDataId;
68+
id: TokenDataId;
5369
maximum: string;
5470
mutability_config: {
5571
description: boolean;
@@ -79,6 +95,6 @@ export type DepositEvent = {
7995
type: "0x3::token::DepositEvent";
8096
data: {
8197
amount: string;
82-
id: TokenTypes.TokenId;
98+
id: TokenId;
8399
};
84100
};

0 commit comments

Comments
 (0)