Skip to content

Commit

Permalink
Missing getEquityTiers native function (#69)
Browse files Browse the repository at this point in the history
* Missing getEquityTiers native function

* UInt8Array needs to be encoded as BigInt string

* lint

* update for native

* Uint8Array encoding needs options. Could be txHash
  • Loading branch information
johnqh authored Oct 26, 2023
1 parent cd84dbd commit 1e4e378
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
55 changes: 48 additions & 7 deletions v4-client-js/__native__/__ios__/v4-native-client.js

Large diffs are not rendered by default.

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.0.0",
"version": "1.0.1",
"description": "General client library for the new dYdX system (v4 decentralized)",
"main": "build/src/index.js",
"scripts": {
Expand Down
16 changes: 15 additions & 1 deletion v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Long from 'long';

import { BECH32_PREFIX } from '../lib/constants';
import { UserError } from '../lib/errors';
import { encodeJson } from '../lib/helpers';
import { ByteArrayEncoding, encodeJson } from '../lib/helpers';
import { deriveHDKeyFromEthereumSignature } from '../lib/onboarding';
import { NetworkOptimizer } from '../network_optimizer';
import { CompositeClient, MarketInfo } from './composite-client';
Expand Down Expand Up @@ -172,6 +172,20 @@ export async function getUserFeeTier(address: string): Promise<string> {
}
}

export async function getEquityTiers(): Promise<string> {
try {
const client = globalThis.client;
if (client === undefined) {
throw new UserError('client is not connected. Call connectClient() first');
}
const equityTiers = await globalThis.client?.validatorClient.get
.getEquityTierLimitConfiguration();
return encodeJson(equityTiers, ByteArrayEncoding.BIGINT);
} catch (e) {
return wrappedError(e);
}
}

export async function getPerpetualMarkets(): Promise<string> {
try {
const client = globalThis.client;
Expand Down
33 changes: 30 additions & 3 deletions v4-client-js/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,26 @@ export function stripHexPrefix(input: string): string {
return input;
}

export function encodeJson(object?: Object): string {
function toBigInt(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;
}

export enum ByteArrayEncoding {
HEX = 'hex',
BIGINT = 'bigint',
}

export function encodeJson(
object?: Object,
byteArrayEncoding: ByteArrayEncoding = ByteArrayEncoding.HEX,
): string {
// eslint-disable-next-line prefer-arrow-callback
return JSON.stringify(object, function replacer(_key, value) {
// Even though we set the an UInt8Array as the value,
Expand All @@ -50,9 +69,17 @@ export function encodeJson(object?: Object): string {
return value.toString();
}
if (value?.buffer instanceof Uint8Array) {
return toHex(value.buffer);
if (byteArrayEncoding === ByteArrayEncoding.HEX) {
return toHex(value.buffer);
} else {
return toBigInt(value.buffer).toString();
}
} else if (value instanceof Uint8Array) {
return toHex(value);
if (byteArrayEncoding === ByteArrayEncoding.HEX) {
return toHex(value);
} else {
return toBigInt(value).toString();
}
}
return value;
});
Expand Down

0 comments on commit 1e4e378

Please sign in to comment.