Skip to content

Commit

Permalink
feat: Add adapter for Solana (#472)
Browse files Browse the repository at this point in the history
* feat: Add adapter for Solana

* fix: conflict

* chore: format

* test: update snapshot

* test: fix test cases

* chore: update deps

* refactor: update types

* docs: del solana basic demo

* chore(solana): remove re-export web3-assets

* feat: revert chain.id type to number

* test: add chain id duplicate check

* chore: add changeset

* feat: handle switch chain

* docs: add SolanaWeb3ConfigProvider

* feat: SolanaProvider support pass chains and balance

* feat: price prop

* feat: add solana testnet and devnet

* docs: udpate api doc

* feat: add address prefix

* docs: add solana demos

* test: add solana test cases

* feat: improve

* test: update test

* test: fix test

* chore: remove log

* chore: update web3-assets entry point

* feat: delete mainnet

* fix: rename solana index.tsx to index.ts for fix ci

* chore: fix build and typescript error

* docs: enable demos

* test: add tests

* refactor: format

* chore: update pnpm-lock.yaml

* feat: Components adapts Solana's address format

* test: add address format tests

* chore: add changeset

* chore: update changeset

* feat: remove selectWallet event and solana chain check

* test: add test

* test: remove unused tests

* chore: remove comment

* test: fix vitest config

* chore: improve assets multi entry conf

---------

Co-authored-by: tingzhao.ytz <[email protected]>
  • Loading branch information
gin-lsl and tingzhao.ytz authored Feb 18, 2024
1 parent 62262c8 commit d601345
Show file tree
Hide file tree
Showing 57 changed files with 5,710 additions and 196 deletions.
7 changes: 7 additions & 0 deletions .changeset/many-falcons-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@ant-design/web3-solana': major
'@ant-design/web3-assets': minor
'@ant-design/web3': minor
---

Add support for Solana
5 changes: 5 additions & 0 deletions .changeset/tall-clocks-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ant-design/web3-solana': major
---

Add support for Solana
6 changes: 6 additions & 0 deletions .changeset/tidy-starfishes-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ant-design/web3-assets': minor
'@ant-design/web3': minor
---

Address-like components support addressPrefix prop, add solana chain configs
15 changes: 13 additions & 2 deletions .dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ import { defineConfig } from 'dumi';
// utils must build before core
// runtime must build before renderer-react
const pkgList = readdirSync(join(__dirname, 'packages')).map((pkg) => {
const packageJson = require(join(__dirname, 'packages', pkg, 'package.json'));

return {
name: require(join(__dirname, 'packages', pkg, 'package.json')).name,
name: packageJson.name,
exports: packageJson.exports,
path: join(__dirname, 'packages', pkg, 'src'),
};
});

const alias = pkgList.reduce(
(pre, pkg) => {
pre[pkg.name] = pkg.path;

// has multiple entries
if (pkg.exports && pkg.exports['.']) {
Object.keys(pkg.exports).forEach((key) => {
pre[`${pkg.name}/${key}`] = join(pkg.path, key);
});
}

return {
...pre,
};
Expand All @@ -32,7 +43,7 @@ export default defineConfig({
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-C31HWEY1D4');
`,
],
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@
"@ant-design/web3-common": "workspace:*",
"@ant-design/web3-icons": "workspace:*",
"@ant-design/web3-wagmi": "workspace:*",
"@ant-design/web3-solana": "workspace:*",
"@biomejs/biome": "^1.4.1",
"@changesets/changelog-git": "^0.2.0",
"@changesets/cli": "^2.27.1",
"@ianvs/prettier-plugin-sort-imports": "^4.1.1",
"@solana/wallet-adapter-wallets": "^0.19.24",
"@tanstack/react-query": "^5.17.0",
"@testing-library/react": "^14.1.2",
"@types/lodash": "^4.14.202",
Expand Down
28 changes: 25 additions & 3 deletions packages/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,31 @@
"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"
".": {
"import": "./dist/esm/index.js",
"require": "./dist/lib/index.js",
"types": "./dist/esm/index.d.ts"
},
"./ethereum": {
"import": "./dist/esm/chains/ethereum.js",
"require": "./dist/lib/chains/ethereum.js",
"types": "./dist/esm/chains/ethereum.d.ts"
},
"./solana": {
"import": "./dist/esm/solana/index.js",
"require": "./dist/lib/solana/index.js",
"types": "./dist/esm/solana/index.d.ts"
}
},
"typesVersions": {
"*": {
"solana": [
"./dist/esm/solana/index.d.ts"
],
"*": [
"./dist/esm/index.d.ts"
]
}
},
"files": [
"dist",
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/assets/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './wallets';
export * from './chains';
export * from './chains/ethereum';
39 changes: 39 additions & 0 deletions packages/assets/src/solana/chains.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createGetBrowserLink, SolanaChainIds, type Chain } from '@ant-design/web3-common';
import { SolanaColorful } from '@ant-design/web3-icons';

export interface SolanaChain extends Chain {
id: SolanaChainIds;
}

export const Solana: SolanaChain = {
id: SolanaChainIds.MainnetBeta,
name: 'Solana',
icon: <SolanaColorful />,
browser: {
icon: <SolanaColorful />,
getBrowserLink: createGetBrowserLink('https://explorer.solana.com'),
},
nativeCurrency: { name: 'Solana', symbol: 'SOL', decimals: 9 },
};

export const SolanaDevnet: SolanaChain = {
id: SolanaChainIds.Devnet,
name: 'Solana Devnet',
icon: <SolanaColorful />,
browser: {
icon: <SolanaColorful />,
getBrowserLink: createGetBrowserLink('https://explorer.solana.com/?cluster=devnet'),
},
nativeCurrency: { name: 'Solana', symbol: 'SOL', decimals: 9 },
};

export const SolanaTestnet: SolanaChain = {
id: SolanaChainIds.Testnet,
name: 'Solana Testnet',
icon: <SolanaColorful />,
browser: {
icon: <SolanaColorful />,
getBrowserLink: createGetBrowserLink('https://explorer.solana.com/?cluster=testnet'),
},
nativeCurrency: { name: 'Solana', symbol: 'SOL', decimals: 9 },
};
1 change: 1 addition & 0 deletions packages/assets/src/solana/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './chains';
1 change: 1 addition & 0 deletions packages/assets/src/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './coinbase-wallet';
export * from './token-pocket';
export * from './safeheron';
export * from './okx-wallet';
export * from './phantom';
20 changes: 20 additions & 0 deletions packages/assets/src/wallets/phantom.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { WalletMetadata } from '@ant-design/web3-common';
import { ChromeCircleColorful, PhantomColorful } from '@ant-design/web3-icons';

export const metadata_Phantom: WalletMetadata = {
icon: <PhantomColorful />,
name: 'Phantom',
remark: 'Phantom Wallet',
app: {
link: 'https://phantom.app/',
},
extensions: [
{
key: 'Chrome',
browserIcon: <ChromeCircleColorful />,
browserName: 'Chrome',
link: 'https://chromewebstore.google.com/detail/phantom/bfnaelmomeimhlpmgjnjophhpkkoljpa',
description: 'Access your wallet right from your favorite web browser.',
},
],
};
9 changes: 9 additions & 0 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export enum ChainIds {
Avalanche = 43_114,
}

export enum SolanaChainIds {
MainnetBeta = 2,
Devnet = 3,
Testnet = 4,
}

export type BrowserLinkType = 'address' | 'transaction';

export type BalanceMetadata = {
Expand Down Expand Up @@ -61,6 +67,9 @@ export interface UniversalWeb3ProviderInterface {

extendsContextFromParent?: boolean;

/** Such as `0x` */
addressPrefix?: string | false;

connect?: (wallet?: Wallet) => Promise<void>;
disconnect?: () => Promise<void>;
switchChain?: (chain: Chain) => Promise<void>;
Expand Down
5 changes: 5 additions & 0 deletions packages/solana/.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',
});
3 changes: 3 additions & 0 deletions packages/solana/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @ant-design/web3-solana

[https://web3.ant.design](https://web3.ant.design)
69 changes: 69 additions & 0 deletions packages/solana/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@ant-design/web3-solana",
"version": "0.0.1",
"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"
},
"files": [
"dist",
"CHANGELOG.md",
"README.md"
],
"keywords": [
"ant",
"component",
"components",
"design",
"framework",
"frontend",
"react",
"react-component",
"ui",
"web3",
"solana"
],
"homepage": "https://we3.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:*",
"@metaplex-foundation/js": "^0.20.1",
"@solana/wallet-adapter-react": "^0.15.35",
"@solana/wallet-adapter-wallets": "^0.19.24",
"debug": "^4.3.4"
},
"devDependencies": {
"@types/debug": "^4.1.12",
"father": "^4.3.8",
"typescript": "^5.3.3",
"@solana/wallet-adapter-base": "^0.9.23"
},
"peerDependencies": {
"@solana/web3.js": "^1.89.1"
},
"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/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',
},
};
6 changes: 6 additions & 0 deletions packages/solana/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { useWallet } from '@solana/wallet-adapter-react';

export * from './solana-provider';
export { solana, solanaDevnet, solanaTestnet, type SolanaChainConfig } from './chains';
export * from './wallets/factory';
export * from './wallets/built-in';
Loading

0 comments on commit d601345

Please sign in to comment.