Skip to content

Commit

Permalink
refactor(js): add a helper function to convert a string to hexadecima…
Browse files Browse the repository at this point in the history
…l format
  • Loading branch information
Flouse committed May 8, 2023
1 parent 9382ee2 commit 42bf1b5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions js/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export function calculateTxFee(
return fee * ratio < base ? fee + 1n: fee;
}

export function encodeStringToHex(str: string): HexString {
return "0x" + Buffer.from(str).toString("hex");
}

/**
* Get faucet from https://github.com/Flouse/nervos-functions#faucet
*/
Expand Down
4 changes: 2 additions & 2 deletions js/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Hash, Cell, RPC, commons, helpers as lumosHelpers, HexString, hd } from "@ckb-lumos/lumos";
import { minimalScriptCapacity } from "@ckb-lumos/helpers"
import {
CKB_TESTNET_EXPLORER, TESTNET_SCRIPTS,
CKB_TESTNET_EXPLORER, TESTNET_SCRIPTS, encodeStringToHex,
generateAccountFromPrivateKey, ckbIndexer, collectInputCells, calculateTxFee, addWitness
} from "./helper";
import { CHARLIE } from "./test-keys";
Expand All @@ -22,7 +22,7 @@ console.assert(testAccount.address === CHARLIE.ADDRESS);
const constructHelloWorldTx = async (
onChainMemo: string
): Promise<lumosHelpers.TransactionSkeletonType> => {
const onChainMemoHex: string = "0x" + Buffer.from(onChainMemo).toString("hex");
const onChainMemoHex: HexString = encodeStringToHex(onChainMemo);
console.log(`onChainMemoHex: ${onChainMemoHex}`);

// CapacityUnit.Byte = 100000000, because 1 CKB = 100000000 shannon
Expand Down

0 comments on commit 42bf1b5

Please sign in to comment.