Skip to content

Commit

Permalink
refactor: simplify usage of useFaucet (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang authored Jul 14, 2023
1 parent c01dc68 commit 37e9dcf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 92 deletions.
50 changes: 20 additions & 30 deletions packages/nibijs/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,10 @@ export interface Chain {
feeDenom: string
}

/**
* A function for strongly typing. Returns true if the input object satisfies
* the Chain interface.
*/
export const instanceOfChain = (obj: any): obj is Chain =>
["endptTm", "endptRest", "chainId", "chainName", "feeDenom"].every(
(attr) => attr in obj
)

export interface ChainIdParts {
prefix: string
shortName: string
number: number
prefix: string // e.g. `nibiru`
shortName: string // e.g. `itn`
number: number // e.g. `1`
}

/** CustomChain is a convenience class for intializing the endpoints of a chain
Expand Down Expand Up @@ -75,30 +66,15 @@ export class CustomChain implements Chain {
this.chainIdParts = chainIdParts
this.chainId = this.initChainId()
this.chainName = this.chainId
this.endptTm = this.initTendermintEndpoint()
this.endptRest = this.initRestEndpoint()
this.endptGrpc = this.initGrpcEndpoint()
this.endptTm = `https://rpc.${chainIdParts.shortName}-${chainIdParts.number}.nibiru.fi`
this.endptRest = `https://lcd.${chainIdParts.shortName}-${chainIdParts.number}.nibiru.fi`
this.endptGrpc = `grpc.${chainIdParts.shortName}-${chainIdParts.number}.nibiru.fi`
}

private initChainId = () => {
const { prefix, shortName, number } = this.chainIdParts
return [prefix, shortName, number].join("-")
}

public initTendermintEndpoint = () => {
const { shortName, number } = this.chainIdParts
return `https://rpc.${shortName}-${number}.nibiru.fi`
}

public initRestEndpoint = () => {
const { shortName, number } = this.chainIdParts
return `https://lcd.${shortName}-${number}.nibiru.fi`
}

public initGrpcEndpoint = () => {
const { shortName, number } = this.chainIdParts
return `grpc.${shortName}-${number}.nibiru.fi`
}
}

export const Localnet: Chain = {
Expand Down Expand Up @@ -139,3 +115,17 @@ export const isRestEndptLive = async (chain: Chain) => {
const [_chainId, err] = await queryChainIdWithRest(chain)
return err === undefined
}

/**
* Converts a Chain object to its constituent parts.
* @param chain a Chain object
* @returns a ChainIdParts object
*/
export const chainToParts = (chain: Chain) => {
const parts = chain.chainId.split("-")
return {
prefix: parts[0],
shortName: parts[1],
number: Number(parts[2]),
} as ChainIdParts
}
86 changes: 24 additions & 62 deletions packages/nibijs/src/chain/useFaucet.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,32 @@
import { Chain, instanceOfChain } from "./chain"
import { Chain, ChainIdParts, chainToParts } from "./chain"

/**
* Sends 10 NIBI and 100 NUSD to the given address from the testnet faucet.
* Sends 11 NIBI, 100 NUSD, and 100 USDT to the given address from the testnet faucet.
*/
export async function useFaucet({
address,
amts,
chain,
faucetUrl,
amts,
}: {
address: string
chain: Chain
amts?: { nibi: number; nusd: number; usdt: number }
chain?: Chain | string
faucetUrl?: string | Error
}): Promise<Response> {
amts = {
nibi: amts?.nibi ?? 11,
nusd: amts?.nusd ?? 100,
usdt: amts?.usdt ?? 100,
}
const micro = 1_000_000
const coins: string[] = [
`${(micro * amts.nibi).toString()}unibi`,
`${(micro * amts.nusd).toString()}unusd`,
`${(micro * amts.usdt).toString()}uusdt`,
]

if (chain) {
// deduce faucet URL from 'chain' if possible
if (typeof chain === "string" || chain instanceof String) {
const [outFaucetUrl, err] = faucetUrlFromEndpoint(chain as string)
if (err) throw err
faucetUrl = outFaucetUrl
} else if (instanceOfChain(chain)) {
const [outFaucetUrl, err] = faucetUrlFromChain(chain)
if (err) throw err
faucetUrl = outFaucetUrl
} else {
throw TypeError("'chain' must be a string or Chain")
if (!amts) {
// default values
amts = {
nibi: 11,
nusd: 100,
usdt: 100,
}
} else if (faucetUrl === undefined) {
faucetUrl = "https://faucet.testnet-1.nibiru.fi/"
}

if (faucetUrl instanceof Error || !faucetUrl) {
throw Error(`Faucet URL undefined\n${faucetUrl}`)
}
const coins: string[] = [
`${amts.nibi * 1e6}unibi`,
`${amts.nusd * 1e6}unusd`,
`${amts.usdt * 1e6}uusdt`,
]
const faucetUrl = faucetUrlFromChain(chain)

// Execute faucet request
console.info(
Expand All @@ -70,31 +51,12 @@ export async function useFaucet({
})
}

/** TODO doc */
const faucetUrlFromEndpoint = (endptTm: string) => {
const endptTmParts: string[] = endptTm.split(".")
let rpcIdx: number = -1
endptTmParts.forEach((part, idx) => {
if (part.includes("rpc")) {
rpcIdx = idx
}
})

if (rpcIdx === -1) {
return [
"https://faucet.testnet-1.nibiru.fi/",
new Error(
`failed to deduce chain name from Tendermint RPC endpoint: ${endptTm}`
),
]
}

const chainIdx = rpcIdx + 1
const chainName = endptTmParts[chainIdx]
const faucetUrl = `https://faucet.${chainName}.nibiru.fi/`
return [faucetUrl, undefined]
/**
* Constructs a faucet URL from a Chain object.
* @param chain a Chain object
*/
export const faucetUrlFromChain = (chain: Chain) => {
const parts = chainToParts(chain)
// e.g. https://faucet.itn-1.nibiru.fi/
return `https://faucet.${parts.shortName}-${parts.number}.nibiru.fi/`
}

/** TODO doc */
const faucetUrlFromChain = (chain: Chain) =>
faucetUrlFromEndpoint(chain.endptTm)

0 comments on commit 37e9dcf

Please sign in to comment.