Helper functions that simplify the carbon offsetting (retirement) process.
Retiring carbon tokens requires multiple steps and interactions with Toucan Protocol's main contracts:
- Obtain a Toucan pool token such as BCT or NCT (by performing a token swap).
- Redeem the pool token for a TCO2 token.
- Retire the TCO2 token.
These steps are combined in each of the following "auto offset" methods
implemented in OffsetHelper
to allow a retirement within one transaction:
autoOffsetPoolToken()
if the user already owns a Toucan pool token such as BCT or NCT,autoOffsetExactOutETH()
if the user would like to perform a retirement using MATIC, specifying the exact amount of TCO2s to retire,autoOffsetExactInETH()
if the user would like to perform a retirement using MATIC, swapping all sent MATIC into TCO2s,autoOffsetExactOutToken()
if the user would like to perform a retirement using an ERC20 token (USDC, WETH or WMATIC), specifying the exact amount of TCO2s to retire,autoOffsetExactInToken()
if the user would like to perform a retirement using an ERC20 token (USDC, WETH or WMATIC), specifying the exact amount of token to swap into TCO2s.
In these methods, "auto" refers to the fact that these methods use
autoRedeem()
in order to automatically choose a TCO2 token corresponding
to the oldest tokenized carbon project in the specfified token pool.
There are no fees incurred by the user when using autoRedeem()
, i.e., the
user receives 1 TCO2 token for each pool token (BCT/NCT) redeemed.
There are two view
helper functions calculateNeededETHAmount()
and
calculateNeededTokenAmount()
that should be called before using
autoOffsetExactOutETH()
and autoOffsetExactOutToken()
, to determine how
much MATIC, respectively how much of the ERC20 token must be sent to the
OffsetHelper
contract in order to retire the specified amount of carbon.
The two view
helper functions calculateExpectedPoolTokenForETH()
and
calculateExpectedPoolTokenForToken()
can be used to calculate the
expected amount of TCO2s that will be offset using functions
autoOffsetExactInETH()
and autoOffsetExactInToken()
.
constructor(string[] _eligibleTokenSymbols, address[] _eligibleTokenAddresses) public
Contract constructor. Should specify arrays of ERC20 symbols and addresses that can used by the contract.
See isEligible()
for a list of tokens that can be used in the
contract. These can be modified after deployment by the contract owner
using setEligibleTokenAddress()
and deleteEligibleTokenAddress()
.
Name | Type | Description |
---|---|---|
_eligibleTokenSymbols | string[] | A list of token symbols. |
_eligibleTokenAddresses | address[] | A list of token addresses corresponding to the provided token symbols. |
event Redeemed(address who, address poolToken, address[] tco2s, uint256[] amounts)
Emitted upon successful redemption of TCO2 tokens from a Toucan pool token such as BCT or NCT.
Name | Type | Description |
---|---|---|
who | address | The sender of the transaction |
poolToken | address | The address of the Toucan pool token used in the redemption, for example, NCT or BCT |
tco2s | address[] | An array of the TCO2 addresses that were redeemed |
amounts | uint256[] | An array of the amounts of each TCO2 that were redeemed |
modifier onlyRedeemable(address _token)
modifier onlySwappable(address _token)
modifier nativeTokenChain()
constructor(address[] _poolAddresses, string[] _tokenSymbolsForPaths, address[][] _paths, address _dexRouterAddress) public
Contract constructor. Should specify arrays of ERC20 symbols and addresses that can used by the contract.
See isEligible()
for a list of tokens that can be used in the
contract. These can be modified after deployment by the contract owner
using setEligibleTokenAddress()
and deleteEligibleTokenAddress()
.
Name | Type | Description |
---|---|---|
_poolAddresses | address[] | A list of pool token addresses. |
_tokenSymbolsForPaths | string[] | An array of symbols of the token the user want to retire carbon credits for |
_paths | address[][] | An array of arrays of addresses to describe the path needed to swap form the baseToken to the pool Token to the provided token symbols. |
_dexRouterAddress | address |
receive() external payable
fallback() external payable
function initialize() external virtual
function autoOffsetExactOutToken(address _depositedToken, address _poolToken, uint256 _amountToOffset) public returns (address[] tco2s, uint256[] amounts)
Retire carbon credits using the oldest TCO2
tokens available from the specified Toucan token pool by sending ERC20
tokens (USDC, WETH, WMATIC). Use calculateNeededTokenAmount
first in
order to find out how much of the ERC20 token is required to retire the
specified quantity of TCO2.
This function:
- Swaps the ERC20 token sent to the contract for the specified pool token.
- Redeems the pool token for the oldest TCO2 tokens available.
- Retires the TCO2 tokens.
Note: The client must approve the ERC20 token that is sent to the contract.
When automatically redeeming pool tokens for the lowest quality TCO2s there are no fees and you receive exactly 1 TCO2 token for 1 pool token.
Name | Type | Description |
---|---|---|
_depositedToken | address | The address of the ERC20 token that the user sends (must be one of USDC, WETH, WMATIC) |
_poolToken | address | The address of the Toucan pool token that the user wants to use, for example, NCT or BCT |
_amountToOffset | uint256 | The amount of TCO2 to offset |
Name | Type | Description |
---|---|---|
tco2s | address[] | An array of the TCO2 addresses that were redeemed |
amounts | uint256[] | An array of the amounts of each TCO2 that were redeemed |
function autoOffsetExactInToken(address _fromToken, uint256 _amountToSwap, address _poolToken) public returns (address[] tco2s, uint256[] amounts)
Retire carbon credits using the oldest TCO2 tokens available from the specified Toucan token pool by sending ERC20 tokens (USDC, WETH, WMATIC). All provided token is consumed for offsetting.
This function:
- Swaps the ERC20 token sent to the contract for the specified pool token.
- Redeems the pool token for the oldest TCO2 tokens available.
- Retires the TCO2 tokens.
Note: The client must approve the ERC20 token that is sent to the contract.
When automatically redeeming pool tokens for the lowest quality TCO2s there are no fees and you receive exactly 1 TCO2 token for 1 pool token.
Name | Type | Description |
---|---|---|
_fromToken | address | The address of the ERC20 token that the user sends (must be one of USDC, WETH, WMATIC) |
_amountToSwap | uint256 | The amount of ERC20 token to swap into Toucan pool token. Full amount will be used for offsetting. |
_poolToken | address | The address of the Toucan pool token that the user wants to use, for example, NCT or BCT |
Name | Type | Description |
---|---|---|
tco2s | address[] | An array of the TCO2 addresses that were redeemed |
amounts | uint256[] | An array of the amounts of each TCO2 that were redeemed |
function autoOffsetExactOutETH(address _poolToken, uint256 _amountToOffset) public payable returns (address[] tco2s, uint256[] amounts)
Retire carbon credits using the oldest TCO2
tokens available from the specified Toucan token pool by sending MATIC.
Use calculateNeededETHAmount()
first in order to find out how much
MATIC is required to retire the specified quantity of TCO2.
This function:
- Swaps the Matic sent to the contract for the specified pool token.
- Redeems the pool token for the oldest TCO2 tokens available.
- Retires the TCO2 tokens.
If the user sends much MATIC, the leftover amount will be sent back to the user.
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the Toucan pool token that the user wants to use, for example, NCT or BCT. |
_amountToOffset | uint256 | The amount of TCO2 to offset. |
Name | Type | Description |
---|---|---|
tco2s | address[] | An array of the TCO2 addresses that were redeemed |
amounts | uint256[] | An array of the amounts of each TCO2 that were redeemed |
function autoOffsetExactInETH(address _poolToken) public payable returns (address[] tco2s, uint256[] amounts)
Retire carbon credits using the oldest TCO2 tokens available from the specified Toucan token pool by sending MATIC. All provided MATIC is consumed for offsetting.
This function:
- Swaps the Matic sent to the contract for the specified pool token.
- Redeems the pool token for the oldest TCO2 tokens available.
- Retires the TCO2 tokens.
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the Toucan pool token that the user wants to use, for example, NCT or BCT. |
Name | Type | Description |
---|---|---|
tco2s | address[] | An array of the TCO2 addresses that were redeemed |
amounts | uint256[] | An array of the amounts of each TCO2 that were redeemed |
function autoOffsetPoolToken(address _poolToken, uint256 _amountToOffset) public returns (address[] tco2s, uint256[] amounts)
Retire carbon credits using the oldest TCO2 tokens available by sending Toucan pool tokens, for example, BCT or NCT.
This function:
- Redeems the pool token for the oldest TCO2 tokens available.
- Retires the TCO2 tokens.
Note: The client must approve the pool token that is sent.
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the Toucan pool token that the user wants to use, for example, NCT or BCT. |
_amountToOffset | uint256 | The amount of TCO2 to offset. |
Name | Type | Description |
---|---|---|
tco2s | address[] | An array of the TCO2 addresses that were redeemed |
amounts | uint256[] | An array of the amounts of each TCO2 that were redeemed |
function autoRedeem(address _fromToken, uint256 _amount) public returns (address[] tco2s, uint256[] amounts)
Redeems the specified amount of NCT / BCT for TCO2.
Needs to be approved on the client side
Name | Type | Description |
---|---|---|
_fromToken | address | Could be the address of NCT |
_amount | uint256 | Amount to redeem |
Name | Type | Description |
---|---|---|
tco2s | address[] | An array of the TCO2 addresses that were redeemed |
amounts | uint256[] | An array of the amounts of each TCO2 that were redeemed |
function autoRetire(address[] _tco2s, uint256[] _amounts) public
Retire the specified TCO2 tokens.
Name | Type | Description |
---|---|---|
_tco2s | address[] | The addresses of the TCO2s to retire |
_amounts | uint256[] | The amounts to retire from each of the corresponding TCO2 addresses |
function swapExactOutToken(address _fromToken, address _toToken, uint256 _toAmount) public
function swapExactInToken(address _fromToken, uint256 _fromAmount, address _toToken) public returns (uint256)
Swap eligible ERC20 tokens for Toucan pool tokens (BCT/NCT) on SushiSwap. All provided ERC20 tokens will be swapped.
Needs to be approved on the client side.
Name | Type | Description |
---|---|---|
_fromToken | address | The ERC20 token to deposit and swap |
_fromAmount | uint256 | The amount of ERC20 token to swap |
_toToken | address | The Toucan token to swap for (will be held within contract) |
Name | Type | Description |
---|---|---|
[0] | uint256 | Resulting amount of Toucan pool token that got acquired for the swapped ERC20 tokens. |
function swapExactOutETH(address _poolToken, uint256 _toAmount) public payable
Swap native tokens e.g., MATIC for Toucan pool tokens (BCT/NCT) on SushiSwap. Remaining native tokens that was not consumed by the swap is returned.
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token to swap for, e.g., NCT |
_toAmount | uint256 | The required amount of the Toucan pool token (NCT/BCT) |
function swapExactInETH(address _poolToken) public payable returns (uint256 amountOut)
Swap native tokens e.g., MATIC for Toucan pool tokens (BCT/NCT) on SushiSwap. All provided native tokens will be swapped.
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token to swap for, e.g., NCT |
Name | Type | Description |
---|---|---|
amountOut | uint256 | Resulting amount of Toucan pool token that got acquired for the swapped native tokens . |
function withdraw(address _erc20Addr, uint256 _amount) public
Allow users to withdraw tokens they have deposited.
function deposit(address _erc20Addr, uint256 _amount) public
Allow users to deposit BCT / NCT.
Needs to be approved
function calculateNeededTokenAmount(address _fromToken, address _poolToken, uint256 _toAmount) public view returns (uint256 amountIn)
Return how much of the specified ERC20 token is required in order to swap for the desired amount of a Toucan pool token, for example, e.g., NCT.
Name | Type | Description |
---|---|---|
_fromToken | address | The address of the ERC20 token used for the swap |
_poolToken | address | The address of the pool token to swap for, e.g., NCT |
_toAmount | uint256 | The desired amount of pool token to receive |
Name | Type | Description |
---|---|---|
amountIn | uint256 | The amount of the ERC20 token required in order to swap for the specified amount of the pool token |
function calculateNeededETHAmount(address _poolToken, uint256 _toAmount) public view returns (uint256 amountIn)
Return how much native tokens e.g, MATIC is required in order to swap for the desired amount of a Toucan pool token, e.g., NCT.
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token to swap for, for example, NCT |
_toAmount | uint256 | The desired amount of pool token to receive |
Name | Type | Description |
---|---|---|
amountIn | uint256 | The amount of native tokens required in order to swap for the specified amount of the pool token |
function calculateExpectedPoolTokenForToken(address _fromToken, address _poolToken, uint256 _fromAmount) public view returns (uint256 amountOut)
Calculates the expected amount of Toucan Pool token that can be acquired by swapping the provided amount of ERC20 token.
Name | Type | Description |
---|---|---|
_fromToken | address | The address of the ERC20 token used for the swap |
_poolToken | address | The address of the pool token to swap for, e.g., NCT |
_fromAmount | uint256 | The amount of ERC20 token to swap |
Name | Type | Description |
---|---|---|
amountOut | uint256 | The expected amount of Pool token that can be acquired |
function calculateExpectedPoolTokenForETH(address _poolToken, uint256 _fromTokenAmount) public view returns (uint256 amountOut)
Calculates the expected amount of Toucan Pool token that can be acquired by swapping the provided amount of native tokens e.g., MATIC.
Name | Type | Description |
---|---|---|
_poolToken | address | The address of the pool token to swap for, e.g., NCT |
_fromTokenAmount | uint256 | The amount of native tokens to swap |
Name | Type | Description |
---|---|---|
amountOut | uint256 | The expected amount of Pool token that can be acquired |
function calculateExactOutSwap(address _fromToken, address _toToken, uint256 _toAmount) internal view returns (address[] path, uint256[] amounts)
function calculateExactInSwap(address _fromToken, uint256 _fromAmount, address _toToken) internal view returns (address[] path, uint256[] amounts)
function generatePath(address _fromToken, address _toToken) internal view returns (address[])
function routerSushi() internal view returns (contract IUniswapV2Router02)
function setEligibleTokenAddress(string _tokenSymbol, address _address) public virtual
Change or add eligible tokens and their addresses.
Name | Type | Description |
---|---|---|
_tokenSymbol | string | The symbol of the token to add |
_address | address | The address of the token to add |
function deleteEligibleTokenAddress(string _tokenSymbol) public virtual
Delete eligible tokens stored in the contract.
Name | Type | Description |
---|---|---|
_tokenSymbol | string | The symbol of the token to remove |
function setToucanContractRegistry(address _address) public virtual
Change the TCO2 contracts registry.
Name | Type | Description |
---|---|---|
_address | address | The address of the Toucan contract registry to use |