-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathsendAllBalance.js
92 lines (85 loc) · 2.48 KB
/
sendAllBalance.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
/* eslint-disable */
const CKB = require("../lib").default
const nodeUrl = process.env.NODE_URL || "http://localhost:8114" // example node url
const ckb = new CKB(nodeUrl)
const privateKey =
process.env.PRIV_KEY ||
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" // example private key
const address = ckb.utils.privateKeyToAddress(privateKey)
const unspentCells = [
{
lock: {
codeHash:
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
hashType: "type",
args: "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6",
},
outPoint: {
txHash:
"0xdf45112919d4af12b10fed94a93798ae6ddd8c89a11d90980022dcf695babca9",
index: "0x0",
},
capacity: "0x2540be400",
data: "0x",
},
{
lock: {
codeHash:
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
hashType: "type",
args: "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6",
},
outPoint: {
txHash:
"0xdf45112919d4af12b10fed94a93798ae6ddd8c89a11d90980022dcf695babca9",
index: "0x1",
},
capacity: "0x2540be400",
data: "0x",
},
]
const generateRawTransaction = async () => {
await ckb.loadDeps()
const rawTx = ckb.generateRawTransaction({
fromAddress: address,
toAddress: address,
capacity: BigInt(19999999900),
fee: BigInt(100),
safeMode: true,
cells: unspentCells,
deps: ckb.config.secp256k1Dep,
changeThreshold: BigInt(0),
})
console.group("generate raw tx")
// console.info(`raw transaction: ${JSON.stringify(rawTx, null, 2)}`)
console.info(
`inputs capacity:
${rawTx.inputs
.map(
input =>
unspentCells.find(
cell =>
cell.outPoint.txHash === input.previousOutput.txHash &&
cell.outPoint.index === input.previousOutput.index,
).capacity,
)
.map(capacity => BigInt(capacity))}
`,
)
console.info(
`outputs capacity: ${rawTx.outputs.map(output => BigInt(output.capacity))}`,
)
console.groupEnd()
return rawTx
}
const sendAllBalance = async () => {
const rawTx = await generateRawTransaction()
const signedTx = ckb.signTransaction(privateKey)(rawTx)
// console.group('sign and send tx')
// console.info(`signed tx: ${JSON.stringify(signedTx, null, 2)}`)
// const realTxHash = await ckb.rpc.sendTransaction(signedTx)
// console.info(`real tx hash: ${realTxHash}`)
// return realTxHash
// console.groupEnd()
}
sendAllBalance()