1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use lazy_static::lazy_static;

use contract::args_parser::ArgsParser;
use engine_shared::stored_value::StoredValue;
use engine_storage::global_state::StateReader;
use standard_payment::{AccountProvider, MintProvider, ProofOfStakeProvider, StandardPayment};
use types::{bytesrepr::ToBytes, system_contract_errors, ApiError, Key, URef, U512};

use crate::{execution, runtime::Runtime};

lazy_static! {
    static ref SERIALIZED_GET_PAYMENT_PURSE: Vec<u8> = ArgsParser::parse(("get_payment_purse",))
        .expect("args should convert to `Vec<CLValue>`")
        .into_bytes()
        .expect("args should serialize");
}

impl<'a, R> AccountProvider for Runtime<'a, R>
where
    R: StateReader<Key, StoredValue>,
    R::Error: Into<execution::Error>,
{
    fn get_main_purse(&self) -> Result<URef, ApiError> {
        self.context
            .get_main_purse()
            .map_err(|_| ApiError::InvalidPurse)
    }
}

impl<'a, R> MintProvider for Runtime<'a, R>
where
    R: StateReader<Key, StoredValue>,
    R::Error: Into<execution::Error>,
{
    fn transfer_purse_to_purse(
        &mut self,
        source: URef,
        target: URef,
        amount: U512,
    ) -> Result<(), ApiError> {
        let mint_contract_key = Key::from(self.get_mint_contract_uref());
        self.mint_transfer(mint_contract_key, source, target, amount)
            .map_err(|error| match error {
                execution::Error::SystemContract(system_contract_errors::Error::Mint(
                    mint_error,
                )) => ApiError::from(mint_error),
                _ => ApiError::Unhandled,
            })
    }
}

impl<'a, R> ProofOfStakeProvider for Runtime<'a, R>
where
    R: StateReader<Key, StoredValue>,
    R::Error: Into<execution::Error>,
{
    fn get_payment_purse(&mut self) -> Result<URef, ApiError> {
        let pos_contract_key = Key::from(self.get_pos_contract_uref());

        let cl_value = self
            .call_contract(pos_contract_key, SERIALIZED_GET_PAYMENT_PURSE.clone())
            .map_err(|_| {
                ApiError::ProofOfStake(
                    system_contract_errors::pos::Error::PaymentPurseNotFound as u8,
                )
            })?;

        let payment_purse_ref: URef = cl_value.into_t()?;
        Ok(payment_purse_ref)
    }
}

impl<'a, R> StandardPayment for Runtime<'a, R>
where
    R: StateReader<Key, StoredValue>,
    R::Error: Into<execution::Error>,
{
}