Skip to content

Commit

Permalink
feat(nibijs): add fromChainId factory method (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang authored Aug 30, 2023
1 parent a21ae97 commit 0d44203
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/nibijs/docs/classes/StableSwap.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ y()
Calculate x[j] if one makes x[i] = x

Done by solving quadratic equation iteratively.
x*1**2 + x1 * (sum' - (A*n**n - 1) * D / (A _ n**n)) = D ** (n+1)/(n \*\* (2 _ n) \_ prod' \* A)
x*1\*\*2 + x1 * (sum' - (A*n\*\*n - 1) * D / (A _ n**n)) = D ** (n+1)/(n \*\* (2 _ n) \_ prod' \* A)
x_1\*\*2 + b\*x_1 = c

x_1 = (x_1\**2 + c) / (2*x_1 + b)
Expand Down
10 changes: 10 additions & 0 deletions packages/nibijs/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ export class CustomChain implements Chain {
this.endptGrpc = `grpc.${chainIdParts.shortName}-${chainIdParts.number}.nibiru.fi`
}

public static fromChainId(chainId: string): Chain {
const parts = chainId.split("-")
const chainIdParts = {
prefix: parts[0],
shortName: parts[1],
number: Number(parts[2]),
} as ChainIdParts
return new CustomChain(chainIdParts)
}

private initChainId = () => {
const { prefix, shortName, number } = this.chainIdParts
return [prefix, shortName, number].join("-")
Expand Down
7 changes: 7 additions & 0 deletions packages/nibijs/src/test/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Chain,
Coin,
CoinMap,
CustomChain,
isRestEndptLive,
newCoin,
newCoinMapFromCoins,
Expand All @@ -18,6 +19,12 @@ describe("chain connections", () => {
expect(blockHeight).toBeDefined()
expect(blockHeight).toBeGreaterThanOrEqual(0)
})

test("chain from chain-id", async () => {
const chain = CustomChain.fromChainId(TEST_CHAIN.chainId)
expect(chain.chainId).toEqual(TEST_CHAIN.chainId)
})

test("inactive chain validation cases", async () => {
const inactiveChain: Chain = {
endptTm: "",
Expand Down

0 comments on commit 0d44203

Please sign in to comment.