0

I’m trying to figure out how to originate a new contract from within a dapp. I seem to have found what I am looking for here: https://tezostaquito.io/docs/originate

Here is the example code I am looking at, for convenience:

// import { TezosToolkit } from '@taquito/taquito';
// const Tezos = new TezosToolkit('https://jakartanet.ecadinfra.com');

// const genericMultisigJSONfile = require('./generic.json')
// generic.json is referring to Michelson source code in JSON representation

Tezos.contract
  .originate({
    code: genericMultisigJSONfile,
    storage: {
      stored_counter: 0,
      threshold: 1,
      keys: ['edpkuLxx9PQD8fZ45eUzrK3BhfDZJHhBuK4Zi49DcEGANwd2rpX82t'],
    },
  })
  .then((originationOp) => {
    println(`Waiting for confirmation of origination for ${originationOp.contractAddress}...`);
    return originationOp.contract();
  })
  .then((contract) => {
    println(`Origination completed.`);
  })
  .catch((error) => println(`Error: ${JSON.stringify(error, null, 2)}`));

However this appears to require the contract to be michelson source code in JSON representation. How can I convert my archetype smart contract into this format?

Or more broadly - am I taking the wrong approach here altogether? Is there an archetype specific tool or approach here?

For example, I am wondering if the storage parameter that plugs into contract.originate() will even to accept the custom archetype types from the "@completium/archetype-ts-types" library... ? (Because I haven't gotten past converting the smart contract into the right format I haven't been able to test this myself yet)

Any help is appreciated

--EDIT--

Just a note that I eventually found it easiest to use this command to generate the contract code as a json object already embedded in a javascript file: completium-cli generate javascript test.arl --json >> testJSONmich.js

In order to most easily update the storage, I used the instructions under 'option a' here: https://tezostaquito.io/docs/originate/.

This allows an ordinary javascript object to be passed in with the storage variables and initialising parameters.

1 Answer 1

2

there is a completium command to generate the Json Micheline:

completium-cli generate michelson mycontract.arl --json

This generates a Json structure, whose "code" field is what you pass to taquito for origination, and the "storage" field is the initial storage value.

For example, consider the following contract:

archetype simple

variable res : nat = 0

entry exec (n : nat) {
  res := n
}

The command above generates the following:

{
  "code":
    [  {  "prim": "storage",
          "args": [
            {  "prim": "nat"  }
          ]
       },
       {  "prim": "parameter",
          "args": [
            {  "prim": "nat",
               "annots": [
                 "%exec"
               ]
            }
          ]
       },
       {  "prim": "code",
          "args": [
            [  {  "prim": "UNPAIR"  },
            {  "prim": "DUP"  },
            {  "prim": "DIP",
               "args": [
                 {  "int": "1"  },
                 [  {  "prim": "SWAP"  },
                 {  "prim": "DROP",
                    "args": [
                      {  "int": "1"  }
                    ]
                 }  ]
               ]
            },
            {  "prim": "SWAP"  },
            {  "prim": "DROP",
               "args": [
                 {  "int": "1"  }
               ]
            },
            {  "prim": "NIL",
               "args": [
                 {  "prim": "operation"  }
               ]
            },
            {  "prim": "PAIR"  }  ]
          ]
       }  ]
  "storage":
    {  "int": "0"  };
}

If the contract has parameters, you then need to replace them somehow in the generated object.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.