@@ -147,105 +147,215 @@ pub fn get_test_channel(token_address: Address) -> Channel {
147
147
}
148
148
}
149
149
150
- pub async fn mock_set_balance (
151
- token_contract : & Contract < Http > ,
152
- from : [ u8 ; 20 ] ,
153
- address : [ u8 ; 20 ] ,
154
- amount : & BigNum ,
155
- ) -> web3:: contract:: Result < H256 > {
156
- let amount = U256 :: from_dec_str ( & amount. to_string ( ) ) . expect ( "Should create U256" ) ;
157
-
158
- token_contract
159
- . call (
160
- "setBalanceTo" ,
161
- ( H160 ( address) , amount) ,
162
- H160 ( from) ,
163
- ContractOptions :: default ( ) ,
164
- )
165
- . await
150
+ /// The Sweeper contract
151
+ ///
152
+ /// Initialized and ready for calling contract with [`Web3<Http>`].
153
+ #[ derive( Debug , Clone ) ]
154
+ pub struct Sweeper {
155
+ pub contract : Contract < Http > ,
156
+ pub address : Address ,
166
157
}
167
158
168
- pub async fn outpace_deposit (
169
- outpace_contract : & Contract < Http > ,
170
- channel : & Channel ,
171
- to : [ u8 ; 20 ] ,
172
- amount : & BigNum ,
173
- ) -> web3:: contract:: Result < H256 > {
174
- let amount = U256 :: from_dec_str ( & amount. to_string ( ) ) . expect ( "Should create U256" ) ;
175
-
176
- outpace_contract
177
- . call (
178
- "deposit" ,
179
- ( channel. tokenize ( ) , H160 ( to) , amount) ,
180
- H160 ( to) ,
181
- ContractOptions :: with ( |opt| {
159
+ impl Sweeper {
160
+ pub fn new ( web3 : & Web3 < Http > , sweeper_address : Address ) -> Self {
161
+ let sweeper_contract =
162
+ Contract :: from_json ( web3. eth ( ) , H160 ( sweeper_address. to_bytes ( ) ) , & SWEEPER_ABI )
163
+ . expect ( "Failed to init Sweeper contract from JSON ABI!" ) ;
164
+
165
+ Self {
166
+ address : sweeper_address,
167
+ contract : sweeper_contract,
168
+ }
169
+ }
170
+
171
+ /// Deploys the Sweeper contract from [`LEADER`]
172
+ pub async fn deploy ( web3 : & Web3 < Http > ) -> web3:: contract:: Result < Self > {
173
+ let contract = Contract :: deploy ( web3. eth ( ) , & SWEEPER_ABI )
174
+ . expect ( "Invalid ABI of Sweeper contract" )
175
+ . confirmations ( 0 )
176
+ . options ( ContractOptions :: with ( |opt| {
182
177
opt. gas_price = Some ( 1 . into ( ) ) ;
183
178
opt. gas = Some ( 6_721_975 . into ( ) ) ;
184
- } ) ,
185
- )
186
- . await
179
+ } ) )
180
+ . execute ( * SWEEPER_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
181
+ . await ?;
182
+
183
+ let sweeper_address = Address :: from ( contract. address ( ) . to_fixed_bytes ( ) ) ;
184
+
185
+ Ok ( Self {
186
+ contract,
187
+ address : sweeper_address,
188
+ } )
189
+ }
190
+
191
+ pub async fn sweep (
192
+ & self ,
193
+ outpace_address : [ u8 ; 20 ] ,
194
+ channel : & Channel ,
195
+ depositor : [ u8 ; 20 ] ,
196
+ ) -> web3:: contract:: Result < H256 > {
197
+ let from_leader_account = H160 ( * LEADER . as_bytes ( ) ) ;
198
+
199
+ self . contract
200
+ . call (
201
+ "sweep" ,
202
+ (
203
+ Token :: Address ( H160 ( outpace_address) ) ,
204
+ channel. tokenize ( ) ,
205
+ Token :: Array ( vec ! [ Token :: Address ( H160 ( depositor) ) ] ) ,
206
+ ) ,
207
+ from_leader_account,
208
+ ContractOptions :: with ( |opt| {
209
+ opt. gas_price = Some ( 1 . into ( ) ) ;
210
+ opt. gas = Some ( 6_721_975 . into ( ) ) ;
211
+ } ) ,
212
+ )
213
+ . await
214
+ }
215
+ }
216
+ /// The Mocked token API contract
217
+ ///
218
+ /// Used for mocking the tokens of an address.
219
+ ///
220
+ /// Initialized and ready for calling contract with [`Web3<Http>`].
221
+ ///
222
+ /// Mocked ABI: [`MOCK_TOKEN_ABI`]
223
+ ///
224
+ /// Real ABI: [`crate::ethereum::ERC20_ABI`]
225
+ #[ derive( Debug , Clone ) ]
226
+ pub struct Erc20Token {
227
+ pub web3 : Web3 < Http > ,
228
+ pub info : TokenInfo ,
229
+ pub contract : Contract < Http > ,
187
230
}
188
231
189
- pub async fn sweeper_sweep (
190
- sweeper_contract : & Contract < Http > ,
191
- outpace_address : [ u8 ; 20 ] ,
192
- channel : & Channel ,
193
- depositor : [ u8 ; 20 ] ,
194
- ) -> web3:: contract:: Result < H256 > {
195
- let from_leader_account = H160 ( * LEADER . as_bytes ( ) ) ;
196
-
197
- sweeper_contract
198
- . call (
199
- "sweep" ,
200
- (
201
- Token :: Address ( H160 ( outpace_address) ) ,
202
- channel. tokenize ( ) ,
203
- Token :: Array ( vec ! [ Token :: Address ( H160 ( depositor) ) ] ) ,
204
- ) ,
205
- from_leader_account,
206
- ContractOptions :: with ( |opt| {
207
- opt. gas_price = Some ( 1 . into ( ) ) ;
208
- opt. gas = Some ( 6_721_975 . into ( ) ) ;
209
- } ) ,
232
+ impl Erc20Token {
233
+ /// Presumes a default TokenInfo
234
+ pub fn new ( web3 : & Web3 < Http > , token_info : TokenInfo ) -> Self {
235
+ let token_contract = Contract :: from_json (
236
+ web3. eth ( ) ,
237
+ token_info. address . as_bytes ( ) . into ( ) ,
238
+ & MOCK_TOKEN_ABI ,
210
239
)
211
- . await
212
- }
240
+ . expect ( "Failed to init Outpace contract from JSON ABI!" ) ;
213
241
214
- /// Deploys the Sweeper contract from [`LEADER`]
215
- pub async fn deploy_sweeper_contract (
216
- web3 : & Web3 < Http > ,
217
- ) -> web3:: contract:: Result < ( Address , Contract < Http > ) > {
218
- let sweeper_contract = Contract :: deploy ( web3. eth ( ) , & SWEEPER_ABI )
219
- . expect ( "Invalid ABI of Sweeper contract" )
220
- . confirmations ( 0 )
221
- . options ( ContractOptions :: with ( |opt| {
222
- opt. gas_price = Some ( 1 . into ( ) ) ;
223
- opt. gas = Some ( 6_721_975 . into ( ) ) ;
224
- } ) )
225
- . execute ( * SWEEPER_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
226
- . await ?;
242
+ Self {
243
+ web3 : web3. clone ( ) ,
244
+ info : token_info,
245
+ contract : token_contract,
246
+ }
247
+ }
248
+
249
+ /// Deploys the Mock Token contract from [`LEADER`]
250
+ pub async fn deploy ( web3 : & Web3 < Http > , min_token_units : u64 ) -> web3:: contract:: Result < Self > {
251
+ let token_contract = Contract :: deploy ( web3. eth ( ) , & MOCK_TOKEN_ABI )
252
+ . expect ( "Invalid ABI of Mock Token contract" )
253
+ . confirmations ( 0 )
254
+ . options ( ContractOptions :: with ( |opt| {
255
+ opt. gas_price = Some ( 1_i32 . into ( ) ) ;
256
+ opt. gas = Some ( 6_721_975_i32 . into ( ) ) ;
257
+ } ) )
258
+ . execute ( * MOCK_TOKEN_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
259
+ . await ?;
260
+
261
+ let token_address = Address :: from ( token_contract. address ( ) . to_fixed_bytes ( ) ) ;
262
+
263
+ let token_info = TokenInfo {
264
+ min_token_units_for_deposit : BigNum :: from ( min_token_units) ,
265
+ precision : NonZeroU8 :: new ( 18 ) . expect ( "should create NonZeroU8" ) ,
266
+ // 0.000_001
267
+ min_validator_fee : BigNum :: from ( 1_000_000_000_000 ) ,
268
+ address : token_address,
269
+ } ;
270
+
271
+ Ok ( Self {
272
+ web3 : web3. clone ( ) ,
273
+ info : token_info,
274
+ contract : token_contract,
275
+ } )
276
+ }
227
277
228
- let sweeper_address = Address :: from ( sweeper_contract. address ( ) . to_fixed_bytes ( ) ) ;
278
+ /// Set Mocked token balance
279
+ pub async fn set_balance (
280
+ & self ,
281
+ from : [ u8 ; 20 ] ,
282
+ address : [ u8 ; 20 ] ,
283
+ amount : & BigNum ,
284
+ ) -> web3:: contract:: Result < H256 > {
285
+ let amount = U256 :: from_dec_str ( & amount. to_string ( ) ) . expect ( "Should create U256" ) ;
286
+
287
+ self . contract
288
+ . call (
289
+ "setBalanceTo" ,
290
+ ( H160 ( address) , amount) ,
291
+ H160 ( from) ,
292
+ ContractOptions :: default ( ) ,
293
+ )
294
+ . await
295
+ }
296
+ }
229
297
230
- Ok ( ( sweeper_address, sweeper_contract) )
298
+ /// The Outpace contract
299
+ ///
300
+ /// Initialized and ready for calling contract with [`Web3<Http>`].
301
+ #[ derive( Debug , Clone ) ]
302
+ pub struct Outpace {
303
+ pub contract : Contract < Http > ,
304
+ pub address : Address ,
231
305
}
232
306
233
- /// Deploys the Outpace contract from [`LEADER`]
234
- pub async fn deploy_outpace_contract (
235
- web3 : & Web3 < Http > ,
236
- ) -> web3:: contract:: Result < ( Address , Contract < Http > ) > {
237
- let outpace_contract = Contract :: deploy ( web3. eth ( ) , & OUTPACE_ABI )
238
- . expect ( "Invalid ABI of Outpace contract" )
239
- . confirmations ( 0 )
240
- . options ( ContractOptions :: with ( |opt| {
241
- opt. gas_price = Some ( 1 . into ( ) ) ;
242
- opt. gas = Some ( 6_721_975 . into ( ) ) ;
243
- } ) )
244
- . execute ( * OUTPACE_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
245
- . await ?;
246
- let outpace_address = Address :: from ( outpace_contract. address ( ) . to_fixed_bytes ( ) ) ;
307
+ impl Outpace {
308
+ pub fn new ( web3 : & Web3 < Http > , outpace_address : Address ) -> Self {
309
+ let outpace_contract =
310
+ Contract :: from_json ( web3. eth ( ) , outpace_address. as_bytes ( ) . into ( ) , & OUTPACE_ABI )
311
+ . expect ( "Failed to init Outpace contract from JSON ABI!" ) ;
312
+
313
+ Self {
314
+ address : outpace_address,
315
+ contract : outpace_contract,
316
+ }
317
+ }
318
+
319
+ /// Deploys the Outpace contract from [`LEADER`]
320
+ pub async fn deploy ( web3 : & Web3 < Http > ) -> web3:: contract:: Result < Self > {
321
+ let outpace_contract = Contract :: deploy ( web3. eth ( ) , & OUTPACE_ABI )
322
+ . expect ( "Invalid ABI of Outpace contract" )
323
+ . confirmations ( 0 )
324
+ . options ( ContractOptions :: with ( |opt| {
325
+ opt. gas_price = Some ( 1 . into ( ) ) ;
326
+ opt. gas = Some ( 6_721_975 . into ( ) ) ;
327
+ } ) )
328
+ . execute ( * OUTPACE_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
329
+ . await ?;
330
+
331
+ let outpace_address = Address :: from ( outpace_contract. address ( ) . to_fixed_bytes ( ) ) ;
332
+
333
+ Ok ( Self {
334
+ address : outpace_address,
335
+ contract : outpace_contract,
336
+ } )
337
+ }
247
338
248
- Ok ( ( outpace_address, outpace_contract) )
339
+ pub async fn deposit (
340
+ & self ,
341
+ channel : & Channel ,
342
+ to : [ u8 ; 20 ] ,
343
+ amount : & BigNum ,
344
+ ) -> web3:: contract:: Result < H256 > {
345
+ let amount = U256 :: from_dec_str ( & amount. to_string ( ) ) . expect ( "Should create U256" ) ;
346
+
347
+ self . contract
348
+ . call (
349
+ "deposit" ,
350
+ ( channel. tokenize ( ) , H160 ( to) , amount) ,
351
+ H160 ( to) ,
352
+ ContractOptions :: with ( |opt| {
353
+ opt. gas_price = Some ( 1 . into ( ) ) ;
354
+ opt. gas = Some ( 6_721_975 . into ( ) ) ;
355
+ } ) ,
356
+ )
357
+ . await
358
+ }
249
359
}
250
360
251
361
/// Deploys the Identity contract for the give `for_address`
@@ -278,31 +388,3 @@ pub async fn deploy_identity_contract(
278
388
279
389
Ok ( ( identity_address, identity_contract) )
280
390
}
281
-
282
- /// Deploys the Mock Token contract from [`LEADER`]
283
- pub async fn deploy_token_contract (
284
- web3 : & Web3 < Http > ,
285
- min_token_units : u64 ,
286
- ) -> web3:: contract:: Result < ( TokenInfo , Address , Contract < Http > ) > {
287
- let token_contract = Contract :: deploy ( web3. eth ( ) , & MOCK_TOKEN_ABI )
288
- . expect ( "Invalid ABI of Mock Token contract" )
289
- . confirmations ( 0 )
290
- . options ( ContractOptions :: with ( |opt| {
291
- opt. gas_price = Some ( 1_i32 . into ( ) ) ;
292
- opt. gas = Some ( 6_721_975_i32 . into ( ) ) ;
293
- } ) )
294
- . execute ( * MOCK_TOKEN_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
295
- . await ?;
296
-
297
- let token_address = Address :: from ( token_contract. address ( ) . to_fixed_bytes ( ) ) ;
298
-
299
- let token_info = TokenInfo {
300
- min_token_units_for_deposit : BigNum :: from ( min_token_units) ,
301
- precision : NonZeroU8 :: new ( 18 ) . expect ( "should create NonZeroU8" ) ,
302
- // 0.000_001
303
- min_validator_fee : BigNum :: from ( 1_000_000_000_000 ) ,
304
- address : token_address,
305
- } ;
306
-
307
- Ok ( ( token_info, token_address, token_contract) )
308
- }
0 commit comments