@ethersphere/swarm-cli
Version:
CLI tool for Bee
152 lines (151 loc) • 5.55 kB
JavaScript
;
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cashout = void 0;
const chalk_1 = __importDefault(require("chalk"));
const furious_commander_1 = require("furious-commander");
const text_1 = require("../../utils/text");
const cheque_command_1 = require("./cheque-command");
class Cashout extends cheque_command_1.ChequeCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'cashout'
});
Object.defineProperty(this, "alias", {
enumerable: true,
configurable: true,
writable: true,
value: 'co'
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Cashout one or all pending cheques'
});
Object.defineProperty(this, "peer", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "all", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "minimum", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "gasLimit", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "gasPrice", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async run() {
super.init();
if (this.all) {
await this.cashoutAll();
}
if (this.peer) {
const amount = await this.getUncashedAmount(this.peer);
await this.cashoutOne(this.peer, amount);
}
}
async cashoutAll() {
this.console.info(`Collecting cheques with value at least ${this.minimum} PLUR...`);
const cheques = await this.getFilteredCheques(this.minimum);
this.console.info('Found ' + cheques.length + ' cheques.');
for (const { amount, address } of cheques) {
await this.cashoutOne(address, amount);
}
}
async cashoutOne(address, amount) {
try {
this.console.log(chalk_1.default.green('Cashing out:'));
this.printCheque({ address, amount });
const transaction = await this.bee.cashoutLastCheque(address, {
gasLimit: this.gasLimit,
gasPrice: this.gasPrice,
});
this.console.log((0, text_1.createKeyValue)('Tx', transaction.toHex()));
this.console.quiet(transaction.toHex());
}
catch (error) {
this.console.error('Could not cashout ' + address);
this.console.printBeeError(error, { notFoundMessage: 'No peer found with that address.' });
}
}
}
__decorate([
(0, furious_commander_1.Option)({ key: 'peer', alias: 'p', description: 'Peer address', required: true, conflicts: 'all' }),
__metadata("design:type", Object)
], Cashout.prototype, "peer", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'all',
alias: 'a',
type: 'boolean',
description: 'Cashout all cheques',
required: true,
conflicts: 'peer',
}),
__metadata("design:type", Boolean)
], Cashout.prototype, "all", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'minimum',
alias: 'm',
type: 'bigint',
minimum: BigInt(1),
description: 'Cashout cheques with balance above this value in PLUR',
default: BigInt(1),
}),
__metadata("design:type", BigInt)
], Cashout.prototype, "minimum", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'gas-limit',
type: 'bigint',
minimum: BigInt(0),
description: 'Gas limit of each transaction in wei',
}),
__metadata("design:type", BigInt)
], Cashout.prototype, "gasLimit", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'gas-price',
type: 'bigint',
minimum: BigInt(0),
description: 'Gas price of each transaction in wei',
}),
__metadata("design:type", BigInt)
], Cashout.prototype, "gasPrice", void 0);
exports.Cashout = Cashout;