Created
January 18, 2020 17:40
-
-
Save ameensol/2cb4d2a5601c5a0192ee8ba195111719 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Once the page load, check if we have a connected web3 wallet and if we | |
// allow ETH deposits and if so fetch the ETH deposit address | |
// and create a transaction with web3. | |
useEffect(() => { | |
if ( | |
!(exchangeRates && exchangeRates.isDone) || | |
!Web3.givenProvider || | |
!window.ethereum || | |
invoice.acceptedCurrencies.indexOf('ETH') === -1 | |
) return | |
(async () => { | |
const web3WalletAddress = iframeApiClient.GetInvoiceDepositAddress( | |
invoice.apiKey, | |
invoice.id, | |
'ETH', | |
) | |
web3WalletAddressReq.bind(web3WalletAddress) | |
const web3 = new Web3(Web3.givenProvider) | |
let accounts, errAccounts | |
// Proper web3 wallets (like MetaMask) should provide a global ethereum | |
// object with an enable() method. It should trigger a web3 prompt that asks | |
// the user to allow the webpage to read web3 wallet addresses. The method | |
// returns a Promise that’s either resolved with user accounts after | |
// user approval, or rejected with an Error after user rejection. | |
if (window.ethereum.enable) { | |
[accounts, errAccounts] = await maybe(window.ethereum.enable()) | |
} | |
// If some wallet is not fully complying with the enable() method it will | |
// most probably return nothing. In that case we should fallback to | |
// web3's get accounts. | |
if (!accounts && !errAccounts) { | |
[accounts, errAccounts] = await maybe(web3.eth.getAccounts()) | |
} | |
if (errAccounts) { | |
console.log('Could not get web3 accounts: ', errAccounts) | |
return | |
} | |
const gasEth = web3.utils.fromWei(await web3.eth.getGasPrice(), 'ether') | |
const invoiceInEth = exchangeRates!.convert(invoice, 'ETH') | |
const invoiceWithGas = invoiceInEth.add(new MoneyAmount({ | |
amount: gasEth, | |
currency: 'ETH', | |
})) | |
const invoiceInWei = web3.utils.toWei(toWeiCompatibleString(invoiceInEth.amount), 'ether') | |
// find an account with enough ETH balance and send a web3 tx from it | |
for (const account of accounts) { | |
const [balanceWei, errBalance] = await maybe(web3.eth.getBalance(account)) | |
if (errBalance) { | |
console.log('Could not get balance of ' + account + ': ', errBalance) | |
continue | |
} | |
const balanceEth = web3.utils.fromWei(balanceWei, 'ether') | |
console.log(`Balance of ${account}: ${balanceEth}`) | |
const isWeb3WalletBalanceEnough = new MoneyAmount({ | |
amount: balanceEth, | |
currency: 'ETH', | |
}).gte(invoiceWithGas) | |
if (isWeb3WalletBalanceEnough) { | |
web3WalletAddress.then(res => { | |
// The web3Wallet in the store will start a polling for the eth | |
// address and will show instructions for the user. | |
dispatch(invoiceActions.setWeb3Wallet({ | |
address: res.address, | |
wallet: web3.currentProvider.isMetaMask ? 'MetaMask' : 'Web3', | |
})) | |
web3.eth | |
.sendTransaction({ | |
from: account, | |
to: res.address, | |
value: invoiceInWei, | |
}) | |
.on('error', (error) => { | |
dispatch(invoiceActions.setWeb3Wallet(null)) | |
}) | |
}).catch(e => {}) | |
// break from the for loop so we don't accidentaly send another tx | |
break | |
} | |
} | |
})().catch(e => {}) | |
}, [exchangeRates!.isDone]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment