UNPKG

@ethersphere/swarm-cli

Version:
61 lines (60 loc) 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeReadySigner = exports.sendBzzTransaction = exports.sendNativeTransaction = exports.estimateNativeTransferTransactionCost = exports.eth_getBalanceERC20 = exports.eth_getBalance = void 0; const ethers_1 = require("ethers"); const contracts_1 = require("./contracts"); const NETWORK_ID = 100; async function eth_getBalance(address, provider) { if (!address.startsWith('0x')) { address = `0x${address}`; } const balance = await provider.getBalance(address); return balance.toString(); } exports.eth_getBalance = eth_getBalance; async function eth_getBalanceERC20(address, provider, tokenAddress = contracts_1.Contracts.bzz) { if (!address.startsWith('0x')) { address = `0x${address}`; } const contract = new ethers_1.Contract(tokenAddress, contracts_1.ABI.bzz, provider); const balance = await contract.balanceOf(address); return balance.toString(); } exports.eth_getBalanceERC20 = eth_getBalanceERC20; async function estimateNativeTransferTransactionCost(privateKey, jsonRpcProvider) { const signer = await makeReadySigner(privateKey, jsonRpcProvider); const gasLimit = '21000'; const gasPrice = await signer.getGasPrice(); return { gasPrice, totalCost: gasPrice.mul(gasLimit) }; } exports.estimateNativeTransferTransactionCost = estimateNativeTransferTransactionCost; async function sendNativeTransaction(privateKey, to, value, jsonRpcProvider, externalGasPrice) { const signer = await makeReadySigner(privateKey, jsonRpcProvider); const gasPrice = externalGasPrice ?? (await signer.getGasPrice()); const transaction = await signer.sendTransaction({ to, value: ethers_1.BigNumber.from(value), gasPrice, gasLimit: ethers_1.BigNumber.from(21000), type: 0, }); const receipt = await transaction.wait(1); return { transaction, receipt }; } exports.sendNativeTransaction = sendNativeTransaction; async function sendBzzTransaction(privateKey, to, value, jsonRpcProvider) { const signer = await makeReadySigner(privateKey, jsonRpcProvider); const gasPrice = await signer.getGasPrice(); const bzz = new ethers_1.Contract(contracts_1.Contracts.bzz, contracts_1.ABI.bzz, signer); const transaction = await bzz.transfer(to, value, { gasPrice }); const receipt = await transaction.wait(1); return { transaction, receipt }; } exports.sendBzzTransaction = sendBzzTransaction; async function makeReadySigner(privateKey, jsonRpcProvider) { const provider = new ethers_1.providers.JsonRpcProvider(jsonRpcProvider, NETWORK_ID); await provider.ready; const signer = new ethers_1.Wallet(privateKey, provider); return signer; } exports.makeReadySigner = makeReadySigner;