Creating, funding, and transferring from a new purse can be done in just a few lines of code. Using the Casper JS SDK, the below walk-through explains each part of the purse creation process, including deploying and calling the contract on-chain.
Create and Fund New Purse
The following steps walk you through creating and funding a new purse.
-
Create a Purse
This step creates a new purse under the account context.const purse = await casper.createPurse();
2. Fund Purse
This step transfers the amount from the account's main purse to the newly created purse.
const source = await casper.getAccountMainPurse();
const deployHash = await casper.transferFromPurseToPurse(source, purse, amount);
await casper.waitForDeployProcessed(deployHash);
3. store purse into contract named_keys:
This step stores the newly created purse into the contract's named_keys.
const namedKeys = {
"second_purse": purse.toKey(),
};
4. AccountHash
Get the target account hash
const targetAccountHash = casper.utils.accountHash("account-hash-d9758b25962f4cba82ba0047389af97a70acb7df43b391f9ffb293801bea5061");
5. Transfer from funded purse to target public key
const sourcePurse = namedKeys["second_purse"];
const deployHash = await casper.transferFromPurseToAccount(
sourcePurse,
targetAccountHash,
amount
);
await casper.waitForDeployProcessed(deployHash);
Installing the Contract
-
Install the contract and fund with 1000 CSPR:
casper-client put-deploy \
--chain-name casper-test \
--node-address http://localhost:11101 \
--payment-amount 20000000000 \
--session-path /path/to/contract/target/wasm32-unknown-unknown/release/contract.wasm \
--secret-key /path/to/secret_key.pem
--session-arg "amount:U512='1000000000000'" \
2. Call the transfer_amount entrypoint and transfer 123 CSPR to target account:
casper-client put-deploy --chain-name casper-test \
--node-address http://localhost:11101 \
--secret-key /path/to/secret_key.pem \
--session-hash "hash-4f3de8e812f8bfee3e088461b325b5136d69307fc62aafa034298e16b818e332" \
--session-entry-point "transfer_amount" \
--session-arg "account_hash:key='account-hash-d9758b25962f4cba82ba0047389af97a70acb7df43b391f9ffb293801bea5061'" \
--payment-amount 3000000000
--session-arg "amount:U512='123000000000'" \
The above steps will create and fund a new purse, and upon calling the transfer_amount entrypoint, transfers the designated amount to the target account hash.
If you have any concerns, questions, or issues, please submit a request to our support team here:
Comments
0 comments
Please sign in to leave a comment.