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: Add affiliates queries and transaction #259

Merged
merged 14 commits into from
Oct 1, 2024
895 changes: 868 additions & 27 deletions v4-client-js/__native__/__ios__/v4-native-client.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,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
6 changes: 6 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 Down Expand Up @@ -34,6 +35,7 @@ import {
TYPE_URL_BATCH_CANCEL,
TYPE_URL_MSG_DEPOSIT_TO_MEGAVAULT,
TYPE_URL_MSG_WITHDRAW_FROM_MEGAVAULT,
TYPE_URL_MSG_REGISTER_AFFILIATE,
} from '../constants';

export const registry: ReadonlyArray<[string, GeneratedType]> = [];
Expand All @@ -58,11 +60,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
15 changes: 15 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,6 +48,7 @@ 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,
} from '../constants';
Expand Down Expand Up @@ -536,6 +538,19 @@ 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,
};
}

// ------------ 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
20 changes: 20 additions & 0 deletions v4-client-js/src/clients/modules/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,4 +881,24 @@ export class Post {
): EncodeObject {
return this.composer.composeMsgWithdrawFromMegavault(...args);
}

async registerAffiliate(
subaccount: SubaccountInfo,
affiliate: string,
broadcastMode?: BroadcastMode,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msg = this.registerAffiliateMsg(subaccount.address, affiliate);
return this.send(
subaccount.wallet,
() => Promise.resolve([msg]),
false,
undefined,
undefined,
broadcastMode,
);
}

registerAffiliateMsg(...args: Parameters<Composer['composeMsgRegisterAffiliate']>): EncodeObject {
return this.composer.composeMsgRegisterAffiliate(...args);
}
}
1 change: 1 addition & 0 deletions v4-client-js/src/clients/modules/proto-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * as RewardsModule from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/
export * as StakingModule from '@dydxprotocol/v4-proto/src/codegen/cosmos/staking/v1beta1/query';
export * as BridgeModule from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/bridge/query';
export * as DistributionModule from '@dydxprotocol/v4-proto/src/codegen/cosmos/distribution/v1beta1/query';
export * as AffiliateModule from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/affiliates/query';
export * as VaultModule from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/vault/query';

export * from '@dydxprotocol/v4-proto/src/codegen/cosmos/base/abci/v1beta1/abci';
Expand Down
2 changes: 1 addition & 1 deletion v4-client-js/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const BROADCAST_TIMEOUT_MS: number = 8_000;
export const API_TIMEOUT_DEFAULT_MS: number = 5_000;

// Gas
export const GAS_MULTIPLIER: number = 1.4;
export const GAS_MULTIPLIER: number = 1.5;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to bump this mulitplier for the registerAffiliateMsg turns out 1.4 is insufficient.


export const ZERO_FEE: StdFee = {
amount: [],
Expand Down
Loading