Skip to content

Instantly share code, notes, and snippets.

@nhancv
Created November 14, 2022 01:19
Show Gist options
  • Save nhancv/bf3e07e272824d5802ec4dc5909e2ce6 to your computer and use it in GitHub Desktop.
Save nhancv/bf3e07e272824d5802ec4dc5909e2ce6 to your computer and use it in GitHub Desktop.
Example web3 - Metamask
<!-- Localhost start: http-server -->
<html>
<link
rel="icon"
type="image/x-icon"
href="https://www.mozilla.org/media/img/favicons/firefox/browser/favicon.f093404c0135.ico"
/>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script>
const setupWeb3 = async () => {
// Switch to BSC Testnet
const web3 = new Web3(window.ethereum);
// Request switch to Binance Smart Chain Testnet
try {
// check if the chain to connect to is installed
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: "0x61" }], // chainId must be in hexadecimal numbers
});
} catch (error) {
// This error code indicates that the chain has not been added to MetaMask
// if it is not, then install it into the user MetaMask
if (error.code === 4902) {
try {
await window.ethereum.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: "0x61",
rpcUrl: "https://data-seed-prebsc-1-s1.binance.org:8545/",
},
],
});
} catch (addError) {
console.error(addError);
}
}
console.error(error);
}
// Create Presenter Contract Instance
const contractAddress = "0x0dE8FCAE8421fc79B29adE9ffF97854a424Cad09";
const contractAbi =
'[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]';
const instanceContract = new web3.eth.Contract(
JSON.parse(contractAbi),
contractAddress
);
// Extract data
const mockUser = "0x0000000000000000000000000000000000000000";
const balanceInfo = await instanceContract.methods
.balanceOf(mockUser)
.call();
console.log({ balanceInfo: web3.utils.fromWei(balanceInfo) });
};
setupWeb3();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment