forked from findonflow/find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.go
275 lines (233 loc) · 7.96 KB
/
test_utils.go
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
package test_main
import (
"fmt"
"strconv"
"testing"
"github.com/bjartek/overflow/overflow"
"github.com/stretchr/testify/assert"
)
type OverflowTestUtils struct {
T *testing.T
O *overflow.Overflow
}
func NewOverflowTest(t *testing.T) *OverflowTestUtils {
return &OverflowTestUtils{T: t, O: overflow.NewTestingEmulator().Start()}
}
const leaseDurationFloat = 31536000.0
const lockDurationFloat = 7776000.0
const auctionDurationFloat = 86400.0
func (otu *OverflowTestUtils) assertLookupAddress(user, expected string) {
value := otu.O.Script(`import FIND from "../contracts/FIND.cdc"
pub fun main(name: String) : Address? {
return FIND.lookupAddress(name)
}
`).
Args(otu.O.Arguments().String(user)).RunReturnsInterface()
assert.Equal(otu.T, expected, value)
}
func (otu *OverflowTestUtils) setupFIND() *OverflowTestUtils {
//first step create the adminClient as the fin user
otu.O.TransactionFromFile("setup_fin_1_create_client").
SignProposeAndPayAs("find").
Test(otu.T).AssertSuccess().AssertNoEvents()
//link in the server in the versus client
otu.O.TransactionFromFile("setup_fin_2_register_client").
SignProposeAndPayAsService().
Args(otu.O.Arguments().Account("find")).
Test(otu.T).AssertSuccess().AssertNoEvents()
//set up fin network as the fin user
otu.O.TransactionFromFile("setup_fin_3_create_network").
SignProposeAndPayAs("find").
Test(otu.T).AssertSuccess().AssertNoEvents()
return otu.tickClock(1.0)
}
func (otu *OverflowTestUtils) tickClock(time float64) *OverflowTestUtils {
otu.O.TransactionFromFile("clock").SignProposeAndPayAs("find").
Args(otu.O.Arguments().
UFix64(time)).
Test(otu.T).AssertSuccess()
return otu
}
func (otu *OverflowTestUtils) createUser(fusd float64, name string) *OverflowTestUtils {
otu.O.TransactionFromFile("createProfile").
SignProposeAndPayAs(name).
Args(otu.O.Arguments().String(name)).
Test(otu.T).
AssertSuccess()
otu.O.TransactionFromFile("mintFusd").
SignProposeAndPayAsService().
Args(otu.O.Arguments().
Account(name).
UFix64(fusd)).
Test(otu.T).
AssertSuccess().
AssertEventCount(3)
otu.O.TransactionFromFile("mintFlow").
SignProposeAndPayAsService().
Args(otu.O.Arguments().
Account(name).
UFix64(fusd)).
Test(otu.T).
AssertSuccess().
AssertEventCount(3)
return otu
}
func (otu *OverflowTestUtils) registerUser(name string) *OverflowTestUtils {
otu.registerUserTransaction(name)
return otu
}
func (otu *OverflowTestUtils) registerUserTransaction(name string) overflow.TransactionResult {
nameAddress := otu.accountAddress(name)
expireTime := otu.currentTime() + leaseDurationFloat
expireTimeString := fmt.Sprintf("%f00", expireTime)
lockedTime := otu.currentTime() + leaseDurationFloat + lockDurationFloat
lockedTimeString := fmt.Sprintf("%f00", lockedTime)
return otu.O.TransactionFromFile("register").
SignProposeAndPayAs(name).
Args(otu.O.Arguments().
String(name).
UFix64(5.0)).
Test(otu.T).
AssertSuccess().
AssertEmitEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.FIND.Register", map[string]interface{}{
"validUntil": expireTimeString,
"lockedUntil": lockedTimeString,
"owner": nameAddress,
"name": name,
})).
AssertEmitEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.FUSD.TokensDeposited", map[string]interface{}{
"amount": "5.00000000",
"to": "0x1cf0e2f2f715450",
})).
AssertEmitEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.FUSD.TokensWithdrawn", map[string]interface{}{
"amount": "5.00000000",
"from": nameAddress,
}))
}
func (out *OverflowTestUtils) currentTime() float64 {
value, err := out.O.Script(`import Clock from "../contracts/Clock.cdc"
pub fun main() : UFix64 {
return Clock.time()
}`).RunReturns()
assert.NoErrorf(out.T, err, "Could not execute script")
currentTime := value.String()
res, err := strconv.ParseFloat(currentTime, 64)
assert.NoErrorf(out.T, err, "Could not parse as float")
return res
}
func (otu *OverflowTestUtils) accountAddress(name string) string {
return fmt.Sprintf("0x%s", otu.O.Account(name).Address().String())
}
func (otu *OverflowTestUtils) listForSale(name string) *OverflowTestUtils {
otu.O.TransactionFromFile("listForSale").
SignProposeAndPayAs(name).
Args(otu.O.Arguments().
String(name).
UFix64(10.0)).
Test(otu.T).AssertSuccess().
AssertPartialEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.FIND.ForSale", map[string]interface{}{
"directSellPrice": "10.00000000",
"active": "true",
"name": name,
"owner": otu.accountAddress(name),
}))
return otu
}
func (otu *OverflowTestUtils) directOffer(buyer, name string, amount float64) *OverflowTestUtils {
otu.O.TransactionFromFile("bid").SignProposeAndPayAs(buyer).
Args(otu.O.Arguments().
String(name).
UFix64(amount)).
Test(otu.T).
AssertSuccess().
AssertPartialEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.FIND.DirectOffer", map[string]interface{}{
"amount": fmt.Sprintf("%.8f", amount),
"bidder": otu.accountAddress(buyer),
"name": name,
}))
return otu
}
func (otu *OverflowTestUtils) listForAuction(name string) *OverflowTestUtils {
otu.O.TransactionFromFile("listForAuction").
SignProposeAndPayAs(name).
Args(otu.O.Arguments().
String(name).
UFix64(5.0). //startAuctionPrice
UFix64(20.0). //reserve price
UFix64(auctionDurationFloat).
UFix64(300.0)). //extention on late bid
Test(otu.T).AssertSuccess().
AssertPartialEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.FIND.ForAuction", map[string]interface{}{
"auctionStartPrice": "5.00000000",
"auctionReservePrice": "20.00000000",
"active": "true",
"name": name,
"owner": otu.accountAddress(name),
}))
return otu
}
func (otu *OverflowTestUtils) bid(buyer, name string, amount float64) *OverflowTestUtils {
endTime := otu.currentTime() + auctionDurationFloat
endTimeSting := fmt.Sprintf("%f00", endTime)
otu.O.TransactionFromFile("bid").SignProposeAndPayAs(buyer).
Args(otu.O.Arguments().
String(name).
UFix64(amount)).
Test(otu.T).
AssertSuccess().
AssertPartialEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.FIND.AuctionStarted", map[string]interface{}{
"amount": fmt.Sprintf("%.8f", amount),
"auctionEndAt": endTimeSting,
"bidder": otu.accountAddress(buyer),
"name": name,
}))
return otu
}
func (otu *OverflowTestUtils) auctionBid(buyer, name string, amount float64) *OverflowTestUtils {
endTime := otu.currentTime() + auctionDurationFloat
endTimeSting := fmt.Sprintf("%f00", endTime)
otu.O.TransactionFromFile("bid").SignProposeAndPayAs(buyer).
Args(otu.O.Arguments().
String(name).
UFix64(amount)).
Test(otu.T).
AssertSuccess().
AssertPartialEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.FIND.AuctionBid", map[string]interface{}{
"amount": fmt.Sprintf("%.8f", amount),
"auctionEndAt": endTimeSting,
"bidder": otu.accountAddress(buyer),
"name": name,
}))
return otu
}
func (otu *OverflowTestUtils) expireAuction() *OverflowTestUtils {
return otu.tickClock(auctionDurationFloat)
}
func (otu *OverflowTestUtils) expireLease() *OverflowTestUtils {
return otu.tickClock(leaseDurationFloat)
}
func (otu *OverflowTestUtils) expireLock() *OverflowTestUtils {
return otu.tickClock(lockDurationFloat)
}
func (otu *OverflowTestUtils) setupCharity(user string) *OverflowTestUtils {
otu.O.TransactionFromFile("createCharity").SignProposeAndPayAs(user).
Test(otu.T).
AssertSuccess()
return otu
}
func (otu *OverflowTestUtils) mintCharity(name, image, thumbnail, originUrl, description, user string) *OverflowTestUtils {
otu.O.TransactionFromFile("mintCharity").SignProposeAndPayAs("find").
Args(otu.O.Arguments().
String(name).
String(image).
String(thumbnail).
String(description).
String(originUrl).
Account(user)).
Test(otu.T).
AssertSuccess().
AssertPartialEvent(overflow.NewTestEvent("A.f8d6e0586b0a20c7.CharityNFT.Minted", map[string]interface{}{
"to": otu.accountAddress(user),
}))
return otu
}