1
1
import { ethers } from "hardhat" ;
2
2
import hre = require( "hardhat" ) ;
3
- import { KlerosCore , PolicyRegistry , ForeignGateway__factory , HomeGateway , TestERC20 } from "../typechain-types" ;
3
+ import {
4
+ KlerosCore ,
5
+ ForeignGateway__factory ,
6
+ HomeGateway ,
7
+ TestERC20 ,
8
+ IArbitrableV2__factory ,
9
+ } from "../typechain-types" ;
10
+ import { DisputeRequestEventObject } from "../typechain-types/src/arbitration/interfaces/IArbitrableV2" ;
11
+ import { HttpNetworkConfig } from "hardhat/types" ;
12
+ import { DeploymentsExtension } from "hardhat-deploy/types" ;
4
13
5
- async function main ( ) {
14
+ export default async function main (
15
+ foreignNetwork : HttpNetworkConfig ,
16
+ foreignDeployments : DeploymentsExtension ,
17
+ foreignGatewayArtifact : string ,
18
+ homeGatewayArtifact : string ,
19
+ feeTokenArtifact ?: string
20
+ ) {
6
21
const core = ( await ethers . getContract ( "KlerosCore" ) ) as KlerosCore ;
7
- const policyRegistry = ( await ethers . getContract ( "PolicyRegistry" ) ) as PolicyRegistry ;
8
- const homeGateway = ( await ethers . getContract ( "HomeGatewayToGnosis" ) ) as HomeGateway ;
9
- const dai = ( await ethers . getContract ( "DAI" ) ) as TestERC20 ;
22
+ const homeGateway = ( await ethers . getContract ( homeGatewayArtifact ) ) as HomeGateway ;
23
+ const feeToken = feeTokenArtifact ? ( ( await ethers . getContract ( feeTokenArtifact ) ) as TestERC20 ) : undefined ;
10
24
11
- const foreignChainProvider = new ethers . providers . JsonRpcProvider ( hre . config . networks . chiado . url ) ;
12
- const foreignGatewayDeployment = await hre . companionNetworks . foreignChiado . deployments . get ( "ForeignGatewayOnGnosis" ) ;
25
+ const foreignChainProvider = new ethers . providers . JsonRpcProvider ( foreignNetwork . url ) ;
26
+ const foreignGatewayDeployment = await foreignDeployments . get ( foreignGatewayArtifact ) ;
13
27
const foreignGateway = await ForeignGateway__factory . connect ( foreignGatewayDeployment . address , foreignChainProvider ) ;
28
+ const foreignChainId = await foreignChainProvider . getNetwork ( ) . then ( ( network ) => network . chainId ) ;
29
+ const arbitrableInterface = IArbitrableV2__factory . createInterface ( ) ;
14
30
31
+ // Event subscription
32
+ // WARNING: The callback might run more than once if the script is restarted in the same block
33
+ // type Listener = [ eventArg1, ...eventArgN, transactionReceipt ]
15
34
foreignGateway . on (
16
35
"CrossChainDisputeOutgoing" ,
17
- async ( foreignBlockHash , foreignArbitrable , foreignDisputeID , choices , extraData ) => {
36
+ async ( foreignBlockHash , foreignArbitrable , foreignDisputeID , choices , extraData , txReceipt ) => {
18
37
console . log (
19
38
"CrossChainDisputeOutgoing: %s %s %s %s %s" ,
20
39
foreignBlockHash ,
@@ -23,52 +42,52 @@ async function main() {
23
42
choices ,
24
43
extraData
25
44
) ;
45
+ // console.log("tx receipt: %O", txReceipt);
26
46
27
- const cost = ( await core . functions [ "arbitrationCost(bytes,address)" ] ( extraData , dai . address ) ) . cost ;
47
+ // txReceipt is missing the full logs for this tx so we need to request it here
48
+ const fullTxReceipt = await foreignChainProvider . getTransactionReceipt ( txReceipt . transactionHash ) ;
28
49
29
- await dai . approve ( homeGateway . address , cost ) ;
50
+ // Retrieve the DisputeRequest event
51
+ const disputeRequest = fullTxReceipt . logs
52
+ . filter ( ( log ) => log . topics [ 0 ] === arbitrableInterface . getEventTopic ( "DisputeRequest" ) )
53
+ . map ( ( log ) => arbitrableInterface . parseLog ( log ) . args as unknown as DisputeRequestEventObject ) [ 0 ] ;
54
+ console . log ( "tx events DisputeRequest: %O" , disputeRequest ) ;
55
+ // TODO: log a warning if there are multiple DisputeRequest events
30
56
31
- await homeGateway . functions [
32
- "relayCreateDispute((bytes32,uint256,address,uint256,uint256,uint256,string,uint256,bytes),uint256)"
33
- ] (
34
- {
35
- foreignBlockHash : foreignBlockHash ,
36
- foreignChainID : 10200 ,
37
- foreignArbitrable : foreignArbitrable ,
38
- foreignDisputeID : foreignDisputeID ,
39
- externalDisputeID : ethers . utils . keccak256 ( ethers . utils . toUtf8Bytes ( "future of france" ) ) , // FIXME
40
- templateId : 1 , // FIXME
41
- templateUri : "" , // FIXME
42
- choices : choices ,
43
- extraData : extraData ,
44
- } ,
45
- cost
46
- ) ;
57
+ const relayCreateDisputeParams = {
58
+ foreignBlockHash : foreignBlockHash ,
59
+ foreignChainID : foreignChainId ,
60
+ foreignArbitrable : foreignArbitrable ,
61
+ foreignDisputeID : foreignDisputeID ,
62
+ externalDisputeID : disputeRequest . _externalDisputeID ,
63
+ templateId : disputeRequest . _templateId ,
64
+ templateUri : disputeRequest . _templateUri ,
65
+ choices : choices ,
66
+ extraData : extraData ,
67
+ } ;
68
+ console . log ( "Relaying dispute to home chain... %O" , relayCreateDisputeParams ) ;
69
+
70
+ let tx ;
71
+ if ( feeToken === undefined ) {
72
+ // Paying in native Arbitrum ETH
73
+ const cost = ( await core . functions [ "arbitrationCost(bytes)" ] ( extraData ) ) . cost ;
74
+ tx = await homeGateway . functions [
75
+ "relayCreateDispute((bytes32,uint256,address,uint256,uint256,uint256,string,uint256,bytes))"
76
+ ] ( relayCreateDisputeParams , { value : cost } ) ;
77
+ } else {
78
+ // Paying in ERC20
79
+ const cost = ( await core . functions [ "arbitrationCost(bytes,address)" ] ( extraData , feeToken . address ) ) . cost ;
80
+ await ( await feeToken . approve ( homeGateway . address , cost ) ) . wait ( ) ;
81
+ tx = await homeGateway . functions [
82
+ "relayCreateDispute((bytes32,uint256,address,uint256,uint256,uint256,string,uint256,bytes),uint256)"
83
+ ] ( relayCreateDisputeParams , cost ) ;
84
+ }
85
+ tx = tx . wait ( ) ;
86
+ console . log ( "relayCreateDispute txId: %O" , tx . transactionHash ) ;
47
87
}
48
88
) ;
49
89
50
- // type Listener = [ eventArg1, ...eventArgN, transactionReceipt ]
51
-
52
- // policyRegistry.on("PolicyUpdate", (courtId, courtName, policy, receipt) => {
53
- // console.log("PolicyUpdate: %d %s %s %O", courtId, courtName, policy, receipt);
54
- // // WARNING: This callback might run more than once if the script is restarted in the same block
55
- // });
56
-
57
- // core.on("DisputeCreation", (disputeID, arbitrable) => {
58
- // console.log("DisputeCreation: %d %s %s %d %s %s", disputeID, arbitrable);
59
- // // WARNING: This callback might run more than once if the script is restarted in the same block
60
-
61
- // // if phase is staking and minStakingTime has passed, then passPhase()
62
- // });
63
-
64
90
console . log ( "Listening for events..." ) ;
65
91
const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
66
92
await delay ( 1000000 ) ;
67
93
}
68
-
69
- main ( )
70
- . then ( ( ) => process . exit ( 0 ) )
71
- . catch ( ( error ) => {
72
- console . error ( error ) ;
73
- process . exit ( 1 ) ;
74
- } ) ;
0 commit comments