Code Monkey home page Code Monkey logo

sui-kit's Introduction

Tookit for interacting with SUI network

中文文档

Features

  • Much easier to use than the official SDK
  • Transfer SUI & Custom Coin
  • Request faucet from devnet, testnet
  • Stake SUI
  • Compatible with programmable transaction
  • Publish move packages
  • Inspection of transaction (gasless transaction for inspection)
  • Advanced features: multi-accounts

Pre-requisites

1, Install the package

npm install @scallop-dao/sui-kit

2, Install SUI cli (optional: only needed for publishing packages)

Please refer to the official documentation: How to install SUI cli

How to use

Transfer coins

You can use SuiKit to transfer SUI and other coins.

/**
 * This is an example of using SuiKit to transfer coins from one account to another.
 */
import { SuiKit } from '@scallop-dao/sui-kit';

// Supports both hex and base64 secret key
const secretKey = '<Secret key>';
const suiKit = new SuiKit({ secretKey });
const recipient = '0xCAFE';
suiKit.transferSui(recipient, 1000).then(() => console.log('transfered 1000 SUI'));
suiKit.transferCoin(recipient, 1000, '0xCOFFEE::coin::COIN').then(
  () => console.log('transfered 1000 COIN')
);

Request faucet

You can use SuiKit to request faucet from devnet or testnet.

import { SuiKit } from '@scallop-dao/sui-kit';

const secretKey = '<Secret key>';
const suiKit = new SuiKit({ secretyKey,  networkType: 'devnet' });
suiKit.requestFaucet().then(() => {
  console.log('Faucet request success');
});

Stake SUI

You can use SuiKit to stake SUI.

/**
 * This is an example of using SuiKit to stake SUI
 */
import { SuiKit } from '@scallop-dao/sui-kit';

const secretKey = '<Secret key>';
const suiKit = new SuiKit({ secretyKey,  networkType: 'devnet' });
const stakeAmount = 1000;
const validatorAddress = '0x123';
suiKit.stakeSui(stakeAmount, validatorAddress).then(() => {
  console.log('Stake SUI success');
});

Programmable transaction

With programmable transaction, you can send a transaction with multiple actions. The following example shows how to transfer SUI to multiple accounts in one transaction.

/**
 * This example shows how to use programmable transaction with SuiKit
 */

import { SuiKit, TransactionBlock } from '@scallop-dao/sui-kit';

const secretKey = '<Secret key>';
const suiKit = new SuiKit({ secretKey });

// build a transaction block to send coins to multiple accounts
const tx = new TransactionBlock();

const recipients = ['0x123', '0x456', '0x789'];
recipients.forEach(recipient => {
  const [coin] = tx.splitCoins(tx.gas, [tx.pure(1000)]);
  tx.transferObjects([coin], tx.pure(recipient));
});

// send the transaction block
suiKit.signAndSendTxn(tx).then(response => {
  console.log('Transaction digest: ' + response.digest);
});

Publish move packages

You can use SuiKit to publish move packages to the SUI network. Notice: you need to install SUI cli first. (see Pre-requisites)

/**
 * This is an example of using SuiKit to publish a move package
 */
import { SuiKit } from '@scallop-dao/sui-kit';

(async() => {
  const secretKey = '<Secret key>';
  const suiKit = new SuiKit({ secretKey, networkType: 'devnet' });
  const balance = await suiKit.getBalance();
  if (balance.totalBalance <= 3000) {
    await suiKit.requestFaucet();
  }
  // Wait for 3 seconds before publish package
  await new Promise(resolve => setTimeout(() => resolve(true), 3000));

  const packagePath = path.join(__dirname, './example/sample_move/custom_coin');
  const result = await suiKit.publishPackage(packagePath);
  console.log('packageId: ' + result.packageId);
})();

Advanced features

Multi-accounts

SuiKit follows bip32 & bip39 standard, so you can use it to manage multiple accounts. When init SuiKit, you can pass in your mnemonics to create a wallet with multiple accounts.

/**
 * This is an example of using SuiKit to manage multiple accounts.
 */
import { SuiKit } from '@scallop-dao/sui-kit';

async function checkAccounts(suiKit: SuiKit) {
  const displayAccounts = async (suiKit: SuiKit, accountIndex: number) => {
    const coinType = '0x2::sui::SUI'
    const addr = suiKit.getAddress({accountIndex})
    const balance = (await suiKit.getBalance(coinType, {accountIndex})).totalBalance
    console.log(`Account ${accountIndex}: ${addr} has ${balance} SUI`)
  }
  // log the first 10 accounts
  const numAccounts = 10
  for (let i = 0; i < numAccounts; i++) {
    await displayAccounts(suiKit, i)
  }
}

async function internalTransferSui(suiKit: SuiKit, fromAccountIndex: number, toAccountIndex: number, amount: number) {
  const toAddr = suiKit.getAddress({accountIndex: toAccountIndex })
  console.log(`Transfer ${amount} SUI from account ${fromAccountIndex} to account ${toAccountIndex}`)
  return await suiKit.transferSui(toAddr, amount,  {accountIndex: fromAccountIndex})
}

const mnemonics = process.env.MNEMONICS;
const suiKit = new SuiKit({ mnemonics })
checkAccounts(suiKit).then(() => {})
// transfer 1000 SUI from account 0 to account 1
internalTransferSui(suiKit, 0, 1, 1000).then(() => {})

sui-kit's People

Contributors

zengxinhai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.