Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@
"@ant-design/web3-ton": "workspace:*",
"@ant-design/web3-tron": "workspace:*",
"@ant-design/web3-wagmi": "workspace:*",
"@ant-design/web3-solana-v2": "workspace:*",
"@mysten/dapp-kit": "^0.14.50",
"@mysten/sui": "^1.37.1",
"@solana/wallet-adapter-coinbase": "^0.1.22",
"@solana/wallet-adapter-trust": "^0.1.13",
"@solana/web3.js": "^1.98.0",
"@solana/kit": "^5.0.0",
"@tanstack/query-sync-storage-persister": "^5.90.4",
"@tanstack/react-query": "^5.51.11",
"@tanstack/react-query-persist-client": "^5.56.2",
Expand Down
5 changes: 5 additions & 0 deletions packages/solana-v2/.fatherrc.ts
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',
});
69 changes: 69 additions & 0 deletions packages/solana-v2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@ant-design/web3-solana-v2",
Copy link
Collaborator

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 版本

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对,暂时先不动 v1 的代码供大家参考。等完成的差不多评审完后,会直接替换旧代码。

"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"
]
}
35 changes: 35 additions & 0 deletions packages/solana-v2/src/chains.tsx
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',
},
};
3 changes: 3 additions & 0 deletions packages/solana-v2/src/index.ts
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 packages/solana-v2/src/solana-provider/config-provider.tsx
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>
);
}
Loading
Loading