-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
export const DYDX_LOCAL_ADDRESS = 'dydx199tqg4wdlnu4qjlxchpd7seg454937hjrknju4'; | ||
export const DYDX_LOCAL_MNEMONIC = 'merge panther lobster crazy road hollow amused security before critic about cliff exhibit cause coyote talent happy where lion river tobacco option coconut small'; | ||
export const DYDX_LOCAL_ADDRESS_2 = 'dydx10fx7sy6ywd5senxae9dwytf8jxek3t2gcen2vs'; | ||
export const DYDX_LOCAL_MNEMONIC_2 = 'color habit donor nurse dinosaur stable wonder process post perfect raven gold census inside worth inquiry mammal panic olive toss shadow strong name drum'; | ||
|
||
export const MNEMONIC_TO_ADDRESS: Record<string, string> = { | ||
[DYDX_LOCAL_MNEMONIC]: DYDX_LOCAL_ADDRESS, | ||
[DYDX_LOCAL_MNEMONIC_2]: DYDX_LOCAL_ADDRESS_2, | ||
}; | ||
|
||
export const ADDRESS_TO_MNEMONIC: Record<string, string> = { | ||
[DYDX_LOCAL_ADDRESS]: DYDX_LOCAL_MNEMONIC, | ||
[DYDX_LOCAL_ADDRESS_2]: DYDX_LOCAL_MNEMONIC_2, | ||
}; |
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,108 @@ | ||
import Long from 'long'; | ||
import { | ||
BECH32_PREFIX, | ||
IPlaceOrder, | ||
LocalWallet, | ||
Network, | ||
Order_Side, | ||
Order_TimeInForce, | ||
OrderFlags, | ||
SubaccountInfo, | ||
ValidatorClient, | ||
} from '@dydxprotocol/v4-client-js'; | ||
import { DYDX_LOCAL_MNEMONIC, DYDX_LOCAL_MNEMONIC_2 } from './helpers/constants'; | ||
|
||
const PERPETUAL_PAIR_BTC_USD: number = 0; | ||
const PERPETUAL_PAIR_ETH_USD: number = 1; | ||
const quantums: Long = new Long(1_000_000_000); | ||
const subticks: Long = new Long(1_000_000_000); | ||
|
||
const defaultOrder: IPlaceOrder = { | ||
clientId: 0, | ||
orderFlags: OrderFlags.SHORT_TERM, | ||
clobPairId: PERPETUAL_PAIR_BTC_USD, | ||
side: Order_Side.SIDE_BUY, | ||
quantums, | ||
subticks, | ||
timeInForce: Order_TimeInForce.TIME_IN_FORCE_UNSPECIFIED, | ||
reduceOnly: false, | ||
clientMetadata: 0, | ||
}; | ||
|
||
type OrderDetails = { | ||
mnemonic: string; | ||
timeInForce: number; | ||
orderFlags: number; | ||
side: number; | ||
clobPairId: number; | ||
quantums: number; | ||
subticks: number; | ||
}; | ||
|
||
const orderDetails: OrderDetails[] = [ | ||
{ | ||
mnemonic: DYDX_LOCAL_MNEMONIC, | ||
timeInForce: 2, | ||
orderFlags: 64, | ||
side: 1, | ||
clobPairId: PERPETUAL_PAIR_BTC_USD, | ||
quantums: 10000000, | ||
subticks: 40000000000, | ||
}, | ||
{ | ||
mnemonic: DYDX_LOCAL_MNEMONIC_2, | ||
timeInForce: 2, | ||
orderFlags: 64, | ||
side: 1, | ||
clobPairId: PERPETUAL_PAIR_ETH_USD, | ||
quantums: 10000000, | ||
subticks: 40000000000, | ||
}, | ||
]; | ||
|
||
async function placeOrder( | ||
mnemonic: string, | ||
order: IPlaceOrder, | ||
): Promise<void> { | ||
const wallet = await LocalWallet.fromMnemonic(mnemonic, BECH32_PREFIX); | ||
const client = await ValidatorClient.connect(Network.testnet().validatorConfig); | ||
|
||
const subaccount = new SubaccountInfo(wallet, 0); | ||
const modifiedOrder: IPlaceOrder = order; | ||
if (order.orderFlags !== 0) { | ||
modifiedOrder.goodTilBlock = 0; | ||
const now = new Date(); | ||
const millisecondsPerSecond = 1000; | ||
const interval = 60 * millisecondsPerSecond; | ||
const future = new Date(now.valueOf() + interval); | ||
modifiedOrder.goodTilBlockTime = Math.round(future.getTime() / 1000); | ||
} else { | ||
modifiedOrder.goodTilBlockTime = 0; | ||
} | ||
|
||
await client.post.placeOrderObject( | ||
subaccount, | ||
modifiedOrder, | ||
); | ||
} | ||
|
||
describe('orders', () => { | ||
it('test orders', async () => { | ||
// place all orders | ||
for (const order of orderDetails) { | ||
const modifiedOrder: IPlaceOrder = defaultOrder; | ||
modifiedOrder.clientId = Math.floor(Math.random() * 1000000000); | ||
modifiedOrder.goodTilBlock = 0; | ||
modifiedOrder.clobPairId = order.clobPairId; | ||
modifiedOrder.timeInForce = order.timeInForce; | ||
modifiedOrder.reduceOnly = false; // reduceOnly is currently disabled | ||
modifiedOrder.orderFlags = order.orderFlags; | ||
modifiedOrder.side = order.side; | ||
modifiedOrder.quantums = Long.fromNumber(order.quantums); | ||
modifiedOrder.subticks = Long.fromNumber(order.subticks); | ||
|
||
await placeOrder(order.mnemonic, modifiedOrder); | ||
} | ||
|
||
}); | ||
}); |