Skip to content

Commit

Permalink
Merge branch 'main' into samtin0x/update-v2-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
samtin0x authored Oct 18, 2024
2 parents 50cb3f1 + 9b3a6f6 commit 590aa12
Show file tree
Hide file tree
Showing 24 changed files with 2,312 additions and 326 deletions.
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
v4-client-cpp/ @asnefedovv @dydxprotocol/eng
v4-client-py/ @kaloureyes3 @dydxprotocol/eng
v4-client-py-v2/ @samtin0x @therustmonk @piwonskp @dydxprotocol/eng
v4-client-py-v2/ @dydxprotocol/engineering @dydxprotocol/nethermind
v4-client-rs/ @dydxprotocol/engineering @dydxprotocol/nethermind

* @dydxprotocol/eng
3 changes: 2 additions & 1 deletion .github/workflows/commitlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: "[v4-client-js] Conventional PR Title"
on:
pull_request:
types: ['opened', 'edited', 'reopened', 'synchronize']

paths:
- 'v4-client-js/**'
jobs:
conventional-pr-title:
runs-on: ubuntu-latest
Expand Down
2,301 changes: 2,004 additions & 297 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.11",
"description": "General client library for the new dYdX system (v4 decentralized)",
"main": "build/src/index.js",
"scripts": {
Expand Down
10 changes: 10 additions & 0 deletions v4-client-js/src/clients/composite-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,14 @@ export class CompositeClient {

return this.send(wallet, () => msg, false, undefined, memo);
}

async createMarketPermissionless(
subaccount: SubaccountInfo,
ticker: string,
broadcastMode?: BroadcastMode,
gasAdjustment?: number,
memo?: string,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
return this.validatorClient.post.createMarketPermissionless(ticker, subaccount, broadcastMode, gasAdjustment, memo);
}
}
6 changes: 6 additions & 0 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export const TYPE_URL_MSG_UPDATE_CLOB_PAIR = '/dydxprotocol.clob.MsgUpdateClobPa
// x/delaymsg
export const TYPE_URL_MSG_DELAY_MESSAGE = '/dydxprotocol.delaymsg.MsgDelayMessage';

// x/listing
export const TYPE_URL_MSG_CREATE_MARKET_PERMISSIONLESS = '/dydxprotocol.listing.MsgCreateMarketPermissionless';

// x/perpetuals
export const TYPE_URL_MSG_CREATE_PERPETUAL = '/dydxprotocol.perpetuals.MsgCreatePerpetual';

Expand All @@ -97,6 +100,9 @@ export const TYPE_URL_MSG_WITHDRAW_FROM_SUBACCOUNT =
'/dydxprotocol.sending.MsgWithdrawFromSubaccount';
export const TYPE_URL_MSG_DEPOSIT_TO_SUBACCOUNT = '/dydxprotocol.sending.MsgDepositToSubaccount';

// x/affiliates
export const TYPE_URL_MSG_REGISTER_AFFILIATE = '/dydxprotocol.affiliates.MsgRegisterAffiliate';

// x/vault
export const TYPE_URL_MSG_DEPOSIT_TO_MEGAVAULT = '/dydxprotocol.vault.MsgDepositToMegavault';
export const TYPE_URL_MSG_WITHDRAW_FROM_MEGAVAULT = '/dydxprotocol.vault.MsgWithdrawFromMegavault';
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;
}
11 changes: 11 additions & 0 deletions v4-client-js/src/clients/lib/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GeneratedType, Registry } from '@cosmjs/proto-signing';
import { defaultRegistryTypes } from '@cosmjs/stargate';
import { MsgRegisterAffiliate } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/affiliates/tx';
import {
MsgPlaceOrder,
MsgCancelOrder,
Expand All @@ -8,6 +9,7 @@ import {
MsgBatchCancel,
} from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/tx';
import { MsgDelayMessage } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/delaymsg/tx';
import { MsgCreateMarketPermissionless } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/listing/tx';
import { MsgCreatePerpetual } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/perpetuals/tx';
import { MsgCreateOracleMarket } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/prices/tx';
import {
Expand All @@ -34,6 +36,8 @@ import {
TYPE_URL_BATCH_CANCEL,
TYPE_URL_MSG_DEPOSIT_TO_MEGAVAULT,
TYPE_URL_MSG_WITHDRAW_FROM_MEGAVAULT,
TYPE_URL_MSG_REGISTER_AFFILIATE,
TYPE_URL_MSG_CREATE_MARKET_PERMISSIONLESS,
} from '../constants';

export const registry: ReadonlyArray<[string, GeneratedType]> = [];
Expand All @@ -49,6 +53,9 @@ export function generateRegistry(): Registry {
// delaymsg
[TYPE_URL_MSG_DELAY_MESSAGE, MsgDelayMessage as GeneratedType],

// listing
[TYPE_URL_MSG_CREATE_MARKET_PERMISSIONLESS, MsgCreateMarketPermissionless as GeneratedType],

// perpetuals
[TYPE_URL_MSG_CREATE_PERPETUAL, MsgCreatePerpetual as GeneratedType],

Expand All @@ -58,11 +65,15 @@ export function generateRegistry(): Registry {
// vaults
[TYPE_URL_MSG_DEPOSIT_TO_MEGAVAULT, MsgDepositToMegavault as GeneratedType],
[TYPE_URL_MSG_WITHDRAW_FROM_MEGAVAULT, MsgWithdrawFromMegavault as GeneratedType],

// sending
[TYPE_URL_MSG_CREATE_TRANSFER, MsgCreateTransfer as GeneratedType],
[TYPE_URL_MSG_WITHDRAW_FROM_SUBACCOUNT, MsgWithdrawFromSubaccount as GeneratedType],
[TYPE_URL_MSG_DEPOSIT_TO_SUBACCOUNT, MsgDepositToSubaccount as GeneratedType],

// affiliates
[TYPE_URL_MSG_REGISTER_AFFILIATE, MsgRegisterAffiliate as GeneratedType],

// default types
...defaultRegistryTypes,
]);
Expand Down
39 changes: 39 additions & 0 deletions v4-client-js/src/clients/modules/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MsgDelegate,
MsgUndelegate,
} from '@dydxprotocol/v4-proto/src/codegen/cosmos/staking/v1beta1/tx';
import { MsgRegisterAffiliate } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/affiliates/tx';
import { ClobPair_Status } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/clob_pair';
import {
MsgBatchCancel,
Expand Down Expand Up @@ -47,8 +48,10 @@ import {
TYPE_URL_MSG_UNDELEGATE,
TYPE_URL_MSG_WITHDRAW_DELEGATOR_REWARD,
TYPE_URL_BATCH_CANCEL,
TYPE_URL_MSG_REGISTER_AFFILIATE,
TYPE_URL_MSG_DEPOSIT_TO_MEGAVAULT,
TYPE_URL_MSG_WITHDRAW_FROM_MEGAVAULT,
TYPE_URL_MSG_CREATE_MARKET_PERMISSIONLESS,
} from '../constants';
import { DenomConfig } from '../types';
import {
Expand All @@ -60,6 +63,7 @@ import {
MsgPlaceOrder,
MsgCancelOrder,
SubaccountId,
MsgCreateMarketPermissionless,
MsgCreateTransfer,
Transfer,
MsgDepositToSubaccount,
Expand Down Expand Up @@ -536,6 +540,41 @@ export class Composer {
};
}

// ------------ x/affiliates ------------
public composeMsgRegisterAffiliate(referee: string, affiliate: string): EncodeObject {
const msg: MsgRegisterAffiliate = {
referee,
affiliate,
};

return {
typeUrl: TYPE_URL_MSG_REGISTER_AFFILIATE,
value: msg,
};
}

// ------------ x/listing ------------
public composeMsgCreateMarketPermissionless(
address: string,
ticker: string,
subaccountNumber: number,
): EncodeObject {
const subaccountId: SubaccountId = {
owner: address,
number: subaccountNumber,
};

const msg: MsgCreateMarketPermissionless = {
ticker,
subaccountId
};

return {
typeUrl: TYPE_URL_MSG_CREATE_MARKET_PERMISSIONLESS,
value: msg,
}
}

// ------------ util ------------
public validateGoodTilBlockAndTime(
orderFlags: number,
Expand Down
51 changes: 51 additions & 0 deletions v4-client-js/src/clients/modules/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { bigIntToBytes } from '../../lib/helpers';
import { PAGE_REQUEST } from '../constants';
import { UnexpectedClientError } from '../lib/errors';
import {
AffiliateModule,
BridgeModule,
ClobModule,
DistributionModule,
Expand Down Expand Up @@ -559,6 +560,56 @@ export class Get {
return VaultModule.QueryMegavaultWithdrawalInfoResponse.decode(data);
}

async getAffiliateInfo(address: string): Promise<AffiliateModule.AffiliateInfoResponse> {
const requestData = Uint8Array.from(
AffiliateModule.AffiliateInfoRequest.encode({
address,
}).finish(),
);

const data = await this.sendQuery('/dydxprotocol.affiliates.Query/AffiliateInfo', requestData);

return AffiliateModule.AffiliateInfoResponse.decode(data);
}

async getReferredBy(address: string): Promise<AffiliateModule.ReferredByResponse> {
const requestData = Uint8Array.from(
AffiliateModule.ReferredByRequest.encode({
address,
}).finish(),
);

const data = await this.sendQuery('/dydxprotocol.affiliates.Query/ReferredBy', requestData);

return AffiliateModule.ReferredByResponse.decode(data);
}

async getAllAffiliateTiers(): Promise<AffiliateModule.AllAffiliateTiersResponse> {
const requestData = Uint8Array.from(
AffiliateModule.AllAffiliateTiersRequest.encode({}).finish(),
);

const data = await this.sendQuery(
'/dydxprotocol.affiliates.Query/AllAffiliateTiers',
requestData,
);

return AffiliateModule.AllAffiliateTiersResponse.decode(data);
}

async getAffiliateWhitelist(): Promise<AffiliateModule.AffiliateWhitelistResponse> {
const requestData = Uint8Array.from(
AffiliateModule.AffiliateWhitelistRequest.encode({}).finish(),
);

const data = await this.sendQuery(
'/dydxprotocol.affiliates.Query/AffiliateWhitelist',
requestData,
);

return AffiliateModule.AffiliateWhitelistResponse.decode(data);
}

private async sendQuery(requestUrl: string, requestData: Uint8Array): Promise<Uint8Array> {
// eslint-disable-next-line max-len
const resp: QueryAbciResponse = await this.stargateQueryClient.queryAbci(
Expand Down
Loading

0 comments on commit 590aa12

Please sign in to comment.