-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcreate-batch.ts
More file actions
97 lines (83 loc) · 3.31 KB
/
Copy pathcreate-batch.ts
File metadata and controls
97 lines (83 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Utils } from '@ethersphere/bee-js'
import { Numbers, Strings } from 'cafe-utility'
import { Contract, ContractTransactionReceipt, Wallet } from 'ethers'
import { LeafCommand, Option } from 'furious-commander'
import { ABI, Contracts } from '../../utils/contracts'
import { makeReadySigner } from '../../utils/rpc'
import { RootCommand } from '../root-command'
export class CreateBatch extends RootCommand implements LeafCommand {
public readonly name = 'create-batch'
public readonly description = 'Create a postage batch for a given funded private key'
@Option({
key: 'private-key',
description: 'Private key of the wallet to create a postage batch for',
type: 'hex-string',
required: true,
})
public privateKey!: string
@Option({
key: 'depth',
description: 'Depth of the postage stamp',
type: 'number',
required: true,
minimum: 17,
maximum: 255,
})
public depth!: number
@Option({
key: 'amount',
description: 'Value per chunk in PLUR, deprecates over time with new blocks mined',
type: 'bigint',
required: true,
minimum: 1,
})
public amount!: bigint
@Option({
key: 'json-rpc-url',
type: 'string',
description: 'Gnosis JSON-RPC URL',
default: 'https://xdai.fairdatasociety.org',
})
public jsonRpcUrl!: string
public async run(): Promise<void> {
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 Wallet(this.privateKey)
const cost = Utils.getStampCost(this.depth, this.amount)
const { signer } = await makeReadySigner(wallet.privateKey, this.jsonRpcUrl)
this.console.log(`Approving spending of ${cost.toDecimalString()} BZZ to ${wallet.address}`)
const tokenProxyContract = new Contract(Contracts.bzz, ABI.tokenProxy, signer)
const approve = await tokenProxyContract.approve(Contracts.postageStamp, cost.toPLURBigInt().toString(), {
gasLimit: 130_000,
type: 2,
maxFeePerGas: Numbers.make('2gwei'),
maxPriorityFeePerGas: 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 Contract(Contracts.postageStamp, ABI.postageStamp, signer)
const createBatchArgs = [signer.address, this.amount, this.depth, 16, `0x${Strings.randomHex(64)}`, false]
const createBatch = await postageStampContract.createBatch(...createBatchArgs, {
gasLimit: 1_000_000,
type: 2,
maxFeePerGas: Numbers.make('3gwei'),
maxPriorityFeePerGas: Numbers.make('2gwei'),
})
this.console.log(`Waiting 3 blocks on create batch tx ${createBatch.hash}`)
const receipt = (await createBatch.wait(3)) as ContractTransactionReceipt
const batchLog = receipt.logs.find(x => x.address === Contracts.postageStamp)
if (!batchLog || batchLog.topics.length < 2) {
throw new Error(`Could not find postage stamp log in receipt. Logs: ${JSON.stringify(receipt.logs)}`)
}
const batchId = batchLog.topics[1]
this.console.log(`Batch created with ID ${batchId}`)
}
}