# Automata Network Wallet SDK

A monorepo consists of Web3 wallet SDK and React UI component to connect dApp web app to popular web3 wallet like MetaMask and WalletConnect.

## Install

```shell
npm install @automata-network/wallet-react /
@solana/wallet-adapter-react /
@solana/wallet-adapter-wallets /
@polkadot/api /
@polkadot/extension-inject /
@web3-onboard/coinbase /
@web3-onboard/core /
@web3-onboard/enkrypt /
@web3-onboard/fortmatic /
@web3-onboard/gnosis /
@web3-onboard/injected-wallets /
@web3-onboard/ledger /
@web3-onboard/mew-wallet /
@web3-onboard/portis /
@web3-onboard/torus /
@web3-onboard/trezor /
@web3-onboard/walletconnect /
ethers /
react /
react-dom -S
```

## How To

Here's the [example](../../example/).

```jsx
import '@automata-network/wallet-react/style.css'

createRoot(document.getElementById('root') as HTMLElement).render(
  <StrictMode>
    <WalletProvider
      appName="Automata Wallet Demo"
      chains={[
        {
          type: ChainType.EVM,
          chainId: "5",
          chainName: "Ethereum Goerli",
          nativeCurrency: {
            decimals: 18,
            name: "GoerliETH",
            symbol: "GoerliETH",
          },
          rpcUrls: [
            "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161",
          ],
          blockExplorerUrls: ["https://goerli.etherscan.io/"],
          iconUrl:
            "https://raw.githubusercontent.com/ethereum/ethereum-org-website/dev/src/assets/assets/eth-glyph-colored.svg",
        },
        {
          type: ChainType.EVM,
          chainId: "80001",
          chainName: "Polygon",
          nativeCurrency: {
            decimals: 18,
            name: "MATIC",
            symbol: "MATIC",
          },
          rpcUrls: ["https://rpc.ankr.com/polygon_mumbai"],
          blockExplorerUrls: ["https://mumbai.polygonscan.com/"],
          iconUrl:
            "https://raw.githubusercontent.com/maticnetwork/matic-docs/master/static/img/polygon-logo.svg",
        },
        {
          type: ChainType.Polkadot,
          chainId: "Westend",
          chainName: "Westend",
          rpcUrls: ["wss://westend-rpc.polkadot.io"],
          blockExplorerUrls: ["https://westend.subscan.io/"],
          iconUrl:
            "https://polkadot.js.org/apps/static/westend_colour.eb7969da..svg",
        },
        {
          type: ChainType.Solana,
          chainId: "Solana Testnet",
          chainName: "Solana Testnet",
          rpcUrls: ["https://api.testnet.solana.com"],
          blockExplorerUrls: ["https://explorer.solana.com/?cluster=testnet"],
          iconUrl: "https://solana.com/src/img/branding/solanaLogoMark.svg",
        },
      ]}
      providerConfigs={{
        evm: {
          fortmatic: process.env.VITE_FORTMATIC_API_KEY
            ? {
                apiKey: process.env.VITE_FORTMATIC_API_KEY,
              }
            : undefined,
          walletconnect: {},
          // coinbase: {},
          portis: process.env.VITE_PORTIS_API_KEY
            ? {
                apiKey: process.env.VITE_PORTIS_API_KEY,
              }
            : undefined,
          gnosis: {},
          torus: process.env.VITE_TORUS_API_KEY
            ? { apiKey: process.env.VITE_TORUS_API_KEY, showTorusButton: false }
            : undefined,
          trezor:
            process.env.VITE_TREZOR_EMAIL && process.env.VITE_TREZOR_APP_URL
              ? {
                  email: process.env.VITE_TREZOR_EMAIL,
                  appUrl: process.env.VITE_TREZOR_APP_URL,
                }
              : undefined,
          mew: true,
          enkrypt: true,
          subwallet: true,
          talisman: true,
          ledger: {},
        },
        polkadot: {
          enkrypt: true,
          subwallet: true,
          talisman: true,
          polkadotJsExtension: true,
        },
        solana: {
          math: {},
          solflare: {},
          phantom: {},
        },
      }}
    >
      <Navbar>
        <Wallet />
      </Navbar>
      <App />
    </WalletProvider>
  </StrictMode>
)
```

If you want to use the json rpc provider or get the account address, chain ID, etc.. you can use the `useWallet` hook.

```typescript
const { account, chainId, rpcRequest } = useWallet()
```

### Customize styles and class name prefix

If you want to customize your style you can copy the content from @automata-network/wallet-react/src/style.less and use your own variables or component style file. then compile your less to css on your build phase and import it to your website.

If our class name prefix conflict with your code, you can modify the @selector-prefix variable on the @automata-network/wallet-react/src/style.less and compile your customized css.

Then you need to set up the customized class name prefix on the wallet provider.

```jsx
<WalletProvider
  prefixCls="your-class-name-prefix"
>
```

### Integeration with ViteJS

[TBD]

### Integeration with Create React App

[TBD]

### Integeration with NextJS

[TBD]

## Trouble shooting

### Uncaught ReferenceError: process is not defined

That caused by the @walletconnect/web3-provider, it required the process object from Node.js, but there is no process on the browser.

so we need to set up an alias to point @walletconnect/web3-provider to its prebuilt bundle.

```js
{
  alias: {
    '@walletconnect/web3-provider': '@walletconnect/web3-provider/dist/umd/index.min.js'
  }
}
```

## Contribute on Development

Clone this repo and run the script below to install all required dependencies.

```shell
# First we install the dependencies and link
cd wallet_sdk_dir && npm install
```

Then build the SDK and link the `dist` directory so you can use the SDK in the example site.

```shell
npm run build
npm link
```

When you've made some changes on the SDK part, just rebuild it and the example site will be reloaded automatically.

> Don't forget to unlink the SDK when you've finished with the development, using `npm unlink @automata-network/wallet-sdk`.
