2

I wish to be able to create a smart contract taking input from a UI to supply parameters pubKey,prvKey,pubHash for the deployment and minAmount and maxTime to influence the Michelson.

I have the following JS:

function deploy(input_dict){
    const tezosNode='https://rpcalpha.tzbeta.net/';
    const conseilServer={url:'https://conseil-dev.cryptonomic-infra.tech:443',apiKey:'foo',network:'babylonnet'};

    async function contract() {
      const keystore = {
          publicKey:input_dict['pubKey'],
          privateKey:input_dict['prvKey'],
          publicKeyHash:input_dict['tz1-address'],
          seed:'',
          storeType:conseiljs.StoreType.Fundraiser
      };
      const michelson=`parameter (or (or (nat %divisor) (unit %double)) (nat %value));
        storage (nat %storedValue);
        code {
          DUP; CDR; SWAP; CAR;
          IF_LEFT {
            IF_LEFT {
              PAIR; DUP; CAR; PUSH nat 5; SWAP; COMPARE; GT;
              IF { }
              { PUSH string "WrongCondition: params.divisor > 5"; FAILWITH; };
              DUP; CDR; DUUP; CAR; EDIV;
              IF_SOME { CAR; }
              { PUSH string "division by zero"; FAILWITH; };
              SWAP; DROP; NIL operation; PAIR;
            }
            { PAIR; DUP; CDR; PUSH nat 2; MUL; SWAP; DROP; NIL operation; PAIR; };
          }
          { PAIR; DUP; CAR; SWAP; DROP; NIL operation; PAIR; };
        }`;
      const storage='"Sample"';
      const nodeResult = await conseiljs.TezosNodeWriter.sendContractOriginationOperation(tezosNode, keystore, 0, undefined, false, true, 100000, '', 1000, 100000, michelson, storage, conseiljs.TezosParameterFormat.Michelson);
      const groupid = nodeResult['operationGroupID'].replace(/\"/g, '').replace(/\n/, ''); // clean up RPC output
      console.log(`Injected operation group id ${groupid}`);
      const conseilResult = await conseiljs.TezosConseilClient.awaitOperationConfirmation(conseilServer, 'alphanet', groupid, 5);
      console.log(`Originated contract at ${conseilResult[0].originated_accounts}`);
    }
    contract();

Currently when run it produces the following errors:

GET https://rpcalpha.tzbeta.net//chains/main/blocks/head/context/contracts/foo/counter 403

Access to fetch at 'https://rpcalpha.tzbeta.net//chains/main/blocks/head/context/contracts/foo/counter' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Uncaught (in promise) TypeError: Failed to fetch

However, these I'm sure are less relevant as the code is obviously incorrect in its current form.

This code is heavily derived from the docs of Cryptonomic but I am still left unsure about many elements:

  • is seed:'' correct?
  • is storeType:conseiljs.StoreType.Fundraiser correct?
  • what is the value of const storage='"Sample"' for?

In relation to interacting with the Michelson. It was generated and prettified with SmartPy.io and in the test cases there are inputs supplied which influence storage within the smart contract:

def __init__(self,pubHash,minAmount,maxTime):
    self.init(fund=sp.bigMap(tkey=sp.TAddress,tvalue=sp.TMutez),
        owner=pubHash,minAmount=minAmount,maxTime=maxTime,balance=sp.tez(0))

How is this same interaction performed with the Michelson that is statically represented in the JS?

Any help is much appreciated!

3

I cannot answer all your questions but at least, I can say that, in SmartPy.io, the storage sent to ConseilJS is the content of the tab "Storage" (in either Michelson or Micheline formats) and the code is similarly the content of one of the corresponding tabs.

Depending on the form you're using, you need to set Michelson or Micheline (the latter is quite possibly better to use in the long run). To be clear, Micheline is in JSON, Michelson is a custom format.

Then, in your application you need to generate the storage by changing a working version with your own values. I think that there are already released tools that may be able to help you but I don't know them. I wouldn't be surprised if a few teams were working on this subject.

Note that you may wish to try another node if this one doesn't work or you have CORS issues when using it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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