-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
164 lines (140 loc) · 4.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const defiBlocks = require('./defi_blocks');
const xml = defiBlocks.xml();
const fs = require('fs');
let template = fs.readFileSync('templates/yearn_dai.sol', 'utf-8');
try {
let values = xml.getElementsByTagName("value");
for (let elem of values) {
let k = elem.getAttribute("name");
let v = elem.getElementsByTagName("field")[0].innerHTML;
switch(k) {
case "NAME":
renderName(v);
break;
case "PERFORMANCE_FEE":
renderPerformanceFee(v);
break;
case "PERFORMANCE_MAX":
renderPerformanceMax(v);
break;
case "WITHDRAWAL_FEE":
renderWithdrawalFee(v);
break;
case "WITHDRAWAL_MAX":
renderWithdrawalMax(v);
break;
case "DEPOSITOR":
renderDepositor(v);
break;
case "REWARDOR":
renderRewardor(v);
break;
case "EXCHANGE":
renderExchange(v);
break;
case "TOKENSET":
let elems = elem.getElementsByTagName("field");
let values = {};
for (let e of elems) {
let tsKey = e.getAttribute("name");
let tsVal = e.innerHTML;
values[tsKey] = tsVal;
}
renderTokenSet(values);
default:
break;
}
}
}
catch (e) {
console.log(e);
}
const outputFile = 'generated/defi_blocks.sol';
fs.writeFileSync(outputFile, template, 'utf-8');
console.log(`Smart contract has been generated to ${outputFile}`);
/**
* render functions
*/
function renderName(name) {
replaceAll("CONTRACT_NAME", name);
}
function renderPerformanceFee(value) {
replaceAll("PERFORMANCE_FEE", value);
}
function renderPerformanceMax(value) {
replaceAll("PERFORMANCE_MAX", value);
}
function renderWithdrawalFee(value) {
replaceAll("WITHDRAWAL_FEE", value);
}
function renderWithdrawalMax(value) {
replaceAll("WITHDRAWAL_MAX", value);
}
function renderDepositor(depositor) {
switch(depositor) {
case "DEPOSITOR_CONTRACT_COMPOUND":
renderCompoundDepositor();
break;
/** TODO: add more here */
default:
break;
}
}
function renderRewardor(rewardor) {
switch(rewardor) {
case "REWARDOR_CONTRACT_COMPOUND":
renderCompoundRewardor();
break;
/** TODO: add more here */
default:
break;
}
}
function renderCompoundDepositor() {
replaceAll("IMPORT_DEPOSITOR", "../../interfaces/compound/cToken.sol");
replaceAll("DEPOSITOR_TOKEN_ADDRESS", "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643");
replaceAll("DEPOSITOR_CONTRACT", "cToken");
replaceAll("DEPOSITOR_METHOD_MINT", "mint");
replaceAll("DEPOSITOR_METHOD_WITHDRAW", "redeem");
replaceAll("DEPOSITOR_METHOD_EXCHANGE_RATE", "exchangeRateStored");
replaceAll("DEPOSITOR_METHOD_EXCHANGE_RATE_CONVERSION", "div(1e18)");
}
function renderCompoundRewardor() {
replaceAll("IMPORT_REWARDOR", "../../interfaces/compound/Comptroller.sol");
replaceAll("REWARDOR_TOKEN_ADDRESS", "0xc00e94Cb662C3520282E6f5717214004A7f26888");
replaceAll("REWARDOR_FIELD_TYPE", "Comptroller");
replaceAll("REWARDOR_FIELD_ADDRESS", "0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B");
replaceAll("REWARDOR_METHOD_CLAIM", "claimComp");
}
function renderExchange(name) {
// for now it's only uniswap
replaceAll("IMPORT_EXCHANGE", "../../interfaces/uniswap/Uni.sol");
replaceAll("EXCHANGE_ADDRESS", "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D");
replaceAll("EXCHANGE_FIELD_TYPE", "Uni");
replaceAll("EXCHANGE_METHOD", "swapExactTokensForTokens")
}
function renderTokenSet(tokenSet) {
let wantToken = tokenSet['WANT_TOKEN'];
switch (wantToken) {
// TODO DAI is the only case right now
case "TOKEN_DAI":
default:
replaceAll("WANT_TOKEN_ADDRESS", "0x6B175474E89094C44Da98b954EedeAC495271d0F")
break;
}
let intermediaryToken = tokenSet['INTERMEDIARY_TOKEN'];
switch (intermediaryToken) {
// TODO wETH is the only case right now
case "TOKEN_WETH":
default:
replaceAll("INTERMEDIARY_TOKEN_ADDRESS", "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
break;
}
}
function replaceAll(key, replacement) {
const re = new RegExp(getSymbol(key), 'g');
template = template.replace(re, replacement);
}
function getSymbol(key) {
return `!!${key}!!`;
}