Skip to content

Instantly share code, notes, and snippets.

@sauravtom
Created November 28, 2018 09:21
Show Gist options
  • Save sauravtom/59dc36fb669d87a5998230beb752c668 to your computer and use it in GitHub Desktop.
Save sauravtom/59dc36fb669d87a5998230beb752c668 to your computer and use it in GitHub Desktop.
//link to smart contract https://ethfiddle.com/Q4mg9xHlzR
let address = "0x03b6b93cf23f04a32aa94fe1ef96cae383900ea7";
let abi = [
{
"constant": false,
"inputs": [
{
"name": "newMessage",
"type": "string"
}
],
"name": "update",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"name": "initMessage",
"type": "string"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"constant": true,
"inputs": [],
"name": "message",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
provider = ethers.getDefaultProvider('ropsten');
wallet = ethers.Wallet.createRandom();
contract = new ethers.Contract(address, abi, provider);
contractWithSigner = contract.connect(wallet);
await contractWithSigner.update('msgString');
@ricmoo
Copy link

ricmoo commented Nov 28, 2018

You have to attach the provider to the wallet: wallet = wallet.connect(provider)

I think that should be the only change you need. You can also pass Wallet directly into the Contract constructor and skip the read-only contract if you don’t need it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment