Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: copy the v4-web parseToPrimitives utility here for the mobile native #261

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
14 changes: 7 additions & 7 deletions v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
Native app can call JS functions with primitives.
*/
Expand Down Expand Up @@ -30,6 +31,7 @@ import {
SelectedGasDenom,
} from './constants';
import { FaucetClient } from './faucet-client';
import { parseToPrimitives } from './helpers/request-helpers';
import { Response } from './lib/axios';
import LocalWallet from './modules/local-wallet';
import { NobleClient } from './noble-client';
Expand Down Expand Up @@ -1366,7 +1368,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 +1384,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 +1409,7 @@ export async function depositToMegavault(
amountUsdc,
Method.BroadcastTxCommit,
);
return encodeJson(tx);
return encodeJson(parseToPrimitives(tx));
} catch (error) {
return wrappedError(error);
}
Expand All @@ -1434,10 +1436,8 @@ export async function withdrawFromMegavault(
minAmount,
Method.BroadcastTxCommit,
);
return encodeJson(tx);
return encodeJson(parseToPrimitives(tx));
} catch (error) {
return wrappedError(error);
}
}


}
Loading