generated from Tahul/vue-composable-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): index.vue add useEnsName and useBalance
- Loading branch information
1 parent
ecde823
commit 5116909
Showing
3 changed files
with
116 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { formatEther, type Provider } from 'ethers' | ||
|
||
export function useBalance(defaultProvider: Provider) { | ||
const balance = ref(0) | ||
const loading = ref(false) | ||
const error = ref(null) | ||
let latestFetchId = 0 | ||
|
||
async function fetch(address: string) { | ||
error.value = null | ||
balance.value = 0 | ||
loading.value = true | ||
|
||
const fetchId = ++latestFetchId | ||
|
||
try { | ||
if (fetchId === latestFetchId) { | ||
balance.value = Number(formatEther(await defaultProvider.getBalance(address))) | ||
} | ||
} catch (e: any) { | ||
if (fetchId === latestFetchId) { | ||
error.value = e | ||
} | ||
} finally { | ||
if (fetchId === latestFetchId) { | ||
loading.value = false | ||
} | ||
} | ||
} | ||
|
||
function ignorePreviousFetch() { | ||
latestFetchId++ | ||
error.value = null | ||
balance.value = 0 | ||
loading.value = false | ||
} | ||
|
||
return { | ||
balance, | ||
loading, | ||
error, | ||
fetch, | ||
ignorePreviousFetch, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { Provider } from 'ethers' | ||
|
||
export function useEnsName(defaultProvider: Provider) { | ||
const ensName = ref('') | ||
const loading = ref(false) | ||
const error = ref(null) | ||
let latestFetchId = 0 | ||
|
||
async function fetch(address: string) { | ||
error.value = null | ||
ensName.value = '' | ||
loading.value = true | ||
|
||
const fetchId = ++latestFetchId | ||
|
||
try { | ||
const result = await defaultProvider.lookupAddress(address) | ||
|
||
if (fetchId === latestFetchId) { | ||
ensName.value = result ?? '' | ||
} | ||
} catch (e: any) { | ||
if (fetchId === latestFetchId) { | ||
error.value = e | ||
} | ||
} finally { | ||
if (fetchId === latestFetchId) { | ||
loading.value = false | ||
} | ||
} | ||
} | ||
|
||
function ignorePreviousFetch() { | ||
latestFetchId++ | ||
error.value = null | ||
ensName.value = '' | ||
loading.value = false | ||
} | ||
|
||
return { | ||
ensName, | ||
loading, | ||
error, | ||
fetch, | ||
ignorePreviousFetch, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters