Skip to content

Commit 713f6bb

Browse files
committed
move to proto cosmjs/types style queries/decoding
1 parent fe524db commit 713f6bb

File tree

3 files changed

+166
-314
lines changed

3 files changed

+166
-314
lines changed

packages/stargate/src/feemarket.spec.ts

Lines changed: 53 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -22,79 +22,79 @@ describe("Osmosis chain ID detection", () => {
2222

2323
describe("Osmosis production chains", () => {
2424
it('detects "osmosis-1" as Osmosis chain', async () => {
25-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
25+
// Real Osmosis response: DecProto { dec: "30000000000000000" } = 0.03
26+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
2627
const queryClient = createMockQueryClient(response);
2728

2829
const result = await queryDynamicGasPrice(queryClient, "uosmo", "osmosis-1");
29-
expect(result.toString()).toBe("0.0025");
30+
expect(result.toString()).toBe("0.03");
3031
});
3132

3233
it('detects "osmosis-42" as Osmosis chain', async () => {
33-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
34+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
3435
const queryClient = createMockQueryClient(response);
3536

3637
const result = await queryDynamicGasPrice(queryClient, "uosmo", "osmosis-42");
37-
expect(result.toString()).toBe("0.0025");
38+
expect(result.toString()).toBe("0.03");
3839
});
3940

4041
it('detects "OSMOSIS-1" (uppercase) as Osmosis chain', async () => {
41-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
42+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
4243
const queryClient = createMockQueryClient(response);
4344

4445
const result = await queryDynamicGasPrice(queryClient, "uosmo", "OSMOSIS-1");
45-
expect(result.toString()).toBe("0.0025");
46+
expect(result.toString()).toBe("0.03");
4647
});
4748
});
4849

4950
describe("Osmosis test chains", () => {
5051
it('detects "osmo-test-4" as Osmosis chain', async () => {
51-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
52+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
5253
const queryClient = createMockQueryClient(response);
5354

5455
const result = await queryDynamicGasPrice(queryClient, "uosmo", "osmo-test-4");
55-
expect(result.toString()).toBe("0.0025");
56+
expect(result.toString()).toBe("0.03");
5657
});
5758

5859
it('detects "osmo-test-5" as Osmosis chain', async () => {
59-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
60+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
6061
const queryClient = createMockQueryClient(response);
6162

6263
const result = await queryDynamicGasPrice(queryClient, "uosmo", "osmo-test-5");
63-
expect(result.toString()).toBe("0.0025");
64+
expect(result.toString()).toBe("0.03");
6465
});
6566
});
6667

6768
describe("Osmosis local/dev chains", () => {
6869
it('detects "osmosislocal" as Osmosis chain', async () => {
69-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
70+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
7071
const queryClient = createMockQueryClient(response);
7172

7273
const result = await queryDynamicGasPrice(queryClient, "uosmo", "osmosislocal");
73-
expect(result.toString()).toBe("0.0025");
74+
expect(result.toString()).toBe("0.03");
7475
});
7576

7677
it('detects "osmosisdev" as Osmosis chain', async () => {
77-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
78+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
7879
const queryClient = createMockQueryClient(response);
7980

8081
const result = await queryDynamicGasPrice(queryClient, "uosmo", "osmosisdev");
81-
expect(result.toString()).toBe("0.0025");
82+
expect(result.toString()).toBe("0.03");
8283
});
8384

8485
it('detects "osmosistestnet" as Osmosis chain', async () => {
85-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
86+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
8687
const queryClient = createMockQueryClient(response);
8788

8889
const result = await queryDynamicGasPrice(queryClient, "uosmo", "osmosistestnet");
89-
expect(result.toString()).toBe("0.0025");
90+
expect(result.toString()).toBe("0.03");
9091
});
9192
});
9293

9394
describe("Non-Osmosis chains use feemarket", () => {
9495
it('treats "cosmoshub-4" as non-Osmosis chain', async () => {
95-
const feemarketResponse = new Uint8Array([
96-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
97-
]);
96+
// Real feemarket response: GasPricesResponse { prices: [DecCoin { denom: "uatom", amount: "5300000000000000" }] } = 0.0053
97+
const feemarketResponse = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
9898
const queryClient = {
9999
queryAbci: async (path: string, _data: Uint8Array) => {
100100
if (path === "/feemarket.feemarket.v1.Query/GasPrices") {
@@ -105,13 +105,11 @@ describe("Osmosis chain ID detection", () => {
105105
};
106106

107107
const result = await queryDynamicGasPrice(queryClient, "uatom", "cosmoshub-4");
108-
expect(result.toString()).toBe("0.0025");
108+
expect(result.toString()).toBe("0.0053");
109109
});
110110

111111
it('treats "juno-1" as non-Osmosis chain', async () => {
112-
const feemarketResponse = new Uint8Array([
113-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
114-
]);
112+
const feemarketResponse = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
115113
const queryClient = {
116114
queryAbci: async (path: string, _data: Uint8Array) => {
117115
if (path === "/feemarket.feemarket.v1.Query/GasPrices") {
@@ -122,13 +120,11 @@ describe("Osmosis chain ID detection", () => {
122120
};
123121

124122
const result = await queryDynamicGasPrice(queryClient, "ujuno", "juno-1");
125-
expect(result.toString()).toBe("0.0025");
123+
expect(result.toString()).toBe("0.0053");
126124
});
127125

128126
it('does not match "osmosis" alone (no suffix)', async () => {
129-
const feemarketResponse = new Uint8Array([
130-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
131-
]);
127+
const feemarketResponse = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
132128
const queryClient = {
133129
queryAbci: async (path: string, _data: Uint8Array) => {
134130
if (path === "/feemarket.feemarket.v1.Query/GasPrices") {
@@ -140,13 +136,11 @@ describe("Osmosis chain ID detection", () => {
140136

141137
// "osmosis" without suffix or number should use feemarket
142138
const result = await queryDynamicGasPrice(queryClient, "uatom", "osmosis");
143-
expect(result.toString()).toBe("0.0025");
139+
expect(result.toString()).toBe("0.0053");
144140
});
145141

146142
it('does not match chains that merely contain "osmosis" as substring', async () => {
147-
const feemarketResponse = new Uint8Array([
148-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
149-
]);
143+
const feemarketResponse = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
150144
const queryClient = {
151145
queryAbci: async (path: string, _data: Uint8Array) => {
152146
if (path === "/feemarket.feemarket.v1.Query/GasPrices") {
@@ -157,7 +151,7 @@ describe("Osmosis chain ID detection", () => {
157151
};
158152

159153
const result = await queryDynamicGasPrice(queryClient, "uatom", "myosmosischain-1");
160-
expect(result.toString()).toBe("0.0025");
154+
expect(result.toString()).toBe("0.0053");
161155
});
162156
});
163157
});
@@ -174,20 +168,19 @@ describe("queryDynamicGasPrice", () => {
174168

175169
describe("Osmosis chain detection", () => {
176170
it("detects osmosis chain IDs", async () => {
177-
// Create a mock Osmosis response: DecProto { dec: "0.0025" }
178-
// Field tag 1 (0x0a), length 5, string "0.0025"
179-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
171+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
180172
const queryClient = createMockQueryClient(response);
181173

182-
const chainIds = ["osmosis-1", "osmo-test-4", "osmosis-mainnet"];
174+
// Only test valid Osmosis chain IDs that match the regex patterns
175+
const chainIds = ["osmosis-1", "osmo-test-4", "osmosislocal"];
183176
for (const chainId of chainIds) {
184177
const result = await queryDynamicGasPrice(queryClient, "uosmo", chainId);
185-
expect(result.toString()).toBe("0.0025");
178+
expect(result.toString()).toBe("0.03");
186179
}
187180
});
188181

189182
it("uses Osmosis endpoint for osmosis chains", async () => {
190-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
183+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
191184
const queryClient = createMockQueryClient(response);
192185
let calledPath = "";
193186
const mockClient = {
@@ -205,12 +198,7 @@ describe("queryDynamicGasPrice", () => {
205198

206199
describe("Skip feemarket chain detection", () => {
207200
it("uses feemarket endpoint for non-Osmosis chains", async () => {
208-
// Create a mock feemarket response: GasPricesResponse { price: DecCoin { denom: "uatom", amount: "0.0025" } }
209-
// Outer: Field tag 1 (0x0a), length 8, DecCoin message
210-
// Inner DecCoin: Field tag 2 (0x12), length 5, string "0.0025"
211-
const response = new Uint8Array([
212-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
213-
]);
201+
const response = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
214202
const queryClient = createMockQueryClient(response);
215203
let calledPath = "";
216204
let calledData: Uint8Array | null = null;
@@ -229,9 +217,7 @@ describe("queryDynamicGasPrice", () => {
229217
});
230218

231219
it("encodes denom in request for feemarket chains", async () => {
232-
const response = new Uint8Array([
233-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
234-
]);
220+
const response = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
235221
let capturedData: Uint8Array | null = null;
236222
const mockClient = {
237223
queryAbci: async (_path: string, data: Uint8Array) => {
@@ -254,20 +240,18 @@ describe("queryDynamicGasPrice", () => {
254240

255241
describe("Osmosis response parsing", () => {
256242
it("parses valid Osmosis EIP-1559 response", async () => {
257-
// DecProto { dec: "0.0025" }
258-
// Field tag 1 (0x0a = (1 << 3) | 2), length 5, string "0.0025"
259-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
243+
const response = new Uint8Array([10, 17, 50, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
260244
const queryClient = createMockQueryClient(response);
261245

262246
const result = await queryDynamicGasPrice(queryClient, "uosmo", "osmosis-1");
263-
expect(result.toString()).toBe("0.0025");
247+
expect(result.toString()).toBe("0.025");
264248
});
265249

266250
it("parses Osmosis response with different decimal values", async () => {
267251
const testCases = [
268-
{ value: "0.0001", bytes: [0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x31] },
269-
{ value: "1.5", bytes: [0x0a, 0x02, 0x31, 0x2e, 0x35] },
270-
{ value: "100", bytes: [0x0a, 0x03, 0x31, 0x30, 0x30] },
252+
{ value: "0.025", bytes: [10, 17, 50, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48] },
253+
{ value: "1.5", bytes: [10, 19, 49, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48] },
254+
{ value: "100", bytes: [10, 21, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48] },
271255
];
272256

273257
for (const testCase of testCases) {
@@ -284,47 +268,34 @@ describe("queryDynamicGasPrice", () => {
284268
const queryClient = createMockQueryClient(emptyResponse);
285269

286270
await expectAsync(queryDynamicGasPrice(queryClient, "uosmo", "osmosis-1")).toBeRejectedWithError(
287-
/Failed to parse Osmosis EIP-1559 gas price response/i,
271+
/DecProto: dec field not found/i,
288272
);
289273
});
290274
});
291275

292276
describe("Skip feemarket response parsing", () => {
293277
it("parses valid feemarket response", async () => {
294-
// GasPricesResponse { price: DecCoin { amount: "0.0025" } }
295-
// Outer: Field tag 1 (0x0a), length 8, DecCoin message
296-
// Inner: Field tag 2 (0x12 = (2 << 3) | 2), length 5, string "0.0025"
297-
const response = new Uint8Array([
298-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
299-
]);
278+
const response = new Uint8Array([10, 19, 18, 17, 50, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
300279
const queryClient = createMockQueryClient(response);
301280

302281
const result = await queryDynamicGasPrice(queryClient, "uatom", "cosmoshub-4");
303-
expect(result.toString()).toBe("0.0025");
282+
expect(result.toString()).toBe("0.025");
304283
});
305284

306285
it("parses feemarket response with denom field", async () => {
307-
// GasPricesResponse { price: DecCoin { denom: "uatom" (field 1), amount: "0.0025" (field 2) } }
308-
// Outer: Field tag 1 (0x0a), length 13
309-
// Inner: Field tag 1 (0x0a), length 5, "uatom"; Field tag 2 (0x12), length 5, "0.0025"
310-
const response = new Uint8Array([
311-
0x0a, 0x0d, 0x0a, 0x05, 0x75, 0x61, 0x74, 0x6f, 0x6d, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32,
312-
0x35,
313-
]);
286+
// GasPricesResponse { prices: [DecCoin { denom: "uatom", amount: "5300000000000000" }] }
287+
const response = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
314288
const queryClient = createMockQueryClient(response);
315289

316290
const result = await queryDynamicGasPrice(queryClient, "uatom", "cosmoshub-4");
317-
expect(result.toString()).toBe("0.0025");
291+
expect(result.toString()).toBe("0.0053");
318292
});
319293

320294
it("parses feemarket response with different decimal values", async () => {
321295
const testCases = [
322-
{
323-
value: "0.0001",
324-
bytes: [0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x31],
325-
},
326-
{ value: "1.5", bytes: [0x0a, 0x05, 0x12, 0x02, 0x31, 0x2e, 0x35] },
327-
{ value: "100", bytes: [0x0a, 0x06, 0x12, 0x03, 0x31, 0x30, 0x30] },
296+
{ value: "0.025", bytes: [10, 19, 18, 17, 50, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48] },
297+
{ value: "1.5", bytes: [10, 21, 18, 19, 49, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48] },
298+
{ value: "100", bytes: [10, 23, 18, 21, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48] },
328299
];
329300

330301
for (const testCase of testCases) {
@@ -341,7 +312,7 @@ describe("queryDynamicGasPrice", () => {
341312
const queryClient = createMockQueryClient(emptyResponse);
342313

343314
await expectAsync(queryDynamicGasPrice(queryClient, "uatom", "cosmoshub-4")).toBeRejectedWithError(
344-
/Failed to parse feemarket gas price response/i,
315+
/GasPricesResponse: amount not found/i,
345316
);
346317
});
347318

@@ -353,17 +324,15 @@ describe("queryDynamicGasPrice", () => {
353324
const queryClient = createMockQueryClient(response);
354325

355326
await expectAsync(queryDynamicGasPrice(queryClient, "uatom", "cosmoshub-4")).toBeRejectedWithError(
356-
/Failed to find amount field in DecCoin/i,
327+
/premature EOF|amount not found/i,
357328
);
358329
});
359330
});
360331

361332
describe("request encoding", () => {
362333
it("encodes different denoms correctly", async () => {
363334
const denoms = ["uatom", "uosmo", "ustake", "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"];
364-
const response = new Uint8Array([
365-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
366-
]);
335+
const response = new Uint8Array([10, 19, 18, 17, 50, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
367336

368337
for (const denom of denoms) {
369338
let capturedData: Uint8Array | null = null;
@@ -401,7 +370,7 @@ describe("queryDynamicGasPrice", () => {
401370

402371
describe("checkDynamicGasPriceSupport", () => {
403372
it("returns true for Osmosis chain with EIP-1559 support", async () => {
404-
const response = new Uint8Array([0x0a, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35]);
373+
const response = new Uint8Array([10, 17, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
405374
const queryClient = {
406375
queryAbci: async (path: string, _data: Uint8Array) => {
407376
if (path === "/osmosis.txfees.v1beta1.Query/GetEipBaseFee") {
@@ -416,9 +385,7 @@ describe("checkDynamicGasPriceSupport", () => {
416385
});
417386

418387
it("returns true for chain with Skip feemarket support", async () => {
419-
const response = new Uint8Array([
420-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
421-
]);
388+
const response = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
422389
const queryClient = {
423390
queryAbci: async (path: string, _data: Uint8Array) => {
424391
if (path === "/feemarket.feemarket.v1.Query/GasPrices") {
@@ -433,9 +400,7 @@ describe("checkDynamicGasPriceSupport", () => {
433400
});
434401

435402
it("falls back to Skip feemarket if Osmosis endpoint fails", async () => {
436-
const response = new Uint8Array([
437-
0x0a, 0x08, 0x12, 0x05, 0x30, 0x2e, 0x30, 0x30, 0x32, 0x35,
438-
]);
403+
const response = new Uint8Array([10, 25, 10, 5, 117, 97, 116, 111, 109, 18, 16, 53, 51, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]);
439404
const queryClient = {
440405
queryAbci: async (path: string, _data: Uint8Array) => {
441406
if (path === "/osmosis.txfees.v1beta1.Query/GetEipBaseFee") {

0 commit comments

Comments
 (0)