Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"whatwg-fetch": "^2.0.3"
},
"dependencies": {
"@decent-bet/contract-migration": "^1.0.0",
"@decent-bet/contract-playdbet": "^1.0.3",
"@fortawesome/fontawesome": "^1.1.8",
"@fortawesome/fontawesome-svg-core": "^1.2.6",
"@fortawesome/free-brands-svg-icons": "^5.0.13",
Expand Down
46 changes: 20 additions & 26 deletions src/Components/Base/BalanceListener.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@


import { Subscription, timer, from, zip, Observable } from 'rxjs'
import { map, switchMap } from 'rxjs/operators'

const LISTENER_INTERVAL = 8000

export type ListenerParam = (result: { vthoBalance: number, ethBalance: number, updatedAt: Date }) => void
export type ListenerParam = (
result: { vthoBalance: number; ethBalance: number; updatedAt: Date }
) => void

export default class BalanceListener {
private _subscription$: Subscription

constructor(private web3: any,
private thor: any,) {}
constructor(private thor: any) {}

public onBalancesChange(listener: ListenerParam): BalanceListener {
this.stop()
this._subscription$ = this.createBalanceSubscription()
.subscribe(listener)
this._subscription$ = this.createBalanceSubscription().subscribe(
listener
)
return this
}

public stop(): void {
if(this._subscription$) {
if (this._subscription$) {
this._subscription$.unsubscribe()
}
}

private createBalanceSubscription(): Observable<any> {

// Polling works best with RPC endpoint
const web3Http = (window as any).web3Http
const web3DefaultAccount = this.web3.eth.defaultAccount
const thorDefaultAccount = this.thor.eth.defaultAccount

return timer(0, LISTENER_INTERVAL).pipe(
switchMap(() => zip(
from(this.thor.eth.getEnergy(thorDefaultAccount)),
from(web3Http.eth.getBalance(web3DefaultAccount))
)),
map((i) => {
return {
vthoBalance: i[0],
ethBalance: i[1],
updatedAt: new Date()
}
})
)
switchMap(() =>
zip(from(this.thor.eth.getEnergy(thorDefaultAccount)))
),
map(i => {
return {
vthoBalance: i[0],
updatedAt: new Date()
}
})
)
}

}
}
14 changes: 1 addition & 13 deletions src/Components/Base/Web3Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ const keyHandler = new KeyHandler()


let initWeb3 = async () => {
let provider = new Web3.providers.WebsocketProvider(Config.gethUrl, { timeout: 60*60*1000 })

window.web3Http = new Web3(Config.gethRpcUrl)
window.web3Object = new Web3(provider)

provider.on('error', e => console.error('WS Error', e))
provider.on('end', e => {
provider = new Web3.providers.WebsocketProvider(Config.gethUrl, { timeout: 60*60*1000 })
window.web3Object.setProvider(provider)
})

window.thor = thorify(
new Web3(),
process.env.THOR_URL || Config.thorUrl
Expand All @@ -35,7 +24,7 @@ let initWeb3 = async () => {
})


const contractHelper = new ContractHelper(window.web3Object, window.thor)
const contractHelper = new ContractHelper(window.thor)
window.contractHelper = contractHelper
window.web3Loaded = true
EventBus.publish('web3Loaded')
Expand All @@ -52,7 +41,6 @@ class Web3Loader {

setDefaultAccounts() {
if (keyHandler.isLoggedIn()) {
window.web3Object.eth.defaultAccount = keyHandler.getAddress()
window.thor.eth.defaultAccount = keyHandler.getPubAddress().toLowerCase()
}
}
Expand Down
27 changes: 6 additions & 21 deletions src/Components/Dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ class Dashboard extends Component {
constructor(props) {
super(props)
this._balanceListener = new BalanceListener(
window.web3Object,
window.thor
)

i18n = getI18nFn(props.intl, messages)
this.state = {
view: props.view,
address: helper.getWeb3().eth.defaultAccount,
address: window.thor.eth.defaultAccount,
ethNetwork: 0,
ethBalance: {
loading: true,
Expand Down Expand Up @@ -92,28 +91,17 @@ class Dashboard extends Component {
}

setPubAddress(type) {
const selectedToken = type || this.state.selectedTokenContract
const vetPubAddress = keyHandler.getPubAddress()
this.setState({
address: vetPubAddress
})

if (selectedToken === '2') {
const vetPubAddress = keyHandler.getPubAddress()
this.setState({
address: vetPubAddress
})
} else {
this.setState({
address: helper.getWeb3().eth.defaultAccount
})
}
}

loadBalances = () => {
this._balanceListener.onBalancesChange(
({ ethBalance, vthoBalance }) => {
({ vthoBalance }) => {
this.setState({
ethBalance: {
amount: helper.formatEther(ethBalance.toString()),
loading: false
},
vthoBalance: {
amount: helper.formatEther(vthoBalance.toString()),
loading: false
Expand Down Expand Up @@ -279,10 +267,8 @@ class Dashboard extends Component {
selectedTokenContract={this.state.selectedTokenContract}
address={this.state.address}
currency={this.state.currency}
ethBalance={this.state.ethBalance.amount}
vthoBalance={this.state.vthoBalance.amount}
isLoading={
this.state.ethBalance.loading &&
this.state.vthoBalance.loading
}
onMenuToggle={this.onMenuToggle}
Expand Down Expand Up @@ -317,7 +303,6 @@ class Dashboard extends Component {
{this.renderAppBar()}
<div>
<DashboardRouter
ethBalance={this.state.ethBalance.amount}
vthoBalance={this.state.vthoBalance.amount}
selectedTokenContract={
this.state.selectedTokenContract
Expand Down
18 changes: 2 additions & 16 deletions src/Components/Dashboard/DashboardAppBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,13 @@ const constants = require('../Constants')
const BalanceSelector = ({
contractType,
currency,
ethBalance,
vthoBalance,
isLoading
}) => {
if (contractType === constants.TOKEN_TYPE_DBET_TOKEN_VET) {
return (
<EtherBalanceCounter
currency="VTHO"
balance={vthoBalance}
isLoading={isLoading}
/>
)
}
return (
<EtherBalanceCounter
currency="ETH"
balance={ethBalance}
currency="VTHO"
balance={vthoBalance}
isLoading={isLoading}
/>
)
Expand Down Expand Up @@ -80,17 +70,13 @@ function DashboardAppBar({
<BalanceSelector
contractType={selectedTokenContract}
currency={currency}
ethBalance={ethBalance}
vthoBalance={vthoBalance}
isLoading={isLoading}
/>
<AddressCounter
address={address}
listener={onAddressCopyListener}
/>
<TokenVersionSelector
contractType={selectedTokenContract} isLoading={isLoading}
/>
</div>
</Toolbar>
</AppBar>
Expand Down
45 changes: 0 additions & 45 deletions src/Components/Dashboard/DashboardDrawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,51 +205,6 @@ class DashboardDrawer extends React.Component {
className={this.props.classes.menuItem}
iconClass={this.props.classes.icon}
/>
<ListItem
button
onClick={this.handleToogleTokenVersionDrawerSubmenu}
className={this.props.classes.menuItem}
>
<ListItemIcon className={this.props.classes.icon}>
<FontAwesomeIcon icon="code-branch" />
</ListItemIcon>
<ListItemText
inset
primary={i18n('TokenVersions')}
/>
{this.state.isDrawerTokenVersionSubmenuOpen ? (
<ExpandLess
className={this.props.classes.icon}
/>
) : (
<ExpandMore
className={this.props.classes.icon}
/>
)}
</ListItem>
<Collapse
in={this.state.isDrawerTokenVersionSubmenuOpen}
timeout="auto"
unmountOnExit
>
<List component="nav">
{this.renderTokenVersionListItem(
i18n('V3Vet'),
constants.TOKEN_TYPE_DBET_TOKEN_VET
)}

{this.renderTokenVersionListItem(
i18n('V2Current'),
constants.TOKEN_TYPE_DBET_TOKEN_NEW
)}

{this.renderTokenVersionListItem(
i18n('V1Initial'),
constants.TOKEN_TYPE_DBET_TOKEN_OLD
)}
</List>
</Collapse>

{/* <CustomListItem
label={i18n('TokenInfo')}
icon="info"
Expand Down
5 changes: 1 addition & 4 deletions src/Components/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ class Helper {
}

getSelectedTokenContract = () => {
return localStorage.getItem(constants.LS_KEY_SELECTED_TOKEN_CONTRACT) !=
null
? localStorage.getItem(constants.LS_KEY_SELECTED_TOKEN_CONTRACT)
: constants.DBET_VET_TOKEN_ADDRESS
return constants.TOKEN_TYPE_DBET_TOKEN_VET;
}

setSelectedTokenContract = type => {
Expand Down
50 changes: 3 additions & 47 deletions src/Components/Send/Send.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Send extends Component {
super(props)
i18n = getI18nFn(props.intl, messages)
TOKEN_BALANCE_LOADING = i18n('Loading')
let address = helper.getWeb3().eth.defaultAccount
let address = window.thor.eth.defaultAccount
console.log('Pending txs', pendingTxHandler.getTxs())
this.state = new SendState(address, props.selectedTokenContract)
}
Expand Down Expand Up @@ -92,16 +92,8 @@ class Send extends Component {
}

initWeb3Data = () => {
if (
this.state.selectedTokenContract ===
constants.TOKEN_TYPE_DBET_TOKEN_VET
) {
this.vetTokenBalance()
this.loadEnergyCost()
} else {
this.oldTokenBalance()
this.newTokenBalance()
}
this.vetTokenBalance()
this.loadEnergyCost()
}

async loadEnergyCost(amount) {
Expand Down Expand Up @@ -132,42 +124,6 @@ class Send extends Component {
}
}

async oldTokenBalance() {
const contracts = helper.getContractHelper()
try {
const balance = await contracts.V1Token.balanceOf(
helper.getWeb3().eth.defaultAccount
)
let balances = this.state.balances
balances.oldToken = {
amount: helper.formatDbetsMax(balance),
loading: false
}
this.setState({ balances: balances })
console.log('V1 token balance', balance)
} catch (err) {
console.log('dbetBalance V1 err', err.message)
}
}

async newTokenBalance() {
const contracts = helper.getContractHelper()
try {
const balance = await contracts.V2Token.balanceOf(
helper.getWeb3().eth.defaultAccount
)
let balances = this.state.balances
balances.newToken = {
amount: helper.formatDbetsMax(balance),
loading: false
}
this.setState({ balances: balances })
console.log('V2 token balance', balance)
} catch (err) {
console.log('dbetBalance V2 err', err.message)
}
}

toggleDialog = (type, open) => {
let dialogs = this.state.dialogs

Expand Down
Loading