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

Allow Denoms to be configurable #56

Merged
merged 6 commits into from
Oct 16, 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
126 changes: 75 additions & 51 deletions v4-client-js/__native__/__ios__/v4-native-client.js

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions v4-client-js/examples/transfer_example_deposit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Long from 'long';

import { BECH32_PREFIX } from '../src';
import { STAGING_CHAIN_ID, Network, ValidatorConfig } from '../src/clients/constants';
import { Network } from '../src/clients/constants';
import LocalWallet from '../src/clients/modules/local-wallet';
import { Subaccount } from '../src/clients/subaccount';
import { ValidatorClient } from '../src/clients/validator-client';
Expand All @@ -13,11 +13,7 @@ async function test(): Promise<void> {
const wallet = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX);
console.log(wallet);

const config = new ValidatorConfig(
Network.staging().validatorConfig.restEndpoint,
STAGING_CHAIN_ID,
);
const client = await ValidatorClient.connect(config);
const client = await ValidatorClient.connect(Network.staging().validatorConfig);
console.log('**Client**');
console.log(client);

Expand Down
20 changes: 5 additions & 15 deletions v4-client-js/examples/transfer_example_send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import Long from 'long';

import { TEST_RECIPIENT_ADDRESS } from '../__tests__/helpers/constants';
import { BECH32_PREFIX } from '../src';
import {
STAGING_CHAIN_ID,
Network,
ValidatorConfig,
DYDX_DENOM,
GAS_PRICE_DYDX_DENOM,
} from '../src/clients/constants';
import { Network } from '../src/clients/constants';
import LocalWallet from '../src/clients/modules/local-wallet';
import { Subaccount } from '../src/clients/subaccount';
import { ValidatorClient } from '../src/clients/validator-client';
Expand All @@ -25,11 +19,7 @@ async function test(): Promise<void> {
);
console.log(wallet);

const config = new ValidatorConfig(
Network.staging().validatorConfig.restEndpoint,
STAGING_CHAIN_ID,
);
const client = await ValidatorClient.connect(config);
const client = await ValidatorClient.connect(Network.staging().validatorConfig);
console.log('**Client**');
console.log(client);

Expand All @@ -41,7 +31,7 @@ async function test(): Promise<void> {
const msg = client.post.composer.composeMsgSendToken(
subaccount.address,
TEST_RECIPIENT_ADDRESS,
DYDX_DENOM,
client.config.denoms.CHAINTOKEN_DENOM,
amount,
);

Expand All @@ -51,7 +41,7 @@ async function test(): Promise<void> {
const totalFee = await client.post.simulate(
subaccount.wallet,
() => msgs,
GAS_PRICE_DYDX_DENOM,
undefined,
undefined,
);
console.log('**Total Fee**');
Expand All @@ -64,7 +54,7 @@ async function test(): Promise<void> {
const tx = await client.post.sendToken(
subaccount,
TEST_RECIPIENT_ADDRESS,
DYDX_DENOM,
client.config.denoms.CHAINTOKEN_DENOM,
amountAfterFee,
false,
Method.BroadcastTxCommit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Long from 'long';

import { BECH32_PREFIX } from '../src';
import { STAGING_CHAIN_ID, Network, ValidatorConfig } from '../src/clients/constants';
import { Network } from '../src/clients/constants';
import LocalWallet from '../src/clients/modules/local-wallet';
import { Subaccount } from '../src/clients/subaccount';
import { ValidatorClient } from '../src/clients/validator-client';
Expand All @@ -13,11 +13,7 @@ async function test(): Promise<void> {
const wallet = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX);
console.log(wallet);

const config = new ValidatorConfig(
Network.staging().validatorConfig.restEndpoint,
STAGING_CHAIN_ID,
);
const client = await ValidatorClient.connect(config);
const client = await ValidatorClient.connect(Network.staging().validatorConfig);
console.log('**Client**');
console.log(client);

Expand Down
8 changes: 2 additions & 6 deletions v4-client-js/examples/transfer_example_withdraw.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Long from 'long';

import { BECH32_PREFIX } from '../src';
import { STAGING_CHAIN_ID, Network, ValidatorConfig } from '../src/clients/constants';
import { Network } from '../src/clients/constants';
import LocalWallet from '../src/clients/modules/local-wallet';
import { Subaccount } from '../src/clients/subaccount';
import { ValidatorClient } from '../src/clients/validator-client';
Expand All @@ -13,11 +13,7 @@ async function test(): Promise<void> {
const wallet = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX);
console.log(wallet);

const config = new ValidatorConfig(
Network.staging().validatorConfig.restEndpoint,
STAGING_CHAIN_ID,
);
const client = await ValidatorClient.connect(config);
const client = await ValidatorClient.connect(Network.staging().validatorConfig);
console.log('**Client**');
console.log(client);

Expand Down
8 changes: 2 additions & 6 deletions v4-client-js/examples/transfer_example_withdraw_other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Long from 'long';

import { TEST_RECIPIENT_ADDRESS } from '../__tests__/helpers/constants';
import { BECH32_PREFIX } from '../src';
import { STAGING_CHAIN_ID, Network, ValidatorConfig } from '../src/clients/constants';
import { Network } from '../src/clients/constants';
import LocalWallet from '../src/clients/modules/local-wallet';
import { Subaccount } from '../src/clients/subaccount';
import { ValidatorClient } from '../src/clients/validator-client';
Expand All @@ -16,11 +16,7 @@ async function test(): Promise<void> {
const wallet = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX);
console.log(wallet);

const config = new ValidatorConfig(
Network.staging().validatorConfig.restEndpoint,
STAGING_CHAIN_ID,
);
const client = await ValidatorClient.connect(config);
const client = await ValidatorClient.connect(Network.staging().validatorConfig);
console.log('**Client**');
console.log(client);

Expand Down
9 changes: 3 additions & 6 deletions v4-client-js/examples/validator_post_example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Long from 'long';
import protobuf from 'protobufjs';

import { BECH32_PREFIX } from '../src';
import { STAGING_CHAIN_ID, Network, ValidatorConfig } from '../src/clients/constants';
import { Network } from '../src/clients/constants';
import LocalWallet from '../src/clients/modules/local-wallet';
import { Subaccount } from '../src/clients/subaccount';
import { IPlaceOrder } from '../src/clients/types';
Expand Down Expand Up @@ -32,11 +32,8 @@ async function sleep(ms: number): Promise<void> {
async function test(): Promise<void> {
const wallet = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX);
console.log(wallet);
const config = new ValidatorConfig(
Network.staging().validatorConfig.restEndpoint,
STAGING_CHAIN_ID,
);
const client = await ValidatorClient.connect(config);

const client = await ValidatorClient.connect(Network.staging().validatorConfig);
console.log('**Client**');
console.log(client);

Expand Down
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": "0.38.6",
"version": "0.39.0",
"description": "General client library for the new dYdX system (v4 decentralized)",
"main": "build/src/index.js",
"scripts": {
Expand Down
16 changes: 9 additions & 7 deletions v4-client-js/src/clients/composite-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import protobuf from 'protobufjs';
import { isStatefulOrder, verifyOrderFlags } from '../lib/validation';
import { OrderFlags } from '../types';
import {
DYDX_DENOM,
GAS_PRICE,
Network,
OrderExecution,
OrderSide,
Expand Down Expand Up @@ -103,7 +101,7 @@ export class CompositeClient {
wallet: LocalWallet,
messaging: () => Promise<EncodeObject[]>,
zeroFee: boolean,
gasPrice: GasPrice = GAS_PRICE,
gasPrice?: GasPrice,
memo?: string,
account?: () => Promise<Account>,
): Promise<Uint8Array> {
Expand All @@ -129,7 +127,7 @@ export class CompositeClient {
wallet: LocalWallet,
messaging: () => Promise<EncodeObject[]>,
zeroFee: boolean,
gasPrice: GasPrice = GAS_PRICE,
gasPrice?: GasPrice,
memo?: string,
account?: () => Promise<Account>,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
Expand Down Expand Up @@ -175,7 +173,7 @@ export class CompositeClient {
async simulate(
wallet: LocalWallet,
messaging: () => Promise<EncodeObject[]>,
gasPrice: GasPrice = GAS_PRICE,
gasPrice?: GasPrice,
memo?: string,
account?: () => Promise<Account>,
): Promise<StdFee> {
Expand Down Expand Up @@ -825,7 +823,7 @@ export class CompositeClient {
}

/**
* @description Create message to send dydx token from subaccount to wallet
* @description Create message to send chain token from subaccount to wallet
* with human readable input.
*
* @param subaccount The subaccount to withdraw from
Expand All @@ -841,11 +839,15 @@ export class CompositeClient {
amount: number,
recipient: string,
): EncodeObject {
const chainTokenDenom = this._validatorClient?.config.denoms.CHAINTOKEN_DENOM;
if (chainTokenDenom === undefined) {
throw new Error('Chain token denom not set in validator config');
}
const quantums: Long = Long.fromNumber(amount * (10 ** 6));
return this.validatorClient.post.composer.composeMsgSendToken(
subaccount.address,
recipient,
DYDX_DENOM,
chainTokenDenom,
quantums,
);
}
Expand Down
26 changes: 22 additions & 4 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PageRequest } from '@dydxprotocol/v4-proto/src/codegen/cosmos/base/query/v1beta1/pagination';
import Long from 'long';

import { BroadcastOptions } from './types';
import { BroadcastOptions, DenomConfig } from './types';

export * from '../lib/constants';

Expand Down Expand Up @@ -150,18 +150,21 @@ export class IndexerConfig {
export class ValidatorConfig {
public restEndpoint: string;
public chainId: string;
public denoms: DenomConfig;
public broadcastOptions?: BroadcastOptions;

constructor(
restEndpoint: string,
chainId: string,
denoms: DenomConfig,
broadcastOptions?: BroadcastOptions,
) {
if ((restEndpoint?.endsWith('/'))) {
this.restEndpoint = restEndpoint.slice(0, -1);
}
this.restEndpoint = restEndpoint;
this.chainId = chainId;
this.denoms = denoms;
this.broadcastOptions = broadcastOptions;
}
}
Expand All @@ -178,7 +181,12 @@ export class Network {
IndexerApiHost.DEV,
IndexerWSHost.DEV,
);
const validatorConfig = new ValidatorConfig(ValidatorApiHost.DEV, DEV_CHAIN_ID);
const validatorConfig = new ValidatorConfig(ValidatorApiHost.DEV, DEV_CHAIN_ID,
{
CHAINTOKEN_DENOM: 'adv4tnt',
USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
USDC_GAS_DENOM: 'uusdc',
});
return new Network('dev', indexerConfig, validatorConfig);
}

Expand All @@ -187,7 +195,12 @@ export class Network {
IndexerApiHost.STAGING,
IndexerWSHost.STAGING,
);
const validatorConfig = new ValidatorConfig(ValidatorApiHost.STAGING, STAGING_CHAIN_ID);
const validatorConfig = new ValidatorConfig(ValidatorApiHost.STAGING, STAGING_CHAIN_ID,
{
CHAINTOKEN_DENOM: 'adv4tnt',
USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
USDC_GAS_DENOM: 'uusdc',
});
return new Network('staging', indexerConfig, validatorConfig);
}

Expand All @@ -196,7 +209,12 @@ export class Network {
IndexerApiHost.TESTNET,
IndexerWSHost.TESTNET,
);
const validatorConfig = new ValidatorConfig(ValidatorApiHost.TESTNET, TESTNET_CHAIN_ID);
const validatorConfig = new ValidatorConfig(ValidatorApiHost.TESTNET, TESTNET_CHAIN_ID,
{
CHAINTOKEN_DENOM: 'adv4tnt',
USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
USDC_GAS_DENOM: 'uusdc',
});
return new Network('testnet', indexerConfig, validatorConfig);
}

Expand Down
5 changes: 0 additions & 5 deletions v4-client-js/src/clients/modules/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Coin } from 'cosmjs-types/cosmos/base/v1beta1/coin';
import Long from 'long';
import protobuf from 'protobufjs';

import { USDC_DENOM, DYDX_DENOM } from '../../lib/constants';
import {
OrderId,
Order,
Expand Down Expand Up @@ -200,10 +199,6 @@ export class Composer {
coinDenom: string,
quantums: Long,
): EncodeObject {
if (coinDenom !== USDC_DENOM && coinDenom !== DYDX_DENOM) {
throw new Error('Unsupported coinDenom');
}

const coin: Coin = {
denom: coinDenom,
amount: quantums.toString(),
Expand Down
Loading