import BigNumber from "bignumber.js"; import { INSTRUCTION_PREFIX_TOKEN, MESSAGE_PREFIX_TOKEN, TOKEN_PROGRAM } from "./Constants"; import { decodeBase58 } from "./Encoding" import { getBytes } from "./TransactionHelper"; export enum TxType{ CreateATA, Transfer } export type BuildTokenTransactionArgs = { authorizer?: string owner: string, fromAssociatedTokenAccount: string, toAssociatedTokenAccount: string, tokenAddress: string, tokenDecimals: number, amount: string, fees: string, blockHash: string, signature?: Buffer txType: TxType } export function buildTokenTransactionHelper(args: BuildTokenTransactionArgs): any{ let ownerBytes = decodeBase58(args.owner); let fromAssociatedTokenAccountBytes = decodeBase58(args.fromAssociatedTokenAccount); let toAssociatedTokenAccountBytes = decodeBase58(args.toAssociatedTokenAccount); let tokenAddressBytes = decodeBase58(args.tokenAddress) let blockBytes = decodeBase58(args.blockHash); let buffer = Buffer.alloc(0); if(args.signature !=null){ buffer = Buffer.concat([ buffer, Buffer.from([0x01]), //Signature count args.signature//Signature ]); } //Add message prefix: buffer = Buffer.concat([ buffer, MESSAGE_PREFIX_TOKEN ]); //Add accounts buffer = Buffer.concat([ buffer, Buffer.from([0x05]), //Number of accounts ownerBytes, fromAssociatedTokenAccountBytes, toAssociatedTokenAccountBytes, tokenAddressBytes, TOKEN_PROGRAM ]); //Add blockhash buffer = Buffer.concat([buffer, blockBytes]); //Get amountBytes let decimals = new BigNumber(10).pow(new BigNumber(args.tokenDecimals)); let amountBN = new BigNumber(args.amount).times(decimals); let amount = BigInt(amountBN.toFixed()); let amountBytes = getBytes(amount).reverse(); while(amountBytes.length % 4 != 0){ amountBytes = Buffer.concat([amountBytes, Buffer.alloc(1)]) } let precisionBytes = getBytes(BigInt(args.tokenDecimals)); while(precisionBytes.length % 4 != 0){ precisionBytes = Buffer.concat([Buffer.alloc(1), precisionBytes]) } let totalLength = amountBytes.length + precisionBytes.length + 2; //2 = length bytes //Add instruction buffer = Buffer.concat([ buffer, INSTRUCTION_PREFIX_TOKEN, Buffer.from([totalLength]), Buffer.from([12]), //Amount Id amountBytes, Buffer.from([0]), //Precision Id precisionBytes ]); return buffer; } export function getTokenHolaParameters_LINKHelper(args: BuildTokenTransactionArgs): {blk: any[]}{ //Get blk let blkData = []; //Get tx let tx = buildTokenTransactionHelper(args); blkData.push(tx); return {blk: blkData} }