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"; var bigInt = require('big-integer') export type BuildERC721TransactionArgs = { nonce: string, gasPrice: string, gasLimit: number, contractAddress: string, fromAddress: string, toAddress: string, tokenId: string, contractName: string signature?: Signature, network: Network } export function buildERC721TransactionHelper(args: BuildERC721TransactionArgs): 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 transfer fucntion call, fromAddress, toAddress and tokenId to data let fullFromAddress = fillBytes(getAddressBytes(args.fromAddress)); let fullToAddress = fillBytes(getAddressBytes(args.toAddress)); let fullTokenId = fillBytes(getBytes(token)); let dataMessage = Buffer.concat([ Buffer.from([0x23, 0xb8, 0x72, 0xdd]), fullFromAddress, fullToAddress, fullTokenId ]); 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 getERC721HolaParametersHelper(args: BuildERC721TransactionArgs): { blk: any[], u32: number[] } { //Get blk let blkData = []; blkData.push(buildERC721TransactionHelper(args)); //Add contractName let contractId = " " + args.contractName; blkData.push(Buffer.from(contractId, 'ascii')); //Add tokenId let tokenBN = new BigNumber(args.tokenId); //Get tokenId let token = bigInt(tokenBN.toFixed()); blkData.push(getBytes(token)); //Get u32 let u32Data = []; let gasPriceB = new BigNumber(args.gasPrice); gasPriceB = gasPriceB.times(new BigNumber(args.gasLimit)).dividedBy(new BigNumber(1000000000)); let feesD = gasPriceB.toFixed(); let feesPrecision = 1; if (feesD.includes(".")) { feesPrecision = feesD.split(".")[1].length; } u32Data.push(feesPrecision); return { blk: blkData, u32: u32Data }; } export function getERC721HolaParameters_LINKHelper(args: BuildERC721TransactionArgs): {blk: any[]}{ let tx = buildERC721TransactionHelper(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; }