Skip to content

Commit

Permalink
Copy the v4-web parseToPrimitives utility here for the mobile native …
Browse files Browse the repository at this point in the history
…client
  • Loading branch information
ruixhuang committed Sep 27, 2024
1 parent e62c959 commit 92ca627
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 18 deletions.
69 changes: 61 additions & 8 deletions v4-client-js/__native__/__ios__/v4-native-client.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions v4-client-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v4-client-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dydxprotocol/v4-client-js",
"version": "1.3.7",
"version": "1.3.8",
"description": "General client library for the new dYdX system (v4 decentralized)",
"main": "build/src/index.js",
"scripts": {
Expand Down
57 changes: 57 additions & 0 deletions v4-client-js/src/clients/helpers/request-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Long from "long";

/* eslint-disable @typescript-eslint/no-explicit-any */
export function generateQueryPath(url: string, params: {}): string {
const definedEntries = Object.entries(params).filter(
([_key, value]: [string, unknown]) => value !== undefined,
Expand All @@ -12,3 +15,57 @@ export function generateQueryPath(url: string, params: {}): string {
.join('&');
return `${url}?${paramsString}`;
}

export function parseToPrimitives<T>(x: T): T {
if (typeof x === 'number' || typeof x === 'string' || typeof x === 'boolean' || x === null) {
return x;
}

if (Array.isArray(x)) {
return x.map((item) => parseToPrimitives(item)) as T;
}

if (Long.isLong(x)) {
return x.toString() as T;
}

if (x instanceof Uint8Array) {
return bytesToBigInt(x).toString() as T;
}

if (x instanceof Date) {
return x.toString() as T;
}

if (typeof x === 'object') {
const parsedObj: { [key: string]: any } = {};
// eslint-disable-next-line no-restricted-syntax
for (const key in x) {
if (Object.prototype.hasOwnProperty.call(x, key)) {
parsedObj[key] = parseToPrimitives((x as any)[key]);
}
}
return parsedObj as T;
}

if (typeof x === 'bigint') {
return x.toString() as T;
}

throw new Error(`Unsupported data type: ${typeof x}`);
}

/**
* Converts a byte array (representing an arbitrary-size signed integer) into a bigint.
* @param u Array of bytes represented as a Uint8Array.
*/
function bytesToBigInt(u: Uint8Array): bigint {
if (u.length <= 1) {
return BigInt(0);
}
// eslint-disable-next-line no-bitwise
const negated: boolean = (u[0] & 1) === 1;
const hex: string = Buffer.from(u.slice(1)).toString('hex');
const abs: bigint = BigInt(`0x${hex}`);
return negated ? -abs : abs;
}
13 changes: 6 additions & 7 deletions v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import LocalWallet from './modules/local-wallet';
import { NobleClient } from './noble-client';
import { SubaccountInfo } from './subaccount';
import { OrderFlags, SquidIBCPayload } from './types';
import { parseToPrimitives } from './helpers/request-helpers';

Check failure on line 38 in v4-client-js/src/clients/native.ts

View workflow job for this annotation

GitHub Actions / CI

`./helpers/request-helpers` import should occur before import of `./lib/axios`

declare global {
// eslint-disable-next-line vars-on-top, no-var
Expand Down Expand Up @@ -1366,7 +1367,7 @@ export async function getMegavaultOwnerShares(payload: string): Promise<string>
}
const response =
await globalThis.client?.validatorClient.get.getMegavaultOwnerShares(address);
return encodeJson(response);
return encodeJson(parseToPrimitives(response));
} catch (e) {
return wrappedError(e);
}
Expand All @@ -1382,7 +1383,7 @@ export async function getMegavaultWithdrawalInfo(
}
const response =
await globalThis.client?.validatorClient.get.getMegavaultWithdrawalInfo(sharesToWithdraw);
return encodeJson(response);
return encodeJson(parseToPrimitives(response));
} catch (e) {
return wrappedError(e);
}
Expand All @@ -1407,7 +1408,7 @@ export async function depositToMegavault(
amountUsdc,
Method.BroadcastTxCommit,
);
return encodeJson(tx);
return encodeJson(parseToPrimitives(tx));
} catch (error) {
return wrappedError(error);
}
Expand All @@ -1434,10 +1435,8 @@ export async function withdrawFromMegavault(
minAmount,
Method.BroadcastTxCommit,
);
return encodeJson(tx);
return encodeJson(parseToPrimitives(tx));
} catch (error) {
return wrappedError(error);
}
}


}

0 comments on commit 92ca627

Please sign in to comment.