@drift-labs/sdk 2.136.0-beta.7 → 2.136.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/idl/drift.json +1 -1
- package/lib/node/idl/drift.json +1 -1
- package/package.json +1 -1
- package/playground/swiftMsgs.ts +132 -0
- package/src/idl/drift.json +2 -2
package/lib/node/idl/drift.json
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Connection, Keypair } from '@solana/web3.js';
|
|
2
|
+
import {
|
|
3
|
+
BASE_PRECISION,
|
|
4
|
+
BN,
|
|
5
|
+
DriftClient,
|
|
6
|
+
getMarketOrderParams,
|
|
7
|
+
getUserAccountPublicKey,
|
|
8
|
+
MarketType,
|
|
9
|
+
OrderParams,
|
|
10
|
+
OrderType,
|
|
11
|
+
PositionDirection,
|
|
12
|
+
PostOnlyParams,
|
|
13
|
+
PRICE_PRECISION,
|
|
14
|
+
Wallet
|
|
15
|
+
} from '../src';
|
|
16
|
+
import { nanoid } from 'nanoid';
|
|
17
|
+
|
|
18
|
+
function hexDump(buf: Buffer) {
|
|
19
|
+
for (let i = 0; i < buf.length; i += 16) {
|
|
20
|
+
const chunk = buf.slice(i, i + 16);
|
|
21
|
+
const hex = Array.from(chunk).map(b => b.toString(16).padStart(2, '0')).join(' ');
|
|
22
|
+
const ascii = Array.from(chunk).map(b => (b >= 0x20 && b <= 0x7e) ? String.fromCharCode(b) : '.').join('');
|
|
23
|
+
console.log(i.toString(16).padStart(8, '0'), hex.padEnd(16 * 3 - 1, ' '), ascii);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const with_delegate = async (driftClient: DriftClient) => {
|
|
28
|
+
const signedMsgOrderParams = getMarketOrderParams({
|
|
29
|
+
marketIndex: 0,
|
|
30
|
+
direction: PositionDirection.SHORT,
|
|
31
|
+
baseAssetAmount: BASE_PRECISION,
|
|
32
|
+
price: new BN(237).mul(PRICE_PRECISION),
|
|
33
|
+
auctionStartPrice: new BN(240).mul(PRICE_PRECISION),
|
|
34
|
+
auctionEndPrice: new BN(238).mul(PRICE_PRECISION),
|
|
35
|
+
auctionDuration: 10,
|
|
36
|
+
userOrderId: 2,
|
|
37
|
+
postOnly: PostOnlyParams.NONE,
|
|
38
|
+
marketType: MarketType.PERP,
|
|
39
|
+
}) as OrderParams;
|
|
40
|
+
|
|
41
|
+
// const uuid = Uint8Array.from(Buffer.from(nanoid(8)));
|
|
42
|
+
const uuid = Uint8Array.from([
|
|
43
|
+
67, 82, 79, 51,
|
|
44
|
+
105, 114, 71, 49
|
|
45
|
+
]);
|
|
46
|
+
const orderParamsMessage = {
|
|
47
|
+
signedMsgOrderParams,
|
|
48
|
+
takerPubkey: await getUserAccountPublicKey(driftClient.program.programId, driftClient.wallet.publicKey, 0),
|
|
49
|
+
slot: new BN(2345),
|
|
50
|
+
uuid,
|
|
51
|
+
takeProfitOrderParams: {
|
|
52
|
+
triggerPrice: new BN(230).mul(PRICE_PRECISION),
|
|
53
|
+
baseAssetAmount: BASE_PRECISION
|
|
54
|
+
},
|
|
55
|
+
stopLossOrderParams: {
|
|
56
|
+
triggerPrice: new BN(250).mul(PRICE_PRECISION),
|
|
57
|
+
baseAssetAmount: BASE_PRECISION
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
const signedMsgOrderParamsMessage = driftClient.signSignedMsgOrderParamsMessage(
|
|
61
|
+
orderParamsMessage,
|
|
62
|
+
true);
|
|
63
|
+
const asciiHex = signedMsgOrderParamsMessage.orderParams; // Buffer of ASCII digits
|
|
64
|
+
const payload = Buffer.from(asciiHex.toString('utf8'), 'hex'); // parse to raw bytes
|
|
65
|
+
console.log('orderParamsMessage', orderParamsMessage);
|
|
66
|
+
console.log('payload len:', payload.length);
|
|
67
|
+
// console.log('payload:', payload.toString('hex'));
|
|
68
|
+
console.log('numbers array:', JSON.stringify(Array.from(payload)));
|
|
69
|
+
hexDump(payload);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const generate_payload = async (driftClient: DriftClient, delegate: boolean, tpsl: boolean) => {
|
|
73
|
+
console.log('delegate:', delegate, 'tpsl:', tpsl);
|
|
74
|
+
const base = 3.456 * BASE_PRECISION.toNumber();
|
|
75
|
+
const signedMsgOrderParams = getMarketOrderParams({
|
|
76
|
+
marketIndex: 0,
|
|
77
|
+
direction: PositionDirection.LONG,
|
|
78
|
+
baseAssetAmount: new BN(base),
|
|
79
|
+
price: new BN(237).mul(PRICE_PRECISION),
|
|
80
|
+
auctionStartPrice: new BN(230).mul(PRICE_PRECISION),
|
|
81
|
+
auctionEndPrice: new BN(237).mul(PRICE_PRECISION),
|
|
82
|
+
auctionDuration: 10,
|
|
83
|
+
userOrderId: 3,
|
|
84
|
+
postOnly: PostOnlyParams.NONE,
|
|
85
|
+
marketType: MarketType.PERP,
|
|
86
|
+
}) as OrderParams;
|
|
87
|
+
|
|
88
|
+
// const uuid = Uint8Array.from(Buffer.from(nanoid(8)));
|
|
89
|
+
const uuid = Uint8Array.from([
|
|
90
|
+
67, 82, 79, 51,
|
|
91
|
+
105, 114, 71, 49
|
|
92
|
+
]);
|
|
93
|
+
const orderParamsMessage = {
|
|
94
|
+
signedMsgOrderParams,
|
|
95
|
+
takerPubkey: delegate ? await getUserAccountPublicKey(driftClient.program.programId, driftClient.wallet.publicKey, 0) : undefined,
|
|
96
|
+
subAccountId: delegate ? undefined : 2,
|
|
97
|
+
slot: new BN(2345),
|
|
98
|
+
uuid,
|
|
99
|
+
takeProfitOrderParams: tpsl ? {
|
|
100
|
+
triggerPrice: new BN(240).mul(PRICE_PRECISION),
|
|
101
|
+
baseAssetAmount: new BN(base),
|
|
102
|
+
} : null,
|
|
103
|
+
stopLossOrderParams: tpsl ? {
|
|
104
|
+
triggerPrice: new BN(225).mul(PRICE_PRECISION),
|
|
105
|
+
baseAssetAmount: new BN(base),
|
|
106
|
+
} : null,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const signedMsgOrderParamsMessage = driftClient.signSignedMsgOrderParamsMessage(
|
|
110
|
+
orderParamsMessage,
|
|
111
|
+
delegate);
|
|
112
|
+
|
|
113
|
+
const asciiHex = signedMsgOrderParamsMessage.orderParams; // Buffer of ASCII digits
|
|
114
|
+
const payload = Buffer.from(asciiHex.toString('utf8'), 'hex'); // parse to raw bytes
|
|
115
|
+
console.log('orderParamsMessage', orderParamsMessage);
|
|
116
|
+
console.log('payload len:', payload.length);
|
|
117
|
+
// console.log('payload:', payload.toString('hex'));
|
|
118
|
+
console.log('numbers array:', JSON.stringify(Array.from(payload)));
|
|
119
|
+
hexDump(payload);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const main = async () => {
|
|
123
|
+
const connection = new Connection('https://rpc.helius.xyz/?api-key=91020679-6d19-4e39-a1db-e61df95c729f');
|
|
124
|
+
const driftClient = new DriftClient({
|
|
125
|
+
connection,
|
|
126
|
+
wallet: new Wallet(Keypair.generate())
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
await generate_payload(driftClient, false, true);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
main();
|
package/src/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
2
|
+
"version": "2.136.0",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -16113,4 +16113,4 @@
|
|
|
16113
16113
|
"metadata": {
|
|
16114
16114
|
"address": "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
|
|
16115
16115
|
}
|
|
16116
|
-
}
|
|
16116
|
+
}
|