-
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.
- Loading branch information
1 parent
61d5f93
commit a8e785c
Showing
2 changed files
with
75 additions
and
2 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 |
---|---|---|
@@ -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); | ||
}); |
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