-
Notifications
You must be signed in to change notification settings - Fork 185
chore: solana v2 #1549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gin-lsl
wants to merge
2
commits into
main
Choose a base branch
from
feat/solana-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
chore: solana v2 #1549
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,5 @@ | ||
| import { defineConfig } from 'father'; | ||
|
|
||
| export default defineConfig({ | ||
| extends: '../../.fatherrc.base.ts', | ||
| }); |
This file contains hidden or 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,69 @@ | ||
| { | ||
| "name": "@ant-design/web3-solana-v2", | ||
| "version": "2.0.0", | ||
| "main": "dist/lib/index.js", | ||
| "module": "dist/esm/index.js", | ||
| "typings": "dist/esm/index.d.ts", | ||
| "exports": { | ||
| "import": "./dist/esm/index.js", | ||
| "require": "./dist/lib/index.js", | ||
| "types": "./dist/esm/index.d.ts" | ||
| }, | ||
| "sideEffects": false, | ||
| "files": [ | ||
| "dist", | ||
| "CHANGELOG.md", | ||
| "README.md" | ||
| ], | ||
| "keywords": [ | ||
| "ant", | ||
| "design", | ||
| "web3", | ||
| "antd", | ||
| "component", | ||
| "components", | ||
| "framework", | ||
| "frontend", | ||
| "react", | ||
| "react-component", | ||
| "ui", | ||
| "solana" | ||
| ], | ||
| "homepage": "https://web3.ant.design", | ||
| "bugs": { | ||
| "url": "https://github.com/ant-design/ant-design-web3/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/ant-design/ant-design-web3" | ||
| }, | ||
| "scripts": { | ||
| "dev": "father dev", | ||
| "build": "father build" | ||
| }, | ||
| "dependencies": { | ||
| "@ant-design/web3-assets": "workspace:*", | ||
| "@ant-design/web3-common": "workspace:*", | ||
| "@solana/kit": "^5.0.0", | ||
| "@solana/react": "^5.0.0", | ||
| "@wallet-standard/core": "^1.1.1", | ||
| "@solana/wallet-standard": "^1.1.4", | ||
| "@wallet-standard/react": "^1.0.1", | ||
| "@metaplex-foundation/mpl-token-metadata": "^3.4.0", | ||
| "bs58": "^6.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "father": "^4.6.2", | ||
| "typescript": "^5.6.2" | ||
| }, | ||
| "publishConfig": { | ||
| "registry": "https://registry.npmjs.org", | ||
| "access": "public" | ||
| }, | ||
| "browserslist": [ | ||
| "last 2 versions", | ||
| "Firefox ESR", | ||
| "> 1%", | ||
| "ie >= 11" | ||
| ] | ||
| } | ||
This file contains hidden or 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 { SolanaChainIds } from '@ant-design/web3-common'; | ||
|
|
||
| export interface SolanaChainConfig { | ||
| id: SolanaChainIds; | ||
| name: string; | ||
| network: string; | ||
| rpcUrls: Record<string, string>; | ||
| } | ||
|
|
||
| export const solana: SolanaChainConfig = { | ||
| id: SolanaChainIds.MainnetBeta, | ||
| name: 'Solana', | ||
| network: 'mainnet-beta', | ||
| rpcUrls: { | ||
| default: 'https://api.mainnet-beta.solana.com', | ||
| }, | ||
| }; | ||
|
|
||
| export const solanaDevnet: SolanaChainConfig = { | ||
| id: SolanaChainIds.Devnet, | ||
| name: 'Solana Devnet', | ||
| network: 'devnet', | ||
| rpcUrls: { | ||
| default: 'https://api.devnet.solana.com', | ||
| }, | ||
| }; | ||
|
|
||
| export const solanaTestnet: SolanaChainConfig = { | ||
| id: SolanaChainIds.Testnet, | ||
| name: 'Solana Testnet', | ||
| network: 'testnet', | ||
| rpcUrls: { | ||
| default: 'https://api.testnet.solana.com', | ||
| }, | ||
| }; |
This file contains hidden or 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,3 @@ | ||
| export * from './chains'; | ||
| export * from './solana-provider'; | ||
| export * from './solana-provider/nft-metadata-provider'; |
248 changes: 248 additions & 0 deletions
248
packages/solana-v2/src/solana-provider/config-provider.tsx
This file contains hidden or 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,248 @@ | ||
| import { useEffect, useMemo, useState, type PropsWithChildren } from 'react'; | ||
| import type { Account, Chain, Locale, Wallet } from '@ant-design/web3-common'; | ||
| import { ConnectStatus, Web3ConfigProvider } from '@ant-design/web3-common'; | ||
| import { address } from '@solana/kit'; | ||
| import { useSignIn } from '@solana/react'; | ||
| import { SOLANA_CHAINS } from '@solana/wallet-standard'; | ||
| import { StandardConnect, StandardDisconnect, type IdentifierString } from '@wallet-standard/core'; | ||
| import { useConnect, useDisconnect, useWallets, type UiWallet } from '@wallet-standard/react'; | ||
|
|
||
| import type { SolanaChainConfig } from '../chains'; | ||
| import { useRpc } from './rpc-provider'; | ||
|
|
||
| interface ProviderCommonProps extends PropsWithChildren { | ||
| locale?: Locale; | ||
| chainAssets?: Chain[]; | ||
| availableChains: SolanaChainConfig[]; | ||
| currentChain?: SolanaChainConfig; | ||
| balance?: boolean; | ||
| onCurrentChainChange: (chain?: SolanaChainConfig) => void; | ||
| } | ||
|
|
||
| interface AntDesignWeb3ConfigProviderInnerProps extends ProviderCommonProps { | ||
| uiWallet?: UiWallet; | ||
| } | ||
|
|
||
| export function AntDesignWeb3ConfigProviderInner(props: AntDesignWeb3ConfigProviderInnerProps) { | ||
| const [uiWallet, setUiWallet] = useState<UiWallet>(); | ||
|
|
||
| const wallets = useWallets(); | ||
|
|
||
| const standardSolanaWallets = useMemo( | ||
| () => | ||
| wallets.filter((w) => { | ||
| return ( | ||
| // only include solana chains | ||
| w.chains.some((c) => (SOLANA_CHAINS as unknown as IdentifierString[]).includes(c)) && | ||
| // only include standard:connect and standard:disconnect features | ||
| w.features.includes(StandardConnect) && | ||
| w.features.includes(StandardDisconnect) | ||
| ); | ||
| }), | ||
| [wallets], | ||
| ); | ||
|
|
||
| const chainList = useMemo(() => { | ||
| return props.availableChains | ||
| .map((item) => { | ||
| const c = props.chainAssets?.find((asset) => { | ||
| return asset.id === item.id; | ||
| }) as Chain; | ||
|
|
||
| if (c?.id) { | ||
| return { | ||
| ...item, | ||
| ...c, | ||
| id: c.id, | ||
| name: c.name, | ||
| icon: c.icon, | ||
| }; | ||
| } | ||
| console.error( | ||
| `Can not find chain ${item.id}, SolanaWeb3ConfigProvider only support Solana`, | ||
| ); | ||
| return null; | ||
| }) | ||
| .filter((item) => item !== null) as (Chain & SolanaChainConfig)[]; | ||
| }, [props.availableChains, props.chainAssets]); | ||
|
|
||
| const availableWallets = useMemo(() => { | ||
| return standardSolanaWallets.map<Wallet>((w) => { | ||
| return { | ||
| name: w.name, | ||
| remark: w.name, | ||
| icon: w.icon, | ||
| hasExtensionInstalled: () => Promise.resolve(true), | ||
| hasWalletReady: () => Promise.resolve(true), | ||
| }; | ||
| }); | ||
| }, [standardSolanaWallets]); | ||
|
|
||
| const adapter = useMemo(() => { | ||
| if (uiWallet) { | ||
| return ( | ||
| <AntDesignWeb3ConfigProviderWithWalletAdapter | ||
| uiWallet={uiWallet} | ||
| chainList={chainList} | ||
| availableWallets={availableWallets} | ||
| availableChains={props.availableChains} | ||
| currentChain={props.currentChain} | ||
| balance={props.balance} | ||
| locale={props.locale} | ||
| onCurrentChainChange={props.onCurrentChainChange} | ||
| > | ||
| {props.children} | ||
| </AntDesignWeb3ConfigProviderWithWalletAdapter> | ||
| ); | ||
| } else { | ||
| return ( | ||
| <AntDesignWeb3ConfigProviderWithoutWalletAdapter | ||
| chainList={chainList} | ||
| availableWallets={availableWallets} | ||
| availableChains={props.availableChains} | ||
| onCurrentChainChange={props.onCurrentChainChange} | ||
| onConnectWallet={(wallet) => { | ||
| const foundUiWallet = wallets.find((w) => w.name === wallet?.name); | ||
|
|
||
| setUiWallet(foundUiWallet); | ||
| }} | ||
| > | ||
| {props.children} | ||
| </AntDesignWeb3ConfigProviderWithoutWalletAdapter> | ||
| ); | ||
| } | ||
| }, [availableWallets, chainList, props.availableChains, props.children, uiWallet, wallets]); | ||
|
|
||
| return adapter; | ||
| } | ||
|
|
||
| interface AntDesignWeb3ConfigProviderWithWalletAdapterProps extends ProviderCommonProps { | ||
| chainList: (Chain & SolanaChainConfig)[]; | ||
| availableWallets: Wallet[]; | ||
| uiWallet: UiWallet; | ||
| } | ||
|
|
||
| function AntDesignWeb3ConfigProviderWithWalletAdapter( | ||
| props: AntDesignWeb3ConfigProviderWithWalletAdapterProps, | ||
| ) { | ||
| const { uiWallet, ...othersProps } = props; | ||
|
|
||
| const { rpc } = useRpc(); | ||
|
|
||
| const [account, setAccount] = useState<Account>(); | ||
| const [balance, setBalance] = useState<bigint>(); | ||
|
|
||
| const signIn = useSignIn(uiWallet); | ||
| const [isConnecting, connect] = useConnect(uiWallet); | ||
| const [isDisconnecting, disconnect] = useDisconnect(uiWallet); | ||
|
|
||
| const handleConnectWallet = async () => { | ||
| const result = await connect(); | ||
|
|
||
| if (result.length > 0) { | ||
| const defaultAccount = result[0]; | ||
| setAccount({ | ||
| address: defaultAccount.address, | ||
| name: defaultAccount.label, | ||
| avatar: defaultAccount.icon, | ||
| status: ConnectStatus.Connected, | ||
| }); | ||
| } | ||
| console.log('connect result', result); | ||
| }; | ||
|
|
||
| // get balance | ||
| useEffect(() => { | ||
| if (!(account && othersProps.balance)) { | ||
| return; | ||
| } | ||
|
|
||
| const getBalance = async () => { | ||
| const { value: balanceVal } = await rpc.getBalance(address(account.address)).send(); | ||
|
|
||
| setBalance(balanceVal); | ||
| }; | ||
| getBalance(); | ||
| }, [account, othersProps.balance, rpc]); | ||
|
|
||
| useEffect(() => { | ||
| handleConnectWallet(); | ||
| }, [uiWallet]); | ||
|
|
||
| const currentChain = useMemo(() => { | ||
| return othersProps.chainList.find((c) => c.id === othersProps.currentChain?.id); | ||
| }, [othersProps.chainList, othersProps.currentChain]); | ||
|
|
||
| const currency = currentChain?.nativeCurrency; | ||
|
|
||
| return ( | ||
| <Web3ConfigProvider | ||
| locale={othersProps.locale} | ||
| account={account} | ||
| balance={ | ||
| othersProps.balance | ||
| ? { | ||
| value: balance, | ||
| icon: currency?.icon, | ||
| symbol: currency?.symbol, | ||
| decimals: currency?.decimals, | ||
| } | ||
| : undefined | ||
| } | ||
| chain={othersProps.currentChain} | ||
| addressPrefix={false} | ||
| availableChains={othersProps.chainList} | ||
| availableWallets={othersProps.availableWallets} | ||
| switchChain={async (_chain) => { | ||
| console.log('[TODO] with_wallet switchChain', _chain); | ||
| const foundChain = othersProps.chainList.find((c) => c.id === _chain.id); | ||
| othersProps.onCurrentChainChange(foundChain); | ||
| }} | ||
| connect={async (_wallet) => { | ||
| console.log('[TODO] with_wallet connect', _wallet); | ||
| // othersProps.onConnectWallet(_wallet); | ||
| }} | ||
| disconnect={async () => { | ||
| console.log('[TODO] with_wallet disconnect'); | ||
| }} | ||
| > | ||
| {othersProps.children} | ||
| </Web3ConfigProvider> | ||
| ); | ||
| } | ||
|
|
||
| interface AntDesignWeb3ConfigProviderWithoutWalletAdapterProps extends ProviderCommonProps { | ||
| chainList: (Chain & SolanaChainConfig)[]; | ||
| availableWallets?: Wallet[]; | ||
| onConnectWallet: (wallet?: Wallet) => void; | ||
| } | ||
|
|
||
| function AntDesignWeb3ConfigProviderWithoutWalletAdapter( | ||
| props: AntDesignWeb3ConfigProviderWithoutWalletAdapterProps, | ||
| ) { | ||
| return ( | ||
| <Web3ConfigProvider | ||
| locale={props.locale} | ||
| account={undefined} | ||
| balance={undefined} | ||
| chain={props.currentChain} | ||
| addressPrefix={false} | ||
| availableChains={props.chainList} | ||
| availableWallets={props.availableWallets} | ||
| switchChain={async (_chain) => { | ||
| console.log('[TODO] without_wallet switchChain', _chain); | ||
| const foundChain = props.chainList.find((c) => c.id === _chain.id); | ||
| props.onCurrentChainChange(foundChain); | ||
| }} | ||
| connect={async (_wallet) => { | ||
| console.log('[TODO] without_wallet connect', _wallet); | ||
| props.onConnectWallet(_wallet); | ||
| }} | ||
| disconnect={async () => { | ||
| console.log('[TODO] without_wallet disconnect'); | ||
| }} | ||
| > | ||
| {props.children} | ||
| </Web3ConfigProvider> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉应该是 "@ant-design/web3-solana“ 发 2.0 版本
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对,暂时先不动 v1 的代码供大家参考。等完成的差不多评审完后,会直接替换旧代码。