Search code examples
nearprotocol

How to call different contract from its address?


In solidity (ethereum) one needs the contract address to call that contract.

contract KittyInterface {
  function getKitty(uint256 _id) external view returns (
    bool isGestating,
    bool isReady,
    uint256 cooldownIndex,
    uint256 nextActionAt,
    uint256 siringWithId,
    uint256 birthTime,
    uint256 matronId,
    uint256 sireId,
    uint256 generation,
    uint256 genes
  );
}

contract ZombieFeeding is ZombieFactory {

  address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d; 
  KittyInterface kittyContract = KittyInterface(ckAddress);

Can I do that in near protocol? The examples in near are something different which requires supplying the wasm file. https://github.com/near-examples/rust-high-level-cross-contract

pub fn deploy_status_message(&self, account_id: String, amount: u64) {
        Promise::new(account_id)
            .create_account()
            .transfer(amount as u128)
            .add_full_access_key(env::signer_account_pk())
            .deploy_contract(
                include_bytes!("../status-message-contract/status_message.wasm").to_vec(),
            );
    }

Also, I am getting errors while using the high level cross contract code. https://gateway.ipfs.io/ipfs/QmPvcjeEE5PJvaJNN2axgKVWGbWVEQCe9q4e95t9NCeGFt/


Solution

  • Take a look at the lockup contract example instead. It uses the following:

            ext_whitelist::is_whitelisted(
                staking_pool_account_id.clone(),
                &self.staking_pool_whitelist_account_id,
                NO_DEPOSIT,
                gas::whitelist::IS_WHITELISTED,
            )
            .then(ext_self_owner::on_whitelist_is_whitelisted(
                staking_pool_account_id,
                &env::current_account_id(),
                NO_DEPOSIT,
                gas::owner_callbacks::ON_WHITELIST_IS_WHITELISTED,
            ))
    

    from https://github.com/near/core-contracts/blob/cd221798a77d646d5f1200910d45326d11951732/lockup/src/owner.rs#L29-L40

    The first call interface is (<arg_0>, <arg_1>, ..., <arg_n>, <ACCOUNT_ID>, <ATTACHED_DEPOSIT>, <ATTACHED_GAS>)

    • <arg_0>, <arg_1>, ..., <arg_n> - arguments from the interface defined below
    • <ACCOUNT_ID> - the account where the contract to call is deployed
    • <ATTACHED_DEPOSIT> - the amount in yocto-NEAR to attach to the call
    • <ATTACHED_GAS> - the amount of Gas to pass to the call

    It later attached a callback to the first promise using .then. The callback is just another async function call.

    The high-level interface ext_whitelist is defined like this:

    #[ext_contract(ext_whitelist)]
    pub trait ExtStakingPoolWhitelist {
        fn is_whitelisted(&self, staking_pool_account_id: AccountId) -> bool;
    }
    

    from https://github.com/near/core-contracts/blob/cd221798a77d646d5f1200910d45326d11951732/lockup/src/lib.rs#L64-L67