@layerzerolabs/scan-client
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

LayerZero

@layerzerolabs/scan-client

NPM Version Downloads Snyk Vulnerabilities for npm package version NPM License

Installation

yarn add @layerzerolabs/scan-client

pnpm add @layerzerolabs/scan-client

npm install @layerzerolabs/scan-client

Usage

createClient

import {createClient} from '@layerzerolabs/scan-client';

// Initialize a client with the desired environment
const client = createClient('testnet');

// Get a list of messages by transaction hash
const {messages} = await client.getMessagesBySrcTxHash(
  '0xc6071e98336996084a18a6c47ab38449e31d7d4b48040f35bbcd4106189e3036',
);

// There also is a convenience method that creates the client
// and calls getMessagesBySrcTxHash
import {getMessagesBySrcTxHash} from '@layerzerolabs/scan-client';

const {messages} = getMessagesBySrcTxHash(
  101,
  '0xc6071e98336996084a18a6c47ab38449e31d7d4b48040f35bbcd4106189e3036',
);

// The items in the messages adhere to the following type spec
type Message = {
  srcUaAddress: string;
  dstUaAddress: string;
  srcChainId: number;
  dstChainId: number;
  dstTxHash?: string;
  dstTxError?: string;
  srcTxHash: string;
  srcBlockHash: string;
  srcBlockNumber: string;
  srcUaNonce: number;
  status: MessageStatus;
};

enum MessageStatus {
  INFLIGHT = 'INFLIGHT',
  DELIVERED = 'DELIVERED',
  FAILED = 'FAILED',
  PAYLOAD_STORED = 'PAYLOAD_STORED',
  BLOCKED = 'BLOCKED',
  CONFIRMING = 'CONFIRMING',
}

For new transactions the message list may be empty. Polling should be implemented at the application level since it is app specific. Expect the messages list to be empty for recent transactions, before moving to status INFLIGHT, and finally status DELIVERED.

waitForMessageReceived

We've extracted a common use case for the scan client into the helper waitForMessageReceived. Provide the srcChainId, srcTxHash, and optional pollInterval and receive a promise that resolves when the message status is updated to DELIVERED.

Call signature

function waitForMessageReceived(
  srcChainId: number,
  srcTxHash: string,
  pollInterval = 3000,
): Promise<DeliveredMessage>;

Example usage:

import {waitForMessageReceived} from '@layerzerolabs/scan-client';

...

waitForMessageReceived(srcChainId, receipt.txHash)
  .then((message) => {
    updateTx({
      completed: true,
      confirmation: {
        chainId: message.dstChainId,
        txHash: message.dstTxHash,
      },
    });
  })
  .finally(() => {
    updateBalances();
  });

Readme

Keywords

none

Package Sidebar

Install

npm i @layerzerolabs/scan-client

Weekly Downloads

3,728

Version

0.0.8

License

BUSL-1.1

Unpacked Size

22.8 kB

Total Files

7

Last publish

Collaborators

  • layerzero-bot