add better global handling of SNT user balance
This commit is contained in:
parent
962d0b03c7
commit
b9113b9f20
|
@ -8,8 +8,8 @@ export const fetchAndDispatchAccountsWithBalances = (web3, dispatch) => {
|
||||||
const defaultAccount = web3.eth.defaultAccount || addresses[0]
|
const defaultAccount = web3.eth.defaultAccount || addresses[0]
|
||||||
const accounts = addresses.map(async address => {
|
const accounts = addresses.map(async address => {
|
||||||
const balance = await web3.eth.getBalance(address, 'latest')
|
const balance = await web3.eth.getBalance(address, 'latest')
|
||||||
const ERC20TokenBalance = await ERC20Token.methods.balanceOf(address).call()
|
const SNTBalance = await ERC20Token.methods.balanceOf(address).call()
|
||||||
return { address, balance, ERC20TokenBalance }
|
return { address, balance, SNTBalance }
|
||||||
})
|
})
|
||||||
Promise.all(accounts).then(accounts => {
|
Promise.all(accounts).then(accounts => {
|
||||||
dispatch(receiveAccounts(defaultAccount, accounts))
|
dispatch(receiveAccounts(defaultAccount, accounts))
|
||||||
|
|
|
@ -76,7 +76,7 @@ class ERC20TokenUI extends React.Component {
|
||||||
<h3> Read your account token balance </h3>
|
<h3> Read your account token balance </h3>
|
||||||
<Form inline>
|
<Form inline>
|
||||||
<FormGroup>
|
<FormGroup>
|
||||||
{!isLoading && <HelpBlock>Your test token balance is <span className="accountBalance">{account.ERC20TokenBalance}</span></HelpBlock>}
|
{!isLoading && <HelpBlock>Your test token balance is <span className="accountBalance">{account.SNTBalance}</span></HelpBlock>}
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
</Form>
|
</Form>
|
||||||
|
|
||||||
|
|
|
@ -10,24 +10,18 @@ import "react-toggle/style.css";
|
||||||
const unlimitedAllowance = new BigNumber(2).pow(256).sub(1);
|
const unlimitedAllowance = new BigNumber(2).pow(256).sub(1);
|
||||||
const getDefaultAccount = () => web3.eth.defaultAccount;
|
const getDefaultAccount = () => web3.eth.defaultAccount;
|
||||||
const SUPPORTED_TOKENS = ['SNT'];
|
const SUPPORTED_TOKENS = ['SNT'];
|
||||||
|
const BALANCE_KEYS = { 'SNT': 'SNTBalance' };
|
||||||
|
|
||||||
class TokenHandle extends PureComponent {
|
class TokenHandle extends PureComponent {
|
||||||
constructor(props){
|
constructor(props){
|
||||||
super(props);
|
super(props);
|
||||||
this.state = { balance: 0, approved: 0 };
|
this.state = { approved: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
setTimeout(() => {
|
__embarkContext.execWhenReady(() => {
|
||||||
this.getBalance();
|
|
||||||
this.getAllowance();
|
this.getAllowance();
|
||||||
}, 1000)
|
});
|
||||||
}
|
|
||||||
|
|
||||||
getBalance = () => {
|
|
||||||
this.props.methods.balanceOf(getDefaultAccount())
|
|
||||||
.call()
|
|
||||||
.then(balance => { this.setState({ ...this.state, balance }) });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllowance = () => {
|
getAllowance = () => {
|
||||||
|
@ -45,30 +39,33 @@ class TokenHandle extends PureComponent {
|
||||||
const isApproved = !!Number(approved);
|
const isApproved = !!Number(approved);
|
||||||
let amountToApprove = isApproved ? 0 : unlimitedAllowance;
|
let amountToApprove = isApproved ? 0 : unlimitedAllowance;
|
||||||
console.log("approve(\""+spender+"\",\"" +amountToApprove +"\")")
|
console.log("approve(\""+spender+"\",\"" +amountToApprove +"\")")
|
||||||
approve(
|
approve(
|
||||||
spender,
|
spender,
|
||||||
amountToApprove
|
amountToApprove
|
||||||
)
|
)
|
||||||
.send()
|
.send()
|
||||||
.then(approval => {
|
.then(approval => {
|
||||||
const { events: { Approval: { returnValues: { _value } } } } = approval
|
const { events: { Approval: { returnValues: { _value } } } } = approval
|
||||||
this.setState({ ...this.state, approved: _value })
|
this.setState({ ...this.state, approved: _value })
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log("Approve failed: " + err);
|
console.log("Approve failed: " + err);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { symbol } = this.props;
|
const { symbol, account, isLoading } = this.props;
|
||||||
const { balance, approved } = this.state;
|
const { approved } = this.state;
|
||||||
|
console.log('account:', this.props)
|
||||||
return (
|
return (
|
||||||
<div style={{ display: 'flex', justifyContent: 'center', margin: '10px', paddingLeft: '60px' }}>
|
<Fragment>
|
||||||
|
{!isLoading && !!account && <div style={{ display: 'flex', justifyContent: 'center', margin: '10px', paddingLeft: '60px' }}>
|
||||||
<Toggle
|
<Toggle
|
||||||
checked={!!Number(approved)}
|
checked={!!Number(approved)}
|
||||||
name={symbol}
|
name={symbol}
|
||||||
onChange={this.toggleApproved} />
|
onChange={this.toggleApproved} />
|
||||||
<label style={{ margin: '2px 0px 0px 10px', fontWeight: 400 }}>{`${Number(balance).toLocaleString()} ${symbol.toUpperCase()}`}</label>
|
<label style={{ margin: '2px 0px 0px 10px', fontWeight: 400 }}>{`${Number(account[BALANCE_KEYS[symbol]]).toLocaleString()} ${symbol.toUpperCase()}`}</label>
|
||||||
</div>
|
</div>}
|
||||||
|
</Fragment>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { connect } from 'react-redux';
|
||||||
|
import TokenPermissions from './TokenPermission';
|
||||||
|
import { getCurrentAccount, accountsIsLoading } from '../../reducers/accounts';
|
||||||
|
|
||||||
|
const mapStateToProps = state => ({
|
||||||
|
account: getCurrentAccount(state),
|
||||||
|
isLoading: accountsIsLoading(state),
|
||||||
|
});
|
||||||
|
export default connect(mapStateToProps)(TokenPermissions);
|
|
@ -57,7 +57,7 @@ class TestTokenUI extends React.Component {
|
||||||
|
|
||||||
const mapDispatchToProps = dispatch => ({
|
const mapDispatchToProps = dispatch => ({
|
||||||
addToBalance(amount) {
|
addToBalance(amount) {
|
||||||
dispatch(accountActions.addToErc20TokenBalance(amount));
|
dispatch(accountActions.addToSntTokenBalance(amount));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ import ENSSubManagement from './components/ensSubManagement';
|
||||||
import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry';
|
import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry';
|
||||||
import NameLookup from './components/ens/nameLookup';
|
import NameLookup from './components/ens/nameLookup';
|
||||||
import AdminMode from './components/AdminMode';
|
import AdminMode from './components/AdminMode';
|
||||||
import TokenPermissions from './components/standard/TokenPermission';
|
import TokenPermissions from './components/standard/TokenPermissionConnect';
|
||||||
|
|
||||||
import './dapp.css';
|
import './dapp.css';
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,12 @@ import { createSelector } from 'reselect'
|
||||||
export const types = createTypes([
|
export const types = createTypes([
|
||||||
'RECEIVE_ACCOUNTS',
|
'RECEIVE_ACCOUNTS',
|
||||||
'UPDATE_DEFAULT_ACCOUNT',
|
'UPDATE_DEFAULT_ACCOUNT',
|
||||||
'ADD_TO_ERC20_TOKEN_BALANCE'
|
'ADD_TO_SNT_TOKEN_BALANCE'
|
||||||
], 'ACCOUNTS')
|
], 'ACCOUNTS')
|
||||||
export const actions = {
|
export const actions = {
|
||||||
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
|
receiveAccounts: actionCreator(types.RECEIVE_ACCOUNTS, 'defaultAccount','accounts'),
|
||||||
updateDefaultAccount: actionCreator(types.UPDATE_DEFAULT_ACCOUNT, 'defaultAccount'),
|
updateDefaultAccount: actionCreator(types.UPDATE_DEFAULT_ACCOUNT, 'defaultAccount'),
|
||||||
addToErc20TokenBalance: actionCreator(types.ADD_TO_ERC20_TOKEN_BALANCE, 'amount')
|
addToSntTokenBalance: actionCreator(types.ADD_TO_SNT_TOKEN_BALANCE, 'amount')
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function(state = { loading: true, accounts: [] }, action) {
|
export default function(state = { loading: true, accounts: [] }, action) {
|
||||||
|
@ -27,9 +27,9 @@ export default function(state = { loading: true, accounts: [] }, action) {
|
||||||
const { defaultAccount } = action.payload
|
const { defaultAccount } = action.payload
|
||||||
return { ...state, defaultAccount }
|
return { ...state, defaultAccount }
|
||||||
}
|
}
|
||||||
case types.ADD_TO_ERC20_TOKEN_BALANCE: {
|
case types.ADD_TO_SNT_TOKEN_BALANCE: {
|
||||||
const currentAccount = { ...getCurrentAccount({accounts: state}) }
|
const currentAccount = { ...getCurrentAccount({accounts: state}) }
|
||||||
currentAccount.ERC20TokenBalance = Number(currentAccount.ERC20TokenBalance) + Number(action.payload.amount)
|
currentAccount.SNTTokenBalance = Number(currentAccount.SNTTokenBalance) + Number(action.payload.amount)
|
||||||
const accounts = [ ...state.accounts ]
|
const accounts = [ ...state.accounts ]
|
||||||
const idx = accounts.findIndex(a => a.address === currentAccount.address)
|
const idx = accounts.findIndex(a => a.address === currentAccount.address)
|
||||||
accounts[idx] = currentAccount
|
accounts[idx] = currentAccount
|
||||||
|
|
Loading…
Reference in New Issue