Contract interactions
Interact with Toucan smart contracts using our JavaScript SDK
The Toucan SDK provides you with several useful tools to quickly redeem and retire carbon credits programmatically. In case you can't find the function that you need with the Toucan SDK, you can also directly interact with the contracts.
OffsetHelper related methods
The OffsetHelper combines these steps in each of the following "auto offset" methods to allow carbon credit retirement (offsetting) within one transaction:
Obtain a pool token such as NCT (by performing a token swap)
Redeem the pool token for a TCO2 token
Retire the TCO2 token
autoOffsetPoolToken
The autoOffsetPoolToken
retires carbon credits using the lowest quality (oldest) TCO2 tokens available from the specified carbon pool. This method does not include a token swap — the user must already hold reference tokens. All provided reference tokens are consumed for offsetting.
This method may take up to 1 minute to return a result. It returns the redeem transaction.
When automatically redeeming pool tokens for the lowest quality TCO2s there are no fees — you receive exactly 1 TCO2 token for 1 reference token.
Also, note that "Pool Token" in the method name refers to "carbon reference token".
Params
autoOffsetExactInToken
The autoOffsetExactInToken
extends the functionality described in autoOffsetPoolToken
by including a step to swap another token, like USDC, WETH or WMATIC, for the carbon reference tokens.
This method allows you to specify exactly how many tokens you want to use to swap and retire — i.e. how many USDC, WETH or WMATIC tokens you want to spend.
The autoOffsetExactOutToken
allows you to specify exactly how many carbon reference tokens you want to swap for, redeem and retire. The amount of swapTokens
needed will be found using calculation methods described below.
With
autoOffsetExactInToken
, you know how much you'll spend, but not how many TCO2 tokens will be retiredWith
autoOffsetExactOutToken
, you know how many TCO2 tokens will be retired, but not how much you'll spend
After the swap is completed, subsequent steps are the same as above. This method may take up to 1 minute to return a result. It returns the redeem transaction.
Params
autoOffsetExactOutToken
The autoOffsetExactOutToken
retires a specified amount of carbon credits using the lowest quality (oldest) TCO2 tokens available from the specified token pool by sending ERC20 tokens (cUSD, USDC, WETH, WMATIC). This method may take up to 1 minute to return a result. It returns the offset transaction.
Params
autoOffsetExactInETH
Same as autoOffsetExactInToken
, but the autoOffsetExactInETH
swaps the blockchain's native token for carbon reference tokens, instead of allowing you to specify the swapToken
.
Note: While this method refers to "ETH", it actually will use WMATIC on Polygon. The function is not currently available on Celo.
This method may take up to 1 minute to return a result. It returns the offset transaction.
Params
autoOffsetExactOutETH
Same as autoOffsetExactOutToken
, but the autoOffsetExactOutETH
swaps the blockchain's native token for carbon reference tokens, instead of allowing you to specify the swapToken
.
Note that while this method refers to "ETH", it actually will use WMATIC on Polygon. The function is not currently available on Celo.
This method may take up to 1 minute to return a result. It returns the offset transaction.
Params
calculateExpectedPoolTokenForToken
Calculates and returns the expected amount of carbon references tokens that can be acquired by swapping the provided amount of ERC20 token.
Params
calculateExpectedPoolTokenForETH
Calculates and returns the expected amount of carbon references tokens that can be acquired by swapping the provided amount of native token.
Note that while this method refers to "ETH", it actually will use WMATIC on Polygon. The function is not currently available on Celo.
Params
calculateNeededTokenAmount
Calculates how much of the specified ERC20 token is required in order to swap for the desired amount of a specified carbon reference token.
Params
calculateNeededETHAmount
Calculates the amount of native tokens (e.g, MATIC) required to swap for the desired amount of a carbon reference token, e.g., NCT.
Params
TCO2 related methods
retire
The retire
function retires an amount of TCO2 tokens and returns the retirement transaction.
Params
retireFrom
The retireFrom
function retires an amount of TCO2 tokens from a different address/wallet. The function requires approval from the address you're trying to retire from. If you don't own any TCO2s you need to buy pool tokens, e.g., NCTs on a DEX and redeem these first. It returns the retirement transaction.
Params
retireAndMintCertificate
The retireAndMintCertificate
function retires an amount of TCO2s & mints the NFT RetirementCertificate
for it within the same transaction. If you don't own any TCO2s you need to buy pool tokens, e.g., NCTs on a DEX and redeem these first. It returns the retirement transaction.
Params
getDepositCap
The getDepositCap
function gets the cap for TCO2s based on totalVintageQuantity
.
Params
getAttributes
The getAttributes
function retrieves the attributes of the TCO2 token. It returns an array of attributes including vintage and project details.
Params
Return values are described in the smart contract docs for the getAttributes
function ->
getTCO2Remaining
The getTCO2Remaining
function gets the remaining space in TCO2 contract before hitting the deposit cap. It returns a BigNumber
representing the remaining space.
Params
Pool related methods
depositTCO2
The depositTCO2
function deposits TCO2 tokens from a user's account into a carbon pool and mints and deposits an equivalent number of the pool's reference tokens back into the user's account. It returns returns the deposit transaction.
Params
checkEligible
The checkEligible
function checks if a TCO2 is eligible for pool. It returns a boolean.
Params
redeemAuto
The redeemAuto
function automatically redeems TCO2 tokens from the pool up to the deposit cap. It returns the redemption transaction, which returns an array containing TCO2 contract addresses (string
) and amounts (BigNumber
).
Params
redeemAuto2 [deprecated]
This function is deprecated. Use redeemAuto
instead.
Params
redeemMany
The redeemMany
function redeems carbon reference tokens for specified TCO2 tokens in specified amounts in a single transaction. It returns the redeem transaction.
Params
The tco2Addresses
and amounts
arrays must be of equal length, and align based on index, i.e. "Redeem amounts[4]
tokens from TCO2 contract address tco2Addresses[4]
."
calculateRedeemFees
The calculateRedeemFees
function calculates the fees to selectively redeem carbon reference tokens for TCO2s. It returns amount (BigNumber
) of fees it will cost to redeem. Fees are levied in the pool's reference token.
Params
getPoolRemaining
The getPoolRemaining
function gets the remaining space in pool contract before hitting the cap, i.e. supplyCap - totalSupply
. It returns BigNumber
representing the remaining space in the pool.
Params
getScoredTCO2s
The getScoredTCO2s
function gets an array of TCO2s ordered by score for a specific pool; scoredTCO2s[0]
is lowest ranked. It returns an array of TCO2 addresses by rank.
Params
Contract registry related methods
checkIfTCO2
The checkIfTCO2
function checks if an address represents a TCO2. It returns a boolean
.
Params
Interact directly with Toucan's contracts
If you need to interact with a method of our contracts that hasn't been implemented in the SDK yet, you can also connect directly to the contract and call the specific function. Learn more about smart contract functions in Carbon pool contracts, TCO2 Contracts and the OffsetHelper docs.
It's important to note that if you want to use write methods you need to have a signer
set in the Toucan Client!
getPoolAddress
The getPoolAddress
function returns the address of a Toucan pool.
Params
getPoolContract
The getPoolContract
function retrieves an ethers.Contract
object based on the pool symbol.
Params
getTCO2Contract
The getTCO2Contract
function retrieves an ethers.Contract
object based on the TCO2 contract address.
Params
getRegistryContract
The getRegistryContract
function retrieves an ethers.Contract
to interact with the contract registry.
Examples
You can always access any method or property of pool and TCO2 contracts by first getting and storing them in a variable, like this:
Last updated