-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
254bebd
commit fda85b1
Showing
4 changed files
with
137 additions
and
73 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
packages/dapp-kit-react/src/hooks/useVechainDomain/api/fetchVechainDomain.ts
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,78 @@ | ||
import { addressUtils } from '@vechain/sdk-core'; | ||
import type { DAppKitContext } from '../../../types'; | ||
import { getDomain } from './getDomain'; | ||
import { getAddress } from './getAddress'; | ||
|
||
export interface VechainDomainResult { | ||
address: string | undefined; | ||
domain: string | undefined; | ||
isValidAddressOrDomain: boolean; | ||
} | ||
|
||
/** | ||
* Function to fetch the vechain domain of an account and vice versa by passing the connex object | ||
*/ | ||
export const fetchVechainDomain = async ({ | ||
addressOrDomain, | ||
connex, | ||
}: { | ||
addressOrDomain?: string | null; | ||
connex: DAppKitContext['connex']; | ||
}): Promise<VechainDomainResult> => { | ||
if (!addressOrDomain) { | ||
return { | ||
address: undefined, | ||
domain: undefined, | ||
isValidAddressOrDomain: false, | ||
}; | ||
} | ||
|
||
const isValidAddress = addressUtils.isAddress(addressOrDomain); | ||
|
||
if (isValidAddress) { | ||
try { | ||
const domainName = await getDomain({ | ||
address: addressOrDomain, | ||
connex, | ||
}); | ||
return { | ||
address: addressOrDomain, | ||
domain: domainName, | ||
isValidAddressOrDomain: true, | ||
}; | ||
} catch (err) { | ||
console.error('Error getting domain: ', err); | ||
return { | ||
address: addressOrDomain, | ||
domain: undefined, | ||
isValidAddressOrDomain: true, | ||
}; | ||
} | ||
} | ||
|
||
try { | ||
const domainAddress = await getAddress({ | ||
domain: addressOrDomain, | ||
connex, | ||
}); | ||
if (domainAddress === '0x0000000000000000000000000000000000000000') { | ||
return { | ||
address: undefined, | ||
domain: undefined, | ||
isValidAddressOrDomain: false, | ||
}; | ||
} | ||
return { | ||
address: domainAddress, | ||
domain: addressOrDomain, | ||
isValidAddressOrDomain: true, | ||
}; | ||
} catch (err) { | ||
console.error('Error getting address: ', err); | ||
return { | ||
address: undefined, | ||
domain: undefined, | ||
isValidAddressOrDomain: false, | ||
}; | ||
} | ||
}; |
31 changes: 31 additions & 0 deletions
31
packages/dapp-kit-react/src/hooks/useVechainDomain/useVechainDomain.catch.test.tsx
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,31 @@ | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { useVechainDomain } from './useVechainDomain'; | ||
import { wrapper } from '../../../test'; | ||
|
||
vi.mock('./api/fetchVechainDomain', () => ({ | ||
...vi.importActual('./api/fetchVechainDomain'), | ||
fetchVechainDomain: vi.fn().mockImplementation(async () => { | ||
throw new Error('Network error'); | ||
}), | ||
})); | ||
|
||
describe('useVechainDomain error handling', () => { | ||
it('should handle error when fetching domain', async () => { | ||
const { result } = renderHook( | ||
() => useVechainDomain({ addressOrDomain: 'test.vet' }), | ||
{ wrapper }, | ||
); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current).toEqual({ | ||
address: undefined, | ||
domain: undefined, | ||
isLoading: false, | ||
isValidAddressOrDomain: false, | ||
}); | ||
}); | ||
}); | ||
}); |
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
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