Skip to content

Commit

Permalink
refactor: catch block should be around all async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lena Hierzi committed Oct 6, 2023
1 parent 32fff2c commit 5c42f03
Show file tree
Hide file tree
Showing 8 changed files with 8,660 additions and 751 deletions.
10 changes: 5 additions & 5 deletions example-frontend/pages/autoOffsetExactInToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export default function AutoOffsetExactInToken() {
signer || provider
);
const offset = async () => {
// approve spending of deposited tokens
await (
await depositedTokenContract.approve(OffsetHelper.address, amount)
).wait();

try {
// 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,
const result = await offsetHelper.autoOffsetExactInToken(
depositedToken,
Expand Down
12 changes: 6 additions & 6 deletions example-frontend/pages/autoOffsetExactOutETH.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export default function AutoOffsetExactOutETH() {
signer || provider
);
const offset = async () => {
// determine how much native tokens e.g., MATIC, token must be sent
const amountOut = await offsetHelper.calculateNeededETHAmount(
poolAddress,
amount
);

try {
// determine how much native tokens e.g., MATIC, token must be sent
const amountOut = await offsetHelper.calculateNeededETHAmount(
poolAddress,
amount
);

// 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,
Expand Down
26 changes: 13 additions & 13 deletions example-frontend/pages/autoOffsetExactOutToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@ export default function AutoOffsetExactOutToken() {
// get signer & provider
const { data: signer } = useSigner();
const provider = useProvider();

// create contract for approve function of the ERC20 token
const iface = new Interface(ERC20.abi);

iface.format(FormatTypes.full);

const depositedTokenContract = new ethers.Contract(
depositedToken,
iface,
signer || provider
);

// create OffsetHelper contract
const offsetHelper = new ethers.Contract(
OffsetHelper.address,
OffsetHelper.abi,
signer || provider
);
const offset = async () => {
// determine how much of the ERC20 token must be sent
const amountOut = await offsetHelper.calculateNeededTokenAmount(
depositedToken,
poolAddress,
amount
);
try {
// determine how much of the ERC20 token must be sent
const amountOut = await offsetHelper.calculateNeededTokenAmount(
depositedToken,
poolAddress,
amount
);

// approve spending of deposited tokens
await (
await depositedTokenContract.approve(offsetHelper.address, amountOut)
).wait();
// approve spending of deposited tokens
await (
await depositedTokenContract.approve(offsetHelper.address, amountOut)
).wait();

try {
// retire carbon credits using ERC20 token, specifying the exact amount of TCO2s to retire,
const result = await offsetHelper.autoOffsetExactOutToken(
depositedToken,
Expand Down
12 changes: 4 additions & 8 deletions example-frontend/pages/autoOffsetPoolToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ export default function AutoOffsetPoolToken() {
);

const offset = async () => {
// approve spending of pool tokens
await (await poolToken.approve(offsetHelper.address, amount)).wait();

try {
// retire carbon credits if the user already owns a Toucan pool token e.g., NCT,
// 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,
const result = await offsetHelper.autoOffsetPoolToken(
poolAddress,
amount,
{
value: amount,
}
amount
);

setTx(result.hash);
Expand Down
Loading

0 comments on commit 5c42f03

Please sign in to comment.