Repository navigation
Expand file tree
/
Copy pathlib.ts
More file actions
69 lines (57 loc) · 1.82 KB
/
Copy pathlib.ts
File metadata and controls
69 lines (57 loc) · 1.82 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
import { ccc, Script } from "@ckb-ccc/core";
import { cccClient } from "./ccc-client";
type Account = {
lockScript: Script;
address: string;
pubKey: string;
};
export const generateAccountFromPrivateKey = async (
privKey: string,
): Promise<Account> => {
const signer = new ccc.SignerCkbPrivateKey(cccClient, privKey);
const lock = await signer.getAddressObjSecp256k1();
return {
lockScript: lock.script,
address: lock.toString(),
pubKey: signer.publicKey,
};
};
export async function capacityOf(address: string): Promise<bigint> {
const addr = await ccc.Address.fromString(address, cccClient);
let balance = await cccClient.getBalance([addr.script]);
return balance;
}
export async function transfer(
to: string,
amount: string,
signerPrivateKey: string,
): Promise<string> {
const signer = new ccc.SignerCkbPrivateKey(cccClient, signerPrivateKey);
const { script: toLock } = await ccc.Address.fromString(to, cccClient);
const tx = ccc.Transaction.from({
outputs: [{ lock: toLock }],
outputsData: [],
});
tx.outputs.forEach((output, i) => {
if (output.capacity > ccc.fixedPointFrom(amount)) {
alert(
`Output ${i} has capacity ${output.capacity}, which is greater than the transfer amount ${amount}.`,
);
return;
}
output.capacity = ccc.fixedPointFrom(amount);
});
await tx.completeInputsByCapacity(signer);
await tx.completeFeeBy(signer, 1000);
const txHash = await signer.sendTransaction(tx);
console.log(
`Go to explorer to check the sent transaction https://pudge.explorer.nervos.org/transaction/${txHash}`,
);
return txHash;
}
export async function wait(secs: number) {
return new Promise((resolve) => setTimeout(resolve, secs * 1000));
}
export function shannonToCKB(amount: bigint) {
return (amount / 100000000n).toString();
}