@ethersphere/swarm-cli
Version:
CLI tool for Bee
135 lines (134 loc) • 5.79 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateBatch = void 0;
const bee_js_1 = require("@ethersphere/bee-js");
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 CreateBatch extends root_command_1.RootCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'create-batch'
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Create a postage batch for a given funded private key'
});
Object.defineProperty(this, "privateKey", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "depth", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "amount", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "jsonRpcUrl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async run() {
super.init();
if (!this.yes) {
this.yes = await this.console.confirm('This command creates an external batch for advanced usage. Do you want to continue?');
}
if (!this.yes) {
return;
}
const wallet = new ethers_1.Wallet(this.privateKey);
const cost = bee_js_1.Utils.getStampCost(this.depth, this.amount);
const signer = await (0, rpc_1.makeReadySigner)(wallet.privateKey, this.jsonRpcUrl);
this.console.log(`Approving spending of ${cost.toDecimalString()} BZZ to ${wallet.address}`);
const tokenProxyContract = new ethers_1.Contract(contracts_1.Contracts.bzz, contracts_1.ABI.tokenProxy, signer);
const approve = await tokenProxyContract.approve(contracts_1.Contracts.postageStamp, cost.toPLURBigInt().toString(), {
gasLimit: 130000,
type: 2,
maxFeePerGas: cafe_utility_1.Numbers.make('2gwei'),
maxPriorityFeePerGas: cafe_utility_1.Numbers.make('1gwei'),
});
this.console.log(`Waiting 3 blocks on approval tx ${approve.hash}`);
await approve.wait(3);
this.console.log(`Creating postage batch for ${wallet.address} with depth ${this.depth} and amount ${this.amount}`);
const postageStampContract = new ethers_1.Contract(contracts_1.Contracts.postageStamp, contracts_1.ABI.postageStamp, signer);
const createBatch = await postageStampContract.createBatch(signer.address, this.amount, this.depth, 16, `0x${cafe_utility_1.Strings.randomHex(64)}`, false, {
gasLimit: 1000000,
type: 2,
maxFeePerGas: cafe_utility_1.Numbers.make('3gwei'),
maxPriorityFeePerGas: cafe_utility_1.Numbers.make('2gwei'),
});
this.console.log(`Waiting 3 blocks on create batch tx ${createBatch.hash}`);
const receipt = await createBatch.wait(3);
const batchId = receipt.events.find((x) => x.address === contracts_1.Contracts.postageStamp).topics[1];
this.console.log(`Batch created with ID ${batchId}`);
}
}
__decorate([
(0, furious_commander_1.Option)({
key: 'private-key',
description: 'Private key of the wallet to create a postage batch for',
type: 'hex-string',
required: true,
}),
__metadata("design:type", String)
], CreateBatch.prototype, "privateKey", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'depth',
description: 'Depth of the postage stamp',
type: 'number',
required: true,
minimum: 17,
maximum: 255,
}),
__metadata("design:type", Number)
], CreateBatch.prototype, "depth", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'amount',
description: 'Value per chunk in PLUR, deprecates over time with new blocks mined',
type: 'bigint',
required: true,
minimum: 1,
}),
__metadata("design:type", BigInt)
], CreateBatch.prototype, "amount", 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)
], CreateBatch.prototype, "jsonRpcUrl", void 0);
exports.CreateBatch = CreateBatch;