import { Network, Signature } from "./types" import { encodeByte, encodeElement, encodeList } from "./Encoder"; import { getAddressBytes } from "./AddressHelper"; import { BigNumber } from "bignumber.js"; import { Keccak } from "sha3"; import { log } from "console"; var bigInt = require('big-integer') export type BuildTransactionArgs = { nonce: string, gasPrice: string, gasLimit: number, toAddress: string, amount: string, data?: Buffer, signature?: Signature, network: Network } export function buildTransactionHelper(args: BuildTransactionArgs): 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 valueBN = new BigNumber(args.amount); valueBN = valueBN.times(new BigNumber(1000000000000000000)); //Ether to wei let value = bigInt(valueBN.toFixed()); let encodedData = []; //Add chainID let chainId = 421613; if(args.network == Network.Mainnet){ chainId = 42161; } let chainID = getBytes(bigInt(chainId)) if (chainID.length == 1) { encodedData.push(encodeByte(chainID[0])); } else { encodedData.push(encodeElement(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.toAddress))); //Add amount if(value == 0){ encodedData.push(encodeElement(Buffer.alloc(0))); }else{ encodedData.push(encodeElement(getBytes(value))); } //Add message if(args.data == null){ encodedData.push(encodeElement(Buffer.alloc(0))); }else{ // let operationList = getOperationList(args.data); // let message = encodeList(operationList); // console.log(message.toString('hex')) // encodedData.push(message); let message = encodeElement(args.data); 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; } //Pending data tests function getOperationList(data: Buffer): any[]{ let method = data.slice(0,4); let operationList = []; operationList.push(method); data = data.slice(4); let elements = data.length / 32; for(let i = 0; i< elements; i++){ let pos = i*32; let operation = data.slice(pos, pos+32); operationList.push(operation); } return operationList; } export function getHolaParametersHelper(args: BuildTransactionArgs): { blk: any[], u32: number[] } { //Get blk let blkData = []; blkData.push(buildTransactionHelper(args)); //Get u32 let u32Data = []; let valueD = new BigNumber(args.amount).toFixed(); let amountPrecision = 1; if (valueD.includes(".")) { amountPrecision = valueD.split(".")[1].length; } u32Data.push(amountPrecision); 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 getHolaParameters_LINKHelper(args:BuildTransactionArgs): {blk: any[]}{ //Get blk let tx = buildTransactionHelper(args); let hashK256 = new Keccak(256); let resKeccak = hashK256.update(tx).digest(); let blkData = []; blkData.push(resKeccak); return {blk: blkData}; } export function getBytes(data: bigint): Buffer { let hexS = data.toString(16); if (hexS.length % 2 != 0) { hexS = '0' + hexS }; let bytes = Buffer.alloc(hexS.length / 2); let cont = 0; for (var i = 0; i < hexS.length; i += 2) { var byte = parseInt(hexS.substring(i, i + 2), 16); bytes[cont] = byte; cont++; } return bytes; } export function hexToBytes(hex: string): Buffer { for (var bytes = [], c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c, 2), 16)); return Buffer.from(bytes); }