-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5045: Custom Payment QoL r=EdHastingsCasperAssociation a=EdHastingsCasperAssociation This PR adds a guardrail to improve quality of life for custom payment use case. Co-authored-by: Ed Hastings <[email protected]> Co-authored-by: Michał Papierski <[email protected]> Co-authored-by: edhastings <[email protected]>
- Loading branch information
Showing
10 changed files
with
364 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
smart_contracts/contracts/test/payment-purse-persist/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "payment-purse-persist" | ||
version = "0.1.0" | ||
authors = ["Ed Hastings <[email protected]>", "Michał Papierski <[email protected]>"] | ||
edition = "2021" | ||
|
||
[[bin]] | ||
name = "payment_purse_persist" | ||
path = "src/main.rs" | ||
bench = false | ||
doctest = false | ||
test = false | ||
|
||
[dependencies] | ||
casper-contract = { path = "../../../contract" } | ||
casper-types = { path = "../../../../types" } |
63 changes: 63 additions & 0 deletions
63
smart_contracts/contracts/test/payment-purse-persist/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::string::String; | ||
use casper_contract::contract_api::{runtime, runtime::put_key, system}; | ||
use casper_types::{contracts::ContractPackageHash, runtime_args, ApiError, RuntimeArgs, URef}; | ||
|
||
const GET_PAYMENT_PURSE: &str = "get_payment_purse"; | ||
const THIS_SHOULD_FAIL: &str = "this_should_fail"; | ||
|
||
const ARG_METHOD: &str = "method"; | ||
|
||
/// This logic is intended to be used as SESSION PAYMENT LOGIC | ||
/// It gets the payment purse and attempts and attempts to persist it, | ||
/// which should fail. | ||
#[no_mangle] | ||
pub extern "C" fn call() { | ||
let method: String = runtime::get_named_arg(ARG_METHOD); | ||
|
||
// handle payment contract | ||
let handle_payment_contract_hash = system::get_handle_payment(); | ||
|
||
// get payment purse for current execution | ||
let payment_purse: URef = runtime::call_contract( | ||
handle_payment_contract_hash, | ||
GET_PAYMENT_PURSE, | ||
RuntimeArgs::default(), | ||
); | ||
|
||
if method == "put_key" { | ||
// attempt to persist the payment purse, which should fail | ||
put_key(THIS_SHOULD_FAIL, payment_purse.into()); | ||
} else if method == "call_contract" { | ||
// attempt to call a contract with the payment purse, which should fail | ||
let _payment_purse: URef = runtime::call_contract( | ||
handle_payment_contract_hash, | ||
GET_PAYMENT_PURSE, | ||
runtime_args! { | ||
"payment_purse" => payment_purse, | ||
}, | ||
); | ||
|
||
// should never reach here | ||
runtime::revert(ApiError::User(1000)); | ||
} else if method == "call_versioned_contract" { | ||
// attempt to call a versioned contract with the payment purse, which should fail | ||
let _payment_purse: URef = runtime::call_versioned_contract( | ||
ContractPackageHash::new(handle_payment_contract_hash.value()), | ||
None, // Latest | ||
GET_PAYMENT_PURSE, | ||
runtime_args! { | ||
"payment_purse" => payment_purse, | ||
}, | ||
); | ||
|
||
// should never reach here | ||
runtime::revert(ApiError::User(1001)); | ||
} else { | ||
runtime::revert(ApiError::User(2000)); | ||
} | ||
} |
Oops, something went wrong.