@triadxyz/triad-protocol 0.5.3-beta.devnet → 0.5.5-beta.devnet

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.
@@ -0,0 +1,32 @@
1
+ import { AnchorProvider, Program } from '@coral-xyz/anchor';
2
+ import { TriadProtocol } from './types/triad_protocol';
3
+ import { PublicKey } from '@solana/web3.js';
4
+ import { Market, OrderDirection, OrderType } from './types/trade';
5
+ import { RpcOptions } from './types';
6
+ export default class Trade {
7
+ program: Program<TriadProtocol>;
8
+ provider: AnchorProvider;
9
+ constructor(program: Program<TriadProtocol>, provider: AnchorProvider);
10
+ /**
11
+ * Get all Markets
12
+ */
13
+ getMarkets(): Promise<Market[]>;
14
+ getMarketByAddress(address: PublicKey): Promise<Market>;
15
+ /**
16
+ * Initialize Market
17
+ * @param market id - new markert id - length + 1
18
+ * @param name - PYTH/TRD JUP/TRD DRIFT/TRD
19
+ *
20
+ */
21
+ initializeMarket({ marketId, name }: {
22
+ marketId: number;
23
+ name: string;
24
+ }, options?: RpcOptions): Promise<string>;
25
+ openOrder(marketId: number, args: {
26
+ amount: number;
27
+ direction: OrderDirection;
28
+ orderType: OrderType;
29
+ limitPrice?: number;
30
+ comment?: string;
31
+ }, options?: RpcOptions): Promise<string>;
32
+ }
package/dist/trade.js ADDED
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const web3_js_1 = require("@solana/web3.js");
16
+ const bn_js_1 = __importDefault(require("bn.js"));
17
+ const constants_1 = require("./utils/constants");
18
+ const helpers_1 = require("./utils/helpers");
19
+ class Trade {
20
+ constructor(program, provider) {
21
+ this.provider = provider;
22
+ this.program = program;
23
+ }
24
+ /**
25
+ * Get all Markets
26
+ */
27
+ getMarkets() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ return this.program.account.market.all().then((markets) => markets.map(({ account }) => ({
30
+ bump: account.bump,
31
+ authority: account.authority.toString(),
32
+ marketId: account.marketId.toNumber(),
33
+ name: account.name,
34
+ hypePrice: account.hypePrice.toNumber(),
35
+ flopPrice: account.flopPrice.toNumber(),
36
+ hypeLiquidity: account.hypeLiquidity.toNumber(),
37
+ flopLiquidity: account.flopLiquidity.toNumber(),
38
+ totalHypeShares: account.totalHypeShares.toNumber(),
39
+ totalFlopShares: account.totalFlopShares.toNumber(),
40
+ totalVolume: account.totalVolume.toNumber(),
41
+ vaultTokenAccount: account.vaultTokenAccount.toString(),
42
+ mint: account.mint.toString(),
43
+ ts: account.ts.toNumber(),
44
+ updateTs: account.updateTs.toNumber(),
45
+ openOrdersCount: account.openOrdersCount.toNumber(),
46
+ nextOrderId: account.nextOrderId.toNumber(),
47
+ feeBps: account.feeBps,
48
+ feeVault: account.feeVault.toBase58(),
49
+ isActive: account.isActive,
50
+ isOfficial: account.isOfficial
51
+ })));
52
+ });
53
+ }
54
+ getMarketByAddress(address) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const account = yield this.program.account.market.fetch(address);
57
+ return {
58
+ bump: account.bump,
59
+ authority: account.authority.toString(),
60
+ marketId: account.marketId.toNumber(),
61
+ name: account.name,
62
+ hypePrice: account.hypePrice.toNumber(),
63
+ flopPrice: account.flopPrice.toNumber(),
64
+ hypeLiquidity: account.hypeLiquidity.toNumber(),
65
+ flopLiquidity: account.flopLiquidity.toNumber(),
66
+ totalHypeShares: account.totalHypeShares.toNumber(),
67
+ totalFlopShares: account.totalFlopShares.toNumber(),
68
+ totalVolume: account.totalVolume.toNumber(),
69
+ vaultTokenAccount: account.vaultTokenAccount.toString(),
70
+ mint: account.mint.toString(),
71
+ ts: account.ts.toNumber(),
72
+ updateTs: account.updateTs.toNumber(),
73
+ openOrdersCount: account.openOrdersCount.toNumber(),
74
+ nextOrderId: account.nextOrderId.toNumber(),
75
+ feeBps: account.feeBps,
76
+ feeVault: account.feeVault.toBase58(),
77
+ isActive: account.isActive,
78
+ isOfficial: account.isOfficial
79
+ };
80
+ });
81
+ }
82
+ /**
83
+ * Initialize Market
84
+ * @param market id - new markert id - length + 1
85
+ * @param name - PYTH/TRD JUP/TRD DRIFT/TRD
86
+ *
87
+ */
88
+ initializeMarket({ marketId, name }, options) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ const method = this.program.methods
91
+ .initializeMarket({
92
+ marketId: new bn_js_1.default(marketId),
93
+ name: name
94
+ })
95
+ .accounts({
96
+ signer: this.provider.publicKey,
97
+ mint: constants_1.TRD_MINT
98
+ });
99
+ if (options === null || options === void 0 ? void 0 : options.microLamports) {
100
+ method.postInstructions([
101
+ web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
102
+ microLamports: options.microLamports
103
+ })
104
+ ]);
105
+ }
106
+ return method.rpc({ skipPreflight: options === null || options === void 0 ? void 0 : options.skipPreflight });
107
+ });
108
+ }
109
+ openOrder(marketId, args, options) {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ const marketPDA = (0, helpers_1.getMarketAddressSync)(this.program.programId, marketId);
112
+ const method = this.program.methods
113
+ .openOrder({
114
+ amount: new bn_js_1.default(args.amount / Math.pow(10, constants_1.TRD_DECIMALS)),
115
+ direction: args.direction,
116
+ orderType: args.orderType,
117
+ limitPrice: args.limitPrice
118
+ ? new bn_js_1.default(args.limitPrice / Math.pow(10, constants_1.TRD_DECIMALS))
119
+ : undefined,
120
+ comment: (0, helpers_1.encodeString)(args.comment, 64)
121
+ })
122
+ .accounts({
123
+ signer: this.provider.publicKey,
124
+ market: marketPDA,
125
+ mint: constants_1.TRD_MINT
126
+ });
127
+ if (options === null || options === void 0 ? void 0 : options.microLamports) {
128
+ method.postInstructions([
129
+ web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
130
+ microLamports: options.microLamports
131
+ })
132
+ ]);
133
+ }
134
+ return method.rpc({ skipPreflight: options === null || options === void 0 ? void 0 : options.skipPreflight });
135
+ });
136
+ }
137
+ }
138
+ exports.default = Trade;