0

i'm deploying a smart contract which contains several functions as well as properties. The ABI looks as follows:

[ { constant: false,
   inputs: [Array],
   name: 'vote',
   outputs: [],
   payable: false,
   stateMutability: 'nonpayable',
   type: 'function' },
 { constant: true,
   inputs: [Array],
   name: 'options',
   outputs: [Array],
   payable: false,
   stateMutability: 'view',
   type: 'function' },
 { constant: true,
   inputs: [Array],
   name: 'voters',
   outputs: [Array],
   payable: false,
   stateMutability: 'view',
   type: 'function' },
 { constant: true,
   inputs: [],
   name: 'option_amount',
   outputs: [Array],
   payable: false,
   stateMutability: 'view',
   type: 'function' },
 { constant: false,
   inputs: [Array],
   name: 'addOption',
   outputs: [],
   payable: false,
   stateMutability: 'nonpayable',
   type: 'function' },
 { inputs: [],
   payable: false,
   stateMutability: 'nonpayable',
   type: 'constructor' },
 { anonymous: false,
   inputs: [Array],
   name: 'votedEvent',
   type: 'event' } ]

I just want to obtain the values of the properties and call some functions. However, i cannot find the right way. So far i tried (all lead to one error or another):

contractInstance.options.call((err, res) => console.log(err, res))

Error: Invalid number of arguments to Solidity function

contractInstance.addOption('Meeting on Saturday.')

Error: invalid address

contractInstance.methods.options().call().then(console.log)

TypeError: Cannot read property 'options' of undefined

    var result = contractInstance.vote(0, {from: web3.eth.accounts[1], gas: 5500000}).call((error, result) => {
    console.log(result);
});

Error: VM Exception while processing transaction: revert

I am using JavaScript and web3.js (0.20.2). How do i create the contractInstance?

function deployContract(compiledContract, contractName, callback){
contractABI = getContractABI(compiledContract)
newContract = web3.eth.contract(contractABI)
byteCode = compiledContract.contracts[':'+contractName].bytecode
deployedContract = newContract.new([],{
    data: byteCode,
    from: web3.eth.accounts[0],
    gas: 4700000
}, function (e, contract){
      console.log(e);
      if (typeof contract.address !== 'undefined') {
          console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
            callback(newContract, contract.address)
      }
  });
}
2
  • I assume you're writing JavaScript? Are you using web3.js? If so, what version? How are you creating contractInstance? You said each gives you an error. What error does each line give you?
    – user19510
    May 14, 2018 at 15:20
  • 1
    github.com/ethereum/wiki/wiki/JavaScript-API#contract-methods is the relevant documentation. If you still have trouble after that, post your updated code including how you create contractInstance.
    – user19510
    May 14, 2018 at 17:25

1 Answer 1

0

You can create using the same code as given below -

var Web3 = require('web3')
var web3 = new Web3("http://localhost:8545")

var Eth = require('web3-eth');
var eth = new Eth("http://localhost:8545")


//Place your ABI here

var abi = [ { "constant": false, "inputs": [ { "name": "_productname", "type": "string" } ], "name": "setProduct", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "getProduct", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" } ]



//Create instance of your contract -- 
//Second argument - contract address deployed on Ganache using Web3 provider 
var myContract = new web3.eth.Contract(abi, "0x067565eaD5CAFb8C60cdd312A198c191e43Dc573")
console.log(myContract.options)
console.log(myContract.methods)


// Call the function on smart contract
//A sample setProduct() takes one param 
myContract.methods.setProduct("testproduct-created").call()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.