2018-08-14 14:10:31 +00:00
|
|
|
import ERC20Token from 'Embark/contracts/ERC20Token'
|
2018-09-04 21:49:39 +00:00
|
|
|
import UsernameRegistrar from 'Embark/contracts/UsernameRegistrar'
|
2018-08-14 14:10:31 +00:00
|
|
|
import TestToken from 'Embark/contracts/TestToken'
|
|
|
|
|
|
|
|
import { getDefaultAccount } from '../utils/web3Helpers'
|
2018-06-01 16:45:11 +00:00
|
|
|
import { actions as accountActions } from '../reducers/accounts'
|
2018-07-18 18:16:20 +00:00
|
|
|
import { isNil } from 'lodash'
|
|
|
|
|
|
|
|
const { receiveAccounts, receiveStatusContactCode } = accountActions
|
2018-06-01 16:45:11 +00:00
|
|
|
|
|
|
|
export const fetchAndDispatchAccountsWithBalances = (web3, dispatch) => {
|
|
|
|
web3.eth.getAccounts((err, addresses) => {
|
|
|
|
if (addresses) {
|
|
|
|
const defaultAccount = web3.eth.defaultAccount || addresses[0]
|
|
|
|
const accounts = addresses.map(async address => {
|
|
|
|
const balance = await web3.eth.getBalance(address, 'latest')
|
2018-06-13 16:58:19 +00:00
|
|
|
const SNTBalance = await ERC20Token.methods.balanceOf(address).call()
|
|
|
|
return { address, balance, SNTBalance }
|
2018-06-01 16:45:11 +00:00
|
|
|
})
|
|
|
|
Promise.all(accounts).then(accounts => {
|
|
|
|
dispatch(receiveAccounts(defaultAccount, accounts))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-07-18 18:16:20 +00:00
|
|
|
export const checkAndDispatchStatusContactCode = dispatch => {
|
2018-10-08 19:25:42 +00:00
|
|
|
window.web3.currentProvider.status
|
|
|
|
.getContactCode()
|
|
|
|
.then(data => {
|
2018-10-09 13:58:43 +00:00
|
|
|
dispatch(receiveStatusContactCode(data));
|
2018-10-08 19:25:42 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2018-10-09 16:39:37 +00:00
|
|
|
console.warn('Error:', err);
|
2018-10-08 19:25:42 +00:00
|
|
|
})
|
2018-10-09 16:39:37 +00:00
|
|
|
};
|
2018-08-14 14:10:31 +00:00
|
|
|
|
|
|
|
export const fetchAndDispatchSNTAllowance = dispatch => {
|
|
|
|
const { methods: { allowance } } = TestToken;
|
|
|
|
const { receiveSntAllowance } = accountActions;
|
2018-09-04 21:49:39 +00:00
|
|
|
const spender = UsernameRegistrar._address;
|
2018-08-14 14:10:31 +00:00
|
|
|
allowance(getDefaultAccount(), spender)
|
|
|
|
.call()
|
|
|
|
.then(allowance => {
|
|
|
|
dispatch(receiveSntAllowance(allowance))
|
|
|
|
})
|
2018-10-09 16:39:37 +00:00
|
|
|
};
|