Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@layerzerolabs/lz-initia-sdk-v2

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@layerzerolabs/lz-initia-sdk-v2

  • 3.0.27
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@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";

// url is the initia chain full node url
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 // (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(`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 // 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
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";

// your oft contract address.
const address = "0x123";
// url is the initia chain full node url
const sdk = new InitiaSDK({
  stage: Stage.SANDBOX,
  provider: InitiaProvider.from(url),
  accounts: {
    oft: address,
  },
});
// false means native type, true is adapter type.
const oft = new Oft(sdk, false);
get balance
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'/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
// 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
)
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.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 // 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)

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.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 // 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
const sdk = ...
// false means native type, true is adapter type.
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 // 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.

FAQs

Package last updated on 11 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc