-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdeploy.js
293 lines (256 loc) · 9.29 KB
/
deploy.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import BigNum from 'bn.js'
import { readFileSync } from 'fs'
import fetch from 'node-fetch'
import {
contractPrincipalCV,
stringAsciiCV,
uintCV,
broadcastTransaction,
makeContractCall,
makeContractDeploy,
PostConditionMode,
} from '@stacks/transactions'
import { StacksTestnet, StacksMainnet } from '@stacks/network'
import {
getNonce,
wait,
} from './src/tx-utils.js'
import {
MODE,
STACKS_API_URL,
CONTRACT_NAME_SIP010_TRAIT,
CONTRACT_NAME_SWAPR_TRAIT,
CONTRACT_NAME_RESTRICTED_TOKEN_TRAIT,
CONTRACT_NAME_SWAPR,
CONTRACT_NAME_STX,
CONTRACT_NAME_PLAID,
CONTRACT_NAME_PLAID_STX,
CONTRACT_NAME_THING,
CONTRACT_NAME_PLAID_THING,
CONTRACT_NAME_TOKENSOFT,
CONTRACT_NAME_TOKENSOFT_STX,
CONTRACT_NAME_MICRO_NTHNG,
CONTRACT_NAME_WRAPPED_NOTHING,
CONTRACT_NAME_PLAID_WMNO,
SWAPR_SK,
SWAPR_STX,
USER1_SK,
USER2_SK,
} from './src/config.js'
console.log("deploying swapr with", SWAPR_STX, "on", MODE)
const network = MODE === 'mainnet' ? new StacksMainnet() : new StacksTestnet()
network.coreApiUrl = STACKS_API_URL
console.log("using", STACKS_API_URL)
async function deployContract(contract_name, contract_file) {
const body = readFileSync(contract_file).toString()
// TODO(psq): remove minting for tokens
// TODO(psq): SIP-010 trait should use SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-10-ft-standard instead (custom match for mainnet?)
const codeBody = body
.replaceAll('ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.sip-010', `${SWAPR_STX}.${CONTRACT_NAME_SIP010_TRAIT}`) // minting
.replaceAll('ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.swapr-trait', `${SWAPR_STX}.${CONTRACT_NAME_SWAPR_TRAIT}`) // minting
.replaceAll('ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.restricted-token-trait', `${SWAPR_STX}.${CONTRACT_NAME_RESTRICTED_TOKEN_TRAIT}`) // minting
.replaceAll('ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.swapr', `${SWAPR_STX}.${CONTRACT_NAME_SWAPR}`) // minting
.replaceAll('ST000000000000000000002AMW42H', SWAPR_STX)
.replaceAll('ST20ATRN26N9P05V2F1RHFRV24X8C8M3W54E427B2', SWAPR_STX)
.replaceAll('.micro-nthng', `.${CONTRACT_NAME_MICRO_NTHNG}`)
console.log("deploy", contract_name)
// console.log(codeBody)
const transaction = await makeContractDeploy({
contractName: contract_name,
codeBody,
senderKey: SWAPR_SK,
network,
})
const result = await broadcastTransaction(transaction, network)
if (result.error) {
if (result.reason === 'ContractAlreadyExists') {
console.log(`${contract_name} already deployed`)
return
} else {
throw new Error(
`failed to deploy ${contract_name}: ${JSON.stringify(result)}`
)
}
}
const processed = await processing(result, 0)
if (!processed) {
throw new Error(`failed to deploy ${contract_name}: transaction not found`)
}
return result
}
async function createPair(token_1, token_2, token_1_2, name, amount_1, amount_2, user_sk) {
console.log("createPair", token_1, token_2, token_1_2, name, amount_1, amount_2)
// const fee = new BigNum(311)
const addr = SWAPR_STX
const transaction = await makeContractCall({
contractAddress: addr,
contractName: CONTRACT_NAME_SWAPR,
functionName: 'create-pair',
functionArgs: [contractPrincipalCV(addr, token_1), contractPrincipalCV(addr, token_2), contractPrincipalCV(addr, token_1_2), stringAsciiCV(name), uintCV(amount_1), uintCV(amount_2)],
senderKey: user_sk,
network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
],
})
console.log("transaction", transaction.payload)
const serialized = transaction.serialize().toString('hex')
console.log("serialized", serialized)
const result = await broadcastTransaction(transaction, network)
console.log("result", result)
if (result.error) {
console.log(result.reason)
throw new Error(`failed create pair ${token_1} - ${token_2}`)
}
const processed = await processing(result, 0)
if (!processed) {
throw new Error(`failed to execute create-pair`)
}
}
async function addToPosition(token_1, token_2, token_1_2, amount_1, amount_2, user_sk) {
console.log("addToPosition", token_1, token_2, token_1_2, amount_1, amount_2)
// const fee = new BigNum(311)
const addr = SWAPR_STX
const transaction = await makeContractCall({
contractAddress: addr,
contractName: CONTRACT_NAME_SWAPR,
functionName: 'add-to-position',
functionArgs: [contractPrincipalCV(addr, token_1), contractPrincipalCV(addr, token_2), contractPrincipalCV(addr, token_1_2), uintCV(amount_1), uintCV(amount_2)],
senderKey: user_sk,
network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
],
})
console.log("transaction", transaction.payload)
const serialized = transaction.serialize().toString('hex')
console.log("serialized", serialized)
const result = await broadcastTransaction(transaction, network)
console.log("result", result)
if (result.error) {
console.log(result.reason)
throw new Error(`failed to add to position ${token_1} - ${token_2}`)
}
const processed = await processing(result, 0)
if (!processed) {
throw new Error(`failed to execute add-to-position`)
}
}
async function getContractInfo(contract_name) {
var result = await fetch(`${STACKS_API_URL}/extended/v1/contract//${SWAPR_STX}.${contract_name}`)
// console.log("result", result)
var value = await result.json()
// console.log("value", value)
if (value.error) {
return null
}
return value.tx_id
}
async function processing(tx, count) {
console.log("processing", tx, count)
var result = await fetch(`${STACKS_API_URL}/extended/v1/tx/${tx}`)
var value = await result.json()
if (value.tx_status === "success") {
console.log(`transaction ${tx} processed`)
// console.log(value)
return true
}
if (count > 10) {
console.log("failed after 10 attempts", value)
return false
}
await wait(30000)
return processing(tx, count + 1)
}
// TODO(psq): figure out nonces so all contracts can deployed in a batch from nonce...nonce+5
await getContractInfo(CONTRACT_NAME_SIP010_TRAIT)
if (!await getContractInfo(CONTRACT_NAME_SIP010_TRAIT)) {
await deployContract(CONTRACT_NAME_SIP010_TRAIT, './clarinet/contracts/trait-sip-010.clar')
}
if (!await getContractInfo(CONTRACT_NAME_SWAPR_TRAIT)) {
await deployContract(CONTRACT_NAME_SWAPR_TRAIT, './clarinet/contracts/trait-swapr.clar')
}
if (!await getContractInfo(CONTRACT_NAME_RESTRICTED_TOKEN_TRAIT)) {
await deployContract(CONTRACT_NAME_RESTRICTED_TOKEN_TRAIT, './clarinet/contracts/trait-restricted-token.clar')
}
if (!await getContractInfo(CONTRACT_NAME_SWAPR)) {
await deployContract(CONTRACT_NAME_SWAPR, './clarinet/contracts/main-swapr.clar')
}
if (!await getContractInfo(CONTRACT_NAME_STX)) {
await deployContract(CONTRACT_NAME_STX, './clarinet/contracts/token-stx.clar')
}
if (!await getContractInfo(CONTRACT_NAME_PLAID)) {
await deployContract(CONTRACT_NAME_PLAID, './clarinet/contracts/token-plaid.clar')
}
if (!await getContractInfo(CONTRACT_NAME_PLAID_STX)) {
await deployContract(CONTRACT_NAME_PLAID_STX, './clarinet/contracts/swapr-token-plaid-stx.clar')
}
if (!await getContractInfo(CONTRACT_NAME_THING)) {
await deployContract(CONTRACT_NAME_THING, './clarinet/contracts/token-thing.clar')
}
if (!await getContractInfo(CONTRACT_NAME_PLAID_THING)) {
await deployContract(CONTRACT_NAME_PLAID_THING, './clarinet/contracts/swapr-token-plaid-thing.clar')
}
if (!await getContractInfo(CONTRACT_NAME_TOKENSOFT)) {
await deployContract(CONTRACT_NAME_TOKENSOFT, './clarinet/contracts/token-tokensoft.clar')
}
if (!await getContractInfo(CONTRACT_NAME_TOKENSOFT_STX)) {
await deployContract(CONTRACT_NAME_TOKENSOFT_STX, './clarinet/contracts/swapr-token-tokensoft-stx.clar')
}
if (!await getContractInfo(CONTRACT_NAME_MICRO_NTHNG)) {
await deployContract(CONTRACT_NAME_MICRO_NTHNG, './clarinet/contracts/token-micro-nthng.clar')
}
if (!await getContractInfo(CONTRACT_NAME_WRAPPED_NOTHING)) {
await deployContract(CONTRACT_NAME_WRAPPED_NOTHING, './clarinet/contracts/token-wrapped-nothing.clar')
}
if (!await getContractInfo(CONTRACT_NAME_PLAID_WMNO)) {
await deployContract(CONTRACT_NAME_PLAID_WMNO, './clarinet/contracts/swapr-token-plaid-wrapped-nothing.clar')
}
// create plaid-stx pair
const result_pair_1 = await createPair(
`${CONTRACT_NAME_PLAID}`,
`${CONTRACT_NAME_STX}`,
`${CONTRACT_NAME_PLAID_STX}`,
'Plaid-STX',
25_000_000_000_000,
500_000_000_000,
USER1_SK,
)
const result_pair_2 = await createPair(
`${CONTRACT_NAME_PLAID}`,
`${CONTRACT_NAME_THING}`,
`${CONTRACT_NAME_PLAID_THING}`,
'Plaid-Thing',
100_000_000_000,
200_000_000_000,
USER1_SK,
)
const result_pair_3 = await createPair(
`${CONTRACT_NAME_TOKENSOFT}`,
`${CONTRACT_NAME_STX}`,
`${CONTRACT_NAME_TOKENSOFT_STX}`,
'xBTC-STX',
2_000_000_000, // 20 BTC
1_200_000_000_000, // 1_200_000 STX
USER1_SK,
)
const result_pair_4 = await createPair(
`${CONTRACT_NAME_PLAID}`,
`${CONTRACT_NAME_WRAPPED_NOTHING}`,
`${CONTRACT_NAME_PLAID_WMNO}`,
'Plaid-WMNO',
2_000_000_000, // 20 BTC
1_200_000_000_000, // 1_200_000 STX
USER1_SK,
)
const result_pair_5 = await addToPosition(
`${CONTRACT_NAME_TOKENSOFT}`,
`${CONTRACT_NAME_STX}`,
`${CONTRACT_NAME_TOKENSOFT_STX}`,
1_000_000_000, // 10 BTC
600_000_000_000, // 600_000 STX
USER2_SK,
)
2_400_000_000_000
console.log("done")
// create a second pair?