forked from shakeib98/uniswap-exchange-setup
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
262 lines (209 loc) · 7.59 KB
/
Copy pathindex.js
File metadata and controls
262 lines (209 loc) · 7.59 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const Web3 = require('web3')
const constants = require('./constant')
const net = require('net');
require('dotenv').config()
let contract;
const OPTIONS = {
transactionConfirmationBlocks: 1,
transactionBlockTimeout: 5
};
// connecting to geth using ipc
web3 = new Web3(new Web3.providers.IpcProvider(process.env.GETH_IPC_PATH, net));
let owner
const ownerPASSWORD = process.env.OWNER_PASSWORD
async function accounts() {
const accounts = await web3.eth.personal.getAccounts()
owner = accounts[0]
}
async function main() {
await accounts()
console.log('Owner', owner)
let xioAddress = await deployXIO()
console.log(xioAddress, "XIO")
let omgAddress = await deployOMG()
console.log(omgAddress, "OMG")
let exchangeTemplateAddress = await deployExchange()
console.log(exchangeTemplateAddress, "TEMPLATE")
let factoryAddress = await deployFactory(exchangeTemplateAddress)
console.log(factoryAddress, "FACTORY")
let xioExchange = await deployXIOExchange(factoryAddress, xioAddress)
console.log(xioExchange, "XIO EXCHANGE")
let omgExchange = await deployOMGExchange(factoryAddress, omgAddress)
console.log(omgExchange, "OMG EXCHANGE")
await addLiquidityXIO(xioExchange)
await addLiquidityOMG(omgExchange)
return
}
async function deployXIO() {
contract = new web3.eth.Contract(constants.ABI_XIO);
let result = await deploy(contract, constants.BYTE_CODE_XIO)
return result
}
async function deployOMG() {
contract = new web3.eth.Contract(constants.ABI_OMG);
let result = await deploy(contract, constants.BYTE_CODE_OMG)
return result
}
async function deployFactory(exchangeTemplateAddress) {
contract = new web3.eth.Contract(constants.ABI_FACTORY);
let result = await deploy(contract, constants.BYTE_CODE_FACTORY, exchangeTemplateAddress)
return result
}
async function deployExchange() {
contract = new web3.eth.Contract(constants.ABI_EXCHANGE);
let result = await deploy(contract, constants.BYTE_CODE_EXCHANGE)
return result
}
async function deployXIOExchange(factoryAddress, xioTokenAddress) {
contract = new web3.eth.Contract(constants.ABI_FACTORY, factoryAddress);
var contractAddress = '';
let count = await web3.eth.getTransactionCount(owner, "pending")
let txObject = {
from: owner,
to: factoryAddress,
gasPrice: 1 * 1000000000,
gasLimit: 1000000,
gas: 3000000, //only when connected to ganache
nonce: web3.utils.toHex(count),
data: contract.methods.createExchange(xioTokenAddress).encodeABI()
}
let signed = await web3.eth.personal.signTransaction(txObject, ownerPASSWORD)
await web3.eth.sendSignedTransaction(signed.raw).then(res=>{
console.log(res.transactionHash)
})
let blockNumber = await web3.eth.getBlockNumber()
await contract.getPastEvents('NewExchange', {
fromBlock: blockNumber,
toBlock: blockNumber
}).then(events => {
console.log(events)
contractAddress = events[0].returnValues['1']
});
return contractAddress
}
async function deployOMGExchange(factoryAddress, omgTokenAddress) {
contract = new web3.eth.Contract(constants.ABI_FACTORY, factoryAddress);
var contractAddress = '';
let count = await web3.eth.getTransactionCount(owner, "pending")
let txObject = {
from: owner,
to: factoryAddress,
gasPrice: 1 * 1000000000,
gasLimit: 1000000,
gas: 3000000, //only when connected to ganache
chainId: 5777,
nonce: web3.utils.toHex(count),
data: contract.methods.createExchange(omgTokenAddress).encodeABI()
}
let signed = await web3.eth.personal.signTransaction(txObject, ownerPASSWORD)
await web3.eth.sendSignedTransaction(signed.raw).then(res=>{
console.log(res.transactionHash)
})
let blockNumber = await web3.eth.getBlockNumber()
await contract.getPastEvents('NewExchange', {
fromBlock: blockNumber,
toBlock: blockNumber
}).then(events => {
contractAddress = events[0].returnValues['1']
});
return contractAddress
}
async function addLiquidityXIO(xioExchangeAddress) {
let ethAmount = await web3.utils.toWei('0.1')
let tokenAmount = await web3.utils.toWei('50000')
contract = new web3.eth.Contract(constants.ABI_EXCHANGE, xioExchangeAddress);
let count = await web3.eth.getTransactionCount(owner, "pending")
let txObject = {
from: owner,
to: xioExchangeAddress,
gasPrice: 1 * 1000000000,
gasLimit: 1000000,
gas: 3000000, //only when connected to ganache
nonce: web3.utils.toHex(count),
data: contract.methods.addLiquidity(ethAmount, tokenAmount, 1839591241).encodeABI(),
value:Number(ethAmount)
}
let signed = await web3.eth.personal.signTransaction(txObject, ownerPASSWORD)
let tx = await web3.eth.sendSignedTransaction(signed.raw)
.on('confirmation', (confirmationNumber, receipt) => {
if(confirmationNumber === 1){
console.log("LIQUIDITY ADDED XIO")
}
})
.on('error', (error) => {
console.log(error)
})
.on('transactionHash', async (hash) => {
console.log(hash);
});
return
}
async function addLiquidityOMG(omgExchangeAddress) {
const accounts = await web3.eth.personal.getAccounts()
owner = accounts[0]
let ethAmount = await web3.utils.toWei('0.1')
let tokenAmount = await web3.utils.toWei('50000')
contract = new web3.eth.Contract(constants.ABI_EXCHANGE, omgExchangeAddress);
let count = await web3.eth.getTransactionCount(owner, "pending")
let txObject = {
from: owner,
to: omgExchangeAddress,
gasPrice: 1 * 1000000000,
gasLimit: 1000000,
gas: 3000000, //only when connected to ganache
chainId: 5777,
nonce: web3.utils.toHex(count),
data: contract.methods.addLiquidity(ethAmount, tokenAmount, 1839591241).encodeABI(),
value:Number(ethAmount)
}
let signed = await web3.eth.personal.signTransaction(txObject, ownerPASSWORD)
let tx = await web3.eth.sendSignedTransaction(signed.raw)
.on('confirmation', (confirmationNumber, receipt) => {
if(confirmationNumber === 1){
console.log('LIQUIDITY ADDED OMG')
}
})
.on('error', (error) => {
console.log(error)
})
.on('transactionHash', async (hash) => {
console.log(hash);
});
return
}
async function deploy(contract, bytecode, params) {
var contractAddress = '';
let data = ''
let count = await web3.eth.getTransactionCount(owner, "pending")
if (params) {
data = contract.deploy({
data: bytecode,
arguments: [params]
}).encodeABI()
} else {
data = contract.deploy({
data: bytecode
}).encodeABI()
}
let txObject = {
from: owner,
gasPrice: 1 * 1000000000,
gasLimit: 1000000,
gas: 3000000, //only when connected to ganache
nonce: web3.utils.toHex(count),
data: data
}
let signed = await web3.eth.personal.signTransaction(txObject, ownerPASSWORD)
await web3.eth.sendSignedTransaction(signed.raw)
.on('error', function (error) {
console.log(error)
})
.on('transactionHash', function (transactionHash) {
console.log(transactionHash)
})
.on("receipt", function (receipt) {
contractAddress = receipt.contractAddress
})
return contractAddress
}
main()