@ethersphere/swarm-cli
Version:
CLI tool for Bee
131 lines (130 loc) • 5.54 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Redeem = void 0;
const cafe_utility_1 = require("cafe-utility");
const ethers_1 = require("ethers");
const furious_commander_1 = require("furious-commander");
const contracts_1 = require("../../utils/contracts");
const rpc_1 = require("../../utils/rpc");
const root_command_1 = require("../root-command");
class Redeem extends root_command_1.RootCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'redeem'
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Transfer xBZZ and xDAI from a private key to the Bee wallet'
});
Object.defineProperty(this, "wallet", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "jsonRpcUrl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "target", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async run() {
super.init();
if (!this.target) {
this.console.log('Fetching Bee wallet address...');
const { ethereum } = await this.bee.getNodeAddresses();
this.target = ethereum.toHex();
}
this.console.log(`Target wallet address: ${this.target}`);
const provider = new ethers_1.providers.JsonRpcProvider(this.jsonRpcUrl, contracts_1.NETWORK_ID);
this.console.log('Creating wallet...');
const wallet = new ethers_1.Wallet(this.wallet, provider);
this.console.log('Fetching xBZZ balance...');
const xBZZ = await (0, rpc_1.eth_getBalanceERC20)(wallet.address, provider);
this.console.log(`xBZZ balance: ${xBZZ}`);
this.console.log('Fetching xDAI balance...');
let xDAI = await (0, rpc_1.eth_getBalance)(wallet.address, provider);
this.console.log(`xDAI balance: ${xDAI}`);
if (!this.quiet && !this.yes) {
this.yes = await this.console.confirm('Do you want to continue?');
}
if (!this.yes && !this.quiet) {
return;
}
const firstKnownxDAI = xDAI;
if (xBZZ !== '0') {
this.console.log('Transferring xBZZ to Bee wallet...');
await (0, rpc_1.sendBzzTransaction)(this.wallet, this.target, xBZZ, this.jsonRpcUrl);
for (let i = 0; i < 10; i++) {
this.console.log(`Refreshing xDAI balance #${i + 1}...`);
xDAI = await (0, rpc_1.eth_getBalance)(wallet.address, provider);
if (xDAI !== firstKnownxDAI) {
this.console.log(`xDAI balance: ${xDAI}`);
break;
}
await cafe_utility_1.System.sleepMillis(cafe_utility_1.Dates.seconds(3));
}
if (xDAI === firstKnownxDAI) {
this.console.log('xDAI balance did not change, skipping transfer');
return;
}
}
const { gasPrice, totalCost } = await (0, rpc_1.estimateNativeTransferTransactionCost)(this.wallet, this.jsonRpcUrl);
const xDAIValue = ethers_1.BigNumber.from(xDAI);
if (xDAIValue.gt(totalCost)) {
this.console.log('Transferring xDAI to Bee wallet...');
await (0, rpc_1.sendNativeTransaction)(this.wallet, this.target, xDAIValue.sub(totalCost).toString(), this.jsonRpcUrl, gasPrice);
}
this.console.log('Redeem complete');
}
}
__decorate([
(0, furious_commander_1.Argument)({
key: 'wallet',
type: 'hex-string',
description: 'Redeemable wallet address (private key)',
required: true,
}),
__metadata("design:type", String)
], Redeem.prototype, "wallet", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'json-rpc-url',
type: 'string',
description: 'Gnosis JSON-RPC URL',
default: 'https://xdai.fairdatasociety.org',
}),
__metadata("design:type", String)
], Redeem.prototype, "jsonRpcUrl", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'target',
type: 'hex-string',
description: 'Target wallet address',
defaultDescription: 'Bee wallet address',
}),
__metadata("design:type", String)
], Redeem.prototype, "target", void 0);
exports.Redeem = Redeem;