A repository of bitesize articles, tips & tricks
(in both English and French) curated by Mirego’s team.
Ce post assume que vous avez déjà déployé un smart contract et que Metamask est configuré correctement pour le développement local.
Lorsque l'on compile/déploi un smart contract, des définitions d'interface sont automatiquement créés dans le dossier artifacts/contracts/ContractFileName.sol/ContractName.json
de votre projet Hardhat. Votre projet doit avoir accès à ce fichier afin de pouvoir interagir avec votre smart contract.
npm install ethers
import ContractName from 'path/to/ContractName.json';
import {Web3Provider} from '@ethersproject/providers';
import {ethers} from 'ethers';
const example = async () => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
// Pop Metamask and ask the user to connect to the site
await window.ethereum.request({method: 'eth_requestAccounts'});
// The public address of your smart contract
const contractAddress = '0x3454...';
// Create the smart contract classes
const myContract = new ethers.Contract(
contractAddress,
ContractName.abi,
provider.getSigner()
);
// Call read method symbol
const symbol = await myContract.symbol();
// Metamask will prompt the user to sign the transaction of transfering 3 tokens to address 0x0
console.log(await myContract.transfer('0x0', 3));
};