CompliFi
Search…
DSProxy Actions
ETH proxy actions contract address 0x6f0B967a8200c764d09A781397A28f0A0Ec259c0
Polygon proxy actions contract address 0x5FbDB2315678afecb367f032d93F642f64180aa3
In order to atomically perform transactions that involve both issuance and the AMM, a number of proxy actions have been constructed, covering in particular collateral-derivative swaps and adding/removing AMM liquidity.

Issuance and AMM

1
/// @title CompliFi direct and composite AMM and issuance methods
2
3
contract ProxyActions {
4
5
/// @notice Mint derivatives by depositing collateral to vault
6
function mint(
7
address _vault,
8
uint256 _collateralAmount
9
) external {...}
10
11
/// @notice Redeem a symmetric portfolio of live derivatives for underlying collateral
12
function refund(
13
address _vault,
14
uint256 _tokenAmount
15
) external {...}
16
17
/// @notice Redeem settled derivatives for underlying collateral
18
function redeem(
19
address _vault,
20
uint256 _primaryTokenAmount,
21
uint256 _complementTokenAmount,
22
uint256[] memory _underlyingEndRoundHints
23
) external {...}
24
25
/// @notice Withdraw ERC20 token balance
26
function withdraw(
27
address _token
28
) external {...}
29
30
/// @notice Withdraw several ERC20 token balances
31
function withdrawAll(
32
address[] memory _tokens
33
) external {...}
34
35
/// @notice Add liquidity to AMM pool in identical proportion to pool contents
36
function joinPool(
37
address _pool,
38
uint256 _poolAmountOut,
39
uint256[2] calldata _maxAmountsIn
40
) external {...}
41
42
/// @notice Swap between derivatives contained in a single AMM pool
43
function swap(
44
address _pool,
45
address _tokenIn,
46
uint256 _tokenAmountIn,
47
address _tokenOut,
48
uint256 _minAmountOut
49
) external {...}
50
51
/// @notice Remove liquidity from AMM pool
52
function exitPool(
53
address _pool,
54
uint256 _poolAmountIn,
55
uint256[2] calldata _minAmountsOut
56
) external {...}
57
58
/// @notice Swap collateral for a single derivative
59
function mintAndSwapCollateralToDerivative(
60
address _pool,
61
uint256 _collateralAmount,
62
address _tokenIn, // Unwanted Derivative to be swapped
63
uint256 _minAmountOut
64
) external {...}
65
66
/// @notice Swap derivative for collateral
67
function swapDerivativesToCollateral(
68
address _pool,
69
address _derivativeIn,
70
uint256 _derivativeAmount,
71
uint256 _tokenAmountIn,
72
address _derivativeOut,
73
uint256 _derivativeMinAmountOut
74
) external {...}
75
76
// @notice Swap between derivatives in different AMM pools
77
function tradeBetweenDerivatives(
78
address _poolFromAddress,
79
address _derivativeInAddress,
80
uint256 _derivativeInAmount,
81
uint256 _derivativeInAmountToSell,
82
address _derivativeOut,
83
uint256 _minTokenOutAmountForFirstSwap,
84
address _poolToAddress,
85
address _mintedDerivativeToSell,
86
uint256 _minTokenOutAmountForSecondSwap
87
) external {...}
88
89
/// @notice Use collateral to mint derivatives and add them as liquidity to AMM pool
90
function mintAndJoinPool(
91
address _pool,
92
uint256 _collateralAmount,
93
address _tokenIn,
94
uint256 _tokenAmountIn,
95
address _tokenOut,
96
uint256 _minAmountOut,
97
uint256 _minPoolAmountOut
98
) external {...}
99
100
/// @notice Remove settled derivatives from AMM pool redeem for collateral
101
/// @dev User provides amount of LP tokens (method applies only when state = Settled)
102
function removeLiquidityOnSettledState(
103
address _pool,
104
uint256 _poolAmountIn,
105
uint256[2] calldata _minAmountsOut,
106
uint256[] memory _underlyingEndRoundHints
107
) external {...}
108
109
/// @notice Remove live derivatives from pool and redeem symmetric portion for collateral
110
/// @dev User provides amount of LP tokens (method applies only when state = Live)
111
function removeLiquidityOnLiveOrMintingState(
112
address _pool,
113
uint256 _poolAmountIn,
114
address _tokenIn,
115
uint256 _tokenAmountIn,
116
uint256 _minAmountOut,
117
uint256[2] calldata _minAmountsOut
118
) external {...}
119
120
/// @notice Withdraw derivative balances from proxy contract to user's account
121
function extractChange(address _pool) external {...}
122
}
123
Copied!

Liquidity Mining

1
/// @title CompliFi liquidity mining methods
2
3
contract ProxyActionsLiquidityMining {
4
5
/// @notice Deposit tokens to a farm
6
function deposit(
7
address _liquidityMining,
8
address _token,
9
uint256 _tokenAmount
10
) external {...}
11
12
/// @notice Withdraw deposited tokens
13
function withdraw(
14
address _liquidityMining,
15
address _token,
16
uint256 _tokenAmount
17
) external {...}
18
19
/// @notice Claim unlocked rewards
20
function claim(
21
address _liquidityMining
22
) external {...}
23
}
Copied!

Liquidity Mining Methods - Example

Get user's DSProxy and create a contract:
1
const PROXY_REGISTRY_ADDR = "0x4678f0a6958e4D2Bc4F1BAF7Bc52E8F3564f3fE4"; // Maker DsProxy registry
2
const PROXY_REGISTRY_ABI = [...]; // DsProxy basic ABI
3
const PROXY_REGISTRY_CONTRACT = new ethers.Contract(PROXY_REGISTRY_ADDR, PROXY_REGISTRY_ABI);
4
5
const signer = ethers.getDefaultProvider().getSigner();
6
const userProxyAddress = await PROXY_REGISTRY_CONTRACT.proxies(userAddress);
7
const PROXY_ABI = [{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_target","type":"address"},{"name":"_data","type":"bytes"}],"name":"execute","outputs":[{"name":"response","type":"bytes32"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"_code","type":"bytes"},{"name":"_data","type":"bytes"}],"name":"execute","outputs":[{"name":"target","type":"address"},{"name":"response","type":"bytes32"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"cache","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_cacheAddr","type":"address"}],"name":"setCache","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_cacheAddr","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}];
8
const proxyContract = new ethers.Contract(userProxyAddress, PROXY_ABI, signer);
9
10
Copied!
Initialize related actions contract (liquidity mining actions as an example):
1
const PROXY_ACTIONS_ABI = [{"inputs":[{"internalType":"address","name":"_liquidityMining","type":"address"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_liquidityMining","type":"address"},{"internalType":"address","name":"_token","type":"address"},{"internalType":"uint256","name":"_tokenAmount","type":"uint256"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_liquidityMining","type":"address"},{"internalType":"address","name":"_token","type":"address"},{"internalType":"uint256","name":"_tokenAmount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}];
2
3
async function executeProxyAction(proxyContract, proxyActionsAddress, functionName, functionParams, gasLimit){
4
const targetContract = new ethers.Contract(proxyActionsAddress, PROXY_ACTIONS_ABI, signer);
5
const callData = targetContract.interface.encodeFunctionData(functionName, functionParams.map((fp) => fp.value));
6
7
try {
8
const gasEstimation = (await proxyContract.estimateGas['execute(address,bytes)'](proxyActionsAddress, callData)) * 1;
9
if (gasEstimation) {
10
gasLimit = Math.max(gasLimit || 0, gasEstimation);
11
}
12
console.log('Gas estimate: %s', gasEstimation);
13
} catch (error) {
14
console.log(error);
15
}
16
console.log('Gas limit: %s', gasLimit);
17
18
proxyContract['execute(address,bytes)'](proxyActionsAddress, callData, {
19
gasLimit: gasLimit || undefined,
20
})
21
.then(function(t) {
22
return ethers.getDefaultProvider().waitForTransaction(t.hash)
23
})
24
.catch(x => {
25
console.log(x);
26
_print('Something went wrong.')
27
});
28
}
29
30
function truncN(value, n) {
31
return Math.trunc(value * Math.pow(10, n)) / Math.pow(10, n);
32
}
33
34
function trunc6(value) {
35
return truncN(value, 6);
36
}
Copied!
Executing CompliFi liquidity mining methods:
1
async function deposit(proxyContract, proxyActionsAddress, miningAddress, tokenAddress, tokenAmount) {
2
const functionName = 'deposit';
3
const functionParams = [
4
{
5
name: 'liquidityMiningAddress',
6
type: 'address',
7
value: miningAddress,
8
},
9
{
10
name: 'tokenAddress',
11
type: 'address',
12
value: tokenAddress,
13
},
14
{
15
name: 'tokenAmount',
16
type: 'uint256',
17
value: tokenAmount.toString(),
18
},
19
];
20
21
return executeProxyAction(
22
proxyContract,
23
proxyActionsAddress,
24
functionName,
25
functionParams,
26
250000,
27
);
28
29
}
30
31
async function withdraw(proxyContract, proxyActionsAddress, miningAddress, tokenAddress, tokenAmount) {
32
const functionName = 'withdraw';
33
const functionParams = [
34
{
35
name: 'liquidityMiningAddress',
36
type: 'address',
37
value: miningAddress,
38
},
39
{
40
name: 'tokenAddress',
41
type: 'address',
42
value: tokenAddress,
43
},
44
{
45
name: 'tokenAmount',
46
type: 'uint256',
47
value: tokenAmount.toString(),
48
},
49
];
50
51
return executeProxyAction(
52
proxyContract,
53
proxyActionsAddress,
54
functionName,
55
functionParams,
56
200000,
57
);
58
}
59
60
async function claim(proxyContract, proxyActionsAddress, miningAddress) {
61
const functionName = 'claim';
62
const functionParams = [
63
{
64
name: 'liquidityMiningAddress',
65
type: 'address',
66
value: miningAddress,
67
},
68
];
69
70
return executeProxyAction(
71
proxyContract,
72
proxyActionsAddress,
73
functionName,
74
functionParams,
75
500000,
76
);
77
}
78
79
Copied!
Last modified 4mo ago