-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch contract info for contract interactions (#3652)
* feat: fetch contract info for contract interactions
- Loading branch information
Showing
7 changed files
with
102 additions
and
19 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
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,67 @@ | ||
import { render, waitFor } from '@/tests/test-utils' | ||
import NamedAddressInfo from '.' | ||
import { faker } from '@faker-js/faker' | ||
import { getContract, type ChainInfo } from '@safe-global/safe-gateway-typescript-sdk' | ||
|
||
const mockChainInfo = { | ||
chainId: '4', | ||
shortName: 'tst', | ||
blockExplorerUriTemplate: { | ||
address: 'https://test.scan.eth/{address}', | ||
api: 'https://test.scan.eth/', | ||
txHash: 'https://test.scan.eth/{txHash}', | ||
}, | ||
} as ChainInfo | ||
|
||
jest.mock('@safe-global/safe-gateway-typescript-sdk', () => ({ | ||
...jest.requireActual('@safe-global/safe-gateway-typescript-sdk'), | ||
getContract: jest.fn(), | ||
__esModule: true, | ||
})) | ||
|
||
describe('NamedAddressInfo', () => { | ||
const getContractMock = getContract as jest.Mock | ||
it('should not fetch contract info if name / logo is given', async () => { | ||
const result = render( | ||
<NamedAddressInfo | ||
address={faker.finance.ethereumAddress()} | ||
name="TestAddressName" | ||
customAvatar="https://img.test.safe.global" | ||
/>, | ||
{ | ||
initialReduxState: { | ||
chains: { | ||
loading: false, | ||
data: [mockChainInfo], | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
expect(result.getByText('TestAddressName')).toBeVisible() | ||
expect(getContractMock).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should fetch contract info if name / logo is not given', async () => { | ||
const address = faker.finance.ethereumAddress() | ||
getContractMock.mockResolvedValue({ | ||
displayName: 'Resolved Test Name', | ||
name: 'ResolvedTestName', | ||
logoUri: 'https://img-resolved.test.safe.global', | ||
}) | ||
const result = render(<NamedAddressInfo address={address} />, { | ||
initialReduxState: { | ||
chains: { | ||
loading: false, | ||
data: [mockChainInfo], | ||
}, | ||
}, | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(result.getByText('Resolved Test Name')).toBeVisible() | ||
}) | ||
|
||
expect(getContractMock).toHaveBeenCalledWith('4', address) | ||
}) | ||
}) |
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,24 @@ | ||
import useAsync from '@/hooks/useAsync' | ||
import useChainId from '@/hooks/useChainId' | ||
import { getContract } from '@safe-global/safe-gateway-typescript-sdk' | ||
import EthHashInfo from '../EthHashInfo' | ||
import type { EthHashInfoProps } from '../EthHashInfo/SrcEthHashInfo' | ||
|
||
const NamedAddressInfo = ({ address, name, customAvatar, ...props }: EthHashInfoProps) => { | ||
const chainId = useChainId() | ||
const [contract] = useAsync( | ||
() => (!name && !customAvatar ? getContract(chainId, address) : undefined), | ||
[address, chainId, name, customAvatar], | ||
) | ||
|
||
return ( | ||
<EthHashInfo | ||
address={address} | ||
name={name || contract?.displayName || contract?.name} | ||
customAvatar={customAvatar || contract?.logoUri} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
export default NamedAddressInfo |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4336,10 +4336,10 @@ | |
"@safe-global/safe-core-sdk-utils" "^1.7.4" | ||
ethers "5.7.2" | ||
|
||
"@safe-global/[email protected].0-alpha.3": | ||
version "3.21.0-alpha.3" | ||
resolved "https://registry.yarnpkg.com/@safe-global/safe-gateway-typescript-sdk/-/safe-gateway-typescript-sdk-3.21.0-alpha.3.tgz#21e0f01e9fbc6cff504fa9c7f957c27877b3f982" | ||
integrity sha512-31xjB8VYUMsJ9akBd30YqHt4mreAwF5Kwbfa0LO0lE5L/DvfIwYiK/VB9uMse3LF/gVKKVOu9TIxKkaTsh9Y5w== | ||
"@safe-global/[email protected].1": | ||
version "3.21.1" | ||
resolved "https://registry.yarnpkg.com/@safe-global/safe-gateway-typescript-sdk/-/safe-gateway-typescript-sdk-3.21.1.tgz#984ec2d3d4211caf6a96786ab922b39909093538" | ||
integrity sha512-7nakIjcRSs6781LkizYpIfXh1DYlkUDqyALciqz/BjFU/S97sVjZdL4cuKsG9NEarytE+f6p0Qbq2Bo1aocVUA== | ||
|
||
"@safe-global/safe-gateway-typescript-sdk@^3.5.3": | ||
version "3.19.0" | ||
|