Search…
Bond Carbon or LP tokens

Bonding via Solidity

Requirements

  • Get some MATIC to pay for gas fees
  • Get some carbon or LP tokens to bond
  • Get the contract addresses from here:

1. Transfer bondable token to your contract

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

2. Approving the bond depository

Your contract's function calls the bondable token's standard ERC20 approve function to allow the bond depository contract to transfer tokens from your contract. In the example below, an interface named IERC20 to interact with the BCT token smart contact.

3. Bonding your tokens

First, get the current price of the bond from the bond depository. In the example below, an interface named IKlimaBondDepository is used to read the bond depository contract's bondPriceInUSD. (This bond price is actually denominated in BCT instead of USD as KLIMA is fork of OlympusDAO denominated in carbon.)
Then, your contract's function calls the bond depository contract's deposit function with the token balance to bond, the bond price we fetched, and the recipient of the bond.
A require conditional can be used to ensure that a bond has a sufficient discount otherwise the transaction reverts.

Example

1
// SPDX-License-Identifier: MIT
2
​
3
pragma solidity ^0.8.9;
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 IKlimaBondDepository {
78
function bondPriceInUSD() external view returns (uint256 price_);
79
​
80
function deposit(
81
uint256 _amount,
82
uint256 _maxPrice,
83
address _depositor
84
) external returns (uint256);
85
​
86
function pendingPayoutFor(address _depositor)
87
external
88
view
89
returns (uint256 pendingPayout_);
90
}
91
​
92
​
93
contract MyContract {
94
address public immutable BCT;
95
address public immutable bondDepository;
96
97
function bondBCT(uint _amount) public {
98
IERC20(BCT).approve(bondDepository, _amount);
99
uint256 bondPrice = IKlimaBondDepository(bondDepository).bondPriceInUSD();
100
IKlimaBondDepository(bondDepository).deposit(_amount, bondPrice, msg.sender);
101
}
102
103
}
104
​
Export as PDF
Copy link
Outline
Bonding via Solidity
Requirements
Example