Skip to content

Commit

Permalink
short term cancel example
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfung-dydx authored and lucas-dydx committed Sep 28, 2023
1 parent 61d5f93 commit a8e785c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
73 changes: 73 additions & 0 deletions v4-client-js/examples/short_term_cancel_example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { BECH32_PREFIX, OrderFlags, Order_TimeInForce } from '../src';
import { CompositeClient } from '../src/clients/composite-client';
import {
Network, OrderSide,
} from '../src/clients/constants';
import LocalWallet from '../src/clients/modules/local-wallet';
import { Subaccount } from '../src/clients/subaccount';
import { randomInt } from '../src/lib/utils';
import { DYDX_TEST_MNEMONIC, MAX_CLIENT_ID } from './constants';

async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async function test(): Promise<void> {
const wallet = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX);
console.log(wallet);
const network = Network.staging();
const client = await CompositeClient.connect(network);
console.log('**Client**');
console.log(client);
const subaccount = new Subaccount(wallet, 0);

const currentBlock = await client.validatorClient.get.latestBlockHeight();
const nextValidBlockHeight = currentBlock + 1;
// Note, you can change this to any number between `next_valid_block_height`
// to `next_valid_block_height + SHORT_BLOCK_WINDOW`
const goodTilBlock = nextValidBlockHeight + 10;
const shortTermOrderClientId = randomInt(MAX_CLIENT_ID);
try {
// place a short term order
const tx = await client.placeShortTermOrder(
subaccount,
'ETH-USD',
OrderSide.SELL,
40000,
0.01,
shortTermOrderClientId,
goodTilBlock,
Order_TimeInForce.TIME_IN_FORCE_UNSPECIFIED,
false,
);
console.log('**Short Term Order Tx**');
console.log(tx.hash);
} catch (error) {
console.log('**Short Term Order Failed**');
console.log(error.message);
}

await sleep(5000); // wait for placeOrder to complete

try {
// cancel the short term order
const tx = await client.cancelOrder(
subaccount,
shortTermOrderClientId,
OrderFlags.SHORT_TERM,
'ETH-USD',
goodTilBlock + 10,
0,
);
console.log('**Cancel Short Term Order Tx**');
console.log(tx);
} catch (error) {
console.log('**Cancel Short Term Order Failed**');
console.log(error.message);
}
}

test().then(() => {
}).catch((error) => {
console.log(error.message);
});
4 changes: 2 additions & 2 deletions v4-client-js/src/clients/composite-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Order_ConditionType, Order_TimeInForce } from '@dydxprotocol/v4-proto/s
import Long from 'long';
import protobuf from 'protobufjs';

import { isStatefulOrder } from '../lib/validation';
import { OrderFlags } from '../types';
import {
DYDX_DENOM,
Expand All @@ -26,7 +27,6 @@ import { UserError } from './lib/errors';
import LocalWallet from './modules/local-wallet';
import { Subaccount } from './subaccount';
import { ValidatorClient } from './validator-client';
import { isStatefulOrder } from '../lib/validation';

// Required for encoding and decoding queries that are of type Long.
// Must be done once but since the individal modules should be usable
Expand Down Expand Up @@ -239,8 +239,8 @@ export class CompositeClient {
price,
size,
clientId,
timeInForce,
goodTilBlock,
timeInForce,
reduceOnly,
);
msg.then((it) => resolve([it])).catch((err) => {
Expand Down

0 comments on commit a8e785c

Please sign in to comment.