import BigNumber from "bignumber.js"; import { CREATE_ATA_INSTRUCTION, TRANSFER_ATA_INSTRUCTION_PREFIX, MESSAGE_PREFIX_ATA_TOKEN, TOKEN_PROGRAM,SYSTEM_PROGRAM, ASSOCIATED_TOKEN_ACCOUNT_PROGRAM } from "./Constants"; import { decodeBase58 } from "./Encoding" import { getBytes } from "./TransactionHelper"; import { BuildTokenTransactionArgs } from "./TokenTransactionHelper"; export function buildCreateATATokenTransactionHelper(args: BuildTokenTransactionArgs): any{ let authorizerBytes = decodeBase58(args.authorizer); 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_ATA_TOKEN ]); //Add accounts buffer = Buffer.concat([ buffer, Buffer.from([0x08]), //Number of accounts authorizerBytes, fromAssociatedTokenAccountBytes, toAssociatedTokenAccountBytes, SYSTEM_PROGRAM, TOKEN_PROGRAM, tokenAddressBytes, ownerBytes, ASSOCIATED_TOKEN_ACCOUNT_PROGRAM, ]); //Add blockhash buffer = Buffer.concat([buffer, blockBytes]); //Add first instruction (Create ATA) buffer = Buffer.concat([ buffer, CREATE_ATA_INSTRUCTION ]); //Add second instruction (Transfer) //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, TRANSFER_ATA_INSTRUCTION_PREFIX, Buffer.from([totalLength]), Buffer.from([12]), //Amount Id amountBytes, Buffer.from([0]), //Precision Id precisionBytes ]); return buffer; } export function getCreateATATokenHolaParameters_LINKHelper(args: BuildTokenTransactionArgs): {blk: any[]}{ //Get blk let blkData = []; //Get tx let tx = buildCreateATATokenTransactionHelper(args); blkData.push(tx); return {blk: blkData} }