Struct concordium_cis2::Cis2Client
source · pub struct Cis2Client { /* private fields */ }
Expand description
Client for interacting with CIS2 compliant contracts.
§Examples
use concordium_cis2::Cis2Client;
use concordium_std::ContractAddress;
let cis_contract_address = ContractAddress::new(0, 0);
Cis2Client::new(cis_contract_address);
Implementations§
source§impl Cis2Client
impl Cis2Client
pub fn new(contract: ContractAddress) -> Self
sourcepub fn supports_cis2<State, E: Deserial>(
&self,
host: &impl HasHost<State>
) -> Result<SupportResult, Cis2ClientError<E>>
pub fn supports_cis2<State, E: Deserial>( &self, host: &impl HasHost<State> ) -> Result<SupportResult, Cis2ClientError<E>>
Calls the supports
entrypoint of the CIS2 contract to check if the
given contract supports CIS2 standard.
If the contract supports CIS2 standard, it returns
Ok(SupportResult::Support)
, else it returns
Ok(SupportResult::NoSupport)
. If the contract supports CIS2
standard by another contract, it returns
Ok(SupportResult::SupportBy(Vec<ContractAddress>))
. If there is an
error, it returns Err
.
§Examples
use concordium_cis2::*;
use concordium_std::{test_infrastructure::*, *};
let mut host = TestHost::new((), TestStateBuilder::new());
host.setup_mock_entrypoint(
ContractAddress::new(0, 0),
OwnedEntrypointName::new_unchecked("supports".to_string()),
MockFn::new_v1(|_, _, _, _| {
Ok((false, SupportsQueryResponse {
results: vec![SupportResult::Support],
}))
}),
);
let client = Cis2Client::new(ContractAddress::new(0, 0));
let res: Result<SupportResult, Cis2ClientError<()>> = client.supports_cis2(&host);
assert!(res.is_ok());
match res.unwrap() {
SupportResult::NoSupport => fail!(),
SupportResult::Support => (),
SupportResult::SupportBy(_) => fail!(),
}
sourcepub fn operator_of<State, E: Deserial>(
&self,
host: &impl HasHost<State>,
owner: Address,
address: Address
) -> Result<bool, Cis2ClientError<E>>
pub fn operator_of<State, E: Deserial>( &self, host: &impl HasHost<State>, owner: Address, address: Address ) -> Result<bool, Cis2ClientError<E>>
Calls the operatorOf
entrypoint of the CIS2 contract to check if the
given owner is an operator of the given contract. If the owner is an
operator of the given contract, it returns Ok(true)
,
else it returns Ok(false)
.
If there is an error, it returns Err
.
§Examples
use concordium_cis2::*;
use concordium_std::{test_infrastructure::*, *};
let mut host = TestHost::new((), TestStateBuilder::new());
host.setup_mock_entrypoint(
ContractAddress::new(0, 0),
OwnedEntrypointName::new_unchecked("operatorOf".to_string()),
MockFn::new_v1(|_, _, _, _| {
Ok((false, OperatorOfQueryResponse {
0: vec![true],
}))
}),
);
let client = Cis2Client::new(ContractAddress::new(0, 0));
let res: Result<bool, Cis2ClientError<()>> = client.operator_of(
&mut host,
Address::Account(AccountAddress([1; 32])),
Address::Contract(ContractAddress::new(1, 0)),
);
assert_eq!(res.unwrap(), true);
sourcepub fn balance_of<State, T: IsTokenId, A: IsTokenAmount, E: Deserial>(
&self,
host: &impl HasHost<State>,
token_id: T,
address: Address
) -> Result<A, Cis2ClientError<E>>
pub fn balance_of<State, T: IsTokenId, A: IsTokenAmount, E: Deserial>( &self, host: &impl HasHost<State>, token_id: T, address: Address ) -> Result<A, Cis2ClientError<E>>
Calls the balanceOf
entrypoint of the CIS2 contract to get the balance
of the given owner for the given token. If the balance is returned,
it returns Ok(balance)
, else it returns Err
.
§Examples
use concordium_cis2::*;
use concordium_std::{test_infrastructure::*, *};
let mut host = TestHost::new((), TestStateBuilder::new());
host.setup_mock_entrypoint(
ContractAddress::new(0, 0),
OwnedEntrypointName::new_unchecked("balanceOf".to_string()),
MockFn::new_v1(|_, _, _, _| {
Ok((false, BalanceOfQueryResponse::<TokenAmountU8>(vec![1.into()])))
}),
);
let client = Cis2Client::new(ContractAddress::new(0, 0));
let res: Result<TokenAmountU8, Cis2ClientError<()>> =
client.balance_of(&host, TokenIdU8(1), Address::Account(AccountAddress([1; 32])));
assert!(res.is_ok());
assert_eq!(res.unwrap(), 1.into());
sourcepub fn transfer<State, T: IsTokenId, A: IsTokenAmount, E: Deserial>(
&self,
host: &mut impl HasHost<State>,
transfer: Transfer<T, A>
) -> Result<bool, Cis2ClientError<E>>
pub fn transfer<State, T: IsTokenId, A: IsTokenAmount, E: Deserial>( &self, host: &mut impl HasHost<State>, transfer: Transfer<T, A> ) -> Result<bool, Cis2ClientError<E>>
Calls the transfer
entrypoint of the CIS2 contract to transfer the
given amount of tokens from the given owner to the given receiver.
If the transfer is successful and the state is modified, it returns
Ok(true)
, else it returns Ok(false)
. If there is an error, it
returns Err
.
§Examples
use concordium_cis2::*;
use concordium_std::{test_infrastructure::*, *};
let mut host = TestHost::new((), TestStateBuilder::new());
host.setup_mock_entrypoint(
ContractAddress::new(0, 0),
OwnedEntrypointName::new_unchecked("transfer".to_string()),
MockFn::new_v1(|_, _, _, _| Ok((false, ()))),
);
let client = Cis2Client::new(ContractAddress::new(0, 0));
let res: Result<bool, Cis2ClientError<()>> = client.transfer(&mut host, Transfer {
amount: TokenAmountU8(1),
from: Address::Account(AccountAddress([1; 32])),
to: Receiver::Account(AccountAddress([2; 32])),
token_id: TokenIdU8(1),
data: AdditionalData::empty(),
});
assert!(res.is_ok());
sourcepub fn update_operator<State, E: Deserial>(
&self,
host: &mut impl HasHost<State>,
operator: Address,
update: OperatorUpdate
) -> Result<bool, Cis2ClientError<E>>
pub fn update_operator<State, E: Deserial>( &self, host: &mut impl HasHost<State>, operator: Address, update: OperatorUpdate ) -> Result<bool, Cis2ClientError<E>>
Calls the updateOperator
of the CIS2 contract.
If the update is successful and the state is modified, it returns
Ok(true)
, else it returns Ok(false)
. If there is an error, it
returns Err
.
§Examples
use concordium_cis2::*;
use concordium_std::{test_infrastructure::*, *};
let mut host = TestHost::new((), TestStateBuilder::new());
host.setup_mock_entrypoint(
ContractAddress::new(0, 0),
OwnedEntrypointName::new_unchecked("updateOperator".to_string()),
MockFn::new_v1(|_, _, _, _| Ok((false, ()))),
);
let client = Cis2Client::new(ContractAddress::new(0, 0));
let res: Result<bool, Cis2ClientError<()>> = client.update_operator(
&mut host,
Address::Account(AccountAddress([1; 32])),
OperatorUpdate::Add,
);
assert!(res.is_ok());