Search…
⌃K
Links
💻

Building Protocols

How to integrate Spirals gToken vaults into your protocol's smart contracts
The following code snippets show an example of how you can integrate the Spirals vault contracts into your smart contracts. Please create a new file GreenTokenVaultInterface.sol in your project to import the interface in your contract files and get started on these code examples 👇
GreenTokenVaultInterface.sol
2KB
Binary

Depositing into vault

Once again, we start with a simple example where the contract being integrated deposit _amount of token into the vault.
1
contract MyContract {
2
ImpactVaultInterface gTokenVault;
3
4
constructor(address _gTokenVaultAddress) {
5
gTokenVault = ImpactVaultInterface(_gTokenVaultAddress);
6
}
7
8
/**
9
* @dev Deposits tokens into gToken vault on behalf of receiver.
10
* Assumes that the contract has enough tokens already.
11
*/
12
function gTokenDeposit(uint256 _amount, address _receiver) {
13
require(gTokenVault.asset().balanceOf(address(this)) >= _amount, "AMOUNT_EXCEEDS_TOKEN_BALANCE");
14
gTokenVault.asset().approve(address(gTokenVault), _amount);
15
gTokenVault.deposit(_amount, address(this));
16
}
17
}
For native tokens like ETH and Celo, use depositETH(address _receiver) payable

Reading gToken balances

Similarly, we can also pretty easily read the gToken balances using the method that's available on the interface.
1
contract MyContract {
2
ImpactVaultInterface gTokenVault;
3
4
constructor(address _gTokenVaultAddress) {
5
gTokenVault = ImpactVaultInterface(_gTokenVaultAddress);
6
}
7
8
/**
9
* @dev Returns the gToken balance of the user.
10
*/
11
function getGTokenBalance(address _user) {
12
return gTokenVault.balanceOf(_user);
13
}
14
}
Now let's put the 2 snippets together so that on deposit we return the amount of gTokens the receiver gets through a deposit function call.

Putting it together

1
contract MyContract {
2
ImpactVaultInterface gTokenVault;
3
4
constructor(address _gTokenVaultAddress) {
5
gTokenVault = ImpactVaultInterface(_gTokenVaultAddress);
6
}
7
8
/**
9
* @dev Deposits tokens into gToken vault on behalf of receiver.
10
* Assumes that the contract has enough tokens already to support this.
11
* Returns the number of gTokens recieved by the user.
12
*/
13
function gTokenDeposit(uint256 _amount, address _receiver) returns (uint256) {
14
require(gTokenVault.asset().balanceOf(address(this)) >= _amount, "AMOUNT_EXCEEDS_TOKEN_BALANCE");
15
gTokenVault.asset().approve(address(gTokenVault), _amount);
16
17
uint256 gTokenPreDepositBalance = getGTokenBalance(_receiver);
18
gTokenVault.deposit(_amount, address(this));
19
return getGTokenBalance(_receiver) - gTokenPreDepositBalance;
20
}
21
22
/**
23
* @dev Returns the gToken balance of the user.
24
*/
25
function getGTokenBalance(address _user) {
26
return gTokenVault.balanceOf(_user);
27
}
28
}
Sweet! Now we have a full end-to-end example of a smart contract that deposits an ERC-20 token into the gToken vault on behalf of a receiver and returns the amount of gTokens received 😤
​