Pull out toAccAddress/toObject
This commit is contained in:
parent
59687d0fd6
commit
b222f49cab
@ -1,16 +1,8 @@
|
||||
import { Bech32, toAscii } from "@cosmjs/encoding";
|
||||
import { toAscii } from "@cosmjs/encoding";
|
||||
|
||||
import { cosmos } from "../generated/codecimpl";
|
||||
import { QueryClient } from "../queryclient";
|
||||
|
||||
/**
|
||||
* Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin)
|
||||
* in a ways that makes Jasmine's toEqual happy.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
function toObject<I extends object>(thing: I): Omit<I, never> {
|
||||
return { ...thing };
|
||||
}
|
||||
import { toAccAddress, toObject } from "./utils";
|
||||
|
||||
export interface BankExtension {
|
||||
readonly bank: {
|
||||
@ -42,14 +34,13 @@ export function setupBankExtension(base: QueryClient): BankExtension {
|
||||
// it seem like prefix stores just do a dumb concat with the keys (no tricks to avoid overlap)
|
||||
// https://github.com/cosmos/cosmos-sdk/blob/2879c0702c87dc9dd828a8c42b9224dc054e28ad/store/prefix/store.go#L61-L64
|
||||
// https://github.com/cosmos/cosmos-sdk/blob/2879c0702c87dc9dd828a8c42b9224dc054e28ad/store/prefix/store.go#L37-L43
|
||||
const binAddress = Bech32.decode(address).data;
|
||||
const bankKey = Uint8Array.from([...toAscii("balances"), ...binAddress, ...toAscii(denom)]);
|
||||
const responseData = await base.queryVerified("bank", bankKey);
|
||||
const key = Uint8Array.from([...toAscii("balances"), ...toAccAddress(address), ...toAscii(denom)]);
|
||||
const responseData = await base.queryVerified("bank", key);
|
||||
return responseData.length ? toObject(cosmos.Coin.decode(responseData)) : null;
|
||||
},
|
||||
unverified: {
|
||||
allBalances: async (address: string) => {
|
||||
const { balances } = await queryService.allBalances({ address: Bech32.decode(address).data });
|
||||
const { balances } = await queryService.allBalances({ address: toAccAddress(address) });
|
||||
return balances.map(toObject);
|
||||
},
|
||||
},
|
||||
|
||||
1
packages/stargate/src/queries/index.ts
Normal file
1
packages/stargate/src/queries/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export { BankExtension, setupBankExtension } from "./bank";
|
||||
19
packages/stargate/src/queries/utils.ts
Normal file
19
packages/stargate/src/queries/utils.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { Bech32 } from "@cosmjs/encoding";
|
||||
|
||||
/**
|
||||
* Takes a bech32 encoded address and returns the data part. The prefix is ignored and discarded.
|
||||
* This is called AccAddress in Cosmos SDK, which is basically an alias for raw binary data.
|
||||
* The result is typically 20 bytes long but not restricted to that.
|
||||
*/
|
||||
export function toAccAddress(address: string): Uint8Array {
|
||||
return Bech32.decode(address).data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin)
|
||||
* in a ways that makes Jasmine's toEqual happy.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function toObject<I extends object>(thing: I): Omit<I, never> {
|
||||
return { ...thing };
|
||||
}
|
||||
@ -17,7 +17,7 @@ import { assert, assertDefined } from "@cosmjs/utils";
|
||||
import Long from "long";
|
||||
|
||||
import { cosmos } from "./generated/codecimpl";
|
||||
import { BankExtension, setupBankExtension } from "./queries/bank";
|
||||
import { BankExtension, setupBankExtension } from "./queries";
|
||||
import { QueryClient } from "./queryclient";
|
||||
|
||||
/** A transaction that is indexed as part of the transaction history */
|
||||
|
||||
1
packages/stargate/types/queries/index.d.ts
vendored
Normal file
1
packages/stargate/types/queries/index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export { BankExtension, setupBankExtension } from "./bank";
|
||||
11
packages/stargate/types/queries/utils.d.ts
vendored
Normal file
11
packages/stargate/types/queries/utils.d.ts
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Takes a bech32 encoded address and returns the data part. The prefix is ignored and discarded.
|
||||
* This is called AccAddress in Cosmos SDK, which is basically an alias for raw binary data.
|
||||
* The result is typically 20 bytes long but not restricted to that.
|
||||
*/
|
||||
export declare function toAccAddress(address: string): Uint8Array;
|
||||
/**
|
||||
* Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin)
|
||||
* in a ways that makes Jasmine's toEqual happy.
|
||||
*/
|
||||
export declare function toObject<I extends object>(thing: I): Omit<I, never>;
|
||||
Loading…
Reference in New Issue
Block a user