Skip to content

Commit

Permalink
chore(examples): example for devnet transferring (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Dec 8, 2023
1 parent b8e9396 commit 5239bca
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 151 deletions.
72 changes: 72 additions & 0 deletions examples/misc/devnet-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This example shows how to transfer 100 CKB from ALice to Bob on Devnet.
// Note: The ScriptConfig is generated from the genesis block, therefore,
// it does NOT contain all common configs, such as Anyone-can-pay, Omnilock, etc. that are deployed after genesis.

import { commons, config, hd, helpers, Indexer, RPC } from "@ckb-lumos/lumos";
import { parseUnit } from "@ckb-lumos/bi";

const debug = (...args: unknown[]) => console.log(new Date().toJSON(), "[info]", ...args);

// let's treat testnet as a devnet
const RPC_URL = "https://testnet.ckb.dev";
// const RPC_URL = "http://localhost:8114";
const ALICE_PRIVATE_KEY = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
const BOB_PRIVATE_KEY = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

async function main() {
const rpc = new RPC(RPC_URL);
const indexer = new Indexer(RPC_URL);

const genesisBlock = await rpc.getBlockByNumber("0x0");
const scriptConfig = config.generateGenesisScriptConfigs(genesisBlock);

const txSkeleton = helpers.TransactionSkeleton({ cellProvider: indexer }).asMutable();

const secp256k1ScriptConfig = scriptConfig.SECP256K1_BLAKE160;
const managerConfig = { PREFIX: "ckt", SCRIPTS: scriptConfig } as const;
const configParam = { config: managerConfig };

const aliceAddr = helpers.encodeToAddress(
{
codeHash: secp256k1ScriptConfig.CODE_HASH,
hashType: secp256k1ScriptConfig.HASH_TYPE,
args: hd.key.privateKeyToBlake160(ALICE_PRIVATE_KEY),
},
configParam
);

const bobAddr = helpers.encodeToAddress(
{
codeHash: secp256k1ScriptConfig.CODE_HASH,
hashType: secp256k1ScriptConfig.HASH_TYPE,
args: hd.key.privateKeyToBlake160(BOB_PRIVATE_KEY),
},
configParam
);

debug(`Alice(${aliceAddr})`, "->", `Bob(${bobAddr})`);
debug(`Please make sure Alice(${aliceAddr}) has enough capacity before continue`);

await commons.common.transfer(
txSkeleton,
[aliceAddr],
bobAddr,
parseUnit("100", "ckb"),
undefined,
undefined,
configParam
);
await commons.common.payFeeByFeeRate(txSkeleton, [aliceAddr], 1000, undefined, configParam);
commons.common.prepareSigningEntries(txSkeleton);

const digest = txSkeleton.get("signingEntries").get(0)!.message;
debug("digest for signing:", digest);

const signature = hd.key.signRecoverable(digest, ALICE_PRIVATE_KEY);
const tx = helpers.sealTransaction(txSkeleton, [signature]);

const txHash = await rpc.sendTransaction(tx);
debug("TxHash:", txHash);
}

main();
3 changes: 2 additions & 1 deletion examples/misc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"@ckb-lumos/lumos": "next"
"@ckb-lumos/bi": "canary",
"@ckb-lumos/lumos": "canary"
}
}
Loading

2 comments on commit 5239bca

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-canary-5239bca-20231208101648

npm install @ckb-lumos/[email protected]

@vercel
Copy link

@vercel vercel bot commented on 5239bca Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.