TypeScript SDK

The Supra L1 TypeScript SDK provides a convenient way to interact with and perform operations on Supra. It offers a set of utility functions, classes, and types to simplify the integration process and enhance developer productivity.

Install the TypeScript SDK

npm install supra-l1-sdk

Import the Package

import { HexString, SupraAccount, SupraClient, BCS } from "supra-l1-sdk";


Example

1

Initialize a new project

npm init && npm add -D typescript @types/node ts-node && npx tsc --init && mkdir src && > src/quickstart.ts
2

Install the TypeScript SDK

npm install supra-l1-sdk
3

Import dependencies

import {
  HexString,
  SupraAccount,
  SupraClient,
  BCS,
  TxnBuilderTypes,
} from "supra-l1-sdk";
4

Initialize the SupraClient

let supraClient = await SupraClient.init(
  "https://rpc-testnet.supra.com/"
);
5

Initialize a SupraAccount

let senderAccount = new SupraAccount(
  Uint8Array.from(
    Buffer.from(
      "2b9654793a999d1d487dabbd1b8f194156e15281fa1952af121cc97b27578d89",
      "hex"
    )
  )
);
6

Fund the account with the testnet faucet

console.log(
  "Funding Sender With Faucet: ",
  // To Fund Account With Test Supra Tokens
  await supraClient.fundAccountWithFaucet(senderAccount.address())
);
7

Set the receiver address

let receiverAddress = new HexString(
  "0xb8922417130785087f9c7926e76542531b703693fdc74c9386b65cf4427f4e80"
);
8

Transfer supra to the receiver address

// To Transfer Supra Coin From Sender To Receiver
let txResData = await supraClient.transferSupraCoin(
  senderAccount,
  receiverAddress,
  BigInt(100000000),
  {
    enableTransactionWaitAndSimulationArgs: {
      enableWaitForTransaction: true,
      enableTransactionSimulation: true,
    },
  }
);
console.log("Transfer SupraCoin TxRes: ", txResData);
9

Full quickstart code

quickstart.ts
import {
    HexString,
    SupraAccount,
    SupraClient
  } from "supra-l1-sdk";
  
  (async () => {
  
    // To Create Instance Of Supra Client, But In This Method We Don't Need To Pass ChainId.
    // ChainId Will Be Identified At Instance Creation Time By Making RPC Call.
    let supraClient = await SupraClient.init(
      "https://rpc-autonet.supra.com/"
    );
  
    //Init a SupraAccount from a private key.
    let senderAccount = new SupraAccount(
      Uint8Array.from(
        Buffer.from(
          "2b9654793a999d1d487dabbd1b8f194156e15281fa1952af121cc97b27578d89",
          "hex"
        )
      )
    );

    //Fund the sender account with the testnet faucet
    await supraClient.fundAccountWithFaucet(senderAccount.address())

    //Set the receiver address
    let receiverAddress = new HexString(
      "0xb8922417130785087f9c7926e76542531b703693fdc74c9386b65cf4427f4e80"
    );

    // To Transfer Supra Coin From Sender To Receiver
    let txResData = await supraClient.transferSupraCoin(
      senderAccount,
      receiverAddress,
      BigInt(1000),
      {
        enableTransactionWaitAndSimulationArgs: {
          enableWaitForTransaction: true,
          enableTransactionSimulation: true,
        },
      }
    );

    //Output the transaction data
    console.log("Transfer SupraCoin TxRes: ", txResData);

  })();
10

Execute the code: npx ts-node src/quickstart.ts