Skip to content

Commit

Permalink
aptos: migrate sdk to latest version (#766)
Browse files Browse the repository at this point in the history
* aptos: migrate sdk to latest version

use @aptos-labs/ts-sdk instead of deprecated aptos package

* fix tests
  • Loading branch information
kev1n-peters authored Jan 7, 2025
1 parent 641242c commit 5943b6b
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 224 deletions.
308 changes: 219 additions & 89 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions platforms/aptos/__tests__/integration/tokenBridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe("TokenBridge Tests", () => {
expect(attestTx!.chain).toEqual(chain);

const { transaction } = attestTx!;
expect(transaction.arguments).toHaveLength(0);
expect(transaction.functionArguments).toHaveLength(0);
});

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

const { transaction } = xferTx!;
expect(transaction.arguments).toHaveLength(5);
expect(transaction.functionArguments).toHaveLength(5);
// ...
});

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

const { transaction } = xferTx!;
expect(transaction.type_arguments).toHaveLength(1);
expect(transaction.arguments).toHaveLength(5);
expect(transaction.typeArguments).toHaveLength(1);
expect(transaction.functionArguments).toHaveLength(5);
});
});
});
Expand Down
8 changes: 6 additions & 2 deletions platforms/aptos/__tests__/unit/platform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AptosChains, AptosPlatform } from "./../../src/index.js";

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

const network = DEFAULT_NETWORK;

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

const client = new AptosClient(configs[APTOS_CHAINS[0]!]!.rpc);
const config = new AptosConfig({
fullnode: configs[APTOS_CHAINS[0]!]!.rpc,
network: AptosNetwork.MAINNET,
});
const client = new Aptos(config);
const tb = await p.getProtocol("TokenBridge", client);
expect(tb).toBeTruthy();
});
Expand Down
6 changes: 3 additions & 3 deletions platforms/aptos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"prettier": "prettier --write ./src"
},
"dependencies": {
"@wormhole-foundation/sdk-connect": "1.0.3",
"aptos": "1.21.0"
"@aptos-labs/ts-sdk": "^1.33.1",
"@wormhole-foundation/sdk-connect": "1.0.3"
},
"type": "module",
"typesVersions": {
Expand Down Expand Up @@ -99,4 +99,4 @@
}
}
}
}
}
13 changes: 5 additions & 8 deletions platforms/aptos/protocols/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
AptosPlatformType,
} from "@wormhole-foundation/sdk-aptos";
import { AptosPlatform } from "@wormhole-foundation/sdk-aptos";
import type { AptosClient, Types } from "aptos";
import { Aptos } from "@aptos-labs/ts-sdk";

export class AptosWormholeCore<N extends Network, C extends AptosChains>
implements WormholeCore<N, C>
Expand All @@ -26,7 +26,7 @@ export class AptosWormholeCore<N extends Network, C extends AptosChains>
constructor(
readonly network: N,
readonly chain: C,
readonly connection: AptosClient,
readonly connection: Aptos,
readonly contracts: Contracts,
) {
this.chainId = toChainId(chain);
Expand All @@ -46,7 +46,7 @@ export class AptosWormholeCore<N extends Network, C extends AptosChains>
}

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

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

Expand Down
93 changes: 46 additions & 47 deletions platforms/aptos/protocols/tokenBridge/src/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ import {
coalesceModuleAddress,
isValidAptosType,
} from "@wormhole-foundation/sdk-aptos";
import type { AptosClient, Types } from "aptos";
import { serializeForeignAddressSeeds } from "./foreignAddress.js";
import type { OriginInfo, TokenBridgeState } from "./types.js";
import { Aptos, InputGenerateTransactionPayloadData } from "@aptos-labs/ts-sdk";

export class AptosTokenBridge<N extends Network, C extends AptosChains>
implements TokenBridge<N, C>
Expand All @@ -48,7 +48,7 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
constructor(
readonly network: N,
readonly chain: C,
readonly connection: AptosClient,
readonly connection: Aptos,
readonly contracts: Contracts,
) {
this.chainId = toChainId(chain);
Expand All @@ -60,7 +60,7 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
}

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

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

originInfo = (
await this.connection.getAccountResource(
fqt[0]!,
`${this.tokenBridgeAddress}::state::OriginInfo`,
)
).data as OriginInfo;
const originInfo = await this.connection.getAccountResource<OriginInfo>({
accountAddress: fqt[0]!,
resourceType: `${this.tokenBridgeAddress}::state::OriginInfo`,
});

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

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

// check to see if we can get origin info from asset address
await this.connection.getAccountResource(
coalesceModuleAddress(assetFullyQualifiedType),
`${this.tokenBridgeAddress}::state::OriginInfo`,
);
await this.connection.getAccountResource({
accountAddress: coalesceModuleAddress(assetFullyQualifiedType),
resourceType: `${this.tokenBridgeAddress}::state::OriginInfo`,
});

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

async isTransferCompleted(vaa: TokenBridge.TransferVAA): Promise<boolean> {
const state = (
await this.connection.getAccountResource(
this.tokenBridgeAddress,
`${this.tokenBridgeAddress}::state::State`,
)
).data as TokenBridgeState;
const state = await this.connection.getAccountResource<TokenBridgeState>({
accountAddress: this.tokenBridgeAddress,
resourceType: `${this.tokenBridgeAddress}::state::State`,
});

const handle = state.consumed_vaas.elems.handle;

// check if vaa hash is in consumed_vaas
try {
// when accessing Set<T>, key is type T and value is 0
await this.connection.getTableItem(handle, {
key_type: "vector<u8>",
value_type: "u8",
key: `0x${Buffer.from(keccak256(vaa.hash)).toString("hex")}`,
await this.connection.getTableItem({
handle,
data: {
key_type: "vector<u8>",
value_type: "u8",
key: `0x${Buffer.from(keccak256(vaa.hash)).toString("hex")}`,
},
});
return true;
} catch {
Expand All @@ -179,8 +177,8 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
yield this.createUnsignedTx(
{
function: `${this.tokenBridgeAddress}::attest_token::attest_token_entry`,
type_arguments: [assetType],
arguments: [],
typeArguments: [assetType],
functionArguments: [],
},
"Aptos.AttestToken",
);
Expand All @@ -193,8 +191,8 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
yield this.createUnsignedTx(
{
function: `${this.tokenBridgeAddress}::wrapped::create_wrapped_coin_type`,
type_arguments: [],
arguments: [serialize(vaa)],
typeArguments: [],
functionArguments: [serialize(vaa)],
},
"Aptos.CreateWrappedCoinType",
);
Expand All @@ -205,8 +203,8 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
yield this.createUnsignedTx(
{
function: `${this.tokenBridgeAddress}::wrapped::create_wrapped_coin`,
type_arguments: [assetType],
arguments: [serialize(vaa)],
typeArguments: [assetType],
functionArguments: [serialize(vaa)],
},
"Aptos.CreateWrappedCoin",
);
Expand All @@ -230,17 +228,17 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
yield this.createUnsignedTx(
{
function: `${this.tokenBridgeAddress}::transfer_tokens::transfer_tokens_with_payload_entry`,
type_arguments: [fullyQualifiedType],
arguments: [amount, dstChain, dstAddress, nonce, payload],
typeArguments: [fullyQualifiedType],
functionArguments: [amount, dstChain, dstAddress, nonce, payload],
},
"Aptos.TransferTokensWithPayload",
);
} else {
yield this.createUnsignedTx(
{
function: `${this.tokenBridgeAddress}::transfer_tokens::transfer_tokens_entry`,
type_arguments: [fullyQualifiedType],
arguments: [amount, dstChain, dstAddress, fee, nonce],
typeArguments: [fullyQualifiedType],
functionArguments: [amount, dstChain, dstAddress, fee, nonce],
},
"Aptos.TransferTokens",
);
Expand All @@ -262,8 +260,8 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
yield this.createUnsignedTx(
{
function: `${this.tokenBridgeAddress}::complete_transfer::submit_vaa_and_register_entry`,
type_arguments: [assetType],
arguments: [serialize(vaa)],
typeArguments: [assetType],
functionArguments: [serialize(vaa)],
},
"Aptos.CompleteTransfer",
);
Expand Down Expand Up @@ -296,19 +294,20 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
async getTypeFromExternalAddress(address: string): Promise<string | null> {
try {
// get handle
const state = (
await this.connection.getAccountResource(
this.tokenBridgeAddress,
`${this.tokenBridgeAddress}::state::State`,
)
).data as TokenBridgeState;
const state = await this.connection.getAccountResource<TokenBridgeState>({
accountAddress: this.tokenBridgeAddress,
resourceType: `${this.tokenBridgeAddress}::state::State`,
});
const { handle } = state.native_infos;

// get type info
const typeInfo = await this.connection.getTableItem(handle, {
key_type: `${this.tokenBridgeAddress}::token_hash::TokenHash`,
value_type: "0x1::type_info::TypeInfo",
key: { hash: address },
const typeInfo: any = await this.connection.getTableItem({
handle,
data: {
key_type: `${this.tokenBridgeAddress}::token_hash::TokenHash`,
value_type: "0x1::type_info::TypeInfo",
key: { hash: address },
},
});

return typeInfo
Expand Down Expand Up @@ -346,7 +345,7 @@ export class AptosTokenBridge<N extends Network, C extends AptosChains>
}

private createUnsignedTx(
txReq: Types.EntryFunctionPayload,
txReq: InputGenerateTransactionPayloadData,
description: string,
parallelizable: boolean = false,
): AptosUnsignedTransaction<N, C> {
Expand Down
22 changes: 19 additions & 3 deletions platforms/aptos/protocols/tokenBridge/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import type { TokenTypes } from "aptos";
export interface TokenDataId {
/** Token creator address */
creator: string;

/** Unique name within this creator's account for this Token's collection */
collection: string;

/** Name of Token */
name: string;
}

export interface TokenId {
token_data_id: TokenDataId;

/** version number of the property map */
property_version: string;
}

export type TokenBridgeState = {
consumed_vaas: {
Expand Down Expand Up @@ -49,7 +65,7 @@ export type CreateTokenDataEvent = {
type: "0x3::token::CreateTokenDataEvent";
data: {
description: string;
id: TokenTypes.TokenDataId;
id: TokenDataId;
maximum: string;
mutability_config: {
description: boolean;
Expand Down Expand Up @@ -79,6 +95,6 @@ export type DepositEvent = {
type: "0x3::token::DepositEvent";
data: {
amount: string;
id: TokenTypes.TokenId;
id: TokenId;
};
};
Loading

0 comments on commit 5943b6b

Please sign in to comment.