1
1
import { Asset , Chain } from "@renproject/chains" ;
2
+ import { Ethereum } from "@renproject/chains-ethereum" ;
2
3
import RenJS , { Gateway , GatewayTransaction } from "@renproject/ren" ;
3
4
import { RenNetwork } from "@renproject/utils" ;
5
+ import BigNumber from "bignumber.js" ;
6
+ import { ethers } from "ethers" ;
4
7
import { useCallback , useEffect , useState } from "react" ;
5
- import { ChainInstanceMap } from "../chain/chainUtils" ;
8
+ import { alterEthereumBaseChainSigner } from "../chain/chainUtils" ;
9
+ import { useChains } from "../network/networkHooks" ;
10
+ import { useWallet } from "../wallet/walletHooks" ;
6
11
import { createGateway } from "./gatewayUtils" ;
7
12
8
13
type UseGatewayParams = {
9
14
network : RenNetwork ;
10
15
asset : Asset ;
11
- fromChain : Chain ;
12
- toChain : Chain ;
16
+ from : Chain ;
17
+ to : Chain ;
13
18
amount ?: string ;
14
19
nonce ?: number ;
15
20
} ;
16
21
17
- export const useGateway = (
18
- { asset, fromChain, toChain, amount, network, nonce } : UseGatewayParams ,
19
- renJs : RenJS | null ,
20
- chains : ChainInstanceMap
21
- ) => {
22
+ export const useGateway = ( {
23
+ asset,
24
+ from,
25
+ to,
26
+ amount,
27
+ network,
28
+ nonce,
29
+ } : UseGatewayParams ) => {
30
+ const chains = useChains ( network ) ;
31
+ const { provider } = useWallet ( to ) ;
32
+ const [ renJs , setRenJs ] = useState < RenJS | null > ( null ) ;
33
+ const [ error , setError ] = useState ( null ) ;
22
34
const [ gateway , setGateway ] = useState < Gateway | null > ( null ) ;
23
35
const [ transactions , setTransactions ] = useState < Array < GatewayTransaction > > (
24
36
[ ]
@@ -28,33 +40,122 @@ export const useGateway = (
28
40
setTransactions ( ( txs ) => [ ...txs , tx ] ) ;
29
41
} , [ ] ) ;
30
42
43
+ // set up renjs with signers
31
44
useEffect ( ( ) => {
32
- console . log ( "useGateway useEffect" ) ;
45
+ console . log ( "useGateway useEffect renJs and provider" ) ;
46
+ const initProvider = async ( ) => {
47
+ const ethersProvider = new ethers . providers . Web3Provider ( provider ) ;
48
+ const signer = ethersProvider . getSigner ( ) ;
49
+ console . log ( "useGateway altering signer" ) ;
50
+ alterEthereumBaseChainSigner ( chains , signer ) ;
51
+ const renJs = new RenJS ( network ) . withChains (
52
+ // @ts -ignore
53
+ ...Object . values ( chains ) . map ( ( chain ) => chain . chain )
54
+ ) ;
55
+ ( window as any ) . renJs = renJs ;
56
+ return renJs ;
57
+ } ;
58
+ initProvider ( )
59
+ . then ( ( renJs ) => setRenJs ( renJs ) )
60
+ . catch ( ( error ) => {
61
+ setError ( error ) ;
62
+ } ) ;
63
+ } , [ network ] ) ;
64
+
65
+ // initialize gateway
66
+ useEffect ( ( ) => {
67
+ console . log ( "useGateway useEffect gateway init" ) ;
33
68
if ( renJs ) {
34
- console . log ( "initializeGateway" ) ;
35
69
const initializeGateway = async ( ) => {
36
70
const gateway = await createGateway (
37
71
renJs ,
38
- { asset, from : fromChain , to : toChain , amount, nonce } ,
72
+ { asset, from, to, amount, nonce } ,
39
73
chains
40
74
) ;
41
75
console . log ( "gateway created" , gateway ) ;
42
76
gateway . on ( "transaction" , addTransaction ) ;
43
- console . log ( "gateway transaction listener registered" , addTransaction ) ;
44
- ( window as any ) . g = gateway ;
77
+ console . log ( "gateway transaction listener added" ) ;
78
+ ( window as any ) . gateway = gateway ;
45
79
return gateway ;
46
80
} ;
81
+ console . log ( "gateway initializing" ) ;
47
82
initializeGateway ( )
48
83
. then ( ( gateway ) => setGateway ( gateway ) )
49
- . catch ( console . error ) ;
84
+ . catch ( ( error ) => {
85
+ setError ( error ) ;
86
+ } ) ;
50
87
}
51
88
52
89
return ( ) => {
53
90
if ( gateway ) {
91
+ console . log ( "gateway removing listeners" ) ;
54
92
gateway . eventEmitter . removeAllListeners ( ) ;
55
93
}
56
94
} ;
57
- } , [ renJs ] ) ; // of useEffect
95
+ } , [ renJs ] ) ;
96
+
97
+ return { renJs, gateway, transactions, error } ;
98
+ } ;
99
+
100
+ export const useGatewayFees = (
101
+ gateway : Gateway | null ,
102
+ amount : string | number | BigNumber
103
+ ) => {
104
+ const [ decimals , setDecimals ] = useState ( 0 ) ;
105
+ const [ balance , setBalance ] = useState ( "" ) ;
106
+ const [ balancePending , setBalancePending ] = useState ( false ) ;
107
+ const [ minimumAmount , setMinimumAmount ] = useState ( "" ) ;
108
+ const [ outputAmount , setOutputAmount ] = useState ( "" ) ;
109
+ const [ amountsPending , setAmountsPending ] = useState ( false ) ;
110
+
111
+ useEffect ( ( ) => {
112
+ setBalancePending ( true ) ;
113
+ if ( ! gateway ) {
114
+ return ;
115
+ }
116
+ const getFees = async ( ) => {
117
+ const decimals = await gateway . fromChain . assetDecimals (
118
+ gateway . params . asset
119
+ ) ;
120
+ setDecimals ( decimals ) ;
121
+ console . log (
122
+ `gateway decimals ${ gateway . fromChain . chain } /${ gateway . params . asset } : ${ decimals } `
123
+ ) ;
124
+
125
+ const balanceBn = (
126
+ await ( gateway . toChain as Ethereum ) . getBalance ( gateway . params . asset )
127
+ ) . shiftedBy ( - decimals ) ;
128
+ setBalance ( balanceBn . toFixed ( ) ) ;
129
+ console . log ( `gateway balance: ${ balanceBn } ` ) ;
130
+ setBalancePending ( false ) ;
131
+ } ;
132
+ getFees ( ) . catch ( console . error ) ;
133
+ } , [ gateway ] ) ;
134
+
135
+ useEffect ( ( ) => {
136
+ setAmountsPending ( true ) ;
137
+ if ( ! gateway || ! decimals ) {
138
+ return ;
139
+ }
140
+ const estimatedOutputBn = gateway . fees
141
+ // @ts -ignore
142
+ . estimateOutput ( new BigNumber ( amount ) . shiftedBy ( decimals ) )
143
+ . shiftedBy ( - decimals ) ;
144
+ setOutputAmount ( estimatedOutputBn . toFixed ( ) ) ;
145
+ console . log ( `gateway estimated output: ${ estimatedOutputBn } ` ) ;
146
+
147
+ const minimumAmountBn = gateway . fees . minimumAmount . shiftedBy ( - decimals ) ;
148
+ setMinimumAmount ( minimumAmountBn . toFixed ( ) ) ;
149
+ console . log ( `gateway minimum amount: ${ estimatedOutputBn } ` ) ;
150
+ setAmountsPending ( false ) ;
151
+ } , [ gateway , amount ] ) ;
58
152
59
- return { gateway, transactions } ;
153
+ return {
154
+ decimals,
155
+ balance,
156
+ balancePending,
157
+ minimumAmount,
158
+ outputAmount,
159
+ amountsPending,
160
+ } ;
60
161
} ;
0 commit comments