import ToucanModule from"toucan-sdk";import{ ethers }from"ethers";constToucanClient=ToucanModule.default;// ethers signer and providerconstprovider=newethers.providers.JsonRpcProvider("https://rpc.ankr.com/polygon");// make sure to set your private key in your .env fileconstsigner=newethers.Wallet(process.env.PRIVATE_KEY, provider);// set signer & providerconsttoucan=newToucanClient("polygon", provider, signer);
Note that when you are considering using wagmi, only versions under 1.0 will work as this library has not yet been upgraded to viem.
import ToucanClient from"toucan-sdk";import{ useProvider, useSigner }from"wagmi";// get signer & providerconst{ data:signer}=useSigner();constprovider=useProvider();// set signer & providerconsttoucan=newToucanClient("alfajores", provider, signer);
You could also set the signer and / or provider later if you prefer that, they are optional βΒ but you will need to set them if you want to interact with contracts. The provider is read-only, while the signer allows both writing to and reading from the blockchain.
If you don't have a signer or a provider set, you can still interact with the subgraph.
Retire Carbon Credits
To retire carbon credits on mainnet, you will need to have TCO2 tokens in your account. These can be acquired by buying carbon reference tokens from a DEX like Uniswap, then redeeming the reference tokens for TCO2s. You can then retire these TCO2 tokens through the Toucan app UI, or programmatically with the ToucanClient.retire() method.
If you already own NCT tokens, you can follow this example. Get your test tokens at the Toucan Faucet. You can find more ways to retire, and mint RetirementCertificates, in this list of all SDK functions.
Redeem your Pool Tokens and get an array of redeemed TCO2s
Retire the TCO2 tokens
Offset Carbon Credits with the OffsetHelper
The functions in the OffsetHelper will bundle the following steps into a single transaction:
Exchange ERC20 tokens e.g., cUSD or USDC for pool tokens (e.g., NCT) at a DEX like Uniswap or SushiSwap, etc. (depending on the network)
Interact with the pool contract to redeem the tokens for TCO2
Interact with the TCO2 token contract to retire the TCO2 the amount purchased and redeemed
Note that if you're using the swap functionality, the exact number of TCO2 tokens retired will depend on the stablecoin : reference token exchange rate, which can vary moment to moment.
Using these functions, you can easily offset TCO2 tokens using other cryptocurrencies and stablecoins. Bear in mind that using these functions will not let you choose a specific project to retire. To specify your the project you want to retire, you'd need to use the redeemMany function, which is not supported by the OffsetHelper at this point.