node.jseos

How to solve 'missing setcode.vmtype (type=uint8)' error when deploying EOS Smart Contract using eosjs?


I'm trying to deploy an EOS Smart Contract using eosjs. I referred the code from https://eosio.github.io/eosjs/latest/how-to-guides/how-to-deploy-a-smart-contract

 const wasmFilePath = './hello.wasm'
 const abiFilePath = './hello.abi'
 const fs = require('fs')
 const { Api, JsonRpc, Serialize } = require('eosjs');
 const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig');  // development only
 const fetch = require('node-fetch'); //node only
 const { TextDecoder, TextEncoder } = require('util'); //node only
 let privateKey1   = '5J***********'
 const privateKeys = [privateKey1];

 const signatureProvider = new JsSignatureProvider(privateKeys);
 const rpc = new JsonRpc('https://jungle2.cryptolions.io:443', { fetch }); //required to read blockchain state
 const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() }); 


 const wasmHexString = fs.readFileSync(wasmFilePath).toString('hex')

 const buffer = new Serialize.SerialBuffer({
     textEncoder: api.textEncoder,
     textDecoder: api.textDecoder,
 })

 let abiJSON = JSON.parse(fs.readFileSync(abiFilePath, 'utf8'))
 const abiDefinitions = api.abiTypes.get('abi_def')
 abiJSON = abiDefinitions.fields.reduce(
     (acc, { name: fieldName }) =>
         Object.assign(acc, { [fieldName]: acc[fieldName] || [] }),
         abiJSON
     )
 abiDefinitions.serialize(buffer, abiJSON)
 let serializedAbiHexString = Buffer.from(buffer.asUint8Array()).toString('hex')

 deployContract();

 async function deployContract(){

 try{
 const wasmFilePath = './hello.wasm'
 const abiFilePath = './hello.abi'
 const fs = require('fs')
 const { Api, JsonRpc, Serialize } = require('eosjs');
 const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig');  // development only
 const fetch = require('node-fetch'); //node only
 const { TextDecoder, TextEncoder } = require('util'); //node only
 let privateKey1   = '5J***********'
 const privateKeys = [privateKey1];

 const signatureProvider = new JsSignatureProvider(privateKeys);
 const rpc = new JsonRpc('https://jungle2.cryptolions.io:443', { fetch }); //required to read blockchain state
 const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() }); 

 const wasmHexString = fs.readFileSync(wasmFilePath).toString('hex')

 const buffer = new Serialize.SerialBuffer({
     textEncoder: api.textEncoder,
     textDecoder: api.textDecoder,
 })

 let abiJSON = JSON.parse(fs.readFileSync(abiFilePath, 'utf8'))
 const abiDefinitions = api.abiTypes.get('abi_def')
 abiJSON = abiDefinitions.fields.reduce(
     (acc, { name: fieldName }) =>
         Object.assign(acc, { [fieldName]: acc[fieldName] || [] }),
         abiJSON
     )
 abiDefinitions.serialize(buffer, abiJSON)
 let serializedAbiHexString = Buffer.from(buffer.asUint8Array()).toString('hex')

 deployContract();

 async function deployContract(){
   try{
       await api.transact(
           {
             actions: [
               {
                 account: 'eosio',
                 name: 'setcode',
                 authorization: [
                   {
                     actor: user_name,
                     permission: 'active',
                   },
                 ],
                 data: {
                   account: user_name,
                   code: wasmHexString,
                 },
               },
               {
                 account: 'eosio',
                 name: 'setabi',
                 authorization: [
                   {
                     actor: user_name,
                     permission: 'active',
                   },
                 ],
                 data: {
                   account: user_name,
                   abi: serializedAbiHexString,
                 },
               },
             ],
           },
           {
             blocksBehind: 3,
             expireSeconds: 30,
           }
         )
       }
       catch(e){
           console.log(e)
       }
    };
 };   

I'm getting the following error:

PS E:\EOSIO\smartcontract> node .\deploySmartContract.js Error: missing setcode.vmtype (type=uint8) at Object.serializeStruct [as serialize] (E:\EOSIO\node_modules\eosjs\dist\eosjs-serialize.js:571:27) at serializeActionData (E:\EOSIO\node_modules\eosjs\dist\eosjs-serialize.js:1041:12) at Object.serializeAction (E:\EOSIO\node_modules\eosjs\dist\eosjs-serialize.js:1051:15) at Api. (E:\EOSIO\node_modules\eosjs\dist\eosjs-api.js:278:71) at step (E:\EOSIO\node_modules\eosjs\dist\eosjs-api.js:47:23) at Object.next (E:\EOSIO\node_modules\eosjs\dist\eosjs-api.js:28:53) at fulfilled (E:\EOSIO\node_modules\eosjs\dist\eosjs-api.js:19:58) at process._tickCallback (internal/process/next_tick.js:68:7)


Solution

  •  const wasmFilePath = './hello.wasm'
     const abiFilePath = './hello.abi'
     const fs = require('fs')
     const { Api, JsonRpc, Serialize } = require('eosjs');
     const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig');
     const fetch = require('node-fetch'); //node only
     const { TextDecoder, TextEncoder } = require('util'); //node only
     let privateKey1   = '5J***********'
     const privateKeys = [privateKey1];
    
     const signatureProvider = new JsSignatureProvider(privateKeys);
     const rpc = new JsonRpc('https://jungle2.cryptolions.io:443', { fetch }); //required to read blockchain state
     const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() }); 
    
    
     const wasmHexString = fs.readFileSync(wasmFilePath).toString('hex')
    
     const buffer = new Serialize.SerialBuffer({
         textEncoder: api.textEncoder,
         textDecoder: api.textDecoder,
     })
    
     let abiJSON = JSON.parse(fs.readFileSync(abiFilePath, 'utf8'))
     const abiDefinitions = api.abiTypes.get('abi_def')
     abiJSON = abiDefinitions.fields.reduce(
         (acc, { name: fieldName }) =>
             Object.assign(acc, { [fieldName]: acc[fieldName] || [] }),
             abiJSON
         )
     abiDefinitions.serialize(buffer, abiJSON)
     let serializedAbiHexString = Buffer.from(buffer.asUint8Array()).toString('hex')
    
     deployContract();
    
     async function deployContract(){
    
     try{
     const wasmFilePath = './hello.wasm'
     const abiFilePath = './hello.abi'
     const fs = require('fs')
     const { Api, JsonRpc, Serialize } = require('eosjs');
     const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig');  // development only
     const fetch = require('node-fetch'); //node only
     const { TextDecoder, TextEncoder } = require('util'); //node only
     let privateKey1   = '5J***********'
     const privateKeys = [privateKey1];
    
     const signatureProvider = new JsSignatureProvider(privateKeys);
     const rpc = new JsonRpc('https://jungle2.cryptolions.io:443', { fetch }); //required to read blockchain state
     const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() }); 
    
     const wasmHexString = fs.readFileSync(wasmFilePath).toString('hex')
    
     const buffer = new Serialize.SerialBuffer({
         textEncoder: api.textEncoder,
         textDecoder: api.textDecoder,
     })
    
     let abiJSON = JSON.parse(fs.readFileSync(abiFilePath, 'utf8'))
     const abiDefinitions = api.abiTypes.get('abi_def')
     abiJSON = abiDefinitions.fields.reduce(
         (acc, { name: fieldName }) =>
             Object.assign(acc, { [fieldName]: acc[fieldName] || [] }),
             abiJSON
         )
     abiDefinitions.serialize(buffer, abiJSON)
     let serializedAbiHexString = Buffer.from(buffer.asUint8Array()).toString('hex')
    
     deployContract();
    
     async function deployContract(){
       try{
           await api.transact(
               {
                 actions: [
                   {
                     account: 'eosio',
                     name: 'setcode',
                     authorization: [
                       {
                         actor: user_name,
                         permission: 'active',
                       },
                     ],
                     data: {
                       account: user_name,
                       vmtype: '0',
                       vmversion: '0',
                       code: wasmHexString,
                     },
                   },
                   {
                     account: 'eosio',
                     name: 'setabi',
                     authorization: [
                       {
                         actor: user_name,
                         permission: 'active',
                       },
                     ],
                     data: {
                       account: user_name,
                       abi: serializedAbiHexString,
                     },
                   },
                 ],
               },
               {
                 blocksBehind: 3,
                 expireSeconds: 30,
               }
             )
           }
           catch(e){
               console.log(e)
           }
        };
     };
    

    Add vmtype and vmversion in data field of setcode.