Search code examples
nearprotocol

How does near protocol distinguish between contracts?


https://examples.near.org/rust-status-message

You can deploy your smart contract using:

near deploy --wasmFile res/status_message.wasm --accountId YOUR_ACCOUNT_NAME

And we are calling the contract function set_status using:

near call YOUR_ACCOUNT_NAME set_status '{"message": "aloha friend"}' --accountId YOUR_ACCOUNT_NAME

The function is here:

#[near_bindgen]
impl StatusMessage {
    pub fn set_status(&mut self, message: String) {
        env::log(b"A");
        let account_id = env::signer_account_id();
        self.records.insert(&account_id, &message);
    }

    pub fn get_status(&self, account_id: String) -> Option<String> {
        env::log(b"A");
        return self.records.get(&account_id);
    }
}

How does near protocol distinguish between contract?

What if some other contracts have set_status function, which contract function it will call. Also what if I redeploy the contract and run the function. Which contract function will it call?


Solution

  • Here's what you need to know that will hopefully clear this up:

    • Every contract deployed to NEAR must consume 1 account.
    • Every account on NEAR must have 0 or 1 contract.
    • Account names follow a pattern lik DNS with network as top level
      • your-account.testnet could be your main developer account on TestNet
      • contract-v1.your-account.testnet could be v1 of a contract you wrote
      • your-token.your-account.testnet could be a fungible token that you control

    When working with our examples, the deployment process uses the near dev-deploy command which does 4 things in 2 steps (see attached image from NEAR Explorer)

    step 1 (batch transaction)

    • 1.1. create new account (your-account)
    • 1.2. fund the account from faucet with 100 NEAR
    • 1.3. add FullAccess key to that account

    step 2

    • 2.1 deploy the contract (path/to.wasm) to the new account (your-account)

    NEAR Explorer contract deployment transactions

    The deploy command you included above

    near deploy --wasmFile path/to.wasm --accountId YOUR_ACCOUNT_NAME
    

    only does the last step, 2.1.

    For the first 3 things in step 1 (1.1 - 1.3) you will need this:

    near create_account a-contract-on.your-account.testnet --master-account your-account.testnet --helper-url https://helper.testnet.near.org
    

    where your-account.testnet was created beforehand using NEAR Wallet

    You can see these links for more details: