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

Missing getEquityTiers native function #69

Merged
merged 5 commits into from
Oct 26, 2023
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
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