@layerzerolabs/ui-bridge-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

LayerZero

@layerzerolabs/ui-bridge-sdk

NPM Version Downloads Snyk Vulnerabilities for npm package version NPM License

Installation

yarn add @layerzerolabs/ui-bridge-sdk

pnpm add @layerzerolabs/ui-bridge-sdk

npm install @layerzerolabs/ui-bridge-sdk

Usage

import BridgeApi from '@layerzerolabs/ui-bridge-sdk';
import {
  AdapterParams,
  CurrencyAmount,
  getNativeCurrency,
  parseCurrencyAmount,
} from '@layerzerolabs/ui-core';
import {waitForMessageReceived} from '@layerzerolabs/scan-client';

async function test() {
  // evm
  const srcBridge: BridgeApi<any, {eqReward: CurrencyAmount}> = null!;
  // aptos
  const dstBridge: BridgeApi<any, {eqReward: CurrencyAmount}> = null!;

  const srcWallet = {address: '0x', signer: {}};
  const dstWallet = {address: '0x', signer: {}};

  const srcChainId = ChainId.ETHEREUM;
  const dstChainId = ChainId.APTOS;

  const srcCurrency = getNativeCurrency(srcChainId);
  const dstCurrency = getNativeCurrency(dstChainId);

  const srcNative = getNativeCurrency(srcChainId);
  const dstNative = getNativeCurrency(dstChainId);

  const amount = parseCurrencyAmount(srcCurrency, '10');
  const output = await srcBridge.getOutput(amount, dstCurrency);
  const minAmount = output.amount;

  if (!(await srcBridge.isApproved(amount, srcWallet.address))) {
    const tx = await srcBridge.approve(amount);
    const result = await tx.signAndSubmitTransaction(srcWallet.signer);
  }

  // todo: add getBalance to srcBridge
  const dstNative = await dstBridge.getBalance(dstCurrency, '10');

  const registerCost = await (
    await srcBridge.register(dstCurrency)
  ).estimateNative(dstWallet.signer);

  if (!(await dstBridge.isRegistered(dstCurrency, srcWallet.address))) {
    if (dstNative.greaterThan(registerCost)) {
      const tx = await dstBridge.register(dstCurrency);
      const result = await tx.signAndSubmitTransaction(srcWallet.signer);
    }
  }

  const extraGas = await srcBridge.getExtraGas(srcChainId, dstChainId);

  const adapterParams = AdapterParams.forV2({
    extraGas,
    dstNativeAddress: dstWallet.address,
    dstNativeAmount: parseCurrencyAmount(dstNative, '10'),
  });

  const fee = await srcBridge.getMessageFee(srcChainId, dstChainId, adapterParams);

  const swap = await srcBridge.transfer({
    amount,
    minAmount,
    srcCurrency,
    dstCurrency,
    adapterParams,
    fee,
    dstAddress: dstWallet.address,
    srcAddress: srcWallet.address,
    dstChainId,
    srcChainId,
  });

  const result = await swap.signAndSubmitTransaction(srcWallet.signer);

  const receipt = await result.wait();
  const message = await waitForMessageReceived(srcChainId, receipt.txHash);

  console.log('Dst txHash', message.dstTxHash);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @layerzerolabs/ui-bridge-sdk

Weekly Downloads

94

Version

1.0.6

License

BUSL-1.1

Unpacked Size

15.6 kB

Total Files

7

Last publish

Collaborators

  • lzadmin
  • layerzero-bot
  • _cb
  • mrowa44
  • smith-layerzero
  • layerzero-piotr