Skip to content

Instantly share code, notes, and snippets.

@CaliosD
Last active February 23, 2019 07:59
Show Gist options
  • Save CaliosD/195a9223580b7975cac58eca22a248fd to your computer and use it in GitHub Desktop.
Save CaliosD/195a9223580b7975cac58eca22a248fd to your computer and use it in GitHub Desktop.
Some snippets for self testing on local node of IOST blockchain. iwallet is required to install already. Feel free to use for testing, and feel free to star🌟if it's useful to you. 🚀
const shell = require('shelljs')
const fs = require('fs')
const iwallet = 'iwallet --amount_limit "*:unlimited|ram:10000" --gas_limit 3000000 '
// compile js contract to abi
const compile = (path) => {
let cmd = 'iwallet compile ' + path
console.log("👉", cmd)
shell.exec(cmd)
console.log("â›± compile done")
}
// set amountLimit to unlimited in abi for easy test
const updateABI = (abiPath) => {
let abi = fs.readFileSync(abiPath, 'utf8')
let regex = /"amountLimit"\: \[\],/gi;
let replaced = abi.replace(regex, '"amountLimit": [{"token": "*","val": "unlimited"},{"token": "ram","val": "10000"}],')
fs.writeFileSync(abiPath, replaced, 'utf8')
console.log("â›± update abi done")
}
// publish and get contract id
const publish = (jsPath, abiPath, account) => {
let cmd = iwallet + 'publish ' + jsPath + ' ' + abiPath + ' --account ' + account
console.log("👉", cmd)
let result = shell.exec(cmd)
let contractId = result.split("The contract id is: ")[1].trim()
console.log("â›± publish done: ", contractId)
return contractId
}
// update contract
const updateContract = (jsPath, abiPath, contractId, account) => {
let cmd = iwallet + 'publish ' + jsPath + ' ' + abiPath + ' -u ' + contractId + ' --account ' + account
console.log("👉", cmd)
let result = shell.exec(cmd)
let txId = result.split("The transaction hash is: ")[1].substring(0, 44)
console.log("â›± update contract done: ", txId)
return txId
}
// call abi function
const icall = (contractId, actionName, params, account) => {
let cmd = iwallet + 'call "' + contractId + '" "' + actionName + '" ' + '\'' + params + '\'' + ' --account ' + account
console.log("👉", cmd)
let result = shell.exec(cmd)
let txId = result.split("The transaction hash is: ")[1].substring(0, 44)
console.log("â›± icall done: ", txId)
return txId
}
// get receipt by tx id
const receipt = (txId) => {
let cmd = 'iwallet receipt ' + txId
console.log("👉", cmd)
shell.exec(cmd)
}
// transfer token
const transfer = (sym, from, to, amount) => {
const tx = iost.callABI("token.iost", "transfer", [sym, from, to, amount, ""]);
iost.signAndSend(tx)
.on('pending', (response) => {
console.log("transfer pending ===> " + response)
})
.on('success', (response) => {
console.log("transfer success ===> " + response)
})
.on('failed', (response) => {
console.log("transfer failed ===> " + response)
})
}
module.exports = {
compile,
updateABI,
publish,
updateContract,
icall,
receipt,
transfer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment