From ec2a25bd3d02cdeb6b56fad1b1a85c9c249dc697 Mon Sep 17 00:00:00 2001 From: Cameron Gilbert Date: Thu, 15 Aug 2024 09:52:44 -0400 Subject: [PATCH 01/10] chore: develop -> main (#370) * revert: cosmos submodule only (#362) * revert: cosmos submodule only * fix: rem * fix: rem * fix: update * feat: add msg client * fix: paths * fix: try chaosnet ibc * fix: path again * fix: try hm * fix: fixes to pass * feat: eth protos (#366) * fix: eth protos * fix: client * fix: fixes * fix: try older nibiru * fix: index * fix: mainnet * fix: import * revert: build change * chore: tests (#367) * fix: all query tests * chore: final tests * fix: buf * fix: fix * fix: pull latest * fix: build * fix: build * refactor(faucet)!: set default tokens (#369) * chore: develop -> main (#368) * revert: cosmos submodule only (#362) * revert: cosmos submodule only * fix: rem * fix: rem * fix: update * feat: add msg client * fix: paths * fix: try chaosnet ibc * fix: path again * fix: try hm * fix: fixes to pass * feat: eth protos (#366) * fix: eth protos * fix: client * fix: fixes * fix: try older nibiru * fix: index * fix: mainnet * fix: import * revert: build change * chore: tests (#367) * fix: all query tests * chore: final tests * fix: buf * fix: fix * fix: pull latest * fix: build * fix: build * chore(release): 4.5.1 ### [4.5.1](https://github.com/NibiruChain/ts-sdk/compare/v4.5.0...v4.5.1) (2024-08-09) ### Miscellaneous Chores * develop -> main ([#368](https://github.com/NibiruChain/ts-sdk/issues/368)) ([c6d6570](https://github.com/NibiruChain/ts-sdk/commit/c6d657009eed49442243c4b0e9021afd34392a98)), closes [#362](https://github.com/NibiruChain/ts-sdk/issues/362) [#366](https://github.com/NibiruChain/ts-sdk/issues/366) [#367](https://github.com/NibiruChain/ts-sdk/issues/367) [skip ci] * fix(faucet): remove unused tokens from default faucet request * fix: bump test --------- Co-authored-by: Cameron Gilbert Co-authored-by: semantic-release-bot --------- Co-authored-by: Kevin Yang <5478483+k-yang@users.noreply.github.com> Co-authored-by: semantic-release-bot --- src/sdk/utils/faucet.test.ts | 7 +++---- src/sdk/utils/faucet.ts | 19 ++----------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/sdk/utils/faucet.test.ts b/src/sdk/utils/faucet.test.ts index ef3d4825..3e8d5c26 100644 --- a/src/sdk/utils/faucet.test.ts +++ b/src/sdk/utils/faucet.test.ts @@ -78,7 +78,7 @@ describe("useFaucet", () => { test("should request funds from faucet with default amounts", async () => { await useFaucet({ address, chain, grecaptcha }) - const expectedCoins = ["11000000unibi", "100000000unusd", "100000000uusdt"] + const expectedCoins = ["10000000unibi"] expect(fetch).toHaveBeenCalledWith(expectedUrl, { method: "POST", @@ -91,10 +91,9 @@ describe("useFaucet", () => { }) test("should request funds from faucet with custom amounts", async () => { - const amts = { nibi: 5, nusd: 50, usdt: 50 } - await useFaucet({ address, chain, amts, grecaptcha }) + await useFaucet({ address, chain, grecaptcha }) - const expectedCoins = ["5000000unibi", "50000000unusd", "50000000uusdt"] + const expectedCoins = ["10000000unibi"] expect(fetch).toHaveBeenCalledWith(expectedUrl, { method: "POST", diff --git a/src/sdk/utils/faucet.ts b/src/sdk/utils/faucet.ts index afa589ea..2c561efc 100644 --- a/src/sdk/utils/faucet.ts +++ b/src/sdk/utils/faucet.ts @@ -2,33 +2,18 @@ import { fetch } from "cross-fetch" import { Chain, chainToParts } from "." /** - * Sends 11 NIBI, 100 NUSD, and 100 USDT to the given address from the testnet faucet. + * Sends 10 NIBI to the given address from the testnet faucet. */ export async function useFaucet({ address, chain, - amts, grecaptcha, }: { address: string chain: Chain - amts?: { nibi: number; nusd: number; usdt: number } grecaptcha: string }): Promise { - if (!amts) { - // default values - amts = { - nibi: 11, - nusd: 100, - usdt: 100, - } - } - - const coins: string[] = [ - `${amts.nibi * 1e6}unibi`, - `${amts.nusd * 1e6}unusd`, - `${amts.usdt * 1e6}uusdt`, - ] + const coins: string[] = [`${10e6}unibi`] const faucetUrl = faucetUrlFromChain(chain) // Execute faucet request From c2c27e57a5f94f2180f2df0ad67597790809b143 Mon Sep 17 00:00:00 2001 From: Unique Divine Date: Tue, 24 Sep 2024 08:14:06 -0500 Subject: [PATCH 02/10] chore(github): Add project automation for https://tinyurl.com/25uty9w5 --- .github/issue-labeler-config.yml | 3 +++ .github/workflows/gh-issues.yml | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 .github/issue-labeler-config.yml create mode 100644 .github/workflows/gh-issues.yml diff --git a/.github/issue-labeler-config.yml b/.github/issue-labeler-config.yml new file mode 100644 index 00000000..1d7046b6 --- /dev/null +++ b/.github/issue-labeler-config.yml @@ -0,0 +1,3 @@ +# Adds the "S-triage" label ot any issue that gets opened. +S-triage: + - '/.*/' diff --git a/.github/workflows/gh-issues.yml b/.github/workflows/gh-issues.yml new file mode 100644 index 00000000..69054648 --- /dev/null +++ b/.github/workflows/gh-issues.yml @@ -0,0 +1,38 @@ +name: "Auto-add GH issues to project" +# Add all issues opened to the issue board for triage and assignment +# GitHub Org and Project Automation +# https://www.notion.so/nibiru/GitHub-Org-and-Project-Automation-c771d671109849ee9fda7c8b741cd66a?pvs=4 + +on: + issues: + types: ["opened", "labeled"] + +permissions: + issues: write + contents: read + +jobs: + # https://github.com/actions/add-to-project + add-to-project: + name: "Add GH ticket to project" + runs-on: ubuntu-latest + steps: + - uses: actions/add-to-project@v1.0.2 + with: + project-url: https://github.com/orgs/NibiruChain/projects/8 + github-token: ${{ secrets.NIBIRU_PM }} + + label-triage: + name: "Add GH ticket to project" + runs-on: ubuntu-latest + # The action comes from the "Activty types" for the "issues" webhook event + # https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#issues + if: "github.event.action == 'opened'" + steps: + - uses: github/issue-labeler@v3.4 + if: join(github.event.issue.labels) == '' + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" + configuration-path: ".github/issue-labeler-config.yml" + enable-versioned-regex: 0 + not-before: "2024-05-01T00:00:00Z" From 470262f8705362ac4c690322f830dc54edb2dcdc Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Tue, 24 Sep 2024 15:41:50 +0000 Subject: [PATCH 03/10] chore(release): 4.5.2 ### [4.5.2](https://github.com/NibiruChain/ts-sdk/compare/v4.5.1...v4.5.2) (2024-09-24) ### Miscellaneous Chores * develop -> main ([#370](https://github.com/NibiruChain/ts-sdk/issues/370)) ([ec2a25b](https://github.com/NibiruChain/ts-sdk/commit/ec2a25bd3d02cdeb6b56fad1b1a85c9c249dc697)), closes [#362](https://github.com/NibiruChain/ts-sdk/issues/362) [#366](https://github.com/NibiruChain/ts-sdk/issues/366) [#367](https://github.com/NibiruChain/ts-sdk/issues/367) [#369](https://github.com/NibiruChain/ts-sdk/issues/369) [#368](https://github.com/NibiruChain/ts-sdk/issues/368) [#362](https://github.com/NibiruChain/ts-sdk/issues/362) [#366](https://github.com/NibiruChain/ts-sdk/issues/366) [#367](https://github.com/NibiruChain/ts-sdk/issues/367) [#362](https://github.com/NibiruChain/ts-sdk/issues/362) [#366](https://github.com/NibiruChain/ts-sdk/issues/366) [#367](https://github.com/NibiruChain/ts-sdk/issues/367) * **github:** Add project automation for https://tinyurl.com/25uty9w5 ([c2c27e5](https://github.com/NibiruChain/ts-sdk/commit/c2c27e57a5f94f2180f2df0ad67597790809b143)) [skip ci] --- .github/issue-labeler-config.yml | 4 ++-- CHANGELOG.md | 7 +++++++ package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/issue-labeler-config.yml b/.github/issue-labeler-config.yml index 1d7046b6..2fbfe66c 100644 --- a/.github/issue-labeler-config.yml +++ b/.github/issue-labeler-config.yml @@ -1,3 +1,3 @@ # Adds the "S-triage" label ot any issue that gets opened. -S-triage: - - '/.*/' +S-triage: + - "/.*/" diff --git a/CHANGELOG.md b/CHANGELOG.md index 47333df2..92b3e2f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +### [4.5.2](https://github.com/NibiruChain/ts-sdk/compare/v4.5.1...v4.5.2) (2024-09-24) + +### Miscellaneous Chores + +- develop -> main ([#370](https://github.com/NibiruChain/ts-sdk/issues/370)) ([ec2a25b](https://github.com/NibiruChain/ts-sdk/commit/ec2a25bd3d02cdeb6b56fad1b1a85c9c249dc697)), closes [#362](https://github.com/NibiruChain/ts-sdk/issues/362) [#366](https://github.com/NibiruChain/ts-sdk/issues/366) [#367](https://github.com/NibiruChain/ts-sdk/issues/367) [#369](https://github.com/NibiruChain/ts-sdk/issues/369) [#368](https://github.com/NibiruChain/ts-sdk/issues/368) [#362](https://github.com/NibiruChain/ts-sdk/issues/362) [#366](https://github.com/NibiruChain/ts-sdk/issues/366) [#367](https://github.com/NibiruChain/ts-sdk/issues/367) [#362](https://github.com/NibiruChain/ts-sdk/issues/362) [#366](https://github.com/NibiruChain/ts-sdk/issues/366) [#367](https://github.com/NibiruChain/ts-sdk/issues/367) +- **github:** Add project automation for https://tinyurl.com/25uty9w5 ([c2c27e5](https://github.com/NibiruChain/ts-sdk/commit/c2c27e57a5f94f2180f2df0ad67597790809b143)) + ### [4.5.1](https://github.com/NibiruChain/ts-sdk/compare/v4.5.0...v4.5.1) (2024-08-09) ### Miscellaneous Chores diff --git a/package.json b/package.json index a4a475bd..4488fd48 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@nibiruchain/nibijs", "description": "The TypeScript SDK for the Nibiru blockchain.", - "version": "4.5.1", + "version": "4.5.2", "license": "MIT", "repository": { "type": "git", From de60207d5445e44fa5cbf79f2642626aca40d66d Mon Sep 17 00:00:00 2001 From: elshenak Date: Thu, 3 Oct 2024 20:22:57 -0400 Subject: [PATCH 04/10] feat: nibiru account parser --- src/sdk/tx/account.test.ts | 83 ++++++++++++++++++++++++++++++++++++++ src/sdk/tx/account.ts | 36 +++++++++++++++++ src/sdk/tx/txClient.ts | 2 + src/sdk/utils/t.json | 41 +++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 src/sdk/tx/account.test.ts create mode 100644 src/sdk/tx/account.ts create mode 100644 src/sdk/utils/t.json diff --git a/src/sdk/tx/account.test.ts b/src/sdk/tx/account.test.ts new file mode 100644 index 00000000..e0f0ed67 --- /dev/null +++ b/src/sdk/tx/account.test.ts @@ -0,0 +1,83 @@ +import { accountFromEthAccount, accountFromNibiru } from "./account" +import { EthAccount } from "src/protojs/eth/types/v1/account" +import { Any } from "cosmjs-types/google/protobuf/any" +import Long from "long" + +describe("accountFromEthAccount", () => { + it("should return valid account information with nibi address and Long types", () => { + const ethAccount: EthAccount = { + baseAccount: { + address: "nibi1234567", // Use nibi Bech32 encoding + pubKey: { + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: new Uint8Array([1, 2, 3]), + }, + accountNumber: Long.fromNumber(123), + sequence: Long.fromNumber(1), + }, + codeHash: "", + } + + const account = accountFromEthAccount(ethAccount) + + expect(account.address).toEqual("nibi1234567") + expect(account.pubkey).toEqual({ + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: new Uint8Array([1, 2, 3]), + }) + expect(account.accountNumber).toEqual(Long.fromNumber(123)) + expect(account.sequence).toEqual(Long.fromNumber(1)) + }) + + it("should handle null/undefined baseAccount fields and return Long.ZERO", () => { + const ethAccount: EthAccount = { baseAccount: undefined, codeHash: "" } + + const account = accountFromEthAccount(ethAccount) + + expect(account.address).toEqual("") + expect(account.pubkey).toBeNull() + expect(account.accountNumber).toEqual(Long.ZERO) + expect(account.sequence).toEqual(Long.ZERO) + }) +}) + +describe("accountFromNibiru", () => { + it("should parse EthAccount typeUrl", () => { + const input: Any = { + typeUrl: "/eth.types.v1.EthAccount", + value: EthAccount.encode({ + baseAccount: { + address: "nibi1234567", + pubKey: { + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: new Uint8Array([4, 5, 6]), + }, + accountNumber: Long.fromNumber(456), + sequence: Long.fromNumber(2), + }, + codeHash: "", + }).finish(), + } + + const account = accountFromNibiru(input) + + expect(account.address).toEqual("nibi1234567") // Ensure the prefix is 'nibi' + expect(account.pubkey).toEqual({ + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: new Uint8Array([4, 5, 6]), + }) + expect(account.accountNumber).toEqual(Long.fromNumber(456)) + expect(account.sequence).toEqual(Long.fromNumber(2)) + }) + + it("should call accountFromAny for non-EthAccount typeUrl", () => { + const input: Any = { + typeUrl: "/other.types.v1.Account", + value: new Uint8Array([7, 8, 9]), + } + + const account = accountFromNibiru(input) + + expect(account.address).toBe("nibi1otheraddress") + }) +}) diff --git a/src/sdk/tx/account.ts b/src/sdk/tx/account.ts new file mode 100644 index 00000000..808351bf --- /dev/null +++ b/src/sdk/tx/account.ts @@ -0,0 +1,36 @@ +import { decodeOptionalPubkey } from "@cosmjs/proto-signing" +import { Account, accountFromAny, AccountParser } from "@cosmjs/stargate" +import { EthAccount } from "src/protojs/eth/types/v1/account" +import { Any } from "src/protojs/google/protobuf/any" + +/** + * Converts an EthAccount to a general Cosmos Account object. + * + * @param {EthAccount} ethAccount - The EthAccount object containing the account's base information. + * @returns {Account} The Cosmos account object. + */ +export const accountFromEthAccount = ({ + baseAccount, +}: EthAccount): Account => ({ + address: baseAccount?.address ?? "", + pubkey: decodeOptionalPubkey(baseAccount?.pubKey) ?? null, + accountNumber: baseAccount?.accountNumber?.toNumber() ?? 0, + sequence: baseAccount?.sequence?.toNumber() ?? 0, +}) + +/** + * Parses an account input into a Cosmos account. Handles both EthAccount and other standard accounts. + * + * @param {Any} input - The input account information, containing the typeUrl and value. + * @returns {Account} Parsed account object. + */ +export const accountFromNibiru: AccountParser = (input: Any): Account => { + const { typeUrl, value } = input + + if (typeUrl === "/eth.types.v1.EthAccount") { + const ethAccount = EthAccount.decode(value) + return accountFromEthAccount(ethAccount) + } + + return accountFromAny(input) +} diff --git a/src/sdk/tx/txClient.ts b/src/sdk/tx/txClient.ts index 38b718b0..838df533 100644 --- a/src/sdk/tx/txClient.ts +++ b/src/sdk/tx/txClient.ts @@ -18,6 +18,7 @@ import { setupWasmExtension, } from "@cosmjs/cosmwasm-stargate" import { NibiruExtensions, setupNibiruExtension } from ".." +import { accountFromNibiru } from "./account" export const nibiruRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [ ...defaultRegistryTypes, @@ -69,6 +70,7 @@ export class NibiruTxClient extends SigningStargateClient { registry: new Registry(nibiruRegistryTypes), gasPrice: GasPrice.fromString("0.025unibi"), broadcastPollIntervalMs: 1_000, // 1 second poll times + accountParser: accountFromNibiru, ...options, }, wasmClient diff --git a/src/sdk/utils/t.json b/src/sdk/utils/t.json new file mode 100644 index 00000000..d1c73380 --- /dev/null +++ b/src/sdk/utils/t.json @@ -0,0 +1,41 @@ +{ + "chainId": "cataclysm-1", + "chainName": "cataclysm-1", + "rpc": "https://rpc.nibiru.fi", + "rest": "https://lcd.nibiru.fi", + "stakeCurrency": { + "coinDenom": "NIBI", + "coinMinimalDenom": "unibi", + "coinDecimals": 6 + }, + "bip44": { + "coinType": 118 + }, + "bech32Config": { + "bech32PrefixAccAddr": "nibi", + "bech32PrefixAccPub": "nibipub", + "bech32PrefixValAddr": "nibivaloper", + "bech32PrefixValPub": "nibivaloperpub", + "bech32PrefixConsAddr": "nibivalcons", + "bech32PrefixConsPub": "nibivalconspub" + }, + "currencies": [ + { + "coinDenom": "NIBI", + "coinMinimalDenom": "unibi", + "coinDecimals": 6 + } + ], + "feeCurrencies": [ + { + "coinDenom": "NIBI", + "coinMinimalDenom": "unibi", + "coinDecimals": 6, + "gasPriceStep": { + "low": 0.05, + "average": 0.125, + "high": 0.2 + } + } + ] +} From b94846295e5c6588cc1178f677da16e9e60afafb Mon Sep 17 00:00:00 2001 From: elshenak Date: Thu, 3 Oct 2024 22:04:36 -0400 Subject: [PATCH 05/10] refactor: throw if baseaccount is undefined --- src/sdk/tx/account.test.ts | 57 +++++++++++++++++++++++--------------- src/sdk/tx/account.ts | 22 +++++++++------ 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/sdk/tx/account.test.ts b/src/sdk/tx/account.test.ts index e0f0ed67..76346f90 100644 --- a/src/sdk/tx/account.test.ts +++ b/src/sdk/tx/account.test.ts @@ -1,13 +1,25 @@ import { accountFromEthAccount, accountFromNibiru } from "./account" import { EthAccount } from "src/protojs/eth/types/v1/account" -import { Any } from "cosmjs-types/google/protobuf/any" +import { Any } from "src/protojs/google/protobuf/any" import Long from "long" +import * as cosmjs from "@cosmjs/stargate" describe("accountFromEthAccount", () => { - it("should return valid account information with nibi address and Long types", () => { + it("should throw an error when baseAccount is undefined", () => { + const ethAccount: EthAccount = { + baseAccount: undefined, // Simulating undefined baseAccount + codeHash: "", + } + + expect(() => accountFromEthAccount(ethAccount)).toThrowError( + "baseAccount is undefined in EthAccount" + ) + }) + + it("should return a valid account when baseAccount is defined", () => { const ethAccount: EthAccount = { baseAccount: { - address: "nibi1234567", // Use nibi Bech32 encoding + address: "nibi1testaddress", pubKey: { typeUrl: "/cosmos.crypto.secp256k1.PubKey", value: new Uint8Array([1, 2, 3]), @@ -20,34 +32,23 @@ describe("accountFromEthAccount", () => { const account = accountFromEthAccount(ethAccount) - expect(account.address).toEqual("nibi1234567") + expect(account.address).toBe("nibi1testaddress") expect(account.pubkey).toEqual({ typeUrl: "/cosmos.crypto.secp256k1.PubKey", value: new Uint8Array([1, 2, 3]), }) - expect(account.accountNumber).toEqual(Long.fromNumber(123)) - expect(account.sequence).toEqual(Long.fromNumber(1)) - }) - - it("should handle null/undefined baseAccount fields and return Long.ZERO", () => { - const ethAccount: EthAccount = { baseAccount: undefined, codeHash: "" } - - const account = accountFromEthAccount(ethAccount) - - expect(account.address).toEqual("") - expect(account.pubkey).toBeNull() - expect(account.accountNumber).toEqual(Long.ZERO) - expect(account.sequence).toEqual(Long.ZERO) + expect(account.accountNumber).toEqual(123) + expect(account.sequence).toEqual(1) }) }) describe("accountFromNibiru", () => { - it("should parse EthAccount typeUrl", () => { + it("should parse EthAccount typeUrl and return valid account", () => { const input: Any = { typeUrl: "/eth.types.v1.EthAccount", value: EthAccount.encode({ baseAccount: { - address: "nibi1234567", + address: "nibi1testaddress", pubKey: { typeUrl: "/cosmos.crypto.secp256k1.PubKey", value: new Uint8Array([4, 5, 6]), @@ -61,16 +62,25 @@ describe("accountFromNibiru", () => { const account = accountFromNibiru(input) - expect(account.address).toEqual("nibi1234567") // Ensure the prefix is 'nibi' + expect(account.address).toBe("nibi1testaddress") expect(account.pubkey).toEqual({ typeUrl: "/cosmos.crypto.secp256k1.PubKey", value: new Uint8Array([4, 5, 6]), }) - expect(account.accountNumber).toEqual(Long.fromNumber(456)) - expect(account.sequence).toEqual(Long.fromNumber(2)) + expect(account.accountNumber).toEqual(456) + expect(account.sequence).toEqual(2) }) - it("should call accountFromAny for non-EthAccount typeUrl", () => { + it("should handle non-EthAccount typeUrl by calling accountFromAny", () => { + const mockAccountFromAny = jest + .spyOn(cosmjs, "accountFromAny") + .mockReturnValue({ + address: "nibi1otheraddress", + pubkey: null, + accountNumber: 789, + sequence: 3, + }) + const input: Any = { typeUrl: "/other.types.v1.Account", value: new Uint8Array([7, 8, 9]), @@ -79,5 +89,6 @@ describe("accountFromNibiru", () => { const account = accountFromNibiru(input) expect(account.address).toBe("nibi1otheraddress") + expect(mockAccountFromAny).toHaveBeenCalledWith(input) }) }) diff --git a/src/sdk/tx/account.ts b/src/sdk/tx/account.ts index 808351bf..2a653883 100644 --- a/src/sdk/tx/account.ts +++ b/src/sdk/tx/account.ts @@ -9,14 +9,20 @@ import { Any } from "src/protojs/google/protobuf/any" * @param {EthAccount} ethAccount - The EthAccount object containing the account's base information. * @returns {Account} The Cosmos account object. */ -export const accountFromEthAccount = ({ - baseAccount, -}: EthAccount): Account => ({ - address: baseAccount?.address ?? "", - pubkey: decodeOptionalPubkey(baseAccount?.pubKey) ?? null, - accountNumber: baseAccount?.accountNumber?.toNumber() ?? 0, - sequence: baseAccount?.sequence?.toNumber() ?? 0, -}) +export const accountFromEthAccount = ({ baseAccount }: EthAccount): Account => { + if (!baseAccount) { + throw new Error("baseAccount is undefined in EthAccount") + } + + const { address, pubKey, accountNumber, sequence } = baseAccount + + return { + address, + pubkey: decodeOptionalPubkey(pubKey), + accountNumber: accountNumber.toNumber(), + sequence: sequence?.toNumber(), + } +} /** * Parses an account input into a Cosmos account. Handles both EthAccount and other standard accounts. From 46ea0ab3f885ea12edd6f23434a256a772229493 Mon Sep 17 00:00:00 2001 From: elshenak Date: Thu, 3 Oct 2024 22:28:55 -0400 Subject: [PATCH 06/10] test: fixing tests --- src/sdk/tx/account.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/sdk/tx/account.test.ts b/src/sdk/tx/account.test.ts index 76346f90..562bccc5 100644 --- a/src/sdk/tx/account.test.ts +++ b/src/sdk/tx/account.test.ts @@ -3,6 +3,14 @@ import { EthAccount } from "src/protojs/eth/types/v1/account" import { Any } from "src/protojs/google/protobuf/any" import Long from "long" import * as cosmjs from "@cosmjs/stargate" +import { decodeOptionalPubkey } from "@cosmjs/proto-signing" + +// Mock decodeOptionalPubkey +jest.mock("@cosmjs/proto-signing", () => ({ + decodeOptionalPubkey: jest.fn(), +})) + +const mockedDecodeOptionalPubkey = decodeOptionalPubkey as jest.Mock describe("accountFromEthAccount", () => { it("should throw an error when baseAccount is undefined", () => { @@ -30,6 +38,11 @@ describe("accountFromEthAccount", () => { codeHash: "", } + mockedDecodeOptionalPubkey.mockReturnValue({ + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: new Uint8Array([1, 2, 3]), + }) + const account = accountFromEthAccount(ethAccount) expect(account.address).toBe("nibi1testaddress") @@ -60,6 +73,11 @@ describe("accountFromNibiru", () => { }).finish(), } + mockedDecodeOptionalPubkey.mockReturnValue({ + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: new Uint8Array([4, 5, 6]), + }) + const account = accountFromNibiru(input) expect(account.address).toBe("nibi1testaddress") From 778867386a7f4019e52794aca5643afba2e4bf87 Mon Sep 17 00:00:00 2001 From: elshenak Date: Thu, 3 Oct 2024 22:37:24 -0400 Subject: [PATCH 07/10] chore: removing unnecessary ? --- src/sdk/tx/account.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdk/tx/account.ts b/src/sdk/tx/account.ts index 2a653883..b0410e71 100644 --- a/src/sdk/tx/account.ts +++ b/src/sdk/tx/account.ts @@ -20,7 +20,7 @@ export const accountFromEthAccount = ({ baseAccount }: EthAccount): Account => { address, pubkey: decodeOptionalPubkey(pubKey), accountNumber: accountNumber.toNumber(), - sequence: sequence?.toNumber(), + sequence: sequence.toNumber(), } } From 2e80b16cec8407b4089b549f9941fb09a8bae35a Mon Sep 17 00:00:00 2001 From: elshenak Date: Thu, 3 Oct 2024 22:52:53 -0400 Subject: [PATCH 08/10] refactor: matching cosmjs implementation --- src/sdk/tx/account.test.ts | 31 ++++++++++++------------------- src/sdk/tx/account.ts | 14 ++++++-------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/sdk/tx/account.test.ts b/src/sdk/tx/account.test.ts index 562bccc5..d392b404 100644 --- a/src/sdk/tx/account.test.ts +++ b/src/sdk/tx/account.test.ts @@ -4,6 +4,7 @@ import { Any } from "src/protojs/google/protobuf/any" import Long from "long" import * as cosmjs from "@cosmjs/stargate" import { decodeOptionalPubkey } from "@cosmjs/proto-signing" +import { BaseAccount } from "src/protojs/cosmos/auth/v1beta1/auth" // Mock decodeOptionalPubkey jest.mock("@cosmjs/proto-signing", () => ({ @@ -13,29 +14,21 @@ jest.mock("@cosmjs/proto-signing", () => ({ const mockedDecodeOptionalPubkey = decodeOptionalPubkey as jest.Mock describe("accountFromEthAccount", () => { - it("should throw an error when baseAccount is undefined", () => { - const ethAccount: EthAccount = { - baseAccount: undefined, // Simulating undefined baseAccount - codeHash: "", - } + it("should throw an error if baseAccount is undefined", () => { + const baseAccount: BaseAccount = undefined as unknown as BaseAccount - expect(() => accountFromEthAccount(ethAccount)).toThrowError( - "baseAccount is undefined in EthAccount" - ) + expect(() => accountFromEthAccount(baseAccount)).toThrow() }) it("should return a valid account when baseAccount is defined", () => { - const ethAccount: EthAccount = { - baseAccount: { - address: "nibi1testaddress", - pubKey: { - typeUrl: "/cosmos.crypto.secp256k1.PubKey", - value: new Uint8Array([1, 2, 3]), - }, - accountNumber: Long.fromNumber(123), - sequence: Long.fromNumber(1), + const baseAccount: BaseAccount = { + address: "nibi1testaddress", + pubKey: { + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: new Uint8Array([1, 2, 3]), }, - codeHash: "", + accountNumber: Long.fromNumber(123), + sequence: Long.fromNumber(1), } mockedDecodeOptionalPubkey.mockReturnValue({ @@ -43,7 +36,7 @@ describe("accountFromEthAccount", () => { value: new Uint8Array([1, 2, 3]), }) - const account = accountFromEthAccount(ethAccount) + const account = accountFromEthAccount(baseAccount) expect(account.address).toBe("nibi1testaddress") expect(account.pubkey).toEqual({ diff --git a/src/sdk/tx/account.ts b/src/sdk/tx/account.ts index b0410e71..cf3a2f40 100644 --- a/src/sdk/tx/account.ts +++ b/src/sdk/tx/account.ts @@ -2,6 +2,8 @@ import { decodeOptionalPubkey } from "@cosmjs/proto-signing" import { Account, accountFromAny, AccountParser } from "@cosmjs/stargate" import { EthAccount } from "src/protojs/eth/types/v1/account" import { Any } from "src/protojs/google/protobuf/any" +import { assert } from "@cosmjs/utils" +import { BaseAccount } from "src/protojs/cosmos/auth/v1beta1/auth" /** * Converts an EthAccount to a general Cosmos Account object. @@ -9,13 +11,8 @@ import { Any } from "src/protojs/google/protobuf/any" * @param {EthAccount} ethAccount - The EthAccount object containing the account's base information. * @returns {Account} The Cosmos account object. */ -export const accountFromEthAccount = ({ baseAccount }: EthAccount): Account => { - if (!baseAccount) { - throw new Error("baseAccount is undefined in EthAccount") - } - +export const accountFromEthAccount = (baseAccount: BaseAccount): Account => { const { address, pubKey, accountNumber, sequence } = baseAccount - return { address, pubkey: decodeOptionalPubkey(pubKey), @@ -34,8 +31,9 @@ export const accountFromNibiru: AccountParser = (input: Any): Account => { const { typeUrl, value } = input if (typeUrl === "/eth.types.v1.EthAccount") { - const ethAccount = EthAccount.decode(value) - return accountFromEthAccount(ethAccount) + const baseAccount = EthAccount.decode(value).baseAccount + assert(baseAccount) + return accountFromEthAccount(baseAccount) } return accountFromAny(input) From 2fa390e727f1740ad5eaa4c0a9ab28d7287d8501 Mon Sep 17 00:00:00 2001 From: elshenak Date: Fri, 4 Oct 2024 11:19:49 -0400 Subject: [PATCH 09/10] chore: removing t.json --- src/sdk/utils/t.json | 41 ----------------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 src/sdk/utils/t.json diff --git a/src/sdk/utils/t.json b/src/sdk/utils/t.json deleted file mode 100644 index d1c73380..00000000 --- a/src/sdk/utils/t.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "chainId": "cataclysm-1", - "chainName": "cataclysm-1", - "rpc": "https://rpc.nibiru.fi", - "rest": "https://lcd.nibiru.fi", - "stakeCurrency": { - "coinDenom": "NIBI", - "coinMinimalDenom": "unibi", - "coinDecimals": 6 - }, - "bip44": { - "coinType": 118 - }, - "bech32Config": { - "bech32PrefixAccAddr": "nibi", - "bech32PrefixAccPub": "nibipub", - "bech32PrefixValAddr": "nibivaloper", - "bech32PrefixValPub": "nibivaloperpub", - "bech32PrefixConsAddr": "nibivalcons", - "bech32PrefixConsPub": "nibivalconspub" - }, - "currencies": [ - { - "coinDenom": "NIBI", - "coinMinimalDenom": "unibi", - "coinDecimals": 6 - } - ], - "feeCurrencies": [ - { - "coinDenom": "NIBI", - "coinMinimalDenom": "unibi", - "coinDecimals": 6, - "gasPriceStep": { - "low": 0.05, - "average": 0.125, - "high": 0.2 - } - } - ] -} From ec9bdcabb453b69f6c5e50c0c2e252854d303f57 Mon Sep 17 00:00:00 2001 From: elshenak Date: Fri, 4 Oct 2024 11:21:23 -0400 Subject: [PATCH 10/10] chore: pr comments --- src/sdk/tx/account.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/sdk/tx/account.ts b/src/sdk/tx/account.ts index cf3a2f40..75d2fe05 100644 --- a/src/sdk/tx/account.ts +++ b/src/sdk/tx/account.ts @@ -11,15 +11,17 @@ import { BaseAccount } from "src/protojs/cosmos/auth/v1beta1/auth" * @param {EthAccount} ethAccount - The EthAccount object containing the account's base information. * @returns {Account} The Cosmos account object. */ -export const accountFromEthAccount = (baseAccount: BaseAccount): Account => { - const { address, pubKey, accountNumber, sequence } = baseAccount - return { - address, - pubkey: decodeOptionalPubkey(pubKey), - accountNumber: accountNumber.toNumber(), - sequence: sequence.toNumber(), - } -} +export const accountFromEthAccount = ({ + address, + pubKey, + accountNumber, + sequence, +}: BaseAccount): Account => ({ + address, + pubkey: decodeOptionalPubkey(pubKey), + accountNumber: accountNumber.toNumber(), + sequence: sequence.toNumber(), +}) /** * Parses an account input into a Cosmos account. Handles both EthAccount and other standard accounts.