@@ -929,11 +929,11 @@ Description: Deposit funds into your dYdX subaccount
929929
930930### Cancelling an Order
931931
932- Examples on how to deposit funds into a subaccount.
932+ Examples on how to cancel an order
933933
934- ** Deposit Example: ` examples/transfer_example_deposit ` **
934+ ** Cancel Example: ` examples/short_term_order_cancel_examples.ts ` ( https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-js/examples/short_term_order_cancel_example.ts ) **
935935
936- > Deposit
936+ > Order cancel
937937
938938``` typescript
939939/*
@@ -958,10 +958,141 @@ const tx = await client.post.cancelOrder(
958958```
959959
960960``` python
961+ import asyncio
962+ import random
963+ import time
964+
965+ from v4_proto.dydxprotocol.clob.order_pb2 import Order
966+
967+ from dydx_v4_client import MAX_CLIENT_ID , OrderFlags
968+ from dydx_v4_client.indexer.rest.constants import OrderType
969+ from dydx_v4_client.indexer.rest.indexer_client import IndexerClient
970+ from dydx_v4_client.network import TESTNET
971+ from dydx_v4_client.node.client import NodeClient
972+ from dydx_v4_client.node.market import Market
973+ from dydx_v4_client.wallet import Wallet
974+ from tests.conftest import DYDX_TEST_MNEMONIC , TEST_ADDRESS
975+
976+ MARKET_ID = " ETH-USD"
977+
978+
979+ async def test ():
980+
981+ node = await NodeClient.connect(TESTNET .node)
982+ indexer = IndexerClient(TESTNET .rest_indexer)
983+
984+ market = Market(
985+ (await indexer.markets.get_perpetual_markets(MARKET_ID ))[" markets" ][MARKET_ID ]
986+ )
987+ wallet = await Wallet.from_mnemonic(node, DYDX_TEST_MNEMONIC , TEST_ADDRESS )
988+
989+ current_block = await node.latest_block_height()
990+ good_til_block = current_block + 1 + 10
991+
992+ order_id = market.order_id(
993+ TEST_ADDRESS , 0 , random.randint(0 , MAX_CLIENT_ID ), OrderFlags.SHORT_TERM
994+ )
995+
996+ place = await node.place_order(
997+ wallet,
998+ market.order(
999+ order_id,
1000+ OrderType.LIMIT ,
1001+ Order.Side.SIDE_SELL ,
1002+ size = 0.01 ,
1003+ price = 40000 ,
1004+ time_in_force = Order.TimeInForce.TIME_IN_FORCE_UNSPECIFIED ,
1005+ reduce_only = False ,
1006+ good_til_block = good_til_block,
1007+ ),
1008+ )
1009+ print (place)
1010+ # FIXME (piwonskp): Remove
1011+ wallet.sequence += 1
1012+ time.sleep(5 )
1013+
1014+ cancel = await node.cancel_order(
1015+ wallet, order_id, good_til_block = good_til_block + 10
1016+ )
1017+ print (cancel)
9611018
1019+
1020+ asyncio.run(test())
9621021```
9631022
964- See reference implementations: [[ Python]] ( )
1023+ ``` rust
1024+ mod support ;
1025+ use anyhow :: {Error , Result };
1026+ use dydx :: config :: ClientConfig ;
1027+ use dydx :: indexer :: {AnyId , IndexerClient , Ticker };
1028+ use dydx :: node :: {NodeClient , OrderBuilder , OrderSide , Wallet };
1029+ use dydx_proto :: dydxprotocol :: clob :: order :: TimeInForce ;
1030+ use support :: constants :: TEST_MNEMONIC ;
1031+ use tokio :: time :: {sleep, Duration };
1032+
1033+ const ETH_USD_TICKER : & str = " ETH-USD" ;
1034+
1035+ pub struct OrderPlacer {
1036+ client : NodeClient ,
1037+ indexer : IndexerClient ,
1038+ wallet : Wallet ,
1039+ }
1040+
1041+ impl OrderPlacer {
1042+ pub async fn connect () -> Result <Self > {
1043+ let config = ClientConfig :: from_file (" client/tests/testnet.toml" ). await ? ;
1044+ let client = NodeClient :: connect (config . node). await ? ;
1045+ let indexer = IndexerClient :: new (config . indexer);
1046+ let wallet = Wallet :: from_mnemonic (TEST_MNEMONIC )? ;
1047+ Ok (Self {
1048+ client ,
1049+ indexer ,
1050+ wallet ,
1051+ })
1052+ }
1053+ }
1054+
1055+ #[tokio:: main]
1056+ async fn main () -> Result <()> {
1057+ tracing_subscriber :: fmt (). try_init (). map_err (Error :: msg )? ;
1058+ #[cfg(feature = " telemetry" )]
1059+ support :: telemetry :: metrics_dashboard (). await ? ;
1060+ let mut placer = OrderPlacer :: connect (). await ? ;
1061+ let mut account = placer . wallet. account (0 , & mut placer . client). await ? ;
1062+ let subaccount = account . subaccount (0 )? ;
1063+
1064+ let ticker = Ticker (ETH_USD_TICKER . into ());
1065+ let market = placer
1066+ . indexer
1067+ . markets ()
1068+ . get_perpetual_market (& ticker )
1069+ . await ? ;
1070+
1071+ let current_block_height = placer . client. get_latest_block_height (). await ? ;
1072+ let good_until = current_block_height . ahead (10 );
1073+
1074+ let (order_id , order ) = OrderBuilder :: new (market , subaccount )
1075+ . limit (OrderSide :: Buy , 100 , 3 )
1076+ . reduce_only (false )
1077+ . time_in_force (TimeInForce :: Unspecified )
1078+ . until (good_until . clone ())
1079+ . build (AnyId )? ;
1080+
1081+ let place_tx_hash = placer . client. place_order (& mut account , order ). await ? ;
1082+ tracing :: info! (" Place order transaction hash: {:?}" , place_tx_hash );
1083+
1084+ sleep (Duration :: from_secs (5 )). await ;
1085+
1086+ // Cancel order
1087+ let cancel_tx_hash = placer
1088+ . client
1089+ . cancel_order (& mut account , order_id , good_until )
1090+ . await ? ;
1091+ tracing :: info! (" Cancel order transaction hash: {:?}" , cancel_tx_hash );
1092+
1093+ Ok (())
1094+ }
1095+ ```
9651096
9661097#### Request
9671098
@@ -971,12 +1102,15 @@ Programmatic users of the API must take care to store Mnemonics. dYdX does not s
9711102
9721103Description: Deposit funds into your dYdX subaccount
9731104
974- | Parameter | Type | Required? | Description |
975- | --------------- | -------------- | --------- | ------------------------------------ |
976- | ` subaccount ` | SubaccountInfo | yes | The subaccount to deposit to |
977- | ` assetId ` | number | yes | The asset ID of the asset to deposit |
978- | ` quantums ` | Long | yes | quantums to calculate size |
979- | ` broadcastMode ` | BroadcastMode | no | The broadcast mode |
1105+ | Parameter | Type | Required? | Description |
1106+ | ------------------- | --------------- | --------- | ------------------------------------- |
1107+ | ` subaccount ` | SubaccountInfo | yes | The subaccount to deposit to |
1108+ | ` clientId ` | number | yes | The asset ID of the asset to deposit |
1109+ | ` orderFlags ` | OrderFlags | yes | |
1110+ | ` clobPairId ` | number | yes | |
1111+ | ` goodTilBlock ` | number | no | |
1112+ | ` goodTilBlockTime ` | number | no | |
1113+ | ` broadcastMode ` | BroadcastMode | no | The broadcast mode |
9801114
9811115#### Response
9821116
0 commit comments