-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wagmi provider support ENS (#101)
Co-authored-by: yutingzhao1991 <[email protected]>
- Loading branch information
1 parent
89c6077
commit 86ab2eb
Showing
12 changed files
with
202 additions
and
45 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
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
17 changes: 17 additions & 0 deletions
17
packages/wagmi/src/wagmi-provider/methods/addNameToAccounts.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,17 @@ | ||
import type { Account } from '@ant-design/web3-common'; | ||
import { fetchEnsName } from '@wagmi/core'; | ||
|
||
export async function addNameToAccounts(accounts: Account[], chainId?: number): Promise<Account[]> { | ||
return Promise.all( | ||
accounts.map(async (account) => { | ||
const name = await fetchEnsName({ | ||
address: account.address as `0x${string}`, | ||
chainId, | ||
}); | ||
return { | ||
...account, | ||
name: name ?? undefined, | ||
}; | ||
}), | ||
); | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/wagmi/src/wagmi-provider/methods/getNFTMetadata.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,31 @@ | ||
import { requestWeb3Asset, fillAddressWith0x, type NFTMetadata } from '@ant-design/web3-common'; | ||
import { readContract } from '@wagmi/core'; | ||
|
||
export async function getNFTMetadata( | ||
address: string, | ||
tokenId: bigint, | ||
chainId?: number, | ||
): Promise<NFTMetadata> { | ||
const tokenURI = await readContract({ | ||
address: fillAddressWith0x(address), | ||
args: [tokenId], | ||
chainId, | ||
abi: [ | ||
{ | ||
name: 'tokenURI', | ||
inputs: [ | ||
{ | ||
name: 'tokenId', | ||
type: 'uint256', | ||
}, | ||
], | ||
outputs: [{ name: '', type: 'string' }], | ||
stateMutability: 'view', | ||
type: 'function', | ||
}, | ||
], | ||
functionName: 'tokenURI', | ||
}); | ||
const metaInfo = await requestWeb3Asset(tokenURI as string); | ||
return metaInfo; | ||
} |
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,35 @@ | ||
import { addNameToAccounts } from './index'; | ||
import { vi, describe, it, expect } from 'vitest'; | ||
|
||
vi.mock('@wagmi/core', () => { | ||
return { | ||
fetchEnsName: ({ address }: { address: string }) => { | ||
if (address === '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B') { | ||
return 'wanderingearth,eth'; | ||
} | ||
return null; | ||
}, | ||
}; | ||
}); | ||
|
||
describe('wagmi-provider/methods/index.ts', () => { | ||
it('addNameToAccounts', async () => { | ||
const accounts = await addNameToAccounts([ | ||
{ | ||
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B', | ||
}, | ||
{ | ||
address: '0x21CDf0974d53a6e96eF05d7B324a9803735f0000', | ||
}, | ||
]); | ||
expect(accounts).toEqual([ | ||
{ | ||
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B', | ||
name: 'wanderingearth,eth', | ||
}, | ||
{ | ||
address: '0x21CDf0974d53a6e96eF05d7B324a9803735f0000', | ||
}, | ||
]); | ||
}); | ||
}); |
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,2 @@ | ||
export * from './addNameToAccounts'; | ||
export * from './getNFTMetadata'; |
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,42 @@ | ||
import { Connector, type ConnectorTriggerProps, type Account } from '@ant-design/web3'; | ||
import React from 'react'; | ||
import { Button } from 'antd'; | ||
import { render } from '@testing-library/react'; | ||
import { it, describe, expect } from 'vitest'; | ||
|
||
describe('Connector', () => { | ||
it('name', async () => { | ||
const CustomButton: React.FC<React.PropsWithChildren<ConnectorTriggerProps>> = (props) => { | ||
const { name, address, connected, onConnectClick, onDisconnectClick, children } = props; | ||
return ( | ||
<Button | ||
onClick={() => { | ||
if (connected) { | ||
onDisconnectClick?.(); | ||
} else { | ||
onConnectClick?.(); | ||
} | ||
}} | ||
> | ||
{(name || address) ?? children} | ||
</Button> | ||
); | ||
}; | ||
|
||
const App = () => { | ||
const [accounts] = React.useState<Account[]>([ | ||
{ | ||
address: '0x1234567890', | ||
name: 'wanderingearth.eth', | ||
}, | ||
]); | ||
return ( | ||
<Connector accounts={accounts}> | ||
<CustomButton>children</CustomButton> | ||
</Connector> | ||
); | ||
}; | ||
const { baseElement } = render(<App />); | ||
expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('wanderingearth.eth'); | ||
}); | ||
}); |
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,37 @@ | ||
import { createConfig, configureChains, mainnet } from 'wagmi'; | ||
import { publicProvider } from 'wagmi/providers/public'; | ||
import { MetaMaskConnector } from 'wagmi/connectors/metaMask'; | ||
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'; | ||
import { WagmiWeb3ConfigProvider } from '@ant-design/web3-wagmi'; | ||
import { ConnectButton, Connector } from '@ant-design/web3'; | ||
|
||
const { publicClient, chains } = configureChains([mainnet], [publicProvider()]); | ||
|
||
const config = createConfig({ | ||
autoConnect: true, | ||
publicClient, | ||
connectors: [ | ||
new MetaMaskConnector({ | ||
chains, | ||
}), | ||
new WalletConnectConnector({ | ||
chains, | ||
options: { | ||
showQrModal: false, | ||
projectId: YOUR_WALLET_CONNET_PROJECT_ID, | ||
}, | ||
}), | ||
], | ||
}); | ||
|
||
const App: React.FC = () => { | ||
return ( | ||
<WagmiWeb3ConfigProvider ens config={config}> | ||
<Connector> | ||
<ConnectButton /> | ||
</Connector> | ||
</WagmiWeb3ConfigProvider> | ||
); | ||
}; | ||
|
||
export default App; |
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