curve_casper_erc20/
detail.rs1use core::convert::TryInto;
3
4use casper_contract::{
5 contract_api::{runtime, storage},
6 unwrap_or_revert::UnwrapOrRevert,
7};
8use casper_types::{
9 bytesrepr::{FromBytes, ToBytes},
10 system::CallStackElement,
11 ApiError, CLTyped, URef,
12};
13
14use crate::{error::Error, Address};
15
16pub(crate) fn get_uref(name: &str) -> URef {
18 let key = runtime::get_key(name)
19 .ok_or(ApiError::MissingKey)
20 .unwrap_or_revert();
21 key.try_into().unwrap_or_revert()
22}
23
24pub(crate) fn read_from<T>(name: &str) -> T
26where
27 T: FromBytes + CLTyped,
28{
29 let uref = get_uref(name);
30 let value: T = storage::read(uref).unwrap_or_revert().unwrap_or_revert();
31 value
32}
33
34pub(crate) fn write_to<T>(name: &str, value: T)
36where
37 T: ToBytes + CLTyped,
38{
39 let uref = get_uref(name);
40 storage::write(uref, value);
41}
42
43fn get_immediate_call_stack_item() -> Option<CallStackElement> {
45 let call_stack = runtime::get_call_stack();
46 call_stack.into_iter().rev().nth(1)
47}
48
49fn call_stack_element_to_address(call_stack_element: CallStackElement) -> Address {
54 match call_stack_element {
55 CallStackElement::Session { account_hash } => Address::from(account_hash),
56 CallStackElement::StoredSession { account_hash, .. } => {
57 Address::from(account_hash)
60 }
61 CallStackElement::StoredContract {
62 contract_package_hash,
63 ..
64 } => Address::from(contract_package_hash),
65 }
66}
67
68pub(crate) fn get_immediate_caller_address() -> Result<Address, Error> {
73 get_immediate_call_stack_item()
74 .map(call_stack_element_to_address)
75 .ok_or(Error::InvalidContext)
76}
77
78pub(crate) fn get_caller_address() -> Result<Address, Error> {
82 let call_stack = runtime::get_call_stack();
83 let top_of_the_stack = call_stack
84 .into_iter()
85 .rev()
86 .next()
87 .ok_or(Error::InvalidContext)?;
88 let address = call_stack_element_to_address(top_of_the_stack);
89 Ok(address)
90}