Skip to content

Commit

Permalink
feat: Support Sui (#1024)
Browse files Browse the repository at this point in the history
* feat: add Sui adapter

* chore: update dep

* chore: changeset

* chore: fix dep

* fix: remove unused code

* chore: dep

* fix: deps

* feat: improve props

* docs: update demos

* chore: update deps

* docs(sui): Readme

* add tests

* feat: merge queryClient

* udpate demos

* typo

* docs: update

* rename

* Update

* add test cases

* add test of queryclient

* test

* docs: add queryClient demo

* docs: api doc

* en doc

* feat(assets): add sui wallet metadata

* feat: support wallets prop

* Update packages/web3/src/sui/index.md

Co-authored-by: thinkasany <[email protected]>

* fix: update browser link

* cointype

* chore: update lock

* add Suiet wallet metadata

* merge master

* feat: add sui token

* refactor: use SUI token

* chore: replace @mysten/sui with @mysten/sui.js

* fix lock file

* improve wallet connect

* update lock

* merge package

* chore: update lock file

* chore: update lock

---------

Co-authored-by: thinkasany <[email protected]>
  • Loading branch information
gin-lsl and thinkasany authored Aug 26, 2024
1 parent f045e6b commit 9bb1ea1
Show file tree
Hide file tree
Showing 39 changed files with 2,756 additions and 68 deletions.
6 changes: 6 additions & 0 deletions .changeset/hip-comics-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ant-design/web3-sui': major
'@ant-design/web3-common': minor
---

feat: Add Sui adapter
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,15 @@
"@ant-design/web3-ethers": "workspace:*",
"@ant-design/web3-icons": "workspace:*",
"@ant-design/web3-solana": "workspace:*",
"@ant-design/web3-sui": "workspace:*",
"@ant-design/web3-ton": "workspace:*",
"@ant-design/web3-wagmi": "workspace:*",
"@mysten/dapp-kit": "^0.14.11",
"@mysten/sui": "^1.4.0",
"@solana/wallet-adapter-coinbase": "^0.1.19",
"@solana/wallet-adapter-trust": "^0.1.13",
"@tanstack/query-sync-storage-persister": "^5.51.9",
"@tanstack/react-query-persist-client": "^5.51.11",
"@tanstack/react-query": "^5.51.11",
"antd": "^5.20.1",
"antd-style": "^3.6.2",
Expand Down
1 change: 1 addition & 0 deletions packages/assets/src/tokens/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './eth';
export * from './usdc';
export * from './usdt';
export * from './sui';
10 changes: 10 additions & 0 deletions packages/assets/src/tokens/sui.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Token } from '@ant-design/web3-common';
import { SuiColorful } from '@ant-design/web3-icons';

export const SUI: Token = {
name: 'Sui',
symbol: 'SUI',
decimal: 9,
icon: <SuiColorful />,
availableChains: [],
};
2 changes: 2 additions & 0 deletions packages/assets/src/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export * from './unisat-wallet';
export * from './backpack';
export * from './trust';
export * from './mobile-wallet';
export * from './sui-wallet';
export * from './suiet';
20 changes: 20 additions & 0 deletions packages/assets/src/wallets/sui-wallet.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, SuiColorful } from '@ant-design/web3-icons';

export const metadata_SuiWallet: WalletMetadata = {
name: 'Sui Wallet',
remark: 'Sui Wallet',
icon: <SuiColorful />,
app: {
link: 'https://suiwallet.com/',
},
extensions: [
{
key: 'Chrome',
browserIcon: <ChromeCircleColorful />,
browserName: 'Chrome',
link: 'https://chromewebstore.google.com/detail/sui-wallet/opcgpfmipidbgpenhmajoajpbobppdil',
description: 'Access your wallet right from your favorite web browser.',
},
],
};
20 changes: 20 additions & 0 deletions packages/assets/src/wallets/suiet.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, SuietColorful } from '@ant-design/web3-icons';

export const metadata_Suiet: WalletMetadata = {
name: 'Suiet',
remark: 'Suiet Wallet',
icon: <SuietColorful />,
app: {
link: 'https://suiet.app/',
},
extensions: [
{
key: 'Chrome',
browserIcon: <ChromeCircleColorful />,
browserName: 'Chrome',
link: 'https://chromewebstore.google.com/detail/suiet-sui-wallet/khpkpbbcccdmmclmpigdgddabeilkdpd',
description: 'Access your wallet right from your favorite web browser.',
},
],
};
15 changes: 14 additions & 1 deletion packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export enum SolanaChainIds {
Testnet = 4,
}

export enum SuiChainIds {
Mainnet = 1,
Testnet = 2,
Devnet = 3,
Localnet = 4,
}

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

export type BalanceMetadata = {
Expand All @@ -50,6 +57,11 @@ export enum ChainType {
* Bitcoin chain
*/
Bitcoin = 'Bitcoin',

/**
* Sui chain
*/
Sui = 'Sui',
}

export interface Chain {
Expand All @@ -71,7 +83,7 @@ export interface NFTMetadata {
description?: string;
image?: string;
dna?: string;
edition?: number;
edition?: string | number;
date?: number;
attributes?: {
trait_type?: string;
Expand Down Expand Up @@ -109,6 +121,7 @@ export interface UniversalWeb3ProviderInterface {
}

export interface Wallet extends WalletMetadata {
_standardWallet?: any;
hasWalletReady?: () => Promise<boolean>;
hasExtensionInstalled?: () => Promise<boolean>;
getQrCode?: () => Promise<{ uri: string }>;
Expand Down
5 changes: 5 additions & 0 deletions packages/sui/.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',
});
35 changes: 35 additions & 0 deletions packages/sui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# @ant-design/web3-solana

This package provides a [Sui](https://sui.io) adapter for [@ant-design/web3](https://www.npmjs.com/package/@ant-design/web3).

## Installation

```bash
npm install @ant-design/web3 @ant-design/web3-sui --save
```

## Usage

```tsx
import { ConnectButton, Connector } from '@ant-design/web3';
import { SuiWallet, SuiWeb3ConfigProvider } from '@ant-design/web3-sui';

const App: React.FC = () => {
return (
<SuiWeb3ConfigProvider>
<Connector>
<ConnectButton />
</Connector>
</SuiWeb3ConfigProvider>
);
};

export default App;
```

For more examples, refer to [Sui - Ant Design Web3](https://web3.ant.design/components/sui).

## Documentation

- For more information, visit [Ant Design Web3](https://web3.ant.design).
- For an introduction to Sui, visit [Sui](https://sui.io).
71 changes: 71 additions & 0 deletions packages/sui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@ant-design/web3-sui",
"version": "0.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",
"component",
"components",
"design",
"framework",
"frontend",
"react",
"react-component",
"ui",
"web3",
"sui"
],
"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:*",
"debug": "^4.3.4"
},
"devDependencies": {
"@mysten/dapp-kit": "^0.14.11",
"@mysten/sui": "^1.4.0",
"@tanstack/react-query": "^5.40.1",
"@types/debug": "^4.1.12",
"father": "^4.4.4",
"typescript": "^5.5.2"
},
"peerDependencies": {
"@mysten/dapp-kit": "^0.14.11",
"@mysten/sui": "^1.4.0",
"@tanstack/react-query": "^5.40.1"
},
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"browserslist": [
"last 2 versions",
"Firefox ESR",
"> 1%",
"ie >= 11"
]
}
57 changes: 57 additions & 0 deletions packages/sui/src/chain.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import type { Chain } from '@ant-design/web3-common';
import { createGetBrowserLink, SuiChainIds } from '@ant-design/web3-common';
import { SuiColorful } from '@ant-design/web3-icons';
import { SUI_DECIMALS } from '@mysten/sui/utils';

export interface SuiChain extends Chain {
id: SuiChainIds;
network: 'mainnet' | 'testnet' | 'devnet' | 'localnet';
}

export const suiMainnet: SuiChain = {
id: SuiChainIds.Mainnet,
name: 'Sui',
network: 'mainnet',
icon: <SuiColorful />,
browser: {
icon: <SuiColorful />,
getBrowserLink: createGetBrowserLink('https://suiscan.xyz/mainnet'),
},
nativeCurrency: { name: 'Sui', symbol: 'SUI', decimals: SUI_DECIMALS },
};

export const suiTestnet: SuiChain = {
id: SuiChainIds.Testnet,
name: 'Sui Testnet',
network: 'testnet',
icon: <SuiColorful />,
browser: {
icon: <SuiColorful />,
getBrowserLink: createGetBrowserLink('https://suiscan.xyz/testnet'),
},
nativeCurrency: { name: 'Sui', symbol: 'SUI', decimals: SUI_DECIMALS },
};

export const suiDevnet: SuiChain = {
id: SuiChainIds.Devnet,
name: 'Sui Devnet',
network: 'devnet',
icon: <SuiColorful />,
browser: {
icon: <SuiColorful />,
getBrowserLink: createGetBrowserLink('https://suiscan.xyz/devnet'),
},
nativeCurrency: { name: 'Sui', symbol: 'SUI', decimals: SUI_DECIMALS },
};

export const suiLocalnet: SuiChain = {
id: SuiChainIds.Localnet,
name: 'Sui Localnet',
network: 'localnet',
icon: <SuiColorful />,
browser: {
icon: <SuiColorful />,
},
nativeCurrency: { name: 'Sui', symbol: 'SUI', decimals: SUI_DECIMALS },
};
4 changes: 4 additions & 0 deletions packages/sui/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './chain';
export * from './sui-provider';
export * from './wallets/built-in';
export * from './wallets/factory';
84 changes: 84 additions & 0 deletions packages/sui/src/sui-provider/__tests__/account.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from 'react';
import { ConnectButton, Connector } from '@ant-design/web3';
import { fireEvent } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { SuiWeb3ConfigProvider } from '../../';
import { xrender } from './utils';

describe('SuiWeb3ConfigProvider basic tests', () => {
const mockedDatas = vi.hoisted(() => {
return {
// fake address
shortAddress: '0xa123...y123',
address: '0xa123a123b123b123____a00aaf10c9c283aae9498684218____x123x123y123y123',
sns: 'fakesns.sui',
};
});

vi.mock('@mysten/dapp-kit', async () => {
const originModules = await vi.importActual('@mysten/dapp-kit');

return {
...originModules,
useCurrentAccount: () => {
return {
address: mockedDatas.address,
};
},
useResolveSuiNSName: (address?: string) => {
const sns = address ? mockedDatas.sns : undefined;

return { data: sns };
},
};
});

it('available get account info', async () => {
const App = () => {
return (
<SuiWeb3ConfigProvider>
<Connector>
<ConnectButton />
</Connector>
</SuiWeb3ConfigProvider>
);
};

const { selector } = xrender(App);

const connectBtn = selector('.ant-web3-connect-button');

expect(connectBtn?.textContent).toBe(mockedDatas.shortAddress);
fireEvent.click(connectBtn!);

await vi.waitFor(() => {
const addressInfo = selector('.ant-web3-connect-button-profile-modal .ant-web3-address');
expect(addressInfo?.textContent).toBe(mockedDatas.address);
});
});

it('available get sns info', async () => {
const App = () => {
return (
<SuiWeb3ConfigProvider sns>
<Connector>
<ConnectButton />
</Connector>
</SuiWeb3ConfigProvider>
);
};

const { selector } = xrender(App);

const connectBtn = selector('.ant-web3-connect-button');

expect(connectBtn?.textContent).toBe(mockedDatas.sns);
fireEvent.click(connectBtn!);

await vi.waitFor(() => {
const addressInfo = selector('.ant-web3-connect-button-profile-modal .ant-web3-address');
expect(addressInfo?.textContent).toBe(mockedDatas.address);
});
});
});
Loading

0 comments on commit 9bb1ea1

Please sign in to comment.