Skip to content

Commit 8402585

Browse files
authored
Merge pull request #188 from vechain/feat/addd-glodolar
feat: add usdglo
2 parents f9a12ef + 1a56502 commit 8402585

File tree

9 files changed

+76
-5
lines changed

9 files changed

+76
-5
lines changed

packages/vechain-kit/src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type AppConfig = {
3434
vetDomainsPublicResolverAddress: string;
3535
vetDomainsReverseRegistrarAddress: string;
3636
vnsResolverAddress: string;
37+
gloDollarContractAddress: string;
3738
vetDomainAvatarUrl: string;
3839
nodeUrl: string;
3940
indexerUrl: string;

packages/vechain-kit/src/config/mainnet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@ const config: AppConfig = {
8888
'0x5c970901a587BA3932C835D4ae5FAE2BEa7e78Bc',
8989
vnsResolverAddress: '0xA11413086e163e41901bb81fdc5617c975Fa5a1A',
9090
vetDomainAvatarUrl: 'https://vet.domains/api/avatar',
91+
gloDollarContractAddress: '0x29c630cce4ddb23900f5fe66ab55e488c15b9f5e',
9192
};
9293
export default config;

packages/vechain-kit/src/config/solo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const config: AppConfig = {
4141
vetDomainsReverseRegistrarAddress:
4242
'0x5c970901a587BA3932C835D4ae5FAE2BEa7e78Bc',
4343
vnsResolverAddress: '0x0000000000000000000000000000000000000000',
44+
gloDollarContractAddress: '0x0000000000000000000000000000000000000000',
4445
vetDomainAvatarUrl: 'https://testnet.vet.domains/api/avatar',
4546
indexerUrl: 'https://b3tr.testnet.vechain.org/api/v1',
4647
b3trIndexerUrl: 'https://b3tr.testnet.vechain.org/api/v1',

packages/vechain-kit/src/config/testnet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const config: AppConfig = {
4141
vetDomainsReverseRegistrarAddress:
4242
'0x6878f1aD5e3015310CfE5B38d7B7071C5D8818Ca',
4343
vnsResolverAddress: '0xc403b8EA53F707d7d4de095f0A20bC491Cf2bc94',
44+
gloDollarContractAddress: '0x0000000000000000000000000000000000000000',
4445
vetDomainAvatarUrl: 'https://testnet.vet.domains/api/avatar',
4546
indexerUrl: 'https://indexer.testnet.vechain.org/api/v1',
4647
b3trIndexerUrl: 'https://b3tr.testnet.vechain.org/api/v1',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useGetErc20Balance';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useQuery } from '@tanstack/react-query';
2+
import { useConnex } from '@vechain/dapp-kit-react';
3+
import { IERC20__factory } from '../../../contracts/typechain-types';
4+
import { formatEther } from 'ethers';
5+
import { humanNumber } from '@/utils';
6+
7+
export const getErc20Balance = async (
8+
thor: Connex.Thor,
9+
tokenAddress: string,
10+
address?: string,
11+
): Promise<{ original: string; scaled: string; formatted: string }> => {
12+
if (!tokenAddress || !address) {
13+
throw new Error('Token address and user address are required');
14+
}
15+
16+
const functionFragment = IERC20__factory.createInterface()
17+
.getFunction('balanceOf')
18+
.format('json');
19+
20+
const res = await thor
21+
.account(tokenAddress)
22+
.method(JSON.parse(functionFragment))
23+
.call(address);
24+
25+
if (res.reverted) throw new Error('Reverted');
26+
27+
const original = res.decoded[0];
28+
const scaled = formatEther(original);
29+
const formatted = scaled === '0' ? '0' : humanNumber(scaled);
30+
31+
return {
32+
original,
33+
scaled,
34+
formatted,
35+
};
36+
};
37+
38+
export const getErc20BalanceQueryKey = (
39+
tokenAddress: string,
40+
address?: string,
41+
) => ['VECHAIN_KIT_ERC20_BALANCE', tokenAddress, address];
42+
43+
export const useGetErc20Balance = (tokenAddress: string, address?: string) => {
44+
const { thor } = useConnex();
45+
46+
return useQuery({
47+
queryKey: getErc20BalanceQueryKey(tokenAddress, address),
48+
queryFn: async () => getErc20Balance(thor, tokenAddress, address),
49+
enabled: !!thor && !!address && !!tokenAddress,
50+
});
51+
};

packages/vechain-kit/src/hooks/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export * from './ipfs';
99
export * from './blockchain';
1010
export * from './nfts';
1111
export * from './smartAccount';
12+
export * from './erc20';

packages/vechain-kit/src/hooks/api/wallet/useBalances.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
useGetVeDelegateBalance,
77
useGetTokenUsdPrice,
88
useGetCustomTokenBalances,
9+
useGetErc20Balance,
910
} from '..';
1011
import { useVeChainKitConfig } from '@/providers';
1112
import { getConfig } from '@/config';
@@ -16,6 +17,7 @@ type UseBalancesProps = {
1617

1718
export const useBalances = ({ address = '' }: UseBalancesProps) => {
1819
const { network } = useVeChainKitConfig();
20+
const config = getConfig(network.type);
1921

2022
// Base token balances
2123
const { data: vetData, isLoading: vetLoading } = useAccountBalance(address);
@@ -31,6 +33,8 @@ export const useBalances = ({ address = '' }: UseBalancesProps) => {
3133
useGetVot3Balance(address);
3234
const { data: veDelegateBalance, isLoading: veDelegateLoading } =
3335
useGetVeDelegateBalance(address);
36+
const { data: gloDollarBalance, isLoading: gloDollarLoading } =
37+
useGetErc20Balance(config.gloDollarContractAddress, address);
3438

3539
// Custom token balances
3640
const customTokenBalancesQueries = useGetCustomTokenBalances(address);
@@ -50,15 +54,17 @@ export const useBalances = ({ address = '' }: UseBalancesProps) => {
5054
b3trUsdPriceLoading ||
5155
veDelegateLoading ||
5256
vthoUsdPriceLoading ||
53-
customTokensLoading;
57+
customTokensLoading ||
58+
gloDollarLoading;
5459

5560
// Get contract addresses from config
5661
const contractAddresses = {
5762
vet: '0x',
58-
vtho: getConfig(network.type).vthoContractAddress,
59-
b3tr: getConfig(network.type).b3trContractAddress,
60-
vot3: getConfig(network.type).vot3ContractAddress,
61-
veDelegate: getConfig(network.type).veDelegate,
63+
vtho: config.vthoContractAddress,
64+
b3tr: config.b3trContractAddress,
65+
vot3: config.vot3ContractAddress,
66+
veDelegate: config.veDelegate,
67+
USDGLO: config.gloDollarContractAddress,
6268
};
6369

6470
// Base balances using contract addresses
@@ -93,6 +99,12 @@ export const useBalances = ({ address = '' }: UseBalancesProps) => {
9399
symbol: 'veDelegate',
94100
priceAddress: contractAddresses.b3tr, // using b3tr price for veDelegate
95101
},
102+
{
103+
address: contractAddresses.USDGLO,
104+
value: gloDollarBalance?.scaled ?? '0',
105+
symbol: 'USDGLO',
106+
priceAddress: contractAddresses.USDGLO,
107+
},
96108
];
97109

98110
// Add custom token balances
@@ -112,6 +124,7 @@ export const useBalances = ({ address = '' }: UseBalancesProps) => {
112124
{ address: contractAddresses.vet, price: vetUsdPrice || 0 },
113125
{ address: contractAddresses.vtho, price: vthoUsdPrice || 0 },
114126
{ address: contractAddresses.b3tr, price: b3trUsdPrice || 0 },
127+
{ address: contractAddresses.USDGLO, price: 1 }, // gloDollar is pegged to 1 USD
115128
];
116129

117130
// Compute total balance

packages/vechain-kit/src/utils/Constants.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const TOKEN_LOGOS: Record<string, string> = {
99
VOT3: 'https://vechain.github.io/token-registry/assets/17ff70aa1d898bc97ad690dbfad1a3b5643f7e0b.png',
1010
veDelegate:
1111
'https://vechain.github.io/token-registry/assets/1c641b86096d56bf13d49f38388accd6db8b8b2e.png',
12+
USDGLO: 'https://raw.githubusercontent.com/vechain/app-hub/439fba60c80ba2521d435981102d88c4aec050d6/apps/org.glodollar.app/logo.png',
1213
};
1314

1415
export const TOKEN_LOGO_COMPONENTS: Record<string, JSX.Element> = {

0 commit comments

Comments
 (0)