@@ -50,14 +50,25 @@ const Klay = function Klay(...args) {
5050 // sets _requestmanager
5151 core . packageInit ( this , args )
5252
53+ // overwrite package setRequestManager
54+ const setRequestManager = this . setRequestManager
55+ this . setRequestManager = function ( manager ) {
56+ setRequestManager ( manager )
57+
58+ _this . net . setRequestManager ( manager )
59+ _this . personal . setRequestManager ( manager )
60+ _this . accounts . setRequestManager ( manager )
61+ _this . Contract . _requestManager = _this . _requestManager
62+ _this . Contract . currentProvider = _this . _provider
63+
64+ return true
65+ }
66+
5367 // overwrite setProvider
5468 const setProvider = this . setProvider
5569 this . setProvider = function ( ...arg ) {
5670 setProvider . apply ( _this , arg )
57- _this . net . setProvider . apply ( _this , arg )
58- _this . personal . setProvider . apply ( _this , arg )
59- _this . accounts . setProvider . apply ( _this , arg )
60- _this . Contract . setProvider ( _this . currentProvider , _this . accounts )
71+ _this . setRequestManager ( _this . _requestManager )
6172 }
6273
6374 let defaultAccount = null
@@ -113,24 +124,36 @@ const Klay = function Klay(...args) {
113124 this . decodeTransaction = decodeFromRawTransaction
114125
115126 // add net
116- this . net = new Net ( this . currentProvider )
127+ this . net = new Net ( this )
117128 // add chain detection
118129 this . net . getNetworkType = getNetworkType . bind ( this )
119130
120131 // add accounts
121- this . accounts = new Accounts ( this . currentProvider )
132+ this . accounts = new Accounts ( this )
122133
123134 // add personal
124- this . personal = new Personal ( this . currentProvider )
135+ this . personal = new Personal ( this )
125136 this . personal . defaultAccount = this . defaultAccount
126137
127138 // create a proxy Contract type for this instance, as a Contract's provider
128139 // is stored as a class member rather than an instance variable. If we do
129140 // not create this proxy type, changing the provider in one instance of
130141 // caver-klay would subsequently change the provider for _all_ contract
131142 // instances!
143+ const self = this
132144 const Contract = function Contract ( ) {
133145 BaseContract . apply ( this , arguments )
146+
147+ // when Klay.setProvider is called, call packageInit
148+ // on all contract instances instantiated via this Klay
149+ // instances. This will update the currentProvider for
150+ // the contract instances
151+ const _this = this // eslint-disable-line no-shadow
152+ const setProvider = self . setProvider // eslint-disable-line no-shadow
153+ self . setProvider = function ( ) {
154+ setProvider . apply ( self , arguments )
155+ core . packageInit ( _this , [ self ] )
156+ }
134157 }
135158
136159 Contract . setProvider = function ( ) {
@@ -146,12 +169,16 @@ const Klay = function Klay(...args) {
146169 this . Contract = Contract
147170 this . Contract . defaultAccount = this . defaultAccount
148171 this . Contract . defaultBlock = this . defaultBlock
149- this . Contract . setProvider ( this . currentProvider , this . accounts )
172+ this . Contract . _requestManager = this . _requestManager
173+ this . Contract . _klayAccounts = this . accounts
174+ this . Contract . currentProvider = this . _requestManager . provider
150175
151176 this . KIP7 = KIP7
152177 this . KIP7 . defaultAccount = this . defaultAccount
153178 this . KIP7 . defaultBlock = this . defaultBlock
154- this . KIP7 . setProvider ( this . currentProvider , this . accounts )
179+ this . KIP7 . _requestManager = this . _requestManager
180+ this . KIP7 . _klayAccounts = this . accounts
181+ this . KIP7 . currentProvider = this . _requestManager . provider
155182
156183 // add IBAN
157184 this . Iban = utils . Iban
@@ -240,7 +267,7 @@ const Klay = function Klay(...args) {
240267
241268 methods . forEach ( function ( method ) {
242269 method . attachToObject ( _this )
243- // second param means is klay.accounts (necessary for wallet signing)
270+ // second param is the klay.accounts module (necessary for signing transactions locally )
244271 method . setRequestManager ( _this . _requestManager , _this . accounts )
245272 method . defaultBlock = _this . defaultBlock
246273 method . defaultAccount = _this . defaultAccount
0 commit comments