import { Network, Signature } from "./types" import { encodeByte, encodeElement, encodeList } from "./Encoder"; import { getAddressBytes } from "./AddressHelper"; import { getBytes } from "./TransactionHelper"; import BigNumber from "bignumber.js"; import { Keccak } from "sha3"; import { log } from "console"; var bigInt = require('big-integer') export type BuildERC1155TransactionArgs = { nonce: string, gasPrice: string, gasLimit: number, contractAddress: string, fromAddress: string, toAddress: string, tokenId: string signature?: Signature, network: Network } export function buildERC1155TransactionHelper(args: BuildERC1155TransactionArgs): Buffer { let nonce = bigInt(args.nonce); let gasPriceBN = new BigNumber(args.gasPrice); gasPriceBN = gasPriceBN.times(new BigNumber(1000000000)); //GWei to wei let gasPrice = bigInt(gasPriceBN.toFixed()); let gasLimit = bigInt(args.gasLimit); let tokenBN = new BigNumber(args.tokenId); //Get tokenId let token = bigInt(tokenBN.toFixed()); let encodedData = []; //Add chainID let chainId = 5; if(args.network == Network.Mainnet){ chainId = 1; } let chainID = getBytes(bigInt(chainId)) encodedData.push(chainID); //Add nonce let nonceB = getBytes(nonce); if (nonceB.length == 1) { encodedData.push(encodeByte(nonceB[0])); } else { encodedData.push(encodeElement(nonceB)); } //Add maxPriorityFee encodedData.push(encodeElement(getBytes(gasPrice))); //Add maxFee (de momento igual que maxPriority) encodedData.push(encodeElement(getBytes(gasPrice))); //Add gasLimit encodedData.push(encodeElement(getBytes(gasLimit))); //Add toAddress encodedData.push(encodeElement(getAddressBytes(args.contractAddress))); //Add amount encodedData.push(encodeElement(Buffer.alloc(0))); //Add message //Add safe transfer fucntion call, fromAddress, toAddress, tokenId, value, to data let fullFromAddress = fillBytes(getAddressBytes(args.fromAddress)); let fullToAddress = fillBytes(getAddressBytes(args.toAddress)); let fullTokenId = fillBytes(getBytes(token)); let fullValue = fillBytes(getBytes(bigInt(1))); //Always transfer 1 let fullData = Buffer.from("00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000", "hex"); //Pendiente revisar si este data es vĂ¡lido let dataMessage = Buffer.concat([ Buffer.from([0xf2, 0x42, 0x43, 0x2a]), fullFromAddress, fullToAddress, fullTokenId, fullValue, fullData ]); let message = encodeElement(dataMessage); encodedData.push(message); //Add empty accessList encodedData.push(encodeList(null)); if(args.signature != null){ encodedData.push(encodeByte(args.signature.v)); encodedData.push(encodeElement(args.signature.r)); encodedData.push(encodeElement(args.signature.s)); } let transaction = encodeList(encodedData); transaction = Buffer.concat([Buffer.from("02","hex"), transaction]); return transaction; } export function getERC1155HolaParameters_LINKHelper(args: BuildERC1155TransactionArgs): {blk: any[]}{ let tx = buildERC1155TransactionHelper(args); let hashK256 = new Keccak(256); let resKeccak = hashK256.update(tx).digest(); let blkData = []; blkData.push(resKeccak); return {blk: blkData}; } function fillBytes(data: Buffer): Buffer { let fullBytes = Buffer.alloc(32); if (data.length > fullBytes.length) { throw Error("Data is too long"); } for (let i = 0; i <= data.length; i++) { fullBytes[32 - i] = data[data.length - i]; } return fullBytes; }