How to setcode / setabi from Node in beta?
Version of EOSJS 20.0.0-beta2
Describe the bug
I'm running the eosio-1.4 docker image.
I deploy the eosio.token code (from the eosio.contracts 1.4 branch) and abi from Nodejs but I get errors when interacting with it.
For example when running:
cleos -u http://127.0.0.1:7777 get code eosio.token
It throws this error:
thread-0 main.cpp:3143 main ] Failed with error: Out of Range (8)
read datastream of length 4456 over by -10
Error unpacking field new_type_name
error unpacking eosio::chain::type_def
Error unpacking field types
error unpacking eosio::chain::abi_def
Same happens when I call get code instead.
When I run an action on that contract, I get this error from EOS:
fetching abi for eosio.token: Unsupported abi version
I'm reading the file and then calling .toString(`hex`) on the buffer which I pass as the ABI.
Why is it not working?
To Reproduce Deploy the eosio.token contract
const EOSIO_TOKEN_ACCOUNT = `eosio.token`
async function deployTokenContract() {
const contractDir = `./scripts/init_blockchain/eosio.token`
const wasm = fs.readFileSync(path.join(contractDir, `eosio.token.wasm`))
const abi = fs.readFileSync(path.join(contractDir, `eosio.token.abi`))
try {
await sendTransaction({
account: `eosio`,
name: `setcode`,
actor: EOSIO_TOKEN_ACCOUNT,
data: {
account: EOSIO_TOKEN_ACCOUNT,
vmtype: 0,
vmversion: 0,
code: wasm.toString(`hex`),
},
})
console.log(`Contract updated`)
} catch (error) {
console.error(`setcode failed:`, getErrorDetail(error))
}
try {
await sendTransaction({
account: `eosio`,
name: `setabi`,
actor: EOSIO_TOKEN_ACCOUNT,
data: {
account: EOSIO_TOKEN_ACCOUNT,
abi: abi.toString(`hex`),
},
})
console.log(`ABI updated`)
} catch (error) {
console.error(`setabi failed:`, getErrorDetail(error))
}
}
This is the helper sendTransaction function:"
const { CONTRACT_ACCOUNT } = process.env
const createAction = ({
account = CONTRACT_ACCOUNT,
name,
actor = CONTRACT_ACCOUNT,
data = {},
}) => ({
account,
name,
authorization: [
{
actor,
permission: `active`,
},
],
data,
})
const sendTransaction = async args => {
const actions = Array.isArray(args) ? args.map(createAction) : [createAction(args)]
return api.transact(
{
actions,
},
{
blocksBehind: 3,
expireSeconds: 30,
},
)
}
Expected behavior Deploying code + abi should work.
Additional context I'm running the eosio-1.4 docker image.
MIght be the same unpacking issue described here?
setcode needs a packed (binary) abi. It looks like you're converting the json text to hex instead.
Here's the code which converts abis from binary to json; you need to do the opposite: https://github.com/EOSIO/eosjs/blob/776a324b7aebf9c4397a14eaf7436fd623c3799d/src/eosjs-api.ts#L79
Thanks, is there a library that can do that or is it on your plan for the eosjs-2 release? In eosjs-1 it was as simple as calling
const wasm = fs.readFileSync(`test.wasm`))
eos.setcode(process.env.CONTRACT_ACCOUNT, 0, 0, wasm)
It would be nice if setcode would be added as another example to the documentation.
I can help with that if I figure out how to do it.
I got a proof of concept which works 😃
const { Serialize } = require(`eosjs`)
const buffer = new Serialize.SerialBuffer({
textEncoder: api.textEncoder,
textDecoder: api.textDecoder,
})
let abi = JSON.parse(fs.readFileSync(abiPath, `utf8`))
const abiDefinition = api.abiTypes.get(`abi_def`)
// need to make sure abi has every field in abiDefinition.fields
// otherwise serialize throws
abi = abiDefinition.fields.reduce(
(acc, { name: fieldName }) => Object.assign(acc, { [fieldName]: acc[fieldName] || [] }),
abi,
)
abiDefinition.serialize(buffer, abi)
await sendTransaction({
account: `eosio`,
name: `setabi`,
actor: account,
data: {
account,
abi: Buffer.from(buffer.asUint8Array()).toString(`hex`),
},
})
Edit: Cleaned up the code.
combine with @MrToph 's code,this is how i set abi and wasm, i think the problem you have meet is the *.wasm file has lot's of space and line break need to replace to empty string. This is work for me.
let FAUCET ="faucet";
let faucetWasm;
let faucetAbi;
//get wasm
let wasmLocation = path.join(__dirname, "../", "faucet.wasm");
faucetWasm = await fs.readFileSync(wasmLocation, "utf-8");
//replace space and line break
faucetWasm = faucetWasm.replace(/(\r\n\t|\n|\r\t| )/gm, "");
//get abi hex
const buffer = new eosjs.Serialize.SerialBuffer({
textEncoder: api.textEncoder,
textDecoder: api.textDecoder,
});
let abiLocation = path.join(__dirname, "../", "faucet.abi");
let abi = await fs.readFileSync(abiLocation, "utf-8");
abi = JSON.parse(abi);
let abiDefinition = api.abiTypes.get('abi_def');
abi = abiDefinition.fields.reduce(
(acc, {name: fieldName}) => Object.assign(acc, {[fieldName]: acc[fieldName] || []}),
abi,
);
abiDefinition.serialize(buffer, abi);
faucetAbi = Buffer.from(buffer.asUint8Array()).toString(`hex`);
const result = await api.transact({
actions: [{
account: 'eosio',
name: 'setcode',
authorization: [{
actor: FAUCET,
permission: 'active',
}],
data: {
account: FAUCET,
vmtype: 0,
vmversion: 0,
code: faucetWasm
},
},
{
account: 'eosio',
name: 'setabi',
authorization: [{
actor: FAUCET,
permission: 'active',
}],
data: {
account: FAUCET,
abi: faucetAbi
},
}
]
}, {
blocksBehind: 3,
expireSeconds: 30,
});