Skip to content

Commit

Permalink
add metamask support (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenguc authored Oct 20, 2022
1 parent ef6588c commit bf16a06
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainID, ChainType, Unit } from '@harmony-js/utils'
import { ContractAddresses, HarmonyRpcConfig, HarmonyShards, ITransactionOptions } from './interfaces'
import { ChainId, ContractAddresses, HarmonyRpcConfig, HarmonyShards, ITransactionOptions } from './interfaces'

export const AddressZero = '0x0000000000000000000000000000000000000000'
export const DEFAULT_GAS_PRICE = '1000000000'
Expand All @@ -8,6 +8,10 @@ export const HARMONY_RPC_SHARD_0_URL = 'https://api.harmony.one'
export const HARMONY_RPC_SHARD_1_URL = 'https://s1.api.harmony.one'
export const HARMONY_RPC_SHARD_2_URL = 'https://s2.api.harmony.one'
export const HARMONY_RPC_SHARD_3_URL = 'https://s3.api.harmony.one'
export const HARMONY_RPC_SHARD_0_TESTNET_URL = 'https://api.s0.b.hmny.io'
export const HARMONY_RPC_SHARD_1_TESTNET_URL = 'https://api.s1.b.hmny.io'
export const HARMONY_RPC_SHARD_2_TESTNET_URL = 'https://api.s2.b.hmny.io'
export const HARMONY_RPC_SHARD_3_TESTNET_URL = 'https://api.s3.b.hmny.io'
export const HARMONY_RPC_SHARD_0_DEVNET_URL = 'https://api.s0.ps.hmny.io'
export const HARMONY_RPC_WS = 'wss://ws.s0.t.hmny.io'
export const HARMONY_RPC_DEVNET_WS = 'wss://ws.s0.ps.hmny.io'
Expand Down Expand Up @@ -45,6 +49,18 @@ export const HARMONY_SHARDS = {
[HarmonyShards.SHARD_0_DEVNET]: HARMONY_RPC_SHARD_0_DEVNET,
}

export const CHAINS_ID: ChainId = {
1666600000: HARMONY_RPC_SHARD_0_URL,
1666600001: HARMONY_RPC_SHARD_1_URL,
1666600002: HARMONY_RPC_SHARD_2_URL,
1666600003: HARMONY_RPC_SHARD_3_URL,
1666700000: HARMONY_RPC_SHARD_0_TESTNET_URL,
1666700001: HARMONY_RPC_SHARD_1_TESTNET_URL,
1666700002: HARMONY_RPC_SHARD_2_TESTNET_URL,
1666700003: HARMONY_RPC_SHARD_3_TESTNET_URL,
1666900000: HARMONY_RPC_SHARD_0_DEVNET_URL,
}

export enum NetworkInfo {
MAINNET,
DEVNET,
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ export interface BridgeResponse {
addr: string
receiptId: string
}

export interface ChainId {
[key: number]: string
}
10 changes: 9 additions & 1 deletion src/wallets/key.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Wallet } from '@harmony-js/account'
import { Messenger, HttpProvider, WSProvider } from '@harmony-js/network'
import { ChainID, ChainType, isWs } from '@harmony-js/utils'
import { HARMONY_SHARDS } from '../constants'
import { HARMONY_SHARDS, CHAINS_ID } from '../constants'
import { HarmonyRpcConfig, HarmonyShards, RpcProviderType } from '../interfaces'

/**
Expand All @@ -21,6 +21,14 @@ export class Key extends Wallet {
provider = url
} else if (typeof url === 'string') {
provider = isWs(url) ? new WSProvider(url) : new HttpProvider(url)
} else if (url['isMetaMask']) {
const _chain = Number(url['networkVersion'])
const rpc_url = CHAINS_ID[_chain]

if (!rpc_url) throw new Error('Invalid metamask harmony chain.')

provider = new HttpProvider(rpc_url)
chain = _chain
} else {
throw new Error('Invalid url param.')
}
Expand Down

0 comments on commit bf16a06

Please sign in to comment.