@ethersphere/swarm-cli
Version:
CLI tool for Bee
80 lines (79 loc) • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.eth_getBalance = eth_getBalance;
exports.eth_getBalanceERC20 = eth_getBalanceERC20;
exports.estimateNativeTransferTransactionCost = estimateNativeTransferTransactionCost;
exports.sendNativeTransaction = sendNativeTransaction;
exports.sendBzzTransaction = sendBzzTransaction;
exports.makeReadySigner = makeReadySigner;
const ethers_1 = require("ethers");
const contracts_1 = require("./contracts");
async function eth_getBalance(address, provider) {
if (!address.startsWith('0x')) {
address = `0x${address}`;
}
const balance = await provider.getBalance(address);
return balance.toString();
}
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();
}
async function estimateNativeTransferTransactionCost(privateKey, jsonRpcProvider) {
const { provider } = await makeReadySigner(privateKey, jsonRpcProvider);
const gasLimit = 21000n;
const { gasPrice } = await provider.getFeeData();
if (gasPrice === null) {
throw new Error('Unable to determine gas price from provider');
}
return { gasPrice, totalCost: gasPrice * gasLimit };
}
async function sendNativeTransaction(privateKey, to, value, jsonRpcProvider, externalGasPrice) {
if (!to.startsWith('0x')) {
to = `0x${to}`;
}
const { signer, provider } = await makeReadySigner(privateKey, jsonRpcProvider);
const resolvedGasPrice = externalGasPrice ?? (await provider.getFeeData()).gasPrice;
if (resolvedGasPrice === null || resolvedGasPrice === undefined) {
throw new Error('Unable to determine gas price from provider');
}
const transaction = await signer.sendTransaction({
to,
value: BigInt(value),
gasPrice: resolvedGasPrice,
gasLimit: 21000n,
type: 0,
});
const receipt = await transaction.wait(1);
if (receipt === null) {
throw new Error('Transaction was not included in a block');
}
return { transaction, receipt };
}
async function sendBzzTransaction(privateKey, to, value, jsonRpcProvider) {
if (!to.startsWith('0x')) {
to = `0x${to}`;
}
const { signer, provider } = await makeReadySigner(privateKey, jsonRpcProvider);
const { gasPrice } = await provider.getFeeData();
if (gasPrice === null) {
throw new Error('Unable to determine gas price from provider');
}
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);
if (receipt === null) {
throw new Error('Transaction was not included in a block');
}
return { transaction, receipt };
}
async function makeReadySigner(privateKey, jsonRpcProvider) {
const provider = new ethers_1.JsonRpcProvider(jsonRpcProvider, (0, contracts_1.getNetworkId)());
await provider.getNetwork();
const signer = new ethers_1.Wallet(privateKey, provider);
return { signer, provider };
}