Fortmatic
WebsiteDashboardSupport
v1.x
v1.x
  • 🚀Get Started
  • 📦Install with NPM
  • 🧩Examples
  • 💬FAQ
  • Web3 Integration
    • Web3 Provider
    • Network Configuration
    • Get User Account
    • Send Ether Transaction
    • Smart Contract Functions
    • User Signing
    • SDK Error Handling
    • Batch Request
  • Smart Contract
    • ERC20 Transfer
    • ERC20 Approve
    • ERC20 TransferFrom
    • Generic Contract Call
  • Fortmatic Native
    • Log In
    • Log Out
    • Is User Logged In
    • Compose Transaction
    • Deposit Address
    • Configuration
  • Security
    • Domain Verification
Powered by GitBook
On this page

Was this helpful?

  1. Smart Contract

Generic Contract Call

PreviousERC20 TransferFromNextLog In

Last updated 5 years ago

Was this helpful?

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); });
});