@triadxyz/triad-protocol 0.1.0-alpha.5 → 0.1.0-alpha.7

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/dist/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  /// <reference types="bn.js" />
2
- /// <reference types="@coral-xyz/anchor/node_modules/@solana/web3.js" />
3
2
  import { Address, AnchorProvider, Program, Wallet } from '@coral-xyz/anchor';
4
- import { Connection } from '@solana/web3.js';
3
+ import { Connection, PublicKey } from '@solana/web3.js';
5
4
  import { TriadProtocol } from './types/triad_protocol';
6
5
  import Ticker from './ticker';
7
6
  import Vault from './vault';
@@ -18,7 +17,7 @@ export default class TriadProtocolClient {
18
17
  ts: import("bn.js");
19
18
  name: string;
20
19
  bump: number;
21
- authority: import("@solana/web3.js").PublicKey;
20
+ authority: PublicKey;
22
21
  referrer: string;
23
22
  community: string;
24
23
  netDeposits: import("bn.js");
@@ -48,7 +47,7 @@ export default class TriadProtocolClient {
48
47
  ts: import("bn.js");
49
48
  name: string;
50
49
  bump: number;
51
- authority: import("@solana/web3.js").PublicKey;
50
+ authority: PublicKey;
52
51
  referrer: string;
53
52
  community: string;
54
53
  netDeposits: import("bn.js");
package/dist/index.js CHANGED
@@ -13,11 +13,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const anchor_1 = require("@coral-xyz/anchor");
16
+ const web3_js_1 = require("@solana/web3.js");
16
17
  const triad_protocol_1 = require("./types/triad_protocol");
17
18
  const constants_1 = require("./utils/constants");
18
19
  const ticker_1 = __importDefault(require("./ticker"));
19
20
  const helpers_1 = require("./utils/helpers");
20
21
  const vault_1 = __importDefault(require("./vault"));
22
+ const convertSecretKeyToKeypair_1 = require("./utils/convertSecretKeyToKeypair");
21
23
  class TriadProtocolClient {
22
24
  constructor(connection, wallet) {
23
25
  this.provider = new anchor_1.AnchorProvider(connection, wallet, anchor_1.AnchorProvider.defaultOptions());
@@ -73,3 +75,18 @@ class TriadProtocolClient {
73
75
  }
74
76
  }
75
77
  exports.default = TriadProtocolClient;
78
+ const connection = new web3_js_1.Connection('https://devnet.helius-rpc.com/?api-key=3fb2333b-4396-4db0-94c5-663cca63697e', 'confirmed');
79
+ const keypair = (0, convertSecretKeyToKeypair_1.convertSecretKeyToKeypair)('43ZaBtQz9KKRw9n731qoxSk18Crp4v11uAre8ucfh8xJMvvkDC7HbsoGGTLFP1Hr1HXLPwKhLaLwetfJ53FQrMC4');
80
+ const wallet = new anchor_1.Wallet(keypair);
81
+ const triadProtocolClient = new TriadProtocolClient(connection, wallet);
82
+ triadProtocolClient.vault
83
+ .closePosition({
84
+ tickerPDA: new web3_js_1.PublicKey('4gCEAiCm6nHazMsJ1MA1zdAsxLa6kEVkTcaazzpRFMSL'),
85
+ amount: '1',
86
+ position: 'Long',
87
+ mint: new web3_js_1.PublicKey('4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU'),
88
+ positionPubkey: new web3_js_1.PublicKey('4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU')
89
+ })
90
+ .then((a) => {
91
+ console.log(a);
92
+ });
package/dist/ticker.js CHANGED
@@ -32,7 +32,7 @@ class Ticker {
32
32
  */
33
33
  createTicker({ name, protocolProgramId, tokenMint }) {
34
34
  return __awaiter(this, void 0, void 0, function* () {
35
- const TickerPDA = (0, helpers_1.getTickerAddressSync)(this.program.programId, protocolProgramId);
35
+ const TickerPDA = (0, helpers_1.getTickerAddressSync)(this.program.programId, name);
36
36
  const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, TickerPDA);
37
37
  const TokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
38
38
  return this.program.methods
@@ -1,6 +1,6 @@
1
1
  import { PublicKey } from '@solana/web3.js';
2
2
  import BN from 'bn.js';
3
- export declare const getTickerAddressSync: (programId: PublicKey, protocolProgramId: PublicKey) => PublicKey;
3
+ export declare const getTickerAddressSync: (programId: PublicKey, tickerName: string) => PublicKey;
4
4
  export declare const encodeString: (value: string) => number[];
5
5
  export declare const decodeString: (bytes: number[]) => string;
6
6
  export declare const getVaultAddressSync: (programId: PublicKey, tickerAddress: PublicKey) => PublicKey;
@@ -26,8 +26,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.formatNumber = exports.getUserAddressSync = exports.getTokenVaultAddressSync = exports.getVaultAddressSync = exports.decodeString = exports.encodeString = exports.getTickerAddressSync = void 0;
27
27
  const web3_js_1 = require("@solana/web3.js");
28
28
  const anchor = __importStar(require("@coral-xyz/anchor"));
29
- const getTickerAddressSync = (programId, protocolProgramId) => {
30
- const [TickerPDA] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from('ticker'), protocolProgramId.toBuffer()], programId);
29
+ const getTickerAddressSync = (programId, tickerName) => {
30
+ const [TickerPDA] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from('ticker'), Buffer.from(tickerName)], programId);
31
31
  return TickerPDA;
32
32
  };
33
33
  exports.getTickerAddressSync = getTickerAddressSync;
package/dist/vault.js CHANGED
@@ -51,50 +51,55 @@ class Vault {
51
51
  */
52
52
  openPosition({ tickerPDA, amount, position, mint }) {
53
53
  return __awaiter(this, void 0, void 0, function* () {
54
- const UserPDA = (0, helpers_1.getUserAddressSync)(this.program.programId, this.provider.wallet.publicKey);
55
- const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerPDA);
56
- const VaultTokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
57
- const userTokenAccount = yield (0, spl_token_1.getAssociatedTokenAddress)(mint, this.provider.wallet.publicKey);
58
- let hasUser = false;
59
54
  try {
60
- this.program.account.user.fetch(this.provider.wallet.publicKey);
61
- hasUser = true;
62
- }
63
- catch (_a) { }
64
- const instructions = [];
65
- if (!hasUser) {
55
+ const UserPDA = (0, helpers_1.getUserAddressSync)(this.program.programId, this.provider.wallet.publicKey);
56
+ const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerPDA);
57
+ const VaultTokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
58
+ const userTokenAccount = yield (0, spl_token_1.getAssociatedTokenAddress)(mint, this.provider.wallet.publicKey);
59
+ let hasUser = false;
60
+ try {
61
+ yield this.program.account.user.fetch(UserPDA);
62
+ hasUser = true;
63
+ }
64
+ catch (_a) { }
65
+ const instructions = [];
66
+ if (!hasUser) {
67
+ instructions.push(yield this.program.methods
68
+ .createUser({
69
+ name: `User ${Math.floor(Math.random() * 100)}`,
70
+ referrer: '',
71
+ community: ''
72
+ })
73
+ .accounts({
74
+ signer: this.provider.wallet.publicKey,
75
+ user: UserPDA
76
+ })
77
+ .instruction());
78
+ }
66
79
  instructions.push(yield this.program.methods
67
- .createUser({
68
- name: `User ${Math.floor(Math.random() * 100)}`,
69
- referrer: '',
70
- community: ''
80
+ .openPosition({
81
+ amount: new anchor_1.BN(amount),
82
+ isLong: position === 'Long'
71
83
  })
72
84
  .accounts({
73
- signer: this.provider.wallet.publicKey,
74
- user: UserPDA
85
+ user: UserPDA,
86
+ ticker: tickerPDA,
87
+ vault: VaultPDA,
88
+ vaultTokenAccount: VaultTokenAccountPDA,
89
+ userTokenAccount
75
90
  })
76
91
  .instruction());
92
+ const { blockhash } = yield this.provider.connection.getLatestBlockhash();
93
+ const message = new web3_js_1.TransactionMessage({
94
+ payerKey: this.provider.wallet.publicKey,
95
+ recentBlockhash: blockhash,
96
+ instructions
97
+ }).compileToV0Message();
98
+ return this.provider.sendAndConfirm(new web3_js_1.VersionedTransaction(message));
99
+ }
100
+ catch (error) {
101
+ console.error(error);
77
102
  }
78
- instructions.push(yield this.program.methods
79
- .openPosition({
80
- amount: new anchor_1.BN(amount),
81
- isLong: position === 'Long'
82
- })
83
- .accounts({
84
- user: UserPDA,
85
- ticker: tickerPDA,
86
- vault: VaultPDA,
87
- vaultTokenAccount: VaultTokenAccountPDA,
88
- userTokenAccount
89
- })
90
- .instruction());
91
- const { blockhash } = yield this.provider.connection.getLatestBlockhash();
92
- const message = new web3_js_1.TransactionMessage({
93
- payerKey: this.provider.wallet.publicKey,
94
- recentBlockhash: blockhash,
95
- instructions
96
- }).compileToV0Message();
97
- return this.provider.sendAndConfirm(new web3_js_1.VersionedTransaction(message));
98
103
  });
99
104
  }
100
105
  /**
@@ -108,50 +113,59 @@ class Vault {
108
113
  */
109
114
  closePosition({ tickerPDA, amount, position, mint, positionPubkey }) {
110
115
  return __awaiter(this, void 0, void 0, function* () {
111
- const UserPDA = (0, helpers_1.getUserAddressSync)(this.program.programId, this.provider.wallet.publicKey);
112
- const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerPDA);
113
- const VaultTokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
114
- const userTokenAccount = yield (0, spl_token_1.getAssociatedTokenAddress)(mint, this.provider.wallet.publicKey);
115
- let hasUser = false;
116
116
  try {
117
- this.program.account.user.fetch(this.provider.wallet.publicKey);
118
- hasUser = true;
119
- }
120
- catch (_a) { }
121
- const instructions = [];
122
- if (!hasUser) {
117
+ const UserPDA = (0, helpers_1.getUserAddressSync)(this.program.programId, this.provider.wallet.publicKey);
118
+ const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerPDA);
119
+ const VaultTokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
120
+ const userTokenAccount = yield (0, spl_token_1.getAssociatedTokenAddress)(mint, this.provider.wallet.publicKey);
121
+ let hasUser = false;
122
+ try {
123
+ yield this.program.account.user.fetch(UserPDA);
124
+ console.log('User exists');
125
+ hasUser = true;
126
+ }
127
+ catch (e) {
128
+ console.log('User does not exist');
129
+ console.log(e);
130
+ }
131
+ const instructions = [];
132
+ if (!hasUser) {
133
+ instructions.push(yield this.program.methods
134
+ .createUser({
135
+ name: `User ${Math.floor(Math.random() * 100)}`,
136
+ referrer: '',
137
+ community: ''
138
+ })
139
+ .accounts({
140
+ signer: this.provider.wallet.publicKey,
141
+ user: UserPDA
142
+ })
143
+ .instruction());
144
+ }
123
145
  instructions.push(yield this.program.methods
124
- .createUser({
125
- name: `User ${Math.floor(Math.random() * 100)}`,
126
- referrer: '',
127
- community: ''
146
+ .closePosition({
147
+ amount: new anchor_1.BN(amount),
148
+ isLong: position === 'Long',
149
+ pubkey: positionPubkey
128
150
  })
129
151
  .accounts({
130
- signer: this.provider.wallet.publicKey,
131
- user: UserPDA
152
+ user: UserPDA,
153
+ vault: VaultPDA,
154
+ vaultTokenAccount: VaultTokenAccountPDA,
155
+ userTokenAccount
132
156
  })
133
157
  .instruction());
158
+ const { blockhash } = yield this.provider.connection.getLatestBlockhash();
159
+ const message = new web3_js_1.TransactionMessage({
160
+ payerKey: this.provider.wallet.publicKey,
161
+ recentBlockhash: blockhash,
162
+ instructions
163
+ }).compileToV0Message();
164
+ return this.provider.sendAndConfirm(new web3_js_1.VersionedTransaction(message));
165
+ }
166
+ catch (error) {
167
+ console.error(error);
134
168
  }
135
- instructions.push(yield this.program.methods
136
- .closePosition({
137
- amount: new anchor_1.BN(amount),
138
- isLong: position === 'Long',
139
- pubkey: positionPubkey
140
- })
141
- .accounts({
142
- user: UserPDA,
143
- vault: VaultPDA,
144
- vaultTokenAccount: VaultTokenAccountPDA,
145
- userTokenAccount
146
- })
147
- .instruction());
148
- const { blockhash } = yield this.provider.connection.getLatestBlockhash();
149
- const message = new web3_js_1.TransactionMessage({
150
- payerKey: this.provider.wallet.publicKey,
151
- recentBlockhash: blockhash,
152
- instructions
153
- }).compileToV0Message();
154
- return this.provider.sendAndConfirm(new web3_js_1.VersionedTransaction(message));
155
169
  });
156
170
  }
157
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "0.1.0-alpha.5",
3
+ "version": "0.1.0-alpha.7",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",