Skip to content

Commit

Permalink
use decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
rosepuppy committed Oct 17, 2023
1 parent 80bea76 commit 614bcfd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
38 changes: 32 additions & 6 deletions v4-client-js/src/clients/composite-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from '@cosmjs/stargate';
import { BroadcastTxAsyncResponse, BroadcastTxSyncResponse } from '@cosmjs/tendermint-rpc/build/tendermint37';
import { Order_ConditionType, Order_TimeInForce } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/order';
import { parseUnits } from 'ethers';
import Long from 'long';
import protobuf from 'protobufjs';

Expand Down Expand Up @@ -705,7 +706,13 @@ export class CompositeClient {
recipientSubaccountNumber: number,
amount: number,
): EncodeObject {
const quantums: Long = Long.fromNumber(amount * (10 ** 6));
const validatorClient = this._validatorClient;
if (validatorClient === undefined) {
throw new Error('validatorClient not set');
}
const quantums: Long = Long
.fromNumber(amount * (10 ** validatorClient.config.denoms.USDC_DECIMALS));

return this.validatorClient.post.composer.composeMsgTransfer(
subaccount.address,
subaccount.subaccountNumber,
Expand Down Expand Up @@ -756,7 +763,13 @@ export class CompositeClient {
subaccount: Subaccount,
amount: number,
): EncodeObject {
const quantums: Long = Long.fromNumber(amount * (10 ** 6));
const validatorClient = this._validatorClient;
if (validatorClient === undefined) {
throw new Error('validatorClient not set');
}
const quantums: Long = Long
.fromNumber(amount * (10 ** validatorClient.config.denoms.USDC_DECIMALS));

return this.validatorClient.post.composer.composeMsgDepositToSubaccount(
subaccount.address,
subaccount.subaccountNumber,
Expand Down Expand Up @@ -812,7 +825,13 @@ export class CompositeClient {
amount: number,
recipient?: string,
): EncodeObject {
const quantums: Long = Long.fromNumber(amount * (10 ** 6));
const validatorClient = this._validatorClient;
if (validatorClient === undefined) {
throw new Error('validatorClient not set');
}
const quantums: Long = Long
.fromNumber(amount * (10 ** validatorClient.config.denoms.USDC_DECIMALS));

return this.validatorClient.post.composer.composeMsgWithdrawFromSubaccount(
subaccount.address,
subaccount.subaccountNumber,
Expand All @@ -839,15 +858,22 @@ export class CompositeClient {
amount: string,
recipient: string,
): EncodeObject {
const chainTokenDenom = this._validatorClient?.config.denoms.CHAINTOKEN_DENOM;
if (chainTokenDenom === undefined) {
const {
CHAINTOKEN_DENOM: chainTokenDenom,
CHAINTOKEN_DECIMALS: chainTokenDecimals,
} = this._validatorClient?.config.denoms || {};

if (chainTokenDenom === undefined || chainTokenDecimals === undefined) {
throw new Error('Chain token denom not set in validator config');
}

const quantums = parseUnits(amount, chainTokenDecimals);

return this.validatorClient.post.composer.composeMsgSendToken(
subaccount.address,
recipient,
chainTokenDenom,
amount,
quantums.toString(),
);
}

Expand Down
13 changes: 9 additions & 4 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PageRequest } from '@dydxprotocol/v4-proto/src/codegen/cosmos/base/query/v1beta1/pagination';
import Long from 'long';

import { DEFAULT_CHAINTOKEN_DECIMALS, DEFAULT_USDC_DECIMALS } from '../lib/constants';

Check failure on line 4 in v4-client-js/src/clients/constants.ts

View workflow job for this annotation

GitHub Actions / CI

DEFAULT_CHAINTOKEN_DECIMALS not found in '../lib/constants'

Check failure on line 4 in v4-client-js/src/clients/constants.ts

View workflow job for this annotation

GitHub Actions / CI

'DEFAULT_CHAINTOKEN_DECIMALS' is defined but never used

Check failure on line 4 in v4-client-js/src/clients/constants.ts

View workflow job for this annotation

GitHub Actions / CI

DEFAULT_USDC_DECIMALS not found in '../lib/constants'

Check failure on line 4 in v4-client-js/src/clients/constants.ts

View workflow job for this annotation

GitHub Actions / CI

'DEFAULT_USDC_DECIMALS' is defined but never used
import { BroadcastOptions, DenomConfig } from './types';

export * from '../lib/constants';
Expand Down Expand Up @@ -159,11 +160,9 @@ export class ValidatorConfig {
denoms: DenomConfig,
broadcastOptions?: BroadcastOptions,
) {
if ((restEndpoint?.endsWith('/'))) {
this.restEndpoint = restEndpoint.slice(0, -1);
}
this.restEndpoint = restEndpoint;
this.restEndpoint = restEndpoint?.endsWith('/') ? restEndpoint.slice(0, -1) : restEndpoint;
this.chainId = chainId;

this.denoms = denoms;
this.broadcastOptions = broadcastOptions;
}
Expand All @@ -186,6 +185,8 @@ export class Network {
CHAINTOKEN_DENOM: 'adv4tnt',
USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
USDC_GAS_DENOM: 'uusdc',
USDC_DECIMALS: 6,
CHAINTOKEN_DECIMALS: 18,
});
return new Network('dev', indexerConfig, validatorConfig);
}
Expand All @@ -200,6 +201,8 @@ export class Network {
CHAINTOKEN_DENOM: 'adv4tnt',
USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
USDC_GAS_DENOM: 'uusdc',
USDC_DECIMALS: 6,
CHAINTOKEN_DECIMALS: 18,
});
return new Network('staging', indexerConfig, validatorConfig);
}
Expand All @@ -214,6 +217,8 @@ export class Network {
CHAINTOKEN_DENOM: 'adv4tnt',
USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
USDC_GAS_DENOM: 'uusdc',
USDC_DECIMALS: 6,
CHAINTOKEN_DECIMALS: 18,
});
return new Network('testnet', indexerConfig, validatorConfig);
}
Expand Down

0 comments on commit 614bcfd

Please sign in to comment.