Smart Contract communication

In this section we'll give you some instructions on how to communicate with the Smart Contract.

Neo-python cli

The Elight contract's hash is 7ddf3c47eb6ca27e472067c110f4f435478b5fff

As we discussed earlier, there are two scenarios when creating a contract. With and without the deposit. Will cover both of them.

I. Without depositing

First of all, we importing our smart contract like so:

neo> import contract contract.avm 0705 05 True True

The next step is to init the contract:

neo> testinvoke {script_hash} init ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y","1<x<26",b'000000010000001a7ffffffa']

Then we emulate sensor's behavior by typing:

neo> testinvoke script_hash invoke ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y",1,b'0000000a'] //1 < 10 < 26 true 
neo> testinvoke script_hash invoke ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y",1,b'0000001a'] //1 < 26 < 26 false
II. With depositing

Very similar to previous scenario. First of all, we should mint some tokens, to make a deposit:

neo> testinvoke script_hash mint [] --attach-neo=1  //mint 10 EC

You'll see something like this:

[SmartContract.Storage.Get] AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y -> 0
[SmartContract.Storage.Put] AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y -> 10

Then we'll be able to initialize a contract with depositing option like so:

testinvoke script_hash initDeposit ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y","1<x<26",b'000000010000000a7ffffffa',"AKDVzYGLczmykdtRaejgvWeZrvdkVEvQ1X",1]

Afterwards, you'll see something like this:

[SmartContract.Storage.Get] AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y -> 10  //freezing tokens
[SmartContract.Storage.Put] AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y -> 9

Then we invoke a contract, pretending that we are hardware sensor:

neo> testinvoke script_hash invoke ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y",1,b'0000000a'] //1 < 10 < 26 true

In this case, carrier gets back his deposit, because he's transferred the cargo successfully. You'll immediately see:

[SmartContract.Storage.Get] AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y -> 9  //unfreezing tokens in favor of carrier
[SmartContract.Storage.Put] AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y -> 10

Let's look at another scenario:

neo> testinvoke script_hash invoke ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y",1,b'0000001a'] //1 < 26 < 26 false

In this case, carrier has failed his mission, so his tokens are sent to client automatically. You'll se something like this:

[SmartContract.Storage.Get] AKDVzYGLczmykdtRaejgvWeZrvdkVEvQ1X -> 0  //unfreezing tokens in favor of carrier
[SmartContract.Storage.Put] AKDVzYGLczmykdtRaejgvWeZrvdkVEvQ1X -> 1

Javascript

There were a lot of complaints on Discord chat related to invoking smart contract using neon.js library, so here is an example, which is used in our project:

import Neon from '@cityofzion/neon-js';  //"@cityofzion/neon-js": "^3.3.0"
...

invoke(operation, gas, ...args) {
    return new Promise(
      (res, rej) => {
        const { net, address, scriptHash, privKey } = this; 
        getBalance(net, address)
          .then(balances => {

            const intents = [ 
              { 
                assetId: CONST.ASSET_ID.GAS, 
                value: 0.000001, 
                scriptHash 
              }
            ];

            if (operation === Token.OPERATIONS.MINT) {
              intents.push(
                { 
                  assetId: CONST.ASSET_ID.NEO, 
                  value: args[0], 
                  scriptHash 
                })
            }

            const invoke = { operation, args, scriptHash };
            const unsignedTx = Neon.create.invocationTx(balances, intents, invoke, gas);
            const signedTx = Neon.sign.transaction(unsignedTx, privKey);
            const hexTx = Neon.serialize.tx(signedTx);

            return rpc.queryRPC(CONST.DEFAULT_RPC.TEST, {
              method: 'sendrawtransaction', params: [hexTx], id: 1
            });
          })
          .then(m => res(m))
          .catch(err => rej(err));
      }
    );
}

results matching ""

    No results matching ""