@layerzerolabs/lz-initia-sdk-v2
The Initia SDK is a comprehensive SDK designed to interact with the Initia blockchain. It provides a set of utilities and modules to facilitate the development and integration of applications with the Initia 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 Initia SDK, you can use npm or yarn:
npm install @layerzerolabs/lz-initia-sdk-v2
or
yarn add @layerzerolabs/lz-initia-sdk-v2
Usage
Counter SDK Usage
Initialization
import { Stage } from "@layerzerolabs/lz-definitions";
import { SDK as InitiaSDK } from "@layerzerolabs/lz-initia-sdk-v2";
import { InitiaProvider } from "@layerzerolabs/lz-corekit-initia";
import { Counter, Endpoint } from "@layerzerolabs/lz-movevm-sdk-v2";
const sdk = new InitiaSDK({
stage: Stage.SANDBOX,
provider: InitiaProvider.from(url),
}).LayerzeroModule;
const counter = sdk.Counter;
const endpoint = sdk.Endpoint;
getCount
const count = await counter.getCount();
send increment
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'/118'/0'/0/26",
mnemonic: 'test test test test test test test test test test test junk',
}
const eid = SandboxV2EndpointId.INITIA_V2_SANDBOX
const msgType = 1
const value = 0
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
async quote(
eid: number,
msgType: number,
value: string | number = 0
): Promise<[number, number]> {
const receiver = await counter.getPeer(remoteEid)
if (receiver === '') {
throw new Error(`Initia 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
)
}
getExecutorLzReceiveOptions(msgType: number, value: string | number = 0): Options {
const options = Options.newOptions()
if (msgType === 1) {
options.addExecutorLzReceiveOption(300000, value)
} else if (msgType === 2) {
options.addExecutorLzReceiveOption(200000, value).addExecutorComposeOption(0, 200000, 0)
} else if (msgType === 3) {
options.addExecutorLzReceiveOption(500000, 20000)
} else if (msgType === 4) {
options.addExecutorLzReceiveOption(200000, value).addExecutorComposeOption(0, 500000, '200000000000000')
}
return options
}
OFT SDK Usage
Initialization
import { SDK as InitiaSDK } from "@layerzerolabs/lz-initia-sdk-v2";
import { InitiaProvider } from "@layerzerolabs/lz-corekit-initia";
import { Oft } from "@layerzerolabs/lz-movevm-sdk-v2";
const address = "0x123";
const sdk = new InitiaSDK({
stage: Stage.SANDBOX,
provider: InitiaProvider.from(url),
accounts: {
oft: address,
},
});
const oft = new Oft(sdk, false);
get balance
import { MnemonicAndPath } from '@layerzerolabs/move-definitions'
const address = '0x123'
const sdk = ...
const oft = new Oft(sdk, false)
const sender: MnemonicAndPath = {
path: "m/44'/118'/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
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
)
import { SandboxV2EndpointId } from '@layerzerolabs/lz-definitions'
import { addressToBytes32 } from '@layerzerolabs/lz-v2-utilities'
import { MnemonicAndPath } from '@layerzerolabs/move-definitions'
const sdk = ...
const oft = new Oft(sdk, false)
const amountLD = 100
const dstEid = SandboxV2EndpointId.INITIA_V2_SANDBOX
const payInLzToken = false
const sender: MnemonicAndPath = {
path: "m/44'/118'/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
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)
import { SandboxV2EndpointId } from '@layerzerolabs/lz-definitions'
import { addressToBytes32 } from '@layerzerolabs/lz-v2-utilities'
import { MnemonicAndPath } from '@layerzerolabs/move-definitions'
const sdk = ...
const oft = new Oft(sdk, false)
const amountLD = 100
const dstEid = SandboxV2EndpointId.INITIA_V2_SANDBOX
const payInLzToken = false
const sender: MnemonicAndPath = {
path: "m/44'/118'/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
const options = Uint8Array.from([])
const composeMessage = Uint8Array.from([])
const [nativeFee, lzTokenFee] = await oft.quoteSend(
userSender,
dstEid,
toBytes32,
BigInt(amountLD),
minAmountLD,
payInLzToken,
options,
composeMessage
)
send
const sdk = ...
const oft = new Oft(sdk, false)
const amountLD = 100
const dstEid = SandboxV2EndpointId.INITIA_V2_SANDBOX
const payInLzToken = false
const sender: MnemonicAndPath = {
path: "m/44'/118'/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
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