forked from Uniswap/web3-react
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
6,366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module '@blocto/sdk' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/*"] | ||
} | ||
} | ||
} |
Oops, something went wrong.