-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathnervosDAO.js
152 lines (131 loc) · 3.96 KB
/
nervosDAO.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* eslint-disable */
const path = require('path')
const os = require('os')
const { Indexer, CellCollector } = require('@ckb-lumos/indexer')
const CKB = require('../lib').default
const CKB_URL = process.env.CKB_URL || 'http://localhost:8114' // example node url
const LUMOS_DB = path.join(os.tmpdir(), 'lumos_db')
/**
* lumos indexer
*/
const indexer = new Indexer(CKB_URL, LUMOS_DB)
indexer.startForever()
/**
* sdk
*/
const ckb = new CKB(CKB_URL)
const sk = process.env.PRIV_KEY || '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' // example private key
const pk = ckb.utils.privateKeyToPublicKey(sk)
const pkh = `0x${ckb.utils.blake160(pk, 'hex')}`
const addr = ckb.utils.privateKeyToAddress(sk)
const loadCells = async () => {
await ckb.loadDeps()
const lock = {
codeHash: ckb.config.secp256k1Dep.codeHash,
hashType: ckb.config.secp256k1Dep.hashType,
args: pkh
}
/**
* load cells from lumos as `examples/sendTransactionWithLumosCollector.js` shows
*/
await ckb.loadCells({ indexer, CellCollector, lock, save: true })
}
const deposit = async () => {
await loadCells()
await ckb.loadDeps()
const depositTx = ckb.generateDaoDepositTransaction({
fromAddress: addr,
capacity: BigInt(10200000000),
fee: BigInt(100000)
})
const signed = ckb.signTransaction(sk)(depositTx)
const txHash = await ckb.rpc.sendTransaction(signed)
const depositOutPoint = {
txHash,
index: '0x0'
}
console.log(`const depositOutPoint = ${JSON.stringify(depositOutPoint)}`)
}
const depositOutPoint = {
"txHash": "0x40e1d58cf8576d5206d55d242284a28f64cb114d0b9a8292582e7596082e5bda",
"index": "0x0"
}
const logDepositEpoch = async () => {
const tx = await ckb.rpc.getTransaction(depositOutPoint.txHash)
if (tx.txStatus.blockHash) {
const b = await ckb.rpc.getBlock(tx.txStatus.blockHash)
const epoch = b.header.epoch
console.log(`const depositEpoch = ${JSON.stringify(ckb.utils.parseEpoch(epoch), null, 2)}`)
} else {
console.log('not committed')
}
}
const depositEpoch = {
"length": "0xa",
"index": "0x0",
"number": "0x69"
}
const starWithdrawing = async () => {
await loadCells()
await ckb.loadDeps()
const tx = await ckb.generateDaoWithdrawStartTransaction({
outPoint: depositOutPoint,
fee: 10000n
})
const signed = ckb.signTransaction(sk)(tx)
const txHash = await ckb.rpc.sendTransaction(signed)
const outPoint = {
txHash,
index: '0x0'
}
console.log(`const startWithDrawOutPoint = ${JSON.stringify(outPoint, null, 2)}`)
}
const startWithDrawOutPoint = {
"txHash": "0xc8ad01deb8b25c56169992598398ad7d539314ada90c84bff12fa7fc69095076",
"index": "0x0"
}
const logStartWithdrawingEpoch = async () => {
const tx = await ckb.rpc.getTransaction(startWithDrawOutPoint.txHash)
if (tx.txStatus.blockHash) {
const b = await ckb.rpc.getBlock(tx.txStatus.blockHash)
const epoch = b.header.epoch
console.log(`const startWithdrawingEpoch = ${JSON.stringify(ckb.utils.parseEpoch(epoch), null, 2)}`)
} else {
console.log('not committed')
}
}
const startWithdrawingEpoch = {
"length": "0xa",
"index": "0x0",
"number": "0xbe"
}
const logCurrentEpoch = async () => {
ckb.rpc.getTipHeader().then(h => console.log(ckb.utils.parseEpoch(h.epoch)))
}
const withdraw = async () => {
await ckb.loadDeps()
await loadCells()
const tx = await ckb.generateDaoWithdrawTransaction({
depositOutPoint,
withdrawOutPoint: startWithDrawOutPoint,
fee: BigInt(100000)
})
const signed = ckb.signTransaction(sk)(tx)
const txHash = await ckb.rpc.sendTransaction(signed)
const outPoint = {
txHash,
index: '0x0'
}
console.log(`const withdrawOutPoint = ${JSON.stringify(outPoint, null, 2)}`)
}
const withDrawOutPoint = {
"txHash": "0xb1ee185a4e811247b1705a52df487c3ce839bfa2f72e4c7a74b6fc6b0ea4cfa7",
"index": "0x0"
}
// deposit()
// logDepositEpoch()
// starWithdrawing()
// logStartWithdrawingEpoch()
// logCurrentEpoch()
// withdraw()
// setInterval(logCurrentEpoch, 1000)