-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TS] [CLOB-895]
v0.38.2
: Add cancel order examples (#44)
- Loading branch information
1 parent
bfdd391
commit 6aca7bd
Showing
11 changed files
with
272 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { BECH32_PREFIX, OrderFlags } from '../src'; | ||
import { CompositeClient } from '../src/clients/composite-client'; | ||
import { | ||
Network, OrderExecution, OrderSide, OrderTimeInForce, OrderType, | ||
} 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); | ||
|
||
/* | ||
Note this example places a stateful order. | ||
Programmatic traders should generally not use stateful orders for following reasons: | ||
- Stateful orders received out of order by validators will fail sequence number validation | ||
and be dropped. | ||
- Stateful orders have worse time priority since they are only matched after they are included | ||
on the block. | ||
- Stateful order rate limits are more restrictive than Short-Term orders, specifically max 2 per | ||
block / 20 per 100 blocks. | ||
- Stateful orders can only be canceled after they’ve been included in a block. | ||
*/ | ||
const longTermOrderClientId = randomInt(MAX_CLIENT_ID); | ||
try { | ||
// place a long term order | ||
const tx = await client.placeOrder( | ||
subaccount, | ||
'ETH-USD', | ||
OrderType.LIMIT, | ||
OrderSide.SELL, | ||
40000, | ||
0.01, | ||
longTermOrderClientId, | ||
OrderTimeInForce.GTT, | ||
60, | ||
OrderExecution.DEFAULT, | ||
false, | ||
false, | ||
); | ||
console.log('**Long Term Order Tx**'); | ||
console.log(tx.hash); | ||
} catch (error) { | ||
console.log('**Long Term Order Failed**'); | ||
console.log(error.message); | ||
} | ||
|
||
await sleep(5000); // wait for placeOrder to complete | ||
|
||
try { | ||
// cancel the long term order | ||
const tx = await client.cancelOrder( | ||
subaccount, | ||
longTermOrderClientId, | ||
OrderFlags.LONG_TERM, | ||
'ETH-USD', | ||
0, | ||
120, | ||
); | ||
console.log('**Cancel Long Term Order Tx**'); | ||
console.log(tx); | ||
} catch (error) { | ||
console.log('**Cancel Long Term Order Failed**'); | ||
console.log(error.message); | ||
} | ||
} | ||
|
||
test().then(() => { | ||
}).catch((error) => { | ||
console.log(error.message); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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, sleep } from '../src/lib/utils'; | ||
import { DYDX_TEST_MNEMONIC, MAX_CLIENT_ID } from './constants'; | ||
|
||
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); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { BECH32_PREFIX } from '../src'; | ||
import { CompositeClient } from '../src/clients/composite-client'; | ||
import { | ||
Network, OrderExecution, OrderSide, OrderTimeInForce, OrderType, | ||
} 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'; | ||
import ordersParams from './human_readable_orders.json'; | ||
|
||
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); | ||
for (const orderParams of ordersParams) { | ||
try { | ||
const type = OrderType[orderParams.type as keyof typeof OrderType]; | ||
const side = OrderSide[orderParams.side as keyof typeof OrderSide]; | ||
const timeInForceString = orderParams.timeInForce ?? 'GTT'; | ||
const timeInForce = OrderTimeInForce[timeInForceString as keyof typeof OrderTimeInForce]; | ||
const price = orderParams.price ?? 1350; | ||
const timeInForceSeconds = (timeInForce === OrderTimeInForce.GTT) ? 60 : 0; | ||
const postOnly = orderParams.postOnly ?? false; | ||
const tx = await client.placeOrder( | ||
subaccount, | ||
'ETH-USD', | ||
type, | ||
side, | ||
price, | ||
0.01, | ||
randomInt(MAX_CLIENT_ID), | ||
timeInForce, | ||
timeInForceSeconds, | ||
OrderExecution.DEFAULT, | ||
postOnly, | ||
false, | ||
); | ||
console.log('**Order Tx**'); | ||
console.log(tx); | ||
} catch (error) { | ||
console.log(error.message); | ||
} | ||
|
||
await sleep(5000); // wait for placeOrder to complete | ||
} | ||
} | ||
|
||
test().then(() => { | ||
}).catch((error) => { | ||
console.log(error.message); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters