Creating, funding, and transferring from a new purse can be done in just a few lines of code. Using the Rust crate, the below walk-through explains each part of the purse creation process, including deploying and calling the contract on-chain.
1. Create a purse:
This step created a new purse under the account context
let second_purse = system::create_purse();
2. Fund purse
This step transfers the amount from the account's main purse to the newly created purse.
let source: URef = account::get_main_purse();
...
system::transfer_from_purse_to_purse(source, second_purse, amount, None).unwrap_or_revert();
3. Store purse into contract named_keys
This step stores the newly created purse into the contract's named_keys
let mut named_keys: BTreeMap<String, Key> = BTreeMap::new();
//store purse into contract's named_keys
named_keys.insert(String::from("second_purse"), second_purse.into());
4. Entry point
-
get the target account hash
let account_hash_key: Key = runtime::get_named_arg("account_hash");
let target_account_hash = account_hash_key.into_account().unwrap();
-
transfer amount from created purse to target public key
system::transfer_from_purse_to_account(source_purse, target_account_hash, amount, None)
.unwrap_or_revert();
Command-line instructions
1. Install the contract
casper-client put-deploy \
--chain-name casper-test \
--node-address http://<NODE_ADDRESS_HERE>:7777 \
--payment-amount 20000000000 \
--session-path <PATH_TO>/target/wasm32-unknown-unknown/release/contract.wasm \
--session-arg "amount:U512='9000000000'" \
--secret-key <PATH_TO>/secret_key.pem
2. Call entry point "transfer_amount"
casper-client put-deploy --chain-name casper-test \
--node-address http://94.130.10.55:7777 \
--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
The full body of code to initialize, fund, and set up an entry point looks like:
#![no_std]
#![no_main]
extern crate alloc;
use alloc::{collections::BTreeMap, string::String, vec};
use casper_contract::{
contract_api::{account, runtime, storage, system},
unwrap_or_revert::UnwrapOrRevert,
};
use casper_types::{
CLType, EntryPoint, EntryPointAccess, EntryPointType, EntryPoints, Key, URef, U512,
};
#[no_mangle]
pub fn transfer_amount() {
let source_purse = runtime::get_key("second_purse")
.unwrap()
.into_uref()
.unwrap();
let account_hash_key: Key = runtime::get_named_arg("account_hash");
let target_account_hash = account_hash_key.into_account().unwrap();
let amount = U512::from(1000000000);
system::transfer_from_purse_to_account(source_purse, target_account_hash, amount, None)
.unwrap_or_revert();
}
#[no_mangle]
pub extern "C" fn call() {
let amount: U512 = runtime::get_named_arg("amount");
let source: URef = account::get_main_purse();
//create purse
let second_purse = system::create_purse();
//fund purse
system::transfer_from_purse_to_purse(source, second_purse, amount, None).unwrap_or_revert();
let mut named_keys: BTreeMap<String, Key> = BTreeMap::new();
//store purse into contract's named_keys
named_keys.insert(String::from("second_purse"), second_purse.into());
// Create entry point
let mut entry_points = EntryPoints::new();
entry_points.add_entry_point(EntryPoint::new(
"transfer_amount",
vec![],
CLType::Unit,
EntryPointAccess::Public,
EntryPointType::Contract,
));
let (stored_contract_hash, _) =
storage::new_locked_contract(entry_points, Some(named_keys), None, None);
runtime::put_key("transfer_contract", stored_contract_hash.into());
}
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.