Skip to content

Instantly share code, notes, and snippets.

@schadokar
Created April 11, 2019 16:45
Show Gist options
  • Save schadokar/53bfda1f5612c47d83a48516f67eabd2 to your computer and use it in GitHub Desktop.
Save schadokar/53bfda1f5612c47d83a48516f67eabd2 to your computer and use it in GitHub Desktop.
Interact with deployed smart contract
const fs = require("fs-extra");
const {web3} = require("./web3");
const compileContract = require("./build/Message.json");
// Contract object deployed on network (ganache-cli or testnet or mainnet)
// network can be selected in web3 file
// cont
const getContractObject = () => {
const contractReceipt = require("./receipt-ganache.json");
// create a contract object/instance
const contractObject = new web3.eth.Contract(
JSON.parse(compileContract.interface),
contractReceipt.address
);
return contractObject;
};
const setMessage = async (newMessage) => {
const accounts = await web3.eth.getAccounts();
const contractObject = getContractObject();
const receipt = await contractObject.methods
.setMessage(newMessage)
.send({from : accounts[0], gas:1000000});
console.info(receipt);
console.info("Message successfully saved!");
return receipt;
};
const getMessage = async () => {
const contractObject = getContractObject();
const accounts = await web3.eth.getAccounts();
const result = await contractObject.methods
.getMessage()
.call({from:accounts[0]});
console.log(result);
return result;
};
module.exports = {
setMessage,
getMessage
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment