Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: metamask balance #404

Merged
merged 2 commits into from
Apr 28, 2022
Merged
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
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