-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_seed-exchange.js
More file actions
167 lines (129 loc) · 5.4 KB
/
2_seed-exchange.js
File metadata and controls
167 lines (129 loc) · 5.4 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
const config = require('../src/config.json')
const tokens = (n) => {
return ethers.utils.parseUnits(n.toString(), 'ether')
}
const wait = (seconds) => {
const milliseconds = seconds * 1000
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
async function main() {
// Fetch accounts from wallet - these are unlocked
const accounts = await ethers.getSigners()
// Fetch network
const { chainId } = await ethers.provider.getNetwork()
console.log("Using chainId:", chainId)
// Fetch deployed tokens
const DApp = await ethers.getContractAt('Token', config[chainId].DApp.address)
console.log(`Dapp Token fetched: ${DApp.address}\n`)
const mETH = await ethers.getContractAt('Token', config[chainId].mETH.address)
console.log(`mETH Token fetched: ${mETH.address}\n`)
const mDAI = await ethers.getContractAt('Token', config[chainId].mDAI.address)
console.log(`mDAI Token fetched: ${mDAI.address}\n`)
// Fetch the deployed exchange
const exchange = await ethers.getContractAt('Exchange', config[chainId].exchange.address)
console.log(`Exchange fetched: ${exchange.address}\n`)
// Give tokens to account[1]
const sender = accounts[0]
const receiver = accounts[1]
let amount = tokens(10000)
// user1 transfers 10,000 mETH...
let transaction, result
transaction = await mETH.connect(sender).transfer(receiver.address, amount)
console.log(`Transferred ${amount} tokens from ${sender.address} to ${receiver.address}\n`)
// Set up exchange users
const user1 = accounts[0]
const user2 = accounts[1]
amount = tokens(10000)
// user1 approves 10,000 Dapp...
transaction = await DApp.connect(user1).approve(exchange.address, amount)
await transaction.wait()
console.log(`Approved ${amount} tokens from ${user1.address}`)
// user1 deposits 10,000 DApp...
transaction = await exchange.connect(user1).depositToken(DApp.address, amount)
await transaction.wait()
console.log(`Deposited ${amount} Ether from ${user1.address}\n`)
// User 2 Approves mETH
transaction = await mETH.connect(user2).approve(exchange.address, amount)
await transaction.wait()
console.log(`Approved ${amount} tokens from ${user2.address}`)
// User 2 Deposits mETH
transaction = await exchange.connect(user2).depositToken(mETH.address, amount)
await transaction.wait()
console.log(`Deposited ${amount} tokens from ${user2.address}\n`)
/////////////////////////////////////////////////////////////
// Seed a Cancelled Order
//
// User 1 makes order to get tokens
let orderId
transaction = await exchange.connect(user1).makeOrder(mETH.address, tokens(100), DApp.address, tokens(5))
result = await transaction.wait()
console.log(`Made order from ${user1.address}`)
// User 1 cancels order
orderId = result.events[0].args.id
transaction = await exchange.connect(user1).cancelOrder(orderId)
result = await transaction.wait()
console.log(`Cancelled order from ${user1.address}\n`)
// Wait 1 second
await wait(1)
/////////////////////////////////////////////////////////////
// Seed Filled Orders
//
// User 1 makes order
transaction = await exchange.connect(user1).makeOrder(mETH.address, tokens(100), DApp.address, tokens(10))
result = await transaction.wait()
console.log(`Made order from ${user1.address}`)
// User 2 fills order
orderId = result.events[0].args.id
transaction = await exchange.connect(user2).fillOrder(orderId)
result = await transaction.wait()
console.log(`Filled order from ${user1.address}\n`)
// Wait 1 second
await wait(1)
// User 1 makes another order
transaction = await exchange.makeOrder(mETH.address, tokens(50), DApp.address, tokens(15))
result = await transaction.wait()
console.log(`Made order from ${user1.address}`)
// User 2 fills another order
orderId = result.events[0].args.id
transaction = await exchange.connect(user2).fillOrder(orderId)
result = await transaction.wait()
console.log(`Filled order from ${user1.address}\n`)
// Wait 1 second
await wait(1)
// User 1 makes final order
transaction = await exchange.connect(user1).makeOrder(mETH.address, tokens(200), DApp.address, tokens(20))
result = await transaction.wait()
console.log(`Made order from ${user1.address}`)
// User 2 fills final order
orderId = result.events[0].args.id
transaction = await exchange.connect(user2).fillOrder(orderId)
result = await transaction.wait()
console.log(`Filled order from ${user1.address}\n`)
// Wait 1 second
await wait(1)
/////////////////////////////////////////////////////////////
// Seed Open Orders
//
// User 1 makes 10 orders
for(let i = 1; i <= 10; i++) {
transaction = await exchange.connect(user1).makeOrder(mETH.address, tokens(10 * i), DApp.address, tokens(10))
result = await transaction.wait()
console.log(`Made order from ${user1.address}`)
// Wait 1 second
await wait(1)
}
// User 2 makes 10 orders
for (let i = 1; i <= 10; i++) {
transaction = await exchange.connect(user2).makeOrder(DApp.address, tokens(10), mETH.address, tokens(10 * i))
result = await transaction.wait()
console.log(`Made order from ${user2.address}`)
// Wait 1 second
await wait(1)
}
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});