1.1.4 • Published 4 months ago

intmax2-server-sdk v1.1.4

Weekly downloads
-
License
-
Repository
-
Last release
4 months ago

INTMAX2-SERVER-SDK

This SDK is a client library for the INTMAX API. It is designed to help you integrate INTMAX services into your applications.

Installation for Node.js

  npm install intmax2-server-sdk

or

  pnpm install intmax2-server-sdk

or

  yarn add intmax2-server-sdk

Interface

export interface INTMAXClient {
  // properties
  tokenBalances: TokenBalance[] | undefined;
  address: string; // IntMax public_key
  isLoggedIn: boolean;

  // account
  fetchTokenBalances: () => Promise<TokenBalancesResponse>;
  getPrivateKey: () => Promise<string | undefined>;
  signMessage: (message: string) => Promise<SignMessageResponse>;
  verifySignature: (signature: SignMessageResponse, message: string | Uint8Array) => Promise<boolean>;

  // transaction
  fetchTransactions: (params: FetchTransactionsRequest) => Promise<Transaction[]>;
  broadcastTransaction: (
    rawTransfers: BroadcastTransactionRequest[],
    isWithdrawal: boolean,
  ) => Promise<BroadcastTransactionResponse>;
  waitForTransactionConfirmation: (
    params: WaitForTransactionConfirmationRequest,
  ) => Promise<WaitForTransactionConfirmationResponse>;

  // deposit
  deposit: (params: PrepareDepositTransactionRequest) => Promise<PrepareDepositTransactionResponse>;
  fetchDeposits: (params: FetchTransactionsRequest) => Promise<(Transaction | null)[]>;

  // withdrawal
  fetchPendingWithdrawals: (params: FetchWithdrawalsRequest) => Promise<FetchWithdrawalsResponse>;
  withdraw: (params: WithdrawRequest) => Promise<WithdrawalResponse>;
  claimWithdrawal: (params: ContractWithdrawal[]) => Promise<ClaimWithdrawalTransactionResponse>;

  // Fees
  getTransferFee: (token: Token) => Promise<FeeResponse>;
  getWithdrawalFee: () => Promise<FeeResponse>;
  getClaimFee: () => Promise<FeeResponse>;

  // additional services
  login: () => Promise<LoginResponse>;
  logout: () => Promise<void>;
  getTokensList: () => Promise<Token[]>;
}

Usage

Initialization

const { IntmaxNodeClient } = require('intmax2-server-sdk');

const intMaxClient = new IntMaxNodeClient({
  environment: 'devnet', //  'mainnet' | 'devnet' | 'testnet'
  eth_private_key: process.env.ETH_PRIVATE_KEY,
  l1_rpc_url: process.env.L1_RPC_URL, // better to paste your own rpc url, by default it will be use public RPC.
});

Login to get wallet

Here you should sign two message, they will be appeared in the popup window automatically.:

  1. Sign the message confirm your ETH wallet address.
  2. Sign the message with challenge string.
await intmaxClient.login();
const address = this.intmaxClient.address; // Public key of the wallet
const privateKey = this.intmaxClient.getPrivateKey(); // Private key of the wallet. Here you should sign message.

Sign message

const message = 'Hello, World!';
const signature = await intmaxClient.signMessage(message);

Verify signature

const message = 'Hello, World!';
const signature = await intmaxClient.signMessage(message);

const isVerified = await intmaxClient.verifySignature(signature, message);
console.log(isVerified); // true

const isFakeMessageVerify = await intmaxClient.verifySignature(signature, 'Another message');
console.log(isFakeMessageVerify); // false

const isFakeSignatureVerify = await intmaxClient.verifySignature('Another signature', message);
console.log(isFakeSignatureVerify); // false

Get tokens list

const tokens = await intmaxClient.getTokensList();
// tokens: {
//    contractAddress: string;
//    decimals?: number;
//    image?: string;
//    price: number;
//    symbol?: string;
//    tokenIndex: number;
//    tokenType: TokenType;
// }[]

Get token balances

const { balances } = await intmaxClient.fetchTokenBalances();
// balances: {
//    token: Token; // Check get tokens list response
//    amount: bigint;
// }

Deposit Native Token (ETH)

const amount = 0.1; // Amount of the token
const tokens = await intmaxClient.getTokensList(); // Get list of the tokens
let token = tokens.find((token) => token.tokenIndex === 0); // Find token by symbol

if (token) {
  token = {
    ...token,
    tokenType: TokenType.NATIVE,
  };
}

// Estimate gas
const gas = await intmaxClient.estimateDepositGas({
  amount,
  token,
  address, // Your public key of the IntMax wallet or any other IntMax wallet public key
  isGasEstimation: true,
});

// Deposit
const deposit = await intmaxClient.deposit({
  amount,
  token,
  address,
});
// Deposit response
// {
//  txHash: `0x${string}`;
//  status: TransactionStatus;
// }

Deposit ERC20

const amount = 0.1; // Amount of the token
const tokens = await intmaxClient.getTokensList(); // Get list of the tokens
let token = tokens.find((token) => token.tokenIndex === 0); // Find token by symbol

if (!token) {
  token = {
    decimals: 18, // Decimals of the token
    tokenType: TokenType.ERC20,
    contractAddress: '0x....', // Your Token address if not exist on token list
  };
} else {
  token = {
    ...token,
    tokenType: TokenType.ERC20,
  };
}

// Estimate gas if need to show for user
const gas = await intmaxClient.estimateDepositGas({
  amount,
  token,
  address, // Your public key of the IntMax wallet or any other IntMax wallet public key
  isGasEstimation: true,
});

// Deposit
const deposit = await intmaxClient.deposit({
  amount,
  token,
  address,
});
// Deposit response
// {
//  txHash: `0x${string}`;
//  status: TransactionStatus;
// }

Deposit ERC721 / ERC1155

const amount = 1; // Amount of the token for erc721 should be 1, for erc1155 can be more than 1
const token = {
  tokenIndex: 1, // Nft id in contract
  tokenType: TokenType.ERC721, // or TokenType.ERC1155
  contractAddress: '0x....', // Your Token address if not exist on token list
};

// Estimate gas if need to show for user
const gas = await intmaxClient.estimateDepositGas({
  amount,
  token,
  address, // Your public key of the IntMax wallet or any other IntMax wallet public key
  isGasEstimation: true,
});

// Deposit
const deposit = await intmaxClient.deposit({
  amount,
  token,
  address,
});
// Deposit response
// {
//  txHash: `0x${string}`;
//  status: TransactionStatus;
// }

Withdraw

const amount = 0.1; // Amount of the token, for erc721 should be 1, for erc1155 can be more than 1
const { balances } = await intmaxClient.fetchTokenBalances(); // fetch token balances

// You can change filtration by tokenIndex or tokenAddress
const token = balances.find((b) => b.token.tokenIndex === 0).token;

// Withdraw
const withdraw = await intmaxClient.withdraw({
  amount,
  token,
  address, // Your public key of ETH wallet
});
// Withdraw response
// {
//   txTreeRoot: string;
//   transferData: TransferData[];
//   withdrawalData: TransferData[];
//   transferUUIDs: string[];
//   withdrawalUUIDs: string[];
// }

Fetch withdrawals (needToClaim, etc.)

const withdrawals = await intmaxClient.fetchPendingWithdrawals(); // Record<WithdrawalsStatus, ContractWithdrawal[]>

Claim withdrawals

const withdrawals = await intmaxClient.fetchPendingWithdrawals(); // Record<WithdrawalsStatus, ContractWithdrawal[]>
const claim = await intmaxClient.claimWithdrawal(withdrawals.needClaim); // Claim response (should be add additional check for receiver address you can claim withdrawals only for your address)
// {
//   txHash: `0x${string}`;
//   status: TransactionStatus;
// }

Logout

await intmaxClient.logout();
1.1.4

4 months ago

1.1.3

4 months ago

1.1.2

4 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago