Generic Contract Call
If you have replaced your web3 provider with Fortmatic provider, nothing needs to be changed for web3 contract transactions to continue working. The following is an example on how to initiate a smart contract call.
// Initialize provider
import Fortmatic from 'fortmatic';
import Web3 from 'web3';
const fm = new Fortmatic('YOUR_API_KEY');
window.web3 = new Web3(fm.getProvider());
// Get your contract ABI from compiled smart contract json
const contractAbi = [{"constant": true,"inputs": [],"name": "name","outputs": [{"name": "","type": "string"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": true,"inputs": [],"name": "getValue","outputs": [{"name": "","type": "string"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"name": "_name","type": "string"}],"name": "setValue","outputs": [{"name": "","type": "string"}],"payable": false,"stateMutability": "nonpayable","type": "function"}];
// Create contract object
const contract = web3.eth.contract(contractAbi);
// Instantiate contract
const contractInstance = contract.at('0x4efA9a82bFE112a9174325561bcdD1c68044724c');
// Get user account wallet address first
web3.eth.getAccounts(function(error, accounts) {
if (error) throw error;
// Make the contract call.
contractInstance.setValue.sendTransaction('Hello World', {from: accounts[0]}, function(error, txnHash) {
if (error) throw error;
console.log(txnHash);
});
});
// Initialize provider
import Fortmatic from 'fortmatic';
import Web3 from 'web3';
const fm = new Fortmatic('YOUR_API_KEY');
window.web3 = new Web3(fm.getProvider());
// Get your contract ABI from compiled smart contract json
const contractAbi = [{"constant": true,"inputs": [],"name": "name","outputs": [{"name": "","type": "string"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": true,"inputs": [],"name": "getValue","outputs": [{"name": "","type": "string"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"name": "_name","type": "string"}],"name": "setValue","outputs": [{"name": "","type": "string"}],"payable": false,"stateMutability": "nonpayable","type": "function"}];
// Create contract object
const contractAddress = '0x4efA9a82bFE112a9174325561bcdD1c68044724c';
// Instantiate contract
const contract = new web3.eth.Contract(contractAbi, contractAddress);
// Get user account wallet address first
web3.eth.getAccounts().then((accounts) => {
// Make the contract call.
contract.methods.setValue(
'Hello World',
).send({from: accounts[0]})
.once('transactionHash', (hash) => { console.log(hash); })
.once('receipt', (receipt) => { console.log(receipt); });
});
Last updated