@@ -11,7 +11,9 @@ use web3::{
11
11
use primitives:: {
12
12
channel:: { Channel , Nonce } ,
13
13
config:: { ChainInfo , TokenInfo , GANACHE_CONFIG } ,
14
- test_util:: { ADVERTISER , CREATOR , FOLLOWER , GUARDIAN , GUARDIAN_2 , LEADER , PUBLISHER } ,
14
+ test_util:: {
15
+ ADVERTISER , ADVERTISER_2 , CREATOR , FOLLOWER , GUARDIAN , GUARDIAN_2 , LEADER , PUBLISHER ,
16
+ } ,
15
17
Address , BigNum , Chain , ValidatorId ,
16
18
} ;
17
19
@@ -83,6 +85,15 @@ pub static KEYSTORES: Lazy<HashMap<Address, Options>> = Lazy::new(|| {
83
85
* GUARDIAN_2 ,
84
86
keystore_options( & format!( "{}_keystore.json" , * GUARDIAN_2 ) , "ganache6" ) ,
85
87
) ,
88
+ // Address 7
89
+ // (
90
+ // *PUBLISHER_2,
91
+ // keystore_options(&format!("{}_keystore.json", *PUBLISHER_2), "ganache7"),
92
+ // ),
93
+ (
94
+ * ADVERTISER_2 ,
95
+ keystore_options( & format!( "{}_keystore.json" , * ADVERTISER_2 ) , "ganache8" ) ,
96
+ ) ,
86
97
]
87
98
. into_iter ( )
88
99
. collect ( )
@@ -147,105 +158,215 @@ pub fn get_test_channel(token_address: Address) -> Channel {
147
158
}
148
159
}
149
160
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
161
+ /// The Sweeper contract
162
+ ///
163
+ /// Initialized and ready for calling contract with [`Web3<Http>`].
164
+ #[ derive( Debug , Clone ) ]
165
+ pub struct Sweeper {
166
+ pub contract : Contract < Http > ,
167
+ pub address : Address ,
166
168
}
167
169
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| {
170
+ impl Sweeper {
171
+ pub fn new ( web3 : & Web3 < Http > , sweeper_address : Address ) -> Self {
172
+ let sweeper_contract =
173
+ Contract :: from_json ( web3. eth ( ) , H160 ( sweeper_address. to_bytes ( ) ) , & SWEEPER_ABI )
174
+ . expect ( "Failed to init Sweeper contract from JSON ABI!" ) ;
175
+
176
+ Self {
177
+ address : sweeper_address,
178
+ contract : sweeper_contract,
179
+ }
180
+ }
181
+
182
+ /// Deploys the Sweeper contract from [`LEADER`]
183
+ pub async fn deploy ( web3 : & Web3 < Http > ) -> web3:: contract:: Result < Self > {
184
+ let contract = Contract :: deploy ( web3. eth ( ) , & SWEEPER_ABI )
185
+ . expect ( "Invalid ABI of Sweeper contract" )
186
+ . confirmations ( 0 )
187
+ . options ( ContractOptions :: with ( |opt| {
182
188
opt. gas_price = Some ( 1 . into ( ) ) ;
183
189
opt. gas = Some ( 6_721_975 . into ( ) ) ;
184
- } ) ,
185
- )
186
- . await
190
+ } ) )
191
+ . execute ( * SWEEPER_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
192
+ . await ?;
193
+
194
+ let sweeper_address = Address :: from ( contract. address ( ) . to_fixed_bytes ( ) ) ;
195
+
196
+ Ok ( Self {
197
+ contract,
198
+ address : sweeper_address,
199
+ } )
200
+ }
201
+
202
+ pub async fn sweep (
203
+ & self ,
204
+ outpace_address : [ u8 ; 20 ] ,
205
+ channel : & Channel ,
206
+ depositor : [ u8 ; 20 ] ,
207
+ ) -> web3:: contract:: Result < H256 > {
208
+ let from_leader_account = H160 ( * LEADER . as_bytes ( ) ) ;
209
+
210
+ self . contract
211
+ . call (
212
+ "sweep" ,
213
+ (
214
+ Token :: Address ( H160 ( outpace_address) ) ,
215
+ channel. tokenize ( ) ,
216
+ Token :: Array ( vec ! [ Token :: Address ( H160 ( depositor) ) ] ) ,
217
+ ) ,
218
+ from_leader_account,
219
+ ContractOptions :: with ( |opt| {
220
+ opt. gas_price = Some ( 1 . into ( ) ) ;
221
+ opt. gas = Some ( 6_721_975 . into ( ) ) ;
222
+ } ) ,
223
+ )
224
+ . await
225
+ }
226
+ }
227
+ /// The Mocked token API contract
228
+ ///
229
+ /// Used for mocking the tokens of an address.
230
+ ///
231
+ /// Initialized and ready for calling contract with [`Web3<Http>`].
232
+ ///
233
+ /// Mocked ABI: [`MOCK_TOKEN_ABI`]
234
+ ///
235
+ /// Real ABI: [`crate::ethereum::ERC20_ABI`]
236
+ #[ derive( Debug , Clone ) ]
237
+ pub struct Erc20Token {
238
+ pub web3 : Web3 < Http > ,
239
+ pub info : TokenInfo ,
240
+ pub contract : Contract < Http > ,
187
241
}
188
242
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
- } ) ,
243
+ impl Erc20Token {
244
+ /// Presumes a default TokenInfo
245
+ pub fn new ( web3 : & Web3 < Http > , token_info : TokenInfo ) -> Self {
246
+ let token_contract = Contract :: from_json (
247
+ web3. eth ( ) ,
248
+ token_info. address . as_bytes ( ) . into ( ) ,
249
+ & MOCK_TOKEN_ABI ,
210
250
)
211
- . await
212
- }
251
+ . expect ( "Failed to init Outpace contract from JSON ABI!" ) ;
213
252
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 ?;
253
+ Self {
254
+ web3 : web3. clone ( ) ,
255
+ info : token_info,
256
+ contract : token_contract,
257
+ }
258
+ }
259
+
260
+ /// Deploys the Mock Token contract from [`LEADER`]
261
+ pub async fn deploy ( web3 : & Web3 < Http > , min_token_units : u64 ) -> web3:: contract:: Result < Self > {
262
+ let token_contract = Contract :: deploy ( web3. eth ( ) , & MOCK_TOKEN_ABI )
263
+ . expect ( "Invalid ABI of Mock Token contract" )
264
+ . confirmations ( 0 )
265
+ . options ( ContractOptions :: with ( |opt| {
266
+ opt. gas_price = Some ( 1_i32 . into ( ) ) ;
267
+ opt. gas = Some ( 6_721_975_i32 . into ( ) ) ;
268
+ } ) )
269
+ . execute ( * MOCK_TOKEN_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
270
+ . await ?;
271
+
272
+ let token_address = Address :: from ( token_contract. address ( ) . to_fixed_bytes ( ) ) ;
273
+
274
+ let token_info = TokenInfo {
275
+ min_token_units_for_deposit : BigNum :: from ( min_token_units) ,
276
+ precision : NonZeroU8 :: new ( 18 ) . expect ( "should create NonZeroU8" ) ,
277
+ // 0.000_001
278
+ min_validator_fee : BigNum :: from ( 1_000_000_000_000 ) ,
279
+ address : token_address,
280
+ } ;
281
+
282
+ Ok ( Self {
283
+ web3 : web3. clone ( ) ,
284
+ info : token_info,
285
+ contract : token_contract,
286
+ } )
287
+ }
227
288
228
- let sweeper_address = Address :: from ( sweeper_contract. address ( ) . to_fixed_bytes ( ) ) ;
289
+ /// Set Mocked token balance
290
+ pub async fn set_balance (
291
+ & self ,
292
+ from : [ u8 ; 20 ] ,
293
+ address : [ u8 ; 20 ] ,
294
+ amount : & BigNum ,
295
+ ) -> web3:: contract:: Result < H256 > {
296
+ let amount = U256 :: from_dec_str ( & amount. to_string ( ) ) . expect ( "Should create U256" ) ;
297
+
298
+ self . contract
299
+ . call (
300
+ "setBalanceTo" ,
301
+ ( H160 ( address) , amount) ,
302
+ H160 ( from) ,
303
+ ContractOptions :: default ( ) ,
304
+ )
305
+ . await
306
+ }
307
+ }
229
308
230
- Ok ( ( sweeper_address, sweeper_contract) )
309
+ /// The Outpace contract
310
+ ///
311
+ /// Initialized and ready for calling contract with [`Web3<Http>`].
312
+ #[ derive( Debug , Clone ) ]
313
+ pub struct Outpace {
314
+ pub contract : Contract < Http > ,
315
+ pub address : Address ,
231
316
}
232
317
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 ( ) ) ;
318
+ impl Outpace {
319
+ pub fn new ( web3 : & Web3 < Http > , outpace_address : Address ) -> Self {
320
+ let outpace_contract =
321
+ Contract :: from_json ( web3. eth ( ) , outpace_address. as_bytes ( ) . into ( ) , & OUTPACE_ABI )
322
+ . expect ( "Failed to init Outpace contract from JSON ABI!" ) ;
247
323
248
- Ok ( ( outpace_address, outpace_contract) )
324
+ Self {
325
+ address : outpace_address,
326
+ contract : outpace_contract,
327
+ }
328
+ }
329
+
330
+ /// Deploys the Outpace contract from [`LEADER`]
331
+ pub async fn deploy ( web3 : & Web3 < Http > ) -> web3:: contract:: Result < Self > {
332
+ let outpace_contract = Contract :: deploy ( web3. eth ( ) , & OUTPACE_ABI )
333
+ . expect ( "Invalid ABI of Outpace contract" )
334
+ . confirmations ( 0 )
335
+ . options ( ContractOptions :: with ( |opt| {
336
+ opt. gas_price = Some ( 1 . into ( ) ) ;
337
+ opt. gas = Some ( 6_721_975 . into ( ) ) ;
338
+ } ) )
339
+ . execute ( * OUTPACE_BYTECODE , ( ) , H160 ( LEADER . to_bytes ( ) ) )
340
+ . await ?;
341
+
342
+ let outpace_address = Address :: from ( outpace_contract. address ( ) . to_fixed_bytes ( ) ) ;
343
+
344
+ Ok ( Self {
345
+ address : outpace_address,
346
+ contract : outpace_contract,
347
+ } )
348
+ }
349
+
350
+ pub async fn deposit (
351
+ & self ,
352
+ channel : & Channel ,
353
+ to : [ u8 ; 20 ] ,
354
+ amount : & BigNum ,
355
+ ) -> web3:: contract:: Result < H256 > {
356
+ let amount = U256 :: from_dec_str ( & amount. to_string ( ) ) . expect ( "Should create U256" ) ;
357
+
358
+ self . contract
359
+ . call (
360
+ "deposit" ,
361
+ ( channel. tokenize ( ) , H160 ( to) , amount) ,
362
+ H160 ( to) ,
363
+ ContractOptions :: with ( |opt| {
364
+ opt. gas_price = Some ( 1 . into ( ) ) ;
365
+ opt. gas = Some ( 6_721_975 . into ( ) ) ;
366
+ } ) ,
367
+ )
368
+ . await
369
+ }
249
370
}
250
371
251
372
/// Deploys the Identity contract for the give `for_address`
@@ -278,31 +399,3 @@ pub async fn deploy_identity_contract(
278
399
279
400
Ok ( ( identity_address, identity_contract) )
280
401
}
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