Skip to content

Commit

Permalink
Add blocto-connector
Browse files Browse the repository at this point in the history
  • Loading branch information
akira02 authored Jul 27, 2021
1 parent 483bc54 commit 0ee8bc4
Show file tree
Hide file tree
Showing 6 changed files with 6,366 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/blocto-connector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# blocto-connector

Blocto SDK connector for web3-react
For usage, please refer to [Blocto SDK we3-react connector](https://docs.blocto.app/blocto-sdk/evm-sdk/web3-react-connector)

## Development

### Install Dependencies

```bash
yarn
```

### Build

```bash
yarn build
```
49 changes: 49 additions & 0 deletions packages/blocto-connector/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@blocto/blocto-connector",
"version": "0.1.2",
"description": "A Blocto SDK connector for web3-react",
"main": "dist/index.js",
"module": "dist/blocto-connector.esm.js",
"typings": "dist/index.d.ts",
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"lint": "tsdx lint src"
},
"keywords": [
"react",
"react-hooks",
"hooks",
"javascript",
"typescript",
"web3",
"context",
"frontend",
"dapp",
"blocto",
"web3-react",
"blocto-sdk",
"eth",
"ethereum"
],
"author": "Chiaki.C",
"repository": {
"type": "git",
"url": "git://github.com/portto/blocto-connector.git"
},
"dependencies": {
"@blocto/sdk": "^0.2.0",
"@web3-react/abstract-connector": "^6.0.7",
"@web3-react/types": "^6.0.7",
"tiny-invariant": "^1.1.0"
},
"devDependencies": {
"tsdx": "^0.14.1"
}
}
1 change: 1 addition & 0 deletions packages/blocto-connector/src/declarations.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@blocto/sdk'
59 changes: 59 additions & 0 deletions packages/blocto-connector/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ConnectorUpdate } from '@web3-react/types'
import { AbstractConnector } from '@web3-react/abstract-connector'
import invariant from 'tiny-invariant'
import BloctoProvider from '@blocto/sdk'

interface BloctoConnectorArguments {
chainId: number
rpc: string
}

const chainIdToNetwork: { [network: number]: string } = {
1: 'mainnet',
3: 'ropsten',
4: 'rinkeby',
42: 'kovan',
}

export class BloctoConnector extends AbstractConnector {
private readonly chainId: number
private readonly rpc: string
public Blocto: any

constructor({ chainId, rpc }: BloctoConnectorArguments) {
invariant(Object.keys(chainIdToNetwork).includes(chainId.toString()), `Unsupported chainId ${chainId}`)
super({ supportedChainIds: [chainId] })
this.chainId = chainId
this.rpc = rpc
}

public async activate(): Promise<ConnectorUpdate> {
const bloctoProvider = new BloctoProvider({
ethereum: { chainId: this.chainId, rpc: this.rpc },
})

this.Blocto = bloctoProvider.ethereum

const [account] = await bloctoProvider.enable()

return {
provider: this.Blocto,
chainId: this.chainId,
account: account,
}
}

public async getProvider(): Promise<any> {
return this.Blocto
}

public async getChainId(): Promise<number | string> {
return this.chainId
}

public async getAccount(): Promise<null | string> {
return this.Blocto.request({ method: 'eth_accounts' }).then((accounts: string[]): string => accounts[0])
}

public deactivate() {}
}
11 changes: 11 additions & 0 deletions packages/blocto-connector/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"include": ["src"],
"compilerOptions": {
"rootDir": "./",
"baseUrl": "./",
"paths": {
"*": ["src/*", "node_modules/*"]
}
}
}
Loading

0 comments on commit 0ee8bc4

Please sign in to comment.