From 37d2e08d9add784290c0c3abf348fd301a144bd8 Mon Sep 17 00:00:00 2001 From: Lena Hierzi Date: Wed, 4 Oct 2023 12:59:25 +0100 Subject: [PATCH] refactor: remove gasLimit, show tx has, add catchblock, fix signer error --- example-frontend/README.md | 14 ++---- example-frontend/abis/ERC20.json | 3 ++ example-frontend/pages/api/hello.ts | 13 ----- .../pages/autoOffsetExactInETH.tsx | 29 +++++++---- .../pages/autoOffsetExactInToken.tsx | 48 +++++++++--------- .../pages/autoOffsetExactOutETH.tsx | 35 ++++++++----- .../pages/autoOffsetExactOutToken.tsx | 49 ++++++++++--------- .../pages/autoOffsetPoolToken.tsx | 34 +++++++++---- example-frontend/pages/index.tsx | 12 ++--- 9 files changed, 134 insertions(+), 103 deletions(-) create mode 100644 example-frontend/abis/ERC20.json delete mode 100644 example-frontend/pages/api/hello.ts diff --git a/example-frontend/README.md b/example-frontend/README.md index 65456ef..f4f810e 100644 --- a/example-frontend/README.md +++ b/example-frontend/README.md @@ -1,5 +1,3 @@ -export default function Home() { -return (

Welcome to the OffsetHelper Example Frontend

@@ -31,7 +29,7 @@ within one transaction: href="/autoOffsetPoolToken" target="_blank" rel="noopener noreferrer" - > - autoOffsetPoolToken(): + > autoOffsetPoolToken() {" "} if the user already owns a Toucan pool token such as BCT or NCT, @@ -42,7 +40,7 @@ if the user already owns a Toucan pool token such as BCT or NCT, href="/autoOffsetExactOutToken" target="_blank" rel="noopener noreferrer" - > - autoOffsetExactOutToken(): + > autoOffsetExactOutToken() {" "} if the user would like to perform a retirement using an ERC20 token @@ -55,7 +53,7 @@ retire, href="/autoOffsetExactInToken" target="_blank" rel="noopener noreferrer" - > - autoOffsetExactInToken(): + > autoOffsetExactInToken() {" "} if the user would like to perform a retirement using an ERC20 token @@ -68,7 +66,7 @@ into TCO2s. href="/autoOffsetExactOutETH" target="_blank" rel="noopener noreferrer" - > - autoOffsetExactOutETH(): + > autoOffsetExactOutETH() {" "} if the user would like to perform a retirement using MATIC, @@ -80,7 +78,7 @@ specifying the exact amount of TCO2s to retire, href="/autoOffsetExactInETH" target="_blank" rel="noopener noreferrer" - > - autoOffsetExactInETH(): + > autoOffsetExactInETH() {" "} if the user would like to perform a retirement using MATIC, swapping @@ -89,5 +87,3 @@ all sent MATIC into TCO2s,
-); -} diff --git a/example-frontend/abis/ERC20.json b/example-frontend/abis/ERC20.json new file mode 100644 index 0000000..54ed611 --- /dev/null +++ b/example-frontend/abis/ERC20.json @@ -0,0 +1,3 @@ +{ + "abi": "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"childChainManager\",\"type\":\"address\"}],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"addr2\",\"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\":false,\"internalType\":\"address\",\"name\":\"userAddress\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address payable\",\"name\":\"relayerAddress\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bytes\",\"name\":\"functionSignature\",\"type\":\"bytes\"}],\"name\":\"MetaTransactionExecuted\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"},{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"previousAdminRole\",\"type\":\"bytes32\"},{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"newAdminRole\",\"type\":\"bytes32\"}],\"name\":\"RoleAdminChanged\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"}],\"name\":\"RoleGranted\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"}],\"name\":\"RoleRevoked\",\"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\":\"CHILD_CHAIN_ID\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"CHILD_CHAIN_ID_BYTES\",\"outputs\":[{\"internalType\":\"bytes\",\"name\":\"\",\"type\":\"bytes\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"DEFAULT_ADMIN_ROLE\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"DEPOSITOR_ROLE\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"ERC712_VERSION\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"ROOT_CHAIN_ID\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"ROOT_CHAIN_ID_BYTES\",\"outputs\":[{\"internalType\":\"bytes\",\"name\":\"\",\"type\":\"bytes\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"addr2\",\"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\":\"account\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"stateMutability\":\"view\",\"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\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"internalType\":\"bytes\",\"name\":\"depositData\",\"type\":\"bytes\"}],\"name\":\"deposit\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"userAddress\",\"type\":\"address\"},{\"internalType\":\"bytes\",\"name\":\"functionSignature\",\"type\":\"bytes\"},{\"internalType\":\"bytes32\",\"name\":\"sigR\",\"type\":\"bytes32\"},{\"internalType\":\"bytes32\",\"name\":\"sigS\",\"type\":\"bytes32\"},{\"internalType\":\"uint8\",\"name\":\"sigV\",\"type\":\"uint8\"}],\"name\":\"executeMetaTransaction\",\"outputs\":[{\"internalType\":\"bytes\",\"name\":\"\",\"type\":\"bytes\"}],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getChainId\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getDomainSeperator\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"}],\"name\":\"getNonce\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"nonce\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"}],\"name\":\"getRoleAdmin\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"},{\"internalType\":\"uint256\",\"name\":\"index\",\"type\":\"uint256\"}],\"name\":\"getRoleMember\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"}],\"name\":\"getRoleMemberCount\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"grantRole\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"hasRole\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"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\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"renounceRole\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes32\",\"name\":\"role\",\"type\":\"bytes32\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"revokeRole\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"totalSupply\",\"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\":\"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\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"withdraw\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" +} diff --git a/example-frontend/pages/api/hello.ts b/example-frontend/pages/api/hello.ts deleted file mode 100644 index e1c6766..0000000 --- a/example-frontend/pages/api/hello.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from "next" - -type Data = { - name: string -} - -export default function handler( - req: NextApiRequest, - res: NextApiResponse -) { - res.status(200).json({ name: 'John Doe' }) -} diff --git a/example-frontend/pages/autoOffsetExactInETH.tsx b/example-frontend/pages/autoOffsetExactInETH.tsx index 87606a1..349d513 100644 --- a/example-frontend/pages/autoOffsetExactInETH.tsx +++ b/example-frontend/pages/autoOffsetExactInETH.tsx @@ -1,4 +1,5 @@ -import { useSigner } from "wagmi"; +import { useProvider, useSigner } from "wagmi"; +import { useState } from "react"; import { parseEther } from "ethers/lib/utils"; import { ethers } from "ethers"; import OffsetHelper from "../../deployments/polygon/OffsetHelper.json"; @@ -7,33 +8,41 @@ export default function AutoOffsetExactInETH() { // input data for the autoOffsetExactInETH function const poolAddress = "0xD838290e877E0188a4A44700463419ED96c16107"; // Polygon - NCT const amount = parseEther("0.0001"); + const [tx, setTx] = useState(""); - // get signer + // get signer & provider const { data: signer } = useSigner(); + const provider = useProvider(); // create OffsetHelper contract const offsetHelper = new ethers.Contract( OffsetHelper.address, OffsetHelper.abi, - signer + signer || provider ); - // retire carbon credits using native tokens e.g., MATIC, specifying the exact amount of TCO2s to retire (only on Polygon, not on Celo), const offset = async () => { - await offsetHelper.autoOffsetExactInETH(poolAddress, { - gasLimit: 3000000, - value: amount, - }); + try { + const result = await offsetHelper.autoOffsetExactInETH(poolAddress, { + value: amount, + }); + + setTx(result.hash); + } catch (error) { + // Handle the error + console.error("An error occurred:", error); + } }; return ( -
+
+ {tx &&
Transaction: {JSON.stringify(tx)}
}
); } diff --git a/example-frontend/pages/autoOffsetExactInToken.tsx b/example-frontend/pages/autoOffsetExactInToken.tsx index ed93803..44056c0 100644 --- a/example-frontend/pages/autoOffsetExactInToken.tsx +++ b/example-frontend/pages/autoOffsetExactInToken.tsx @@ -1,60 +1,64 @@ -import { useSigner } from "wagmi"; -import { FormatTypes, Interface, parseEther } from "ethers/lib/utils"; +import { useProvider, useSigner } from "wagmi"; +import { useState } from "react"; +import { FormatTypes, Interface, parseUnits } from "ethers/lib/utils"; import { ethers } from "ethers"; import OffsetHelper from "../../deployments/polygon/OffsetHelper.json"; +import ERC20 from "../abis/ERC20.json"; export default function AutoOffsetExactInToken() { // input data for the autoOffsetExactInToken function const poolAddress = "0xD838290e877E0188a4A44700463419ED96c16107"; // Polygon - NCT const depositedToken = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"; // Polygon - USDC - const amount = parseEther("0.0001"); + const amount = parseUnits("0.0001", 6); + const [tx, setTx] = useState(""); - // get signer + // get signer & provider const { data: signer } = useSigner(); - + const provider = useProvider(); // create contract for approve function of the ERC20 token - const iface = new Interface( - '[{"inputs":[{"internalType":"address","name":"childChainManager","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"addr2","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":false,"internalType":"address","name":"userAddress","type":"address"},{"indexed":false,"internalType":"address payable","name":"relayerAddress","type":"address"},{"indexed":false,"internalType":"bytes","name":"functionSignature","type":"bytes"}],"name":"MetaTransactionExecuted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","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":"CHILD_CHAIN_ID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"CHILD_CHAIN_ID_BYTES","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DEPOSITOR_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ERC712_VERSION","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ROOT_CHAIN_ID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ROOT_CHAIN_ID_BYTES","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr2","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":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","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"},{"inputs":[{"internalType":"address","name":"user","type":"address"},{"internalType":"bytes","name":"depositData","type":"bytes"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"userAddress","type":"address"},{"internalType":"bytes","name":"functionSignature","type":"bytes"},{"internalType":"bytes32","name":"sigR","type":"bytes32"},{"internalType":"bytes32","name":"sigS","type":"bytes32"},{"internalType":"uint8","name":"sigV","type":"uint8"}],"name":"executeMetaTransaction","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"getChainId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getDomainSeperator","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"getNonce","outputs":[{"internalType":"uint256","name":"nonce","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"uint256","name":"index","type":"uint256"}],"name":"getRoleMember","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleMemberCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","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":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","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":"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":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]' - ); + const iface = new Interface(ERC20.abi); iface.format(FormatTypes.full); const depositedTokenContract = new ethers.Contract( depositedToken, iface, - signer + signer || provider ); // create OffsetHelper contract const offsetHelper = new ethers.Contract( OffsetHelper.address, OffsetHelper.abi, - signer + signer || provider ); - const offset = async () => { // approve spending of deposited tokens await ( await depositedTokenContract.approve(OffsetHelper.address, amount) ).wait(); - // retire carbon credits if the user already owns a Toucan pool token e.g., NCT, - await offsetHelper.autoOffsetExactInToken( - depositedToken, - poolAddress, - amount, - { - gasLimit: 3000000, - } - ); + try { + // retire carbon credits if the user already owns a Toucan pool token e.g., NCT, + const result = await offsetHelper.autoOffsetExactInToken( + depositedToken, + poolAddress, + amount + ); + setTx(result.hash); + } catch (error) { + // Handle the error + console.error("An error occurred:", error); + } }; return ( -
+
+
{tx &&
Transaction: {JSON.stringify(tx)}
}
); } diff --git a/example-frontend/pages/autoOffsetExactOutETH.tsx b/example-frontend/pages/autoOffsetExactOutETH.tsx index 43961a6..d181c5b 100644 --- a/example-frontend/pages/autoOffsetExactOutETH.tsx +++ b/example-frontend/pages/autoOffsetExactOutETH.tsx @@ -1,4 +1,5 @@ -import { useSigner } from "wagmi"; +import { useProvider, useSigner } from "wagmi"; +import { useState } from "react"; import { parseEther } from "ethers/lib/utils"; import { ethers } from "ethers"; import OffsetHelper from "../../deployments/polygon/OffsetHelper.json"; @@ -7,17 +8,18 @@ export default function AutoOffsetExactOutETH() { // input data for the autoOffsetExactOutETH function const poolAddress = "0xD838290e877E0188a4A44700463419ED96c16107"; // Polygon - NCT const amount = parseEther("0.0001"); + const [tx, setTx] = useState(""); - // get signer + // get signer & provider const { data: signer } = useSigner(); + const provider = useProvider(); // create OffsetHelper contract const offsetHelper = new ethers.Contract( OffsetHelper.address, OffsetHelper.abi, - signer + signer || provider ); - const offset = async () => { // determine how much native tokens e.g., MATIC, token must be sent const amountOut = await offsetHelper.calculateNeededETHAmount( @@ -25,21 +27,32 @@ export default function AutoOffsetExactOutETH() { amount ); - // retire carbon credits using native tokens e.g., MATIC, specifying the exact amount of TCO2s to retire (only on Polygon, not on Celo), - await offsetHelper.autoOffsetExactOutETH(poolAddress, amount, { - gasLimit: 3000000, - value: amountOut, - }); + try { + // retire carbon credits using native tokens e.g., MATIC, specifying the exact amount of TCO2s to retire (only on Polygon, not on Celo), + const result = await offsetHelper.autoOffsetExactOutETH( + poolAddress, + amount, + { + value: amountOut, + } + ); + + setTx(result.hash); + } catch (error) { + // Handle the error + console.error("An error occurred:", error); + } }; return ( -
+
+ {tx &&
Transaction: {JSON.stringify(tx)}
}
); } diff --git a/example-frontend/pages/autoOffsetExactOutToken.tsx b/example-frontend/pages/autoOffsetExactOutToken.tsx index 16a0fbd..b84982e 100644 --- a/example-frontend/pages/autoOffsetExactOutToken.tsx +++ b/example-frontend/pages/autoOffsetExactOutToken.tsx @@ -1,36 +1,36 @@ -import { useSigner } from "wagmi"; +import { useProvider, useSigner } from "wagmi"; +import { useState } from "react"; import { ethers } from "ethers"; import { FormatTypes, Interface, parseUnits } from "ethers/lib/utils"; import OffsetHelper from "../../deployments/polygon/OffsetHelper.json"; +import ERC20 from "../abis/ERC20.json"; export default function AutoOffsetExactOutToken() { // input data for the autoOffsetExactOutToken function const poolAddress = "0xD838290e877E0188a4A44700463419ED96c16107"; // Polygon - NCT - const depositedToken = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"; // Polygon - USDC + const depositedToken = "0x2791Bca1f2de4661ED88åA30C99A7a9449Aa84174"; // Polygon - USDC const amount = parseUnits("0.0001", 6); + const [tx, setTx] = useState(""); - // get signer + // get signer & provider const { data: signer } = useSigner(); - + const provider = useProvider(); // create contract for approve function of the ERC20 token - const iface = new Interface( - '[{"inputs":[{"internalType":"address","name":"childChainManager","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"addr2","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":false,"internalType":"address","name":"userAddress","type":"address"},{"indexed":false,"internalType":"address payable","name":"relayerAddress","type":"address"},{"indexed":false,"internalType":"bytes","name":"functionSignature","type":"bytes"}],"name":"MetaTransactionExecuted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","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":"CHILD_CHAIN_ID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"CHILD_CHAIN_ID_BYTES","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DEPOSITOR_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ERC712_VERSION","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ROOT_CHAIN_ID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ROOT_CHAIN_ID_BYTES","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr2","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":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","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"},{"inputs":[{"internalType":"address","name":"user","type":"address"},{"internalType":"bytes","name":"depositData","type":"bytes"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"userAddress","type":"address"},{"internalType":"bytes","name":"functionSignature","type":"bytes"},{"internalType":"bytes32","name":"sigR","type":"bytes32"},{"internalType":"bytes32","name":"sigS","type":"bytes32"},{"internalType":"uint8","name":"sigV","type":"uint8"}],"name":"executeMetaTransaction","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"getChainId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getDomainSeperator","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"getNonce","outputs":[{"internalType":"uint256","name":"nonce","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"uint256","name":"index","type":"uint256"}],"name":"getRoleMember","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleMemberCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","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":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","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":"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":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]' - ); + const iface = new Interface(ERC20.abi); + iface.format(FormatTypes.full); const depositedTokenContract = new ethers.Contract( depositedToken, iface, - signer + signer || provider ); - // create OffsetHelper contract const offsetHelper = new ethers.Contract( OffsetHelper.address, OffsetHelper.abi, - signer + signer || provider ); - const offset = async () => { // determine how much of the ERC20 token must be sent const amountOut = await offsetHelper.calculateNeededTokenAmount( @@ -44,25 +44,30 @@ export default function AutoOffsetExactOutToken() { await depositedTokenContract.approve(offsetHelper.address, amountOut) ).wait(); - // retire carbon credits using ERC20 token, specifying the exact amount of TCO2s to retire, - await offsetHelper.autoOffsetExactOutToken( - depositedToken, - poolAddress, - amount, - { - gasLimit: 3000000, - } - ); + try { + // retire carbon credits using ERC20 token, specifying the exact amount of TCO2s to retire, + const result = await offsetHelper.autoOffsetExactOutToken( + depositedToken, + poolAddress, + amount + ); + + setTx(result.hash); + } catch (error) { + // Handle the error + console.error("An error occurred:", error); + } }; return ( -
+
+ {tx &&
Transaction: {JSON.stringify(tx)}
}
); } diff --git a/example-frontend/pages/autoOffsetPoolToken.tsx b/example-frontend/pages/autoOffsetPoolToken.tsx index 3b0c3de..28b51b9 100644 --- a/example-frontend/pages/autoOffsetPoolToken.tsx +++ b/example-frontend/pages/autoOffsetPoolToken.tsx @@ -1,5 +1,5 @@ -import { useProvider } from "wagmi"; -import { useSigner } from "wagmi"; +import { useProvider, useSigner } from "wagmi"; +import { useState } from "react"; import { ethers } from "ethers"; import { parseEther } from "ethers/lib/utils"; import ToucanClient from "toucan-sdk"; @@ -9,6 +9,7 @@ export default function AutoOffsetPoolToken() { // input data for the autoOffsetPoolToken function const poolAddress = "0xD838290e877E0188a4A44700463419ED96c16107"; // Polygon - NCT const amount = parseEther("0.0001"); + const [tx, setTx] = useState(""); // get signer & provider const { data: signer } = useSigner(); @@ -17,34 +18,47 @@ export default function AutoOffsetPoolToken() { // get toucanClient from the ToucanSDK to init the poolContract const toucan = new ToucanClient("polygon", provider); signer && toucan.setSigner(signer); + const poolToken = toucan.getPoolContract("NCT"); // create OffsetHelper contract const offsetHelper = new ethers.Contract( OffsetHelper.address, OffsetHelper.abi, - signer + signer || provider ); const offset = async () => { // approve spending of pool tokens await (await poolToken.approve(offsetHelper.address, amount)).wait(); - // retire carbon credits if the user already owns a Toucan pool token e.g., NCT, - await offsetHelper.autoOffsetPoolToken(poolAddress, amount, { - gasLimit: 3000000, - value: amount, - }); + try { + // retire carbon credits if the user already owns a Toucan pool token e.g., NCT, + + const result = await offsetHelper.autoOffsetPoolToken( + poolAddress, + amount, + { + value: amount, + } + ); + + setTx(result.hash); + } catch (error) { + // Handle the error + console.error("An error occurred:", error); + } }; return ( -
+
+ {tx &&
Transaction: {JSON.stringify(tx)}
}
); } diff --git a/example-frontend/pages/index.tsx b/example-frontend/pages/index.tsx index f326e05..c0ec45e 100644 --- a/example-frontend/pages/index.tsx +++ b/example-frontend/pages/index.tsx @@ -1,6 +1,6 @@ export default function Home() { return ( -
+

Welcome to the OffsetHelper Example Frontend @@ -34,7 +34,7 @@ export default function Home() { target="_blank" rel="noopener noreferrer" > - - autoOffsetPoolToken(): + autoOffsetPoolToken() {" "} if the user already owns a Toucan pool token such as BCT or NCT, @@ -46,7 +46,7 @@ export default function Home() { target="_blank" rel="noopener noreferrer" > - - autoOffsetExactOutToken(): + autoOffsetExactOutToken() {" "} if the user would like to perform a retirement using an ERC20 token @@ -60,7 +60,7 @@ export default function Home() { target="_blank" rel="noopener noreferrer" > - - autoOffsetExactInToken(): + autoOffsetExactInToken() {" "} if the user would like to perform a retirement using an ERC20 token @@ -74,7 +74,7 @@ export default function Home() { target="_blank" rel="noopener noreferrer" > - - autoOffsetExactOutETH(): + autoOffsetExactOutETH() {" "} if the user would like to perform a retirement using MATIC, @@ -87,7 +87,7 @@ export default function Home() { target="_blank" rel="noopener noreferrer" > - - autoOffsetExactInETH(): + autoOffsetExactInETH() {" "} if the user would like to perform a retirement using MATIC, swapping