This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
8505690
commit 31a59c9
Showing
3 changed files
with
114 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { ChainRegistryFetcher } from '@chain-registry/client'; | ||
import fs from 'fs'; | ||
import yaml from 'js-yaml'; | ||
import fetch from 'node-fetch'; | ||
|
||
import { ChainConfig, ConfigContext } from './config'; | ||
|
||
export const useRegistry = async (configFile: string): Promise<ChainRegistryFetcher> => { | ||
const config = yaml.load(fs.readFileSync(configFile, 'utf8')) as ChainConfig; | ||
const registryUrl = `http://localhost:${config.registry.ports.rest}`; | ||
|
||
const urls: string[] = []; | ||
config.chains.forEach((chain) => { | ||
urls.push( | ||
`${registryUrl}/chains/${chain.name}`, | ||
`${registryUrl}/chains/${chain.name}/assets` | ||
); | ||
}); | ||
config.relayers.forEach((relayer) => { | ||
urls.push( | ||
`${registryUrl}/ibc/${relayer.chains[0]}/${relayer.chains[1]}`, | ||
`${registryUrl}/ibc/${relayer.chains[1]}/${relayer.chains[0]}` | ||
); | ||
}); | ||
|
||
const options = { | ||
urls | ||
}; | ||
const registry = new ChainRegistryFetcher(options); | ||
await registry.fetchUrls(); | ||
|
||
return registry; | ||
}; | ||
|
||
export const useChain = (chainName: string) => { | ||
const registry = ConfigContext.registry; | ||
const configFile = ConfigContext.configFile; | ||
const config = yaml.load(fs.readFileSync(configFile, 'utf8')) as ChainConfig; | ||
|
||
const chain = registry!.getChain(chainName); | ||
const chainInfo = registry!.getChainInfo(chainName); | ||
const chainID = chainInfo.chain.chain_id; | ||
|
||
const getRpcEndpoint = () => { | ||
return `http://localhost:${ | ||
config.chains.find((chain) => chain.name === chainID)!.ports.rpc | ||
}`; | ||
}; | ||
const getRestEndpoint = () => { | ||
return `http://localhost:${ | ||
config.chains.find((chain) => chain.name === chainID)!.ports.rest | ||
}`; | ||
}; | ||
|
||
const getGenesisMnemonic = async () => { | ||
const url = `http://localhost:${config.registry.ports.rest}/chains/${chainID}/keys`; | ||
const response = await fetch(url, {}); | ||
const data = await response.json(); | ||
return data['genesis'][0]['mnemonic']; | ||
}; | ||
|
||
const getCoin = () => { | ||
return chainInfo.fetcher.getChainAssetList(chainName).assets[0]; | ||
}; | ||
|
||
const creditFromFaucet = async (address: string, denom: string | null = null) => { | ||
const faucetEndpoint = `http://localhost:${ | ||
config.chains.find((chain) => chain.name === chainID)!.ports.faucet | ||
}/credit`; | ||
if (!denom) { | ||
denom = getCoin().base; | ||
} | ||
await fetch(faucetEndpoint, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
address, | ||
denom | ||
}), | ||
headers: { | ||
'Content-type': 'application/json' | ||
} | ||
}); | ||
}; | ||
|
||
return { | ||
chain, | ||
chainInfo, | ||
getCoin, | ||
getRpcEndpoint, | ||
getRestEndpoint, | ||
getGenesisMnemonic, | ||
creditFromFaucet | ||
}; | ||
}; |
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 +1,2 @@ | ||
export * from './config'; | ||
export * from './config'; | ||
export * from './hooks'; |