我正在尝试使用eosjs
. 我从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> node .\deploySmartContract.js 错误:Object.serializeStruct [as serialize] 处缺少 setcode.vmtype (type=uint8) (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) 在阿皮。(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)