Skip to content

Commit

Permalink
fix: metamask ballance (#404)
Browse files Browse the repository at this point in the history
Co-authored-by: Hildegard Lydia <37687466+hilyds@users.noreply.github.com>
  • Loading branch information
azmimuhammad and hilyds authored Apr 28, 2022
1 parent 2cdedd7 commit 8d9d237
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
17 changes: 11 additions & 6 deletions src/components/HeaderWallet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
import {mapState, mapMutations, mapActions} from "vuex"
import {ethAddressByAccountId} from "@/lib/polkadotProvider/query/userProfile"
import {queryBalance} from "@/lib/polkadotProvider/query/balance"
import {getBalanceETH} from "@/lib/metamask/wallet.js"
import {getBalanceDAI} from "@/lib/metamask/wallet.js"
import {fromEther} from "@/lib/balance-format"
import Button from "@/components/Button"
Expand Down Expand Up @@ -162,7 +162,8 @@ export default {
this.$emit("showWalletBinding", true)
},
async checkMetamaskAddress() {
async checkMetamaskBalance() {
this.metamaskAddress = this.metamaskWalletAddress
let address = ""
if (this.metamaskWalletAddress == "") {
const ethRegisterAddress = await ethAddressByAccountId(
Expand All @@ -175,11 +176,9 @@ export default {
}
if (address != "") {
const balance = await getBalanceETH(address)
const balance = await getBalanceDAI(address)
this.setMetamaskAddress(address)
this.setMetamaskBalance(balance)
this.metamaskAddress = address
this.metamaskBalance = balance
}
},
Expand All @@ -201,6 +200,7 @@ export default {
},
disconnectWallet() {
this.metamaskAddress = ""
this.clearWallet()
}
},
Expand All @@ -212,14 +212,19 @@ export default {
walletBalance() {
this.getBalance(this.walletBalance)
},
metamaskWalletAddress() {
this.checkMetamaskBalance()
}
},
async mounted() {
this.polkadotAddress = this.wallet.address
await this.fetchWalletBalance()
await this.checkMetamaskAddress()
if (this.metamaskWalletAddress) await this.checkMetamaskBalance()
}
}
</script>
Expand Down
9 changes: 5 additions & 4 deletions src/components/WalletBinding.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<div class="ml-3 text-left font-weight-bold">
{{ account.name }} (...{{ account.lastAddr }})
</div>
<div class="ml-3 text-left">{{ account.balance }} ETH</div>
<div class="ml-3 text-left">{{ account.balance }} DAI</div>
</v-col>
<v-col cols="12" v-if="account.active" lg="2" md="2" xl="2">
<v-icon color="green" :size="25"> mdi-check-bold </v-icon>
Expand Down Expand Up @@ -102,7 +102,7 @@
{{ selectAccount.name }} (...{{ selectAccount.lastAddr }})
</div>
<div class="ml-3 text-left">
{{ selectAccount.balance }} ETH
{{ selectAccount.balance }} DAI
</div>
</v-col>
<v-col cols="12" lg="2" md="2" xl="2">
Expand Down Expand Up @@ -158,7 +158,7 @@
/* eslint-disable */
import { mapState, mapMutations } from "vuex";
import { startApp } from "@/lib/metamask";
import { getBalanceETH } from "@/lib/metamask/wallet.js";
import { getBalanceETH, getBalanceDAI } from "@/lib/metamask/wallet.js";
import localStorage from "@/lib/local-storage"
export default {
Expand Down Expand Up @@ -223,7 +223,7 @@ export default {
} else {
this.accountList = [];
for (let x = 0; x < this.ethAccount.accountList.length; x++) {
const balance = await getBalanceETH(
const balance = await getBalanceDAI(
this.ethAccount.accountList[x]
);
var lastAddr = this.ethAccount.accountList[x].substr(
Expand All @@ -243,6 +243,7 @@ export default {
active: isActive,
});
}
this.putWallet = false;
this.putAccount = true;
}
Expand Down
10 changes: 10 additions & 0 deletions src/lib/metamask/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export async function getBalanceETH(address) {
}
}

export async function getBalanceDAI(address) {
const contractERC20Interface = store.getters["metamask/contracts/getERC20InterfaceContract"]
const web3 = store.getters["metamask/getWeb3"]

let balance = await contractERC20Interface.methods.balanceOf(address).call()
let daiBalance = web3.utils.fromWei(balance, "ether")

return daiBalance
}

export async function getBalanceUSDT() {
const contractERC20Interface = store.getters["metamask/contracts/getERC20InterfaceContract"];
let balance = await contractERC20Interface.methods
Expand Down

0 comments on commit 8d9d237

Please sign in to comment.