@@ -26,72 +26,28 @@ class EthCommerce {
2626 }
2727 }
2828
29- //metamascara is not working ATM, so we're defaulting to false for now...
30- render ( options , errorCallback , successCallback , tryMetamascara = false ) {
31- const { targetElement, amount, address, currency, custom, type } = options ;
29+ render ( options , errorCallback , successCallback ) {
30+ const { targetElement } = options ;
3231
3332 this . errorCallback = errorCallback ;
3433 this . successCallback = successCallback ;
3534
3635 window . addEventListener ( "load" , _ => {
37- // Checking if Web3 has been injected by the browser (Mist/ MetaMask)
36+ // Checking if Web3 has been injected by the browser (MetaMask)
3837 if ( typeof web3 !== "undefined" ) {
39- // Use Mist/MetaMask's provider
40- const provider = new Web3 ( web3 . currentProvider ) ;
4138 // Use the browser's ethereum provider
42- provider . eth . getAccounts ( ( err , accounts ) => {
4339 this . renderButton ( {
4440 ...options ,
45- account : accounts [ 0 ]
4641 } ) ;
47- } ) ;
4842 } else {
49- //Fallback to metamascara
50- if ( tryMetamascara ) {
51- console . log ( "trying metamascara" ) ;
52- this . injectMetamascara ( options ) ;
53- } else {
54- console . log ( "no web 3" ) ;
55- if ( this . config . HANDLE_UI ) {
56- this . renderNoWeb3 ( targetElement ) ;
57- }
58- this . errorCallback ( { error : "no web3 detected" } ) ;
43+ if ( this . config . HANDLE_UI ) {
44+ this . renderNoWeb3 ( targetElement ) ;
5945 }
46+ this . errorCallback ( { error : "no web3 detected" } ) ;
6047 }
6148 } ) ;
6249 }
6350
64- injectMetamascara ( options ) {
65- const metamascara = document . createElement ( "script" ) ;
66- metamascara . type = "text/javascript" ;
67- metamascara . src = "https://wallet.metamask.io/metamascara.js" ;
68- const ref = document . querySelector ( "script" ) ;
69- ref . parentNode . insertBefore ( metamascara , ref ) ;
70- metamascara . addEventListener ( "load" , _ => {
71- console . log ( "metamascara injected" ) ;
72-
73- const ethJsScript = document . createElement ( "script" ) ;
74- ethJsScript . type = "text/javascript" ;
75- ethJsScript . src =
76- "https://cdn.jsdelivr.net/npm/ethjs@0.3.0/dist/ethjs.min.js" ;
77- const ref = document . querySelector ( "script" ) ;
78- ref . parentNode . insertBefore ( ethJsScript , ref ) ;
79- ethJsScript . addEventListener ( "load" , __ => {
80- console . log ( "ethJS injected" ) ;
81- const provider = metamask . createDefaultProvider ( ) ;
82- const eth = new Eth ( provider ) ;
83- eth . accounts ( ( err , accounts ) => {
84- this . renderButton ( {
85- ...options ,
86- account : accounts [ 0 ]
87- } ) ;
88- } ) ;
89- } ) ;
90-
91- this . render ( options , this . successCallback , this . errorCallback , false ) ;
92- } ) ;
93- }
94-
9551 renderNoWeb3 ( targetElement ) {
9652 this . renderStyles ( ) ;
9753
@@ -119,7 +75,6 @@ class EthCommerce {
11975 )
12076 . then ( response => response . json ( ) )
12177 . then ( data => {
122- console . log ( "GOT ETH PRICE" , data ) ;
12378 resolve ( data [ currency ] ) ;
12479 } )
12580 . catch ( e => {
@@ -189,52 +144,58 @@ class EthCommerce {
189144 let amountIntETH = parseFloat ( amount / price ) ;
190145 const amountToReceive = web3 . toWei ( amountIntETH , "ether" ) ;
191146
192- this . sendTransaction ( account , address , amountToReceive )
193- . then ( tx => {
194- if ( document . getElementById ( "eth-btn-text" ) ) {
195- document
196- . getElementById ( "eth-btn-text" )
197- . classList . add ( "waiting" ) ;
198- }
199-
200- if ( this . config . HANDLE_UI ) {
201- document . getElementById ( "eth-btn-text" ) . textContent =
202- "Waiting for confirmation" ;
203- const waiting = document . createElement ( "p" ) ;
204- waiting . classList . add ( "eth-waiting" ) ;
205- waiting . id = "hold-tight" ;
206- waiting . textContent = "Hold tight! This might take a while..." ;
207- document . getElementById ( targetElement ) . appendChild ( waiting ) ;
208- }
209- this . waitForConfirmation (
210- tx ,
211- this . config . MIN_CONFIRMATIONS ,
212- this . config . INTERVAL
213- ) ;
214- } )
215- . catch ( e => {
216- console . log ( "Error sending transaction" , e ) ;
217-
218- if ( this . config . HANDLE_UI ) {
219- document . getElementById ( "eth-icon-svg" ) . src = this . getImage (
220- "ETHEREUM_ICON"
221- ) ;
222- document . getElementById ( "eth-btn-text" ) . textContent =
223- "Pay with Ethereum" ;
224- }
225-
226- if ( document . getElementById ( "eth-btn-text" ) ) {
227- document
228- . getElementById ( "eth-btn-text" )
229- . classList . remove ( "waiting" ) ;
230- }
231-
232- this . errorCallback ( e ) ;
233- this . loading = false ;
234- } ) ;
147+ return web3 . currentProvider . send ( 'eth_requestAccounts' , [ ] ) . then ( response => {
148+ if ( response . result && response . result . length ) {
149+ const account = response . result [ 0 ] ;
150+
151+ this . sendTransaction ( account , address , amountToReceive )
152+ . then ( tx => {
153+ if ( document . getElementById ( "eth-btn-text" ) ) {
154+ document
155+ . getElementById ( "eth-btn-text" )
156+ . classList . add ( "waiting" ) ;
157+ }
158+
159+ if ( this . config . HANDLE_UI ) {
160+ document . getElementById ( "eth-btn-text" ) . textContent =
161+ "Waiting for confirmation" ;
162+ const waiting = document . createElement ( "p" ) ;
163+ waiting . classList . add ( "eth-waiting" ) ;
164+ waiting . id = "hold-tight" ;
165+ waiting . textContent = "Hold tight! This might take a while..." ;
166+ document . getElementById ( targetElement ) . appendChild ( waiting ) ;
167+ }
168+ this . waitForConfirmation (
169+ tx ,
170+ this . config . MIN_CONFIRMATIONS ,
171+ this . config . INTERVAL
172+ ) ;
173+ } )
174+ . catch ( e => {
175+ console . error ( "Error sending transaction" , e ) ;
176+
177+ if ( this . config . HANDLE_UI ) {
178+ document . getElementById ( "eth-icon-svg" ) . src = this . getImage (
179+ "ETHEREUM_ICON"
180+ ) ;
181+ document . getElementById ( "eth-btn-text" ) . textContent =
182+ "Pay with Ethereum" ;
183+ }
184+
185+ if ( document . getElementById ( "eth-btn-text" ) ) {
186+ document
187+ . getElementById ( "eth-btn-text" )
188+ . classList . remove ( "waiting" ) ;
189+ }
190+
191+ this . errorCallback ( e ) ;
192+ this . loading = false ;
193+ } ) ;
194+ }
195+ } )
235196 } )
236197 . catch ( e => {
237- console . log ( "Error getting ETH price " , e ) ;
198+ console . error ( "Error" , e ) ;
238199 if ( this . config . HANDLE_UI ) {
239200 document . getElementById ( "eth-icon-svg" ) . src = this . getImage (
240201 "ETHEREUM_ICON"
@@ -360,20 +321,16 @@ class EthCommerce {
360321 web3 . eth . getBlockNumber ( ( error , currentBlockNumber ) => {
361322 if ( ! error ) {
362323 const confirmations = currentBlockNumber - txBlockNumber ;
363- console . log ( confirmations , "confirmations" ) ;
364324 if ( confirmations >= minConfirmations ) {
365325 const delta = ( Date . now ( ) - start_time ) / 1000 ;
366- console . log ( `Transaction took ${ delta } seconds to confirm` ) ;
367326 clearInterval ( checkConfirmations ) ;
368327 this . onTransactionConfirmed ( result ) ;
369328 }
370329 } else {
371330 this . errorCallback ( error ) ;
372331 }
373332 } ) ;
374- } else {
375- console . log ( "block height: pending" ) ;
376- }
333+ }
377334 } else {
378335 this . errorCallback ( error ) ;
379336 }
@@ -383,7 +340,6 @@ class EthCommerce {
383340
384341 sendTransaction ( account , address , amount ) {
385342 let tData = { from : account , to : address , value : amount } ;
386- console . log ( "About to send tx data:" , tData ) ;
387343 return new Promise ( ( resolve , reject ) => {
388344 web3 . eth . estimateGas ( tData , ( error , gas ) => {
389345 if ( ! error ) {
@@ -401,7 +357,6 @@ class EthCommerce {
401357 if ( error ) {
402358 reject ( error ) ;
403359 } else {
404- console . log ( "Submitted tx" , txID ) ;
405360 resolve ( txID ) ;
406361 }
407362 }
0 commit comments