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

source

pub fn new(contract: ContractAddress) -> Self

source

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!(),
}
source

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);
source

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());
source

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());
source

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());

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.