DapDap Components: NEAR JS - FAQ

How to get a user account?
const receiver = Ethers.provider().send("eth_requestAccounts", [])[0];
How to get the current chain ID?
Ethers.provider().getNetwork().then((chainIdData) => {
    console.log(chainIdData.chainId);
});
How to show a Web3Login button?
<Web3ConnectButton 
    className="my-class" 
    connectLabel="Connect Web3 Wallet" 
    disconnectLabel="Disconnect Web3 Wallet"
    connectingLabel="Connecting..." 
/>
How to load a contract ABI?
const abi = fetch(`https://eth.blockscout.com/api?module=contract&action=getabi&address=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2`);

if (!abi.ok) {
    return "Loading";
}

console.log(abi.body.result);
How to load data from EVM nodes?
// create a contract interface
const iface = new ethers.utils.Interface(abi.body.result);

// encode the balanceOf get request
const encodedBalanceData = iface.encodeFunctionData("balanceOf", [receiver]);

// perform a call
Ethers.provider().call({
    to: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    data: encodedBalanceData,
})
.then((rawBalance) => {
    // decode the result
    const receiverBalanceHex = iface.decodeFunctionResult(
        "balanceOf",
        rawBalance
    );
    console.log(Big(receiverBalanceHex).toFixed());
});
How to send a transaction to Ethereum nodes?
// create a contract instance
const wEthContract = new ethers.Contract(
    "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    abi.body.result,
    Ethers.provider().getSigner()
);

// perform a given method (withdraw in this case)
wEthContract
    .withdraw(balance, {
        value: 0
    })
    .then((transactionHash) => {
        console.log(transactionHash);
    });
How to convert HEX values to human readable numbers?
// use Big().toFixed method

Big(balanceHex).toFixed()
How to locate the ABI of a Smart Contract?

Utilize platforms like etherscan.com and blockscout.com, or delve into the project's GitHub repositories and documentation.

Last updated