UNPKG

@layerzerolabs/lz-aptos-sdk-v2

Version:

289 lines (249 loc) 8.71 kB
# @layerzerolabs/lz-aptos-sdk-v2 The Aptos SDK is a comprehensive SDK designed to interact with the Aptos blockchain. It provides a set of utilities and modules to facilitate the development and integration of applications with the Aptos blockchain. ## Features - **Price Feed Management**: Interact with price feeds, set and get prices. - **ULN Configuration**: Manage ULN configurations for sending and receiving messages. - **Executor Configuration**: Set and get executor configurations. - **Worker Management**: Manage worker configurations and price feeds. - **View Functions**: Execute view functions to retrieve data from the blockchain. - **Transaction Management**: Build, sign, and send transactions. - **Counter Management**: Interact with Counter, getCount, quote and send increment transactions. - **OFT Management**: Interact with OFT, getBalance, quote and send transactions. ## Installation To install the Aptos SDK, you can use npm or yarn: ```sh npm install @layerzerolabs/lz-aptos-sdk-v2 ``` or ```sh yarn add @layerzerolabs/lz-aptos-sdk-v2 ``` ## Usage ### Counter SDK Usage #### Initialization ```typescript import { Stage } from "@layerzerolabs/lz-definitions"; import { SDK as AptosSDK } from "@layerzerolabs/lz-aptos-sdk-v2"; import { Aptos, AptosConfig } from "@aptos-labs/ts-sdk"; import { Counter, Endpoint } from "@layerzerolabs/lz-movevm-sdk-v2"; // url is the aptos chain full node url const sdk = new AptosSDK({ stage: Stage.SANDBOX, provider: new Aptos(new AptosConfig({ fullnode: url })), }).LayerzeroModule; const counter = sdk.Counter; const endpoint = sdk.Endpoint; ``` #### getCount ```typescript const count = await counter.getCount(); ``` #### send increment ```typescript import { SandboxV2EndpointId } from '@layerzerolabs/lz-definitions' import { Options } from '@layerzerolabs/lz-v2-utilities' import { MnemonicAndPath } from '@layerzerolabs/move-definitions' const counter = sdk.Counter const endpoint = sdk.Endpoint const sender: MnemonicAndPath = { path: "m/44'/637'/0'/0'/26'", mnemonic: 'test test test test test test test test test test test junk', } const eid = SandboxV2EndpointId.APTOS_V2_SANDBOX const msgType = 1 // (VANILLA=1, COMPOSED=2, ABA=3, COMPOSED_ABA=4) const value = 0 // native drop amount const executorOptions = getExecutorLzReceiveOptions(msgType, value) const [feeInNative] = await quote(eid, msgType, value) const response = await this.counter.send( sender, eid, msgType, BigInt(feeInNative), executorOptions.toBytes() ) const txHash = response.hash // get send increment transaction hash. // @return (native_fee, zro_fee) async quote( eid: number, msgType: number, value: string | number = 0 ): Promise<[number, number]> { const receiver = await counter.getPeer(remoteEid) if (receiver === '') { throw new Error(`Aptos Counter Peer not set for ${eid}`) } const message = Uint8Array.from([1, 2, 3, 4, 5]) return endpoint.quoteView( counterAddress, eid, receiver, message, getExecutorLzReceiveOptions(msgType, value).toBytes(), false // whether pay in LzToken ) } getExecutorLzReceiveOptions(msgType: number, value: string | number = 0): Options { const options = Options.newOptions() if (msgType === 1) { // VANILLA // A -> B options.addExecutorLzReceiveOption(300000, value) } else if (msgType === 2) { // COMPOSED // A -> B1 -> B2 options.addExecutorLzReceiveOption(200000, value).addExecutorComposeOption(0, 200000, 0) } else if (msgType === 3) { // ABA // A -> B -> A options.addExecutorLzReceiveOption(500000, 20000) } else if (msgType === 4) { // COMPOSED_ABA // A -> B1 -> B2 -> A options.addExecutorLzReceiveOption(200000, value).addExecutorComposeOption(0, 500000, '200000000000000') } return options } ``` ### OFT SDK Usage #### Initialization ```typescript import { SDK as AptosSDK } from "@layerzerolabs/lz-aptos-sdk-v2"; import { Aptos, AptosConfig } from "@aptos-labs/ts-sdk"; import { Oft } from "@layerzerolabs/lz-movevm-sdk-v2"; // your oft contract address. const address = "0x123"; // url is the aptos chain full node url const sdk = new AptosSDK({ stage: Stage.SANDBOX, provider: new Aptos(new AptosConfig({ fullnode: url })), accounts: { oft: address, }, }); // false means native type, true is adapter type. const oft = new Oft(sdk, false); ``` #### get balance ```typescript import { MnemonicAndPath } from '@layerzerolabs/move-definitions' const address = '0x123' const sdk = ... // false means native type, true is adapter type. const oft = new Oft(sdk, false) const sender: MnemonicAndPath = { path: "m/44'/637'/0'/0'/45'", mnemonic: 'test test test test test test test test test test test junk', } const originalBalance = await oft.balanceOf(address ?? sdk.accountToAddress(sender)) const metadata = await oft.metadata() const decimals = metadata.decimals // convert balance into a human-readable string representation const balance = (BigInt(originalBalance) / BigInt(10) ** BigInt(decimals)).toString() ``` #### quote oft Quote the OFT for a particular send without sending ``` @return ( oft_limit: The minimum and maximum limits that can be sent to the recipient fees: The fees that will be applied to the amount sent amount_sent_ld: The amount that would be debited from the sender in local decimals amount_received_ld: The amount that would be received by the recipient in local decimals ) ``` ```typescript import { SandboxV2EndpointId } from '@layerzerolabs/lz-definitions' import { addressToBytes32 } from '@layerzerolabs/lz-v2-utilities' import { MnemonicAndPath } from '@layerzerolabs/move-definitions' const sdk = ... // false means native type, true is adapter type. const oft = new Oft(sdk, false) const amountLD = 100 const dstEid = SandboxV2EndpointId.APTOS_V2_SANDBOX const payInLzToken = false const sender: MnemonicAndPath = { path: "m/44'/637'/0'/0'/45'", mnemonic: 'test test test test test test test test test test test junk', } const userSender = sdk.accountToAddress(sender) const toBytes32 = addressToBytes32(userSender) const minAmountLD = (BigInt(amountLD) * 9n) / 10n // 10% tolerance const options = Uint8Array.from([]) const composeMessage = Uint8Array.from([]) const [oftLimit, oftFeeDetails, amountSentLD, amountReceivedLD] = await oft.quoteOft( dstEid, toBytes32, BigInt(amountLD), minAmountLD, options, composeMessage ) ``` #### quote send Quote the network fees for a particular send @return (native_fee, zro_fee) ```typescript import { SandboxV2EndpointId } from '@layerzerolabs/lz-definitions' import { addressToBytes32 } from '@layerzerolabs/lz-v2-utilities' import { MnemonicAndPath } from '@layerzerolabs/move-definitions' const sdk = ... // false means native type, true is adapter type. const oft = new Oft(sdk, false) const amountLD = 100 const dstEid = SandboxV2EndpointId.APTOS_V2_SANDBOX const payInLzToken = false const sender: MnemonicAndPath = { path: "m/44'/637'/0'/0'/45'", mnemonic: 'test test test test test test test test test test test junk', } const userSender = sdk.accountToAddress(sender) const toBytes32 = addressToBytes32(userSender) const minAmountLD = (BigInt(amountLD) * 9n) / 10n // 10% tolerance const options = Uint8Array.from([]) const composeMessage = Uint8Array.from([]) const [nativeFee, lzTokenFee] = await oft.quoteSend( userSender, dstEid, toBytes32, BigInt(amountLD), minAmountLD, payInLzToken, options, composeMessage ) ``` #### send ```typescript const sdk = ... // false means native type, true is adapter type. const oft = new Oft(sdk, false) const amountLD = 100 const dstEid = SandboxV2EndpointId.APTOS_V2_SANDBOX const payInLzToken = false const sender: MnemonicAndPath = { path: "m/44'/637'/0'/0'/45'", mnemonic: 'test test test test test test test test test test test junk', } const userSender = sdk.accountToAddress(sender) const toBytes32 = addressToBytes32(userSender) const minAmountLD = (BigInt(amountLD) * 9n) / 10n // 10% tolerance const options = Uint8Array.from([]) const composeMessage = Uint8Array.from([]) const [nativeFee, lzTokenFee] = await oft.quoteSend( userSender, dstEid, toBytes32, BigInt(amountLD), minAmountLD, payInLzToken, options, composeMessage ) const response = await oft.send( sender, dstEid, toBytes32, BigInt(amountLD), minAmountLD, BigInt(nativeFee), BigInt(lzTokenFee), options, composeMessage ) const txHash = response.hash // get send transaction hash. ```