forked from smartcontractkit/truffle-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyContract_test.js
236 lines (210 loc) · 6.71 KB
/
MyContract_test.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
/* eslint-disable @typescript-eslint/no-var-requires */
const { oracle } = require('@chainlink/test-helpers')
const { expectRevert, time } = require('@openzeppelin/test-helpers')
contract('MyContract', accounts => {
const { LinkToken } = require('@chainlink/contracts/truffle/v0.4/LinkToken')
const { Oracle } = require('@chainlink/contracts/truffle/v0.6/Oracle')
const MyContract = artifacts.require('MyContract')
const defaultAccount = accounts[0]
const oracleNode = accounts[1]
const stranger = accounts[2]
const consumer = accounts[3]
// These parameters are used to validate the data was received
// on the deployed oracle contract. The Job ID only represents
// the type of data, but will not work on a public testnet.
// For the latest JobIDs, visit a node listing service like:
// https://market.link/
const jobId = web3.utils.toHex('4c7b7ffb66b344fbaa64995af81e355a')
const url =
'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,JPY'
const path = 'USD'
const times = 100
// Represents 1 LINK for testnet requests
const payment = web3.utils.toWei('1')
let link, oc, cc
beforeEach(async () => {
link = await LinkToken.new({ from: defaultAccount })
oc = await Oracle.new(link.address, { from: defaultAccount })
cc = await MyContract.new(link.address, { from: consumer })
await oc.setFulfillmentPermission(oracleNode, true, {
from: defaultAccount,
})
})
describe('#createRequest', () => {
context('without LINK', () => {
it('reverts', async () => {
await expectRevert.unspecified(
cc.createRequestTo(oc.address, jobId, payment, url, path, times, {
from: consumer,
}),
)
})
})
context('with LINK', () => {
let request
beforeEach(async () => {
await link.transfer(cc.address, web3.utils.toWei('1', 'ether'), {
from: defaultAccount,
})
})
context('sending a request to a specific oracle contract address', () => {
it('triggers a log event in the new Oracle contract', async () => {
const tx = await cc.createRequestTo(
oc.address,
jobId,
payment,
url,
path,
times,
{ from: consumer },
)
request = oracle.decodeRunRequest(tx.receipt.rawLogs[3])
assert.equal(oc.address, tx.receipt.rawLogs[3].address)
assert.equal(
request.topic,
web3.utils.keccak256(
'OracleRequest(bytes32,address,bytes32,uint256,address,bytes4,uint256,uint256,bytes)',
),
)
})
})
})
})
describe('#fulfill', () => {
const expected = 50000
const response = web3.utils.padLeft(web3.utils.toHex(expected), 64)
let request
beforeEach(async () => {
await link.transfer(cc.address, web3.utils.toWei('1', 'ether'), {
from: defaultAccount,
})
const tx = await cc.createRequestTo(
oc.address,
jobId,
payment,
url,
path,
times,
{ from: consumer },
)
request = oracle.decodeRunRequest(tx.receipt.rawLogs[3])
await oc.fulfillOracleRequest(
...oracle.convertFufillParams(request, response, {
from: oracleNode,
gas: 500000,
}),
)
})
it('records the data given to it by the oracle', async () => {
const currentPrice = await cc.data.call()
assert.equal(
web3.utils.padLeft(web3.utils.toHex(currentPrice), 64),
web3.utils.padLeft(expected, 64),
)
})
context('when my contract does not recognize the request ID', () => {
const otherId = web3.utils.toHex('otherId')
beforeEach(async () => {
request.id = otherId
})
it('does not accept the data provided', async () => {
await expectRevert.unspecified(
oc.fulfillOracleRequest(
...oracle.convertFufillParams(request, response, {
from: oracleNode,
}),
),
)
})
})
context('when called by anyone other than the oracle contract', () => {
it('does not accept the data provided', async () => {
await expectRevert.unspecified(
cc.fulfill(request.requestId, response, { from: stranger }),
)
})
})
})
describe('#cancelRequest', () => {
let request
beforeEach(async () => {
await link.transfer(cc.address, web3.utils.toWei('1', 'ether'), {
from: defaultAccount,
})
const tx = await cc.createRequestTo(
oc.address,
jobId,
payment,
url,
path,
times,
{ from: consumer },
)
request = oracle.decodeRunRequest(tx.receipt.rawLogs[3])
})
context('before the expiration time', () => {
it('cannot cancel a request', async () => {
await expectRevert(
cc.cancelRequest(
request.requestId,
request.payment,
request.callbackFunc,
request.expiration,
{ from: consumer },
),
'Request is not expired',
)
})
})
context('after the expiration time', () => {
beforeEach(async () => {
await time.increase(300)
})
context('when called by a non-owner', () => {
it('cannot cancel a request', async () => {
await expectRevert.unspecified(
cc.cancelRequest(
request.requestId,
request.payment,
request.callbackFunc,
request.expiration,
{ from: stranger },
),
)
})
})
context('when called by an owner', () => {
it('can cancel a request', async () => {
await cc.cancelRequest(
request.requestId,
request.payment,
request.callbackFunc,
request.expiration,
{ from: consumer },
)
})
})
})
})
describe('#withdrawLink', () => {
beforeEach(async () => {
await link.transfer(cc.address, web3.utils.toWei('1', 'ether'), {
from: defaultAccount,
})
})
context('when called by a non-owner', () => {
it('cannot withdraw', async () => {
await expectRevert.unspecified(cc.withdrawLink({ from: stranger }))
})
})
context('when called by the owner', () => {
it('transfers LINK to the owner', async () => {
const beforeBalance = await link.balanceOf(consumer)
assert.equal(beforeBalance, '0')
await cc.withdrawLink({ from: consumer })
const afterBalance = await link.balanceOf(consumer)
assert.equal(afterBalance, web3.utils.toWei('1', 'ether'))
})
})
})
})