-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update vault-wallet-toolkit dependency
- Loading branch information
Showing
11 changed files
with
658 additions
and
86 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
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
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
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,60 @@ | ||
import { Blockchain, Network, getNetwork } from 'vault-wallet-toolkit'; | ||
import { Client } from 'xrpl'; | ||
|
||
const xrplPeersMap = { | ||
[Network.PRODUCTION]: ['wss://xrplcluster.com', 'wss://s1.ripple.com', 'wss://s2.ripple.com'], | ||
[Network.DEVELOPMENT]: ['wss://testnet.xrpl-labs.com', 'wss://s.altnet.rippletest.net:51233'], | ||
[Network.LOCAL]: [] | ||
}; | ||
|
||
export class XrplProvider { | ||
instance!: Client; | ||
peers: string[] = xrplPeersMap[getNetwork<Network>(Blockchain.XRPL)]; | ||
|
||
constructor() { | ||
this.createInstanceWithRandomPeer(); | ||
} | ||
|
||
async connect(): Promise<void> { | ||
try { | ||
await this.instance.connect(); | ||
} catch (err: any) { | ||
this.createInstanceWithRandomPeer(); | ||
await this.connect(); | ||
} | ||
} | ||
|
||
private createInstanceWithRandomPeer() { | ||
if (this.peers.length === 0) { | ||
this.peers = xrplPeersMap[getNetwork<Network>(Blockchain.XRPL)]; | ||
throw new Error('Cannot connect to provider'); | ||
} | ||
const randomPeerIndex = Math.floor(Math.random() * this.peers.length); | ||
const randomPeer = this.peers[randomPeerIndex]; | ||
|
||
this.peers.splice(randomPeerIndex, 1); | ||
this.instance = new Client(randomPeer); | ||
} | ||
|
||
async disconnect(): Promise<void> { | ||
await this.instance.disconnect(); | ||
} | ||
|
||
get isConnected(): boolean { | ||
return this.instance.isConnected(); | ||
} | ||
} | ||
|
||
let xrplProvider: XrplProvider; | ||
|
||
export const getXrplProvider = async () => { | ||
if (!xrplProvider) { | ||
xrplProvider = new XrplProvider(); | ||
} | ||
|
||
if (!xrplProvider.isConnected) { | ||
await xrplProvider.connect(); | ||
} | ||
|
||
return xrplProvider; | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { NetworkUtil, getNetworkEnv } from './network'; | ||
import { getNetworkEnv, setNetwork } from './network'; | ||
|
||
NetworkUtil.setNetwork(getNetworkEnv()); | ||
setNetwork(getNetworkEnv()); |
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
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
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
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
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
Oops, something went wrong.