Search…
Retire Carbon
For an in-depth review of the retirement aggregator smart contract, check out this page.

Retiring Carbon via Solidity

Requirements

  • Get some MATIC to pay for gas fees
  • Get some KLIMA tokens to spend to retire carbon
  • Get the contract addresses from here:

1. Transfer your source token to your contract

You can transfer KLIMA tokens to your contract via web3.js and JSON RPC (like using Metamask) or choose to implement a deposit function that calls KLIMA's standard ERC20 approve and transfer functions to allow your contract to transfer users' KLIMA tokens to your contract.

2. Approving the Aggregator contract

Your contract's function calls the KLIMA token's standard ERC20 approve function to allow the Aggregator contract to withdraw KLIMA tokens from your contract. In the example below, an interface named IERC20 to interact with the KLIMA token smart contact.

3. Retiring carbon with the Aggregator contract

Your contract's function calls the Aggregator contract's retireCarbon function. In the example below, an interface named IKlimaRetirementAggregator is used to interact with the Aggregator contract.
To fully understand the parameters for the Aggregator contract's functions, we recommend reviewing this page.

Example 1

Use KLIMA to retire BCT with one beneficiary address, name, and retirement message
1
// SPDX-License-Identifier: MIT
2
​
3
pragma solidity ^0.8.0;
4
​
5
interface IERC20 {
6
function decimals() external view returns (uint8);
7
/**
8
* @dev Returns the amount of tokens in existence.
9
*/
10
function totalSupply() external view returns (uint256);
11
​
12
/**
13
* @dev Returns the amount of tokens owned by `account`.
14
*/
15
function balanceOf(address account) external view returns (uint256);
16
​
17
/**
18
* @dev Moves `amount` tokens from the caller's account to `recipient`.
19
*
20
* Returns a boolean value indicating whether the operation succeeded.
21
*
22
* Emits a {Transfer} event.
23
*/
24
function transfer(address recipient, uint256 amount) external returns (bool);
25
​
26
/**
27
* @dev Returns the remaining number of tokens that `spender` will be
28
* allowed to spend on behalf of `owner` through {transferFrom}. This is
29
* zero by default.
30
*
31
* This value changes when {approve} or {transferFrom} are called.
32
*/
33
function allowance(address owner, address spender) external view returns (uint256);
34
​
35
/**
36
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
37
*
38
* Returns a boolean value indicating whether the operation succeeded.
39
*
40
* IMPORTANT: Beware that changing an allowance with this method brings the risk
41
* that someone may use both the old and the new allowance by unfortunate
42
* transaction ordering. One possible solution to mitigate this race
43
* condition is to first reduce the spender's allowance to 0 and set the
44
* desired value afterwards:
45
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
46
*
47
* Emits an {Approval} event.
48
*/
49
function approve(address spender, uint256 amount) external returns (bool);
50
​
51
/**
52
* @dev Moves `amount` tokens from `sender` to `recipient` using the
53
* allowance mechanism. `amount` is then deducted from the caller's
54
* allowance.
55
*
56
* Returns a boolean value indicating whether the operation succeeded.
57
*
58
* Emits a {Transfer} event.
59
*/
60
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
61
​
62
/**
63
* @dev Emitted when `value` tokens are moved from one account (`from`) to
64
* another (`to`).
65
*
66
* Note that `value` may be zero.
67
*/
68
event Transfer(address indexed from, address indexed to, uint256 value);
69
​
70
/**
71
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
72
* a call to {approve}. `value` is the new allowance.
73
*/
74
event Approval(address indexed owner, address indexed spender, uint256 value);
75
}
76
​
77
interface IKlimaRetirementAggregator {
78
function retireCarbon(address _sourceToken,address _poolToken,uint256 _amount,bool _amountInCarbon,address _beneficiaryAddress,string memory _beneficiaryString,string memory _retirementMessage) external;
79
function isPoolToken(address poolToken) external view returns ( bool );
80
}
81
​
82
contract MyContract {
83
address public immutable KlimaRetirementAggregator;
84
85
address public immutable KLIMA;
86
address public immutable BCT;
87
88
address public beneficiaryAddress;
89
string public beneficiaryName;
90
string public retirementMessage;
91
92
93
/**
94
* @notice This function will attempt to swap KLIMA to the selected carbon token and then retire.
95
*
96
* @param _amount The total amount of KLIMA you want to use to retire.
97
*/
98
function retire( uint _amount ) public {
99
IERC20(KLIMA).approve( KlimaRetirementAggregator, _amount );
100
IKlimaRetirementAggregator( KlimaRetirementAggregator ).retireCarbon( KLIMA, BCT, _amount, false, beneficiaryAddress, beneficiaryName, retirementMessage );
101
}
102
}

Example 2

Use USDC to retire a specific amount of your choice of carbon token (BCT, NCT, UBO, NBO, MCO2) allowing the caller to set the beneficiary information
1
// SPDX-License-Identifier: MIT
2
​
3
pragma solidity ^0.8.0;
4
​
5
interface IERC20 {
6
function decimals() external view returns (uint8);
7
/**
8
* @dev Returns the amount of tokens in existence.
9
*/
10
function totalSupply() external view returns (uint256);
11
​
12
/**
13
* @dev Returns the amount of tokens owned by `account`.
14
*/
15
function balanceOf(address account) external view returns (uint256);
16
​
17
/**
18
* @dev Moves `amount` tokens from the caller's account to `recipient`.
19
*
20
* Returns a boolean value indicating whether the operation succeeded.
21
*
22
* Emits a {Transfer} event.
23
*/
24
function transfer(address recipient, uint256 amount) external returns (bool);
25
​
26
/**
27
* @dev Returns the remaining number of tokens that `spender` will be
28
* allowed to spend on behalf of `owner` through {transferFrom}. This is
29
* zero by default.
30
*
31
* This value changes when {approve} or {transferFrom} are called.
32
*/
33
function allowance(address owner, address spender) external view returns (uint256);
34
​
35
/**
36
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
37
*
38
* Returns a boolean value indicating whether the operation succeeded.
39
*
40
* IMPORTANT: Beware that changing an allowance with this method brings the risk
41
* that someone may use both the old and the new allowance by unfortunate
42
* transaction ordering. One possible solution to mitigate this race
43
* condition is to first reduce the spender's allowance to 0 and set the
44
* desired value afterwards:
45
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
46
*
47
* Emits an {Approval} event.
48
*/
49
function approve(address spender, uint256 amount) external returns (bool);
50
​
51
/**
52
* @dev Moves `amount` tokens from `sender` to `recipient` using the
53
* allowance mechanism. `amount` is then deducted from the caller's
54
* allowance.
55
*
56
* Returns a boolean value indicating whether the operation succeeded.
57
*
58
* Emits a {Transfer} event.
59
*/
60
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
61
​
62
/**
63
* @dev Emitted when `value` tokens are moved from one account (`from`) to
64
* another (`to`).
65
*
66
* Note that `value` may be zero.
67
*/
68
event Transfer(address indexed from, address indexed to, uint256 value);
69
​
70
/**
71
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
72
* a call to {approve}. `value` is the new allowance.
73
*/
74
event Approval(address indexed owner, address indexed spender, uint256 value);
75
}
76
​
77
interface IKlimaRetirementAggregator {
78
function retireCarbon( address _sourceToken, address _poolToken, uint256 _amount, bool _amountInCarbon, address _beneficiaryAddress, string memory _beneficiaryString, string memory _retirementMessage) external;
79
function isPoolToken( address poolToken) external view returns ( bool );
80
function getSourceAmount( address _sourceToken, address _poolToken, uint256 _amount, bool _amountInCarbon ) external view returns ( uint256, uint256 );
81
}
82
​
83
contract MyContract {
84
address public immutable KlimaRetirementAggregator;
85
86
address public immutable USDC;
87
88
/**
89
* @notice This function will attempt to swap USDC to specified number of the selected carbon token and then retire.
90
*
91
* @param _amount The total amount of tons you want to retire.
92
* @param _poolToken The contract address of the carbon token being retired.
93
* @param _beneficiaryAddress Address of the beneficiary of the retirement.
94
* @param _beneficiaryString String representing the beneficiary. A name perhaps.
95
* @param _retirementMessage Specific message relating to this retirement event.
96
*/
97
function swapAndRetire( uint _amount, address _poolToken, address _beneficiaryAddress, string memory _beneficiaryString, string memory _retirementMessage ) public {
98
(uint approvalAmount, ) = IKlimaRetirementAggregator( KlimaRetirementAggregator ).getSourceAmount( USDC, _poolToken, _amount, true );
99
IERC20(USDC).approve( KlimaRetirementAggregator, approvalAmount );
100
IKlimaRetirementAggregator( KlimaRetirementAggregator ).retireCarbon( USDC, _poolToken, _amount, true, beneficiaryAddress, beneficiaryName, retirementMessage );
101
}
102
}
​
​
Export as PDF
Copy link
Outline
Retiring Carbon via Solidity
Requirements
Example 1
Example 2