Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function prepareTx({ tx, provider, sender, gasLimit, gasPrice }) {
const pTx = await utils.populateTransaction(tx, provider, sender)
// TO work with all signers
pTx.gasLimit = gasLimit || pTx.gasLimit.toHexString()
pTx.gasPrice = gasPrice || pTx.gasPrice.toHexString()
pTx.value = pTx.value || utils.hexlify(0)
pTx.nonce = utils.hexlify(pTx.nonce)
return pTx
}
* temporarily fetching balances from node until the backend is fixed
*/
// get ETH balance
const ethBal = yield nahmiiProvider.getBalance(address);
if (onlyEth) {
yield put(loadWalletBalancesSuccess(address, [{ currency: '0x0000000000000000000000000000000000000000', address, decimals: 18, balance: ethBal }]));
return;
}
// get token balances, batching all the requests in an array and sending them all at once
// https://stackoverflow.com/questions/48228662/get-token-balance-with-ethereum-rpc
const supportedTokens = supportedAssets.assets.filter((a) => a.currency !== '0x0000000000000000000000000000000000000000');
const tokenContractAddresses = supportedTokens.map((a) => a.currency);
// pad the 20 byte address to 32 bytes
const paddedAddr = ethersUtils.hexlify(ethersUtils.padZeros(address, 32));
// concat the balanceOf('address') function identifier to the padded address. this shows our intention to call the
// balanceOf method with address as the parameter
const dataArr = ethersUtils.concat([BALANCE_OF_FUNCTION_ID, paddedAddr]);
const data = ethersUtils.hexlify(dataArr);
// send a batch of RPC requests asking for all token balances
// https://www.jsonrpc.org/specification#batch
const requestBatch = tokenContractAddresses.map((contractAddr) => {
const params = [{ from: address, to: contractAddr, data }, 'latest'];
return {
method: 'eth_call',
params,
id: 42,
jsonrpc: '2.0',
};
export const buildTransaction = async(transaction, address, provider) => {
if (transaction.value) {
transaction.value = utils.hexlify(transaction.value as string);
} else {
transaction.value = utils.hexlify(utils.parseEther("0.0"));
}
if (transaction.gasPrice) {
transaction.gasPrice = utils.hexlify(transaction.gasPrice as string);
} else {
const gasPrice = await getGasPrice();
transaction.gasPrice = utils.hexlify(gasPrice.toNumber());
}
if (transaction.gasLimit) {
transaction.gasLimit = utils.hexlify(transaction.gasLimit as string);
} else {
// TODO gasLimit returned by estimateGas is currently failing
// TODO for now using 5500000 as default gasLimit
// const gasLimit = await provider.estimateGas(transaction);
transaction.gasLimit = utils.hexlify(5500000);
}
transaction.nonce = utils.hexlify(await provider.getTransactionCount(address));
export const buildTransaction = async(transaction, address, provider) => {
if (transaction.value) {
transaction.value = utils.hexlify(transaction.value as string);
} else {
transaction.value = utils.hexlify(utils.parseEther("0.0"));
}
if (transaction.gasPrice) {
transaction.gasPrice = utils.hexlify(transaction.gasPrice as string);
} else {
const gasPrice = await getGasPrice();
transaction.gasPrice = utils.hexlify(gasPrice.toNumber());
}
if (transaction.gasLimit) {
transaction.gasLimit = utils.hexlify(transaction.gasLimit as string);
} else {
// TODO gasLimit returned by estimateGas is currently failing
// TODO for now using 5500000 as default gasLimit
// const gasLimit = await provider.estimateGas(transaction);
transaction.gasLimit = utils.hexlify(5500000);
}
transaction.nonce = utils.hexlify(await provider.getTransactionCount(address));
return transaction;
};
export function numToBytes32(num: Parameters[0]): string {
const hexNum = utils.hexlify(num)
const strippedNum = stripHexPrefix(hexNum)
if (strippedNum.length > 32 * 2) {
throw Error(
'Cannot convert number to bytes32 format, value is greater than maximum bytes32 value',
)
}
return addHexPrefix(strippedNum.padStart(32 * 2, '0'))
}
async sign(transaction) {
if (!this.eth) {
await this.transportPromise;
}
if (transaction.value) {
transaction.value = transaction.value.toHexString();
}
transaction.gasPrice = utils.hexlify(transaction.gasPrice);
if (!transaction.chainId) {
transaction.chainId = this.chainId;
}
return utils.resolveProperties(transaction).then(tx => {
const unsignedTx = utils.serializeTransaction(tx).substring(2);
const path = this.getPath(this.addressIndex);
return this.eth.signTransaction(path, unsignedTx).then(signature => {
const sig = {
v: signature.v,
r: '0x' + signature.r,
s: '0x' + signature.s,
};
return utils.serializeTransaction(tx, sig);
});
});
}