@ethersphere/swarm-cli
Version:
CLI tool for Bee
78 lines (77 loc) • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rpc = exports.sendBzzTransaction = exports.sendNativeTransaction = exports.estimateNativeTransferTransactionCost = void 0;
const ethers_1 = require("ethers");
const bzz_abi_1 = require("./bzz-abi");
const NETWORK_ID = 100;
async function getNetworkChainId(url) {
const provider = new ethers_1.providers.JsonRpcProvider(url, NETWORK_ID);
await provider.ready;
const network = await provider.getNetwork();
return network.chainId;
}
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_getBlockByNumber(provider) {
const blockNumber = await provider.getBlockNumber();
return blockNumber.toString();
}
async function eth_getBalanceERC20(address, provider, tokenAddress = bzz_abi_1.BZZ_TOKEN_ADDRESS) {
if (!address.startsWith('0x')) {
address = `0x${address}`;
}
const contract = new ethers_1.Contract(tokenAddress, bzz_abi_1.bzzABI, provider);
const balance = await contract.balanceOf(address);
return balance.toString();
}
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(bzz_abi_1.BZZ_TOKEN_ADDRESS, bzz_abi_1.bzzABI, 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.Rpc = {
getNetworkChainId,
sendNativeTransaction,
sendBzzTransaction,
_eth_getBalance: eth_getBalance,
_eth_getBalanceERC20: eth_getBalanceERC20,
eth_getBalance,
eth_getBalanceERC20,
eth_getBlockByNumber,
};