Skip to content

Commit

Permalink
Allow clients to set a default memo to for transactions (#116)
Browse files Browse the repository at this point in the history
* Allow client to set a default memo

* version bump
  • Loading branch information
rosepuppy authored Feb 2, 2024
1 parent 82f2b61 commit a1ccc25
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 51 deletions.
67 changes: 36 additions & 31 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/examples/noble_example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function test(): Promise<void> {
NOBLE_BECH32_PREFIX,
);

const client = new NobleClient('https://rpc.testnet.noble.strange.love');
const client = new NobleClient('https://rpc.testnet.noble.strange.love', 'Noble example');
await client.connect(nobleWallet);

if (nobleWallet.address === undefined || dydxWallet.address === undefined) {
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": "1.0.18",
"version": "1.0.19",
"description": "General client library for the new dYdX system (v4 decentralized)",
"main": "build/src/index.js",
"scripts": {
Expand Down
27 changes: 22 additions & 5 deletions v4-client-js/src/clients/composite-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export class CompositeClient {
goodTilBlock: number,
timeInForce: Order_TimeInForce,
reduceOnly: boolean,
memo?: string,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
const msg = this.placeShortTermOrderMessage(
Expand All @@ -304,7 +305,7 @@ export class CompositeClient {
() => msgs,
true,
undefined,
undefined,
memo,
() => account,
);
}
Expand Down Expand Up @@ -356,6 +357,7 @@ export class CompositeClient {
triggerPrice?: number,
marketInfo?: MarketInfo,
currentHeight?: number,
memo?: string,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
const msg = this.placeOrderMessage(
Expand Down Expand Up @@ -390,7 +392,7 @@ export class CompositeClient {
() => msgs,
true,
undefined,
undefined,
memo,
() => account,
);
}
Expand Down Expand Up @@ -704,6 +706,7 @@ export class CompositeClient {
recipientAddress: string,
recipientSubaccountNumber: number,
amount: string,
memo?: string,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
const msg = this.transferToSubaccountMessage(
Expand All @@ -717,7 +720,10 @@ export class CompositeClient {
return this.send(
subaccount.wallet,
() => msgs,
true);
true,
undefined,
memo,
);
}

/**
Expand Down Expand Up @@ -774,6 +780,7 @@ export class CompositeClient {
async depositToSubaccount(
subaccount: SubaccountInfo,
amount: string,
memo?: string,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
const msg = this.depositToSubaccountMessage(
Expand All @@ -784,7 +791,10 @@ export class CompositeClient {
});
return this.validatorClient.post.send(subaccount.wallet,
() => msgs,
false);
false,
undefined,
memo,
);
}

/**
Expand Down Expand Up @@ -836,6 +846,7 @@ export class CompositeClient {
subaccount: SubaccountInfo,
amount: string,
recipient?: string,
memo?: string,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
const msg = this.withdrawFromSubaccountMessage(
Expand All @@ -848,7 +859,10 @@ export class CompositeClient {
return this.send(
subaccount.wallet,
() => msgs,
false);
false,
undefined,
memo,
);
}

/**
Expand Down Expand Up @@ -1019,6 +1033,7 @@ export class CompositeClient {
title: string,
summary: string,
initialDepositAmount: number,
memo?: string,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msg: Promise<EncodeObject[]> = new Promise((resolve) => {
const composer = this.validatorClient.post.composer;
Expand Down Expand Up @@ -1093,6 +1108,8 @@ export class CompositeClient {
wallet,
() => msg,
false,
undefined,
memo,
);
}
}
7 changes: 5 additions & 2 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,21 @@ export class ValidatorConfig {
public chainId: string;
public denoms: DenomConfig;
public broadcastOptions?: BroadcastOptions;
public defaultClientMemo?: string;

constructor(
restEndpoint: string,
chainId: string,
denoms: DenomConfig,
broadcastOptions?: BroadcastOptions,
defaultClientMemo?: string,
) {
this.restEndpoint = restEndpoint?.endsWith('/') ? restEndpoint.slice(0, -1) : restEndpoint;
this.chainId = chainId;

this.denoms = denoms;
this.broadcastOptions = broadcastOptions;
this.defaultClientMemo = defaultClientMemo;
}
}

Expand All @@ -214,7 +217,7 @@ export class Network {
USDC_GAS_DENOM: 'uusdc',
USDC_DECIMALS: 6,
CHAINTOKEN_DECIMALS: 18,
});
}, undefined, 'Client Example');
return new Network('testnet', indexerConfig, validatorConfig);
}

Expand All @@ -230,7 +233,7 @@ export class Network {
USDC_GAS_DENOM: 'uusdc',
USDC_DECIMALS: 6,
CHAINTOKEN_DECIMALS: 18,
});
}, undefined, 'Client Example');
return new Network('local', indexerConfig, validatorConfig);
}

Expand Down
5 changes: 4 additions & 1 deletion v4-client-js/src/clients/modules/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class Post {
private readonly chainId: string;
public readonly get: Get;
public readonly denoms: DenomConfig;
public readonly defaultClientMemo?: string;

public readonly defaultGasPrice: GasPrice;
public readonly defaultDydxGasPrice: GasPrice;
Expand All @@ -64,12 +65,14 @@ export class Post {
get: Get,
chainId: string,
denoms: DenomConfig,
defaultClientMemo?: string,
) {
this.get = get;
this.chainId = chainId;
this.registry = generateRegistry();
this.composer = new Composer();
this.denoms = denoms;
this.defaultClientMemo = defaultClientMemo;
this.defaultGasPrice = GasPrice
.fromString(`0.025${denoms.USDC_GAS_DENOM !== undefined ? denoms.USDC_GAS_DENOM : denoms.USDC_DENOM}`);
this.defaultDydxGasPrice = GasPrice
Expand Down Expand Up @@ -156,7 +159,7 @@ export class Post {
msgs,
zeroFee,
gasPrice,
memo,
memo ?? this.defaultClientMemo,
broadcastMode ?? this.defaultBroadcastMode(msgs),
);
}
Expand Down
17 changes: 13 additions & 4 deletions v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ declare global {
var nobleWallet: LocalWallet | undefined;
}

const DEFAULT_NATIVE_MEMO = 'dYdX Frontend (native)';

export async function connectClient(
network: Network,
): Promise<string> {
Expand Down Expand Up @@ -81,15 +83,22 @@ export async function connectNetwork(
throw new UserError('Missing required token params');
}

const indexerConfig = new IndexerConfig(indexerUrl, websocketUrl);
const validatorConfig = new ValidatorConfig(validatorUrl, chainId, {
const denomConfig = {
USDC_DENOM,
USDC_DECIMALS,
USDC_GAS_DENOM,
CHAINTOKEN_DENOM,
CHAINTOKEN_DECIMALS,
CHAINTOKEN_GAS_DENOM,
});
};
const indexerConfig = new IndexerConfig(indexerUrl, websocketUrl);
const validatorConfig = new ValidatorConfig(
validatorUrl,
chainId,
denomConfig,
undefined,
DEFAULT_NATIVE_MEMO,
);
const config = new Network('native', indexerConfig, validatorConfig);
globalThis.client = await CompositeClient.connect(config);
if (faucetUrl !== undefined) {
Expand All @@ -99,7 +108,7 @@ export async function connectNetwork(
}

try {
globalThis.nobleClient = new NobleClient(nobleValidatorUrl);
globalThis.nobleClient = new NobleClient(nobleValidatorUrl, DEFAULT_NATIVE_MEMO);
if (globalThis.nobleWallet) {
await globalThis.nobleClient.connect(globalThis.nobleWallet);
}
Expand Down
8 changes: 5 additions & 3 deletions v4-client-js/src/clients/noble-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export class NobleClient {
private wallet?: LocalWallet;
private restEndpoint: string;
private stargateClient?: SigningStargateClient;
private defaultClientMemo?: string;

constructor(restEndpoint: string) {
constructor(restEndpoint: string, defaultClientMemo?: string) {
this.restEndpoint = restEndpoint;
this.defaultClientMemo = defaultClientMemo;
}

get isConnected(): boolean {
Expand Down Expand Up @@ -74,14 +76,14 @@ export class NobleClient {
throw new Error('NobleClient wallet not initialized');
}
// Simulate to get the gas estimate
const fee = await this.simulateTransaction(messages, gasPrice, memo);
const fee = await this.simulateTransaction(messages, gasPrice, memo ?? this.defaultClientMemo);

// Sign and broadcast the transaction
return this.stargateClient.signAndBroadcast(
this.wallet.address,
messages,
fee,
memo ?? '',
memo ?? this.defaultClientMemo,
);
}

Expand Down
7 changes: 6 additions & 1 deletion v4-client-js/src/clients/validator-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export class ValidatorClient {
setupTxExtension,
);
this._get = new Get(tendermintClient, queryClient);
this._post = new Post(this._get!, this.config.chainId, this.config.denoms);
this._post = new Post(
this._get!,
this.config.chainId,
this.config.denoms,
this.config.defaultClientMemo,
);
}
}

0 comments on commit a1ccc25

Please sign in to comment.