简体   繁体   中英

Call Web3 contract send function node

So I want to have a Node server acting on behalf of an ETH address when it calls my contract's payable function. Some sites have gone over similar concepts but only within the scope of sending ETH, not calling a contract function. How do I do this?

To interact with your contract you need to create a contract instance in node server.

After deploying the contract you will get contract abi and contract address .

 var Web3 = require('web3'); var web3 = new Web3('http://localhost:8545'); var abi = [{"constant":true,"inputs":[],"name":"txcount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]; var contractaddress = "0xf217e1fe69d........."; var contractinstance =new web3.eth.Contract(abi, contadd); contractinstance.methods.nameFunction(param1, param2).send({from:"0xfc312ab....", gas: 100000}, function(error, txHash){ console.log(txHash); }); 

Also you can retrieve value from a function written in solidity as:

 contractinstance.methods.retrieveValue(param1, param2).call({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'}, function(error, result){ console.log(result); }); 

For further information read the documentation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM