我正在尝试使用eosjs部署EOS智能合约。
我引用了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)
       }
    };
 };


我收到以下错误:


  PS E:\ EOSIO \ smartcontract>节点。\ deploySmartContract.js
  错误:缺少setcode.vmtype(type = uint8)
      在Object.serializeStruct [序列化时](E:\ EOSIO \ node_modules \ eosjs \ dist \ eosjs-serialize.js:571:27)
      在serializeActionData(E:\ EOSIO \ node_modules \ eosjs \ dist \ eosjs-serialize.js:1041:12)
      在Object.serializeAction(E:\ EOSIO \ node_modules \ eosjs \ dist \ eosjs-serialize.js:1051:15)
      在Api。 (E:\ EOSIO \ node_modules \ eosjs \ dist \ eosjs-api.js:278:71)
      在步骤(E:\ EOSIO \ node_modules \ eosjs \ dist \ eosjs-api.js:47:23)
      在Object.next(E:\ EOSIO \ node_modules \ eosjs \ dist \ eosjs-api.js:28:53)
      在完成时(E:\ EOSIO \ node_modules \ eosjs \ dist \ eosjs-api.js:19:58)
      在process._tickCallback(内部/进程/next_tick.js:68:7)

最佳答案

 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)
       }
    };
 };


在setcode的数据字段中添加vmtype和vmversion。

10-06 15:25