Skip to content

Commit

Permalink
Merge pull request #89 from GigaHierz/LILA-5572/update-redeemauto-fun…
Browse files Browse the repository at this point in the history
…ction-to-work-like-autoredeem2-in-sdk

P2: LILA-5572/update-redeemauto-function-to-work-like-autoredeem2-in-sdk
  • Loading branch information
GigaHierz authored Oct 6, 2023
2 parents 529c853 + e74080d commit d354467
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 152 deletions.
8 changes: 4 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NODE_PROVIDER_MATIC_RPC_URL=https://matic-mainnet.chainstacklabs.com
TEST_TIMEOUT=300000
NODE_PROVIDER_MUMBAI_RPC_URL=https://matic-mumbai.chainstacklabs.com
NODE_PROVIDER_CELO_RPC_URL=https://forno.celo.org
NODE_PROVIDER_MATIC_RPC_URL=https://rpc.ankr.com/polygon
TEST_TIMEOUT=700000
NODE_PROVIDER_MUMBAI_RPC_URL=https://rpc.ankr.com/polygon_mumbai
NODE_PROVIDER_CELO_RPC_URL=https://rpc.ankr.com/celo
NODE_PROVIDER_ALFAJORES_RPC_URL=https://alfajores-forno.celo-testnet.org
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ To retire Carbon Credits on mainnet, you will have to get Carbon Pool Tokens fro
**Redeem your Pool Tokens and get an array of redeemed TCO2s**

```typescript
const tco2addresses = await toucan.redeemAuto2("NCT", parseEther("1"));
const tco2Addresses = await toucan.redeemAuto("NCT", parseEther("1"));
```

**Retire the Carbon Credits**
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const config: HardhatUserConfig = {
forking: {
url:
process.env.NODE_PROVIDER_MATIC_RPC_URL ||
"https://matic-mainnet.chainstacklabs.com",
"https://rpc.ankr.com/polygon",
},
},
},
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
UserRedeemsMethod,
UserRetirementsMethod,
} from "./types/methods";
import { RedeemAutoResponse } from "./types/responses";

/**
*
Expand Down Expand Up @@ -311,7 +312,7 @@ export default class ToucanClient {
redeemAuto = async (
pool: PoolSymbol,
amount: BigNumber
): Promise<ContractReceipt> => {
): Promise<RedeemAutoResponse> => {
if (!this.signer) throw new Error("No signer set");
const signer = this.signer;

Expand All @@ -320,6 +321,7 @@ export default class ToucanClient {

/**
*
* @deprecated This function is deprecated. Please use `redeemAuto` instead.
* @description automatically redeems pool tokens for TCO2s
* @param pool symbol of the pool (token) to use
* @param amount amount to redeem
Expand All @@ -328,7 +330,7 @@ export default class ToucanClient {
redeemAuto2 = async (
pool: PoolSymbol,
amount: BigNumber
): Promise<{ address: string; amount: BigNumber }[]> => {
): Promise<RedeemAutoResponse> => {
if (!this.signer) throw new Error("No signer set");
const signer = this.signer;

Expand Down
53 changes: 37 additions & 16 deletions src/subclasses/ContractInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
OffsetHelper,
} from "../typechain";
import { Network, PoolSymbol } from "../types";
import { RedeemAutoResponse } from "../types/responses";
import { GAS_LIMIT } from "../utils";
import {
poolTokenABI,
Expand Down Expand Up @@ -316,17 +317,33 @@ class ContractInteractions {
pool: PoolSymbol,
amount: BigNumber,
signer: ethers.Signer
): Promise<ContractReceipt> => {
): Promise<RedeemAutoResponse> => {
const poolToken = this.getPoolContract(pool, signer);
const redeemReceipt = await (
await poolToken.redeemAuto(amount, { gasLimit: GAS_LIMIT })
).wait();

const redeemTxn: ContractTransaction = await poolToken.redeemAuto(amount, {
gasLimit: GAS_LIMIT,
});
return await redeemTxn.wait();
if (!redeemReceipt.events) {
throw new Error("No events to get tco2 addresses and amounts from");
}

return redeemReceipt.events.reduce(
(acc: Array<{ address: string; amount: BigNumber }>, event) => {
if (
event.event === "Redeemed" &&
event.args?.erc20 &&
event.args?.amount
) {
acc.push({ address: event.args.erc20, amount: event.args.amount });
}
return acc;
},
[]
);
};

/**
*
* @deprecated This function is deprecated. Please use `redeemAuto` instead.
* @description automatically redeems pool tokens for TCO2s
* @param pool symbol of the pool (token) to use
* @param amount amount to redeem
Expand All @@ -337,7 +354,7 @@ class ContractInteractions {
pool: PoolSymbol,
amount: BigNumber,
signer: ethers.Signer
): Promise<{ address: string; amount: BigNumber }[]> => {
): Promise<RedeemAutoResponse> => {
const poolToken = this.getPoolContract(pool, signer);
const redeemReceipt = await (
await poolToken.redeemAuto2(amount, { gasLimit: GAS_LIMIT })
Expand All @@ -347,15 +364,19 @@ class ContractInteractions {
throw new Error("No events to get tco2 addresses and amounts from");
}

return redeemReceipt.events
.filter((event) => {
return (
event.event == "Redeemed" && event.args?.erc20 && event.args?.amount
);
})
.map((event) => {
return { address: event.args?.erc20, amount: event.args?.amount };
});
return redeemReceipt.events.reduce(
(acc: Array<{ address: string; amount: BigNumber }>, event) => {
if (
event.event === "Redeemed" &&
event.args?.erc20 &&
event.args?.amount
) {
acc.push({ address: event.args.erc20, amount: event.args.amount });
}
return acc;
},
[]
);
};

/**
Expand Down
7 changes: 6 additions & 1 deletion src/typechain/IToucanPoolToken.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,12 @@ export class IToucanPoolToken extends BaseContract {

getScoredTCO2s(overrides?: CallOverrides): Promise<string[]>;

redeemAuto(amount: BigNumberish, overrides?: CallOverrides): Promise<void>;
redeemAuto(
amount: BigNumberish,
overrides?: CallOverrides
): Promise<
[string[], BigNumber[]] & { tco2s: string[]; amounts: BigNumber[] }
>;

redeemAuto2(
amount: BigNumberish,
Expand Down
Loading

0 comments on commit d354467

Please sign in to comment.