|
| 1 | +import { BASE_PRECISION, BN, getMarketOrderParams, getOrderParams, MarketType, PositionDirection } from '@drift-labs/sdk'; |
| 2 | +import { |
| 3 | + DriftClient, |
| 4 | + Wallet, |
| 5 | + SignedMsgOrderParams, |
| 6 | + SignedMsgOrderParamsDelegateMessage, |
| 7 | + OrderParams, |
| 8 | + // SignedMsgExtensions, |
| 9 | +} from './src/'; |
| 10 | +import { |
| 11 | + Connection, |
| 12 | + Keypair, |
| 13 | + PublicKey, |
| 14 | +} from '@solana/web3.js'; |
| 15 | +import { nanoid } from 'nanoid'; |
| 16 | + |
| 17 | +async function main() { |
| 18 | + const keypair = Keypair.generate(); |
| 19 | + console.log(keypair.publicKey.toString()); |
| 20 | + const driftClient = new DriftClient({ |
| 21 | + wallet: new Wallet(keypair), |
| 22 | + connection: new Connection('https://api.mainnet-beta.solana.com'), |
| 23 | + }); |
| 24 | + const slot = new BN(2345); |
| 25 | + |
| 26 | + // test_deserialize_into_verified_message_delegate_with_max_margin_ratio |
| 27 | + // const e0 = driftClient.encodeSignedMsgOrderParamsMessage( |
| 28 | + // { |
| 29 | + // signedMsgOrderParams: getOrderParams(getMarketOrderParams( |
| 30 | + // { |
| 31 | + // marketIndex: 0, |
| 32 | + // userOrderId: 2, |
| 33 | + // price: new BN("237000000"), |
| 34 | + // marketType: MarketType.PERP, |
| 35 | + // baseAssetAmount: new BN("1000000000"), |
| 36 | + // direction: PositionDirection.SHORT, |
| 37 | + // reduceOnly: false, |
| 38 | + // auctionDuration: 10, |
| 39 | + // auctionStartPrice: new BN("240000000"), |
| 40 | + // auctionEndPrice: new BN("238000000"), |
| 41 | + // } |
| 42 | + // )), |
| 43 | + // takerPubkey: new PublicKey('HG2iQKnRkkasrLptwMZewV6wT7KPstw9wkA8yyu8Nx3m'), |
| 44 | + // slot, |
| 45 | + // uuid: Buffer.from([67, 82, 79, 51, 105, 114, 71, 49]), |
| 46 | + // takeProfitOrderParams: { |
| 47 | + // baseAssetAmount: new BN("1000000000"), |
| 48 | + // triggerPrice: new BN("230000000"), |
| 49 | + // }, |
| 50 | + // stopLossOrderParams: { |
| 51 | + // baseAssetAmount: new BN("1000000000"), |
| 52 | + // triggerPrice: new BN("250000000"), |
| 53 | + // }, |
| 54 | + // ext: SignedMsgExtensions.V1({ maxMarginRatio: 1 }), |
| 55 | + // }, |
| 56 | + // true, |
| 57 | + // ) |
| 58 | + // console.log(JSON.stringify(Array.from(e0))); |
| 59 | + |
| 60 | + // test_deserialize_into_verified_message_non_delegate_with_max_margin_ratio |
| 61 | + const delegateSigner = false; |
| 62 | + const e0 = driftClient.encodeSignedMsgOrderParamsMessage( |
| 63 | + { |
| 64 | + signedMsgOrderParams: getOrderParams(getMarketOrderParams( |
| 65 | + { |
| 66 | + marketIndex: 0, |
| 67 | + userOrderId: 3, |
| 68 | + price: new BN("237000000"), |
| 69 | + marketType: MarketType.PERP, |
| 70 | + baseAssetAmount: new BN("3456000000"), |
| 71 | + direction: PositionDirection.LONG, |
| 72 | + reduceOnly: false, |
| 73 | + auctionDuration: 10, |
| 74 | + auctionStartPrice: new BN("230000000"), |
| 75 | + auctionEndPrice: new BN("237000000"), |
| 76 | + } |
| 77 | + )), |
| 78 | + subAccountId: 2, |
| 79 | + slot, |
| 80 | + uuid: Buffer.from([67, 82, 79, 51, 105, 114, 71, 49]), |
| 81 | + takeProfitOrderParams: { |
| 82 | + baseAssetAmount: new BN("3456000000"), |
| 83 | + triggerPrice: new BN("240000000"), |
| 84 | + }, |
| 85 | + stopLossOrderParams: { |
| 86 | + baseAssetAmount: new BN("3456000000"), |
| 87 | + triggerPrice: new BN("225000000"), |
| 88 | + }, |
| 89 | + // ext: SignedMsgExtensions.V0, |
| 90 | + // ext: SignedMsgExtensions.V1({ maxMarginRatio: 65535 }), |
| 91 | + // ext: SignedMsgExtensions.V2({ maxMarginRatio: 65535, newField: 1, newField2: 2 }), |
| 92 | + }, |
| 93 | + delegateSigner, |
| 94 | + ); |
| 95 | + // console.log('encoded: ', JSON.stringify(Array.from(e0))); |
| 96 | + |
| 97 | + // const messageToDecode = e0; |
| 98 | + |
| 99 | + // v0 message |
| 100 | + // const messageToDecode = Buffer.from([200,213,166,94,34,52,245,93,0,1,0,3,0,96,254,205,0,0,0,0,64,85,32,14,0,0,0,0,0,0,0,0,0,0,0,0,0,1,10,1,128,133,181,13,0,0,0,0,1,64,85,32,14,0,0,0,0,2,0,41,9,0,0,0,0,0,0,67,82,79,51,105,114,71,49,1,0,28,78,14,0,0,0,0,0,96,254,205,0,0,0,0,1,64,58,105,13,0,0,0,0,0,96,254,205,0,0,0,0,0]); |
| 101 | + |
| 102 | + // the v1 message |
| 103 | + // const messageToDecode = Buffer.from([200,213,166,94,34,52,245,93,0,1,0,3,0,96,254,205,0,0,0,0,64,85,32,14,0,0,0,0,0,0,0,0,0,0,0,0,0,1,10,1,128,133,181,13,0,0,0,0,1,64,85,32,14,0,0,0,0,2,0,41,9,0,0,0,0,0,0,67,82,79,51,105,114,71,49,1,0,28,78,14,0,0,0,0,0,96,254,205,0,0,0,0,1,64,58,105,13,0,0,0,0,0,96,254,205,0,0,0,0,1,1,255,255]); |
| 104 | + |
| 105 | + // the v2 message |
| 106 | + const messageToDecode = Buffer.from([200,213,166,94,34,52,245,93,0,1,0,3,0,96,254,205,0,0,0,0,64,85,32,14,0,0,0,0,0,0,0,0,0,0,0,0,0,1,10,1,128,133,181,13,0,0,0,0,1,64,85,32,14,0,0,0,0,2,0,41,9,0,0,0,0,0,0,67,82,79,51,105,114,71,49,1,0,28,78,14,0,0,0,0,0,96,254,205,0,0,0,0,1,64,58,105,13,0,0,0,0,0,96,254,205,0,0,0,0,2,1,255,255,1,1,0,1,2,0]); |
| 107 | + |
| 108 | + console.log('decoding:', JSON.stringify(Array.from((messageToDecode)))); |
| 109 | + const d0 = driftClient.decodeSignedMsgOrderParamsMessage(messageToDecode, delegateSigner) |
| 110 | + console.log(d0) |
| 111 | +} |
| 112 | + |
| 113 | +main(); |
0 commit comments