-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.mjs
More file actions
248 lines (218 loc) · 5.79 KB
/
deploy.mjs
File metadata and controls
248 lines (218 loc) · 5.79 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { readFileSync } from "fs";
import {
LCDClient,
MnemonicKey,
MsgInstantiateContract,
MsgStoreCode,
MsgExecuteContract,
Fee
} from "@terra-money/terra.js";
const POOL_PATH =
"../Pool.wasm";
// Get the wallet seed phrase from the environment variable.
const TERRA_SEED = process.env.TERRA_SEED
const mk = new MnemonicKey({
mnemonic:
TERRA_SEED,
});
const LCD_TEST = new LCDClient({
URL: 'https://bombay-lcd.terra.dev',
chainID: 'bombay-12',
gasPrices: { uusd: 0.45 },
});
const LCD_MAIN = new LCDClient({ //mainnet
URL: 'https://lcd.terra.dev',
chainID: 'columbus-5',
gasPrices: { uusd: 0.45 },
});
const net = "test"
let terra = net=="main"? LCD_MAIN : LCD_TEST;
const codeid_cw20 = net=="main"? 3: 148;
let wallet = terra.wallet(mk);
let poolAddress = "terra1yl8ad8n2uqz3560akwqs2k7zc0zn9dg9z9tjuv";
let vustAddress = "terra1hx9v3xu7kc7fuleqxzsags5pezwn8x5wmjxm5p";
let vlunaAddress = "terra1swr7jq37664wgqn48qtnlfgexg77dglm9ytxgg";
run();
async function run() {
if (poolAddress == "") {
console.log("Deploying Pool Contract");
const poolCodeId = await upload(POOL_PATH);
await sleep(12000);
console.log("instatiating pool contract");
let param = {
"treasury": "terra1qvyj7tqs35hckd395rglc7lsyf2acuhgdcmj77",
"ust_apr": "3487",
"luna_apr": "1861",
"vust": `${mk.accAddress}`,
"vluna": `${mk.accAddress}`
};
poolAddress = await instantiate(poolCodeId, param);
console.log(poolAddress);
await sleep(12000);
}
if (vustAddress == "") {
console.log("Deploying UST Voucher Contract");
vustAddress = await instantiate(codeid_cw20,
{
"name": "VUST",
"symbol": "vust",
"decimals": 6,
"initial_balances": [],
"mint":{
"minter": `${poolAddress}`
}
}
)
console.log(vustAddress);
await sleep(12000);
}
if (vlunaAddress == "") {
console.log("Deploying LUNA Voucher Contract");
vlunaAddress = await instantiate(codeid_cw20,
{
"name": "VLUNA",
"symbol": "vluna",
"decimals": 6,
"initial_balances": [],
"mint":{
"minter": `${poolAddress}`
}
}
)
console.log(vlunaAddress);
await sleep(12000);
}
console.log("configuring");
let result = await config();
console.log("reading contract");
result = await terra.wasm.contractQuery(poolAddress, { "get_history_of_apr_ust": {} })
console.log(result);
}
async function config() {
console.log("pool_contract: " + poolAddress);
console.log("VUst: " + vustAddress);
console.log("VLuna: " + vlunaAddress);
try {
console.log("adding apr")
let apr_ust_config = new MsgExecuteContract(
mk.accAddress,
poolAddress,
{ "set_apr_ust": { "apr": "3487" } }
);
let apr_luna_config = new MsgExecuteContract(
mk.accAddress,
poolAddress,
{ "set_apr_luna": { "apr": "1861" } }
);
let token_address = new MsgExecuteContract(
mk.accAddress,
poolAddress,
{
"set_config":{
"vust" : `${vustAddress}`,
"vluna": `${vlunaAddress}`
}
}
)
let res = await EstimateSend([apr_ust_config, apr_luna_config, token_address], "setting apr");
return res;
}
catch (e) {
console.log(e);
process.exit(1);
}
}
async function upload(contractPath) {
const wasm = readFileSync(contractPath);
const tx = new MsgStoreCode(mk.accAddress, wasm.toString("base64"));
try {
const storeResult = await EstimateSend([tx], `Storing ${contractPath}`);
console.log(storeResult.raw_log);
const codeId = extractCodeId(storeResult.raw_log);
return codeId;
} catch (e) {
console.log(e);
process.exit(1);
}
}
async function instantiate(codeId, instantiateMsg) {
try {
const instantiate = new MsgInstantiateContract(
mk.accAddress,
mk.accAddress,
codeId,
instantiateMsg
);
const instantiateResult = await EstimateSend([instantiate], "instantiating");
return extractContractAddress(instantiateResult.raw_log);
} catch (e) {
console.log(e);
process.exit(1);
}
}
function extractCodeId(logs) {
// TODO improve parsing
const parsed = JSON.parse(logs);
return Number(parsed[0]["events"][1]["attributes"][1]["value"]);
}
function extractContractAddress(logs) {
const parsed = JSON.parse(logs);
let keys = parsed[0]["events"][0]["attributes"];
for(let i=0; i<keys.length; i++){
if(keys[i].key === 'contract_address')
return keys[i].value;
}
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function EstimateSend(msgs, memo) {
try {
const obj = new Fee(10_000, { uusd: 4500 });
let accountInfo;
await terra.auth.accountInfo(
mk.accAddress
)
.then((e) => {
accountInfo = e;
})
let txOptions =
{
msgs: msgs,
memo: memo,
gasPrices: obj.gasPrices(),
gasAdjustment: 1.7,
};
let rawFee;
await terra.tx.estimateFee(
[
{
sequenceNumber: accountInfo.getSequenceNumber(),
publicKey: accountInfo.getPublicKey(),
},
],
txOptions
)
.then((e) => {
rawFee = e;
})
const res = await wallet
.createAndSignTx({
msgs: msgs,
memo: memo,
fee: rawFee,
gasPrice: obj.gasPrices(),
gasAdjustment: 1.7,
sequence: accountInfo.getSequenceNumber()
})
.then((tx) => terra.tx.broadcast(tx))
return res;
}
catch (e) {
console.log(e);
process.exit(1);
}
}
export default {};