Skip to content

Instantly share code, notes, and snippets.

@ezirmusitua
Last active January 17, 2019 15:30
Show Gist options
  • Save ezirmusitua/9362668a41308c97c7aa716399c4aba2 to your computer and use it in GitHub Desktop.
Save ezirmusitua/9362668a41308c97c7aa716399c4aba2 to your computer and use it in GitHub Desktop.
[Extract eosjs push_transactions actions] Generate eosjs push_transactions' actions list from abi string #eos #javascript #documentation
function tmpl(strings, ...keys) {
return (function (...values) {
const dict = values[values.length - 1] || {};
const result = [strings[0]];
keys.forEach(function (key, i) {
const value = Number.isInteger(key) ? values[key] : dict[key];
result.push(value, strings[i + 1]);
});
return result.join('');
});
}
const DOC_SECTION_TMPL = tmpl`### ${'action_name'}\n\n
\`\`\`js
await api.transact({
actions: [{
account: 'eosio',
name: '${'action_name'}',
authorization: [{
actor: <account_name>,
permission: <permission_name>,
}],
data: {
${'fields'}
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
})
\`\`\`
`;
function joinFields(fields, indent = 6) {
let res = '';
for (const field of fields) {
res += `${field.name}: <${field.type}>, \n${' '.repeat(indent)}`;
}
return res;
}
function generate(structs_or_abi) {
let structs = structs_or_abi;
if (structs_or_abi.version) {
structs = structs_or_abi.structs;
}
return structs.map(st => DOC_SECTION_TMPL({
action_name: st.name,
fields: joinFields(st.fields)
})).join('\n\n');
}
function cmd() {
const fpath = process.argv[2] || './abi.json';
const structsOrAbi = require(fpath);
const docStr = generate(structsOrAbi);
console.info(docStr);
}
cmd();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment