@triadxyz/triad-protocol 0.0.1-beta
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/LICENSE +201 -0
- package/README.md +27 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +51 -0
- package/dist/stake.d.ts +180 -0
- package/dist/stake.js +325 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +110 -0
- package/dist/ticker.d.ts +44 -0
- package/dist/ticker.js +77 -0
- package/dist/types/idl_triad_protocol.json +1392 -0
- package/dist/types/index.d.ts +66 -0
- package/dist/types/index.js +20 -0
- package/dist/types/triad_protocol.d.ts +1444 -0
- package/dist/types/triad_protocol.js +2 -0
- package/dist/utils/constants.d.ts +31 -0
- package/dist/utils/constants.js +16 -0
- package/dist/utils/convertSecretKeyToKeypair.d.ts +2 -0
- package/dist/utils/convertSecretKeyToKeypair.js +10 -0
- package/dist/utils/helpers.d.ts +11 -0
- package/dist/utils/helpers.js +86 -0
- package/dist/utils/priorityFee.d.ts +1 -0
- package/dist/utils/priorityFee.js +36 -0
- package/dist/utils/stakes.json +47251 -0
- package/dist/vault.d.ts +79 -0
- package/dist/vault.js +187 -0
- package/package.json +54 -0
package/dist/vault.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { AnchorProvider, BN, Program } from '@coral-xyz/anchor';
|
|
3
|
+
import { PublicKey } from '@solana/web3.js';
|
|
4
|
+
import { TriadProtocol } from './types/triad_protocol';
|
|
5
|
+
export default class Vault {
|
|
6
|
+
program: Program<TriadProtocol>;
|
|
7
|
+
provider: AnchorProvider;
|
|
8
|
+
constructor(program: Program<TriadProtocol>, provider: AnchorProvider);
|
|
9
|
+
/**
|
|
10
|
+
* Get all vaults
|
|
11
|
+
*/
|
|
12
|
+
getVaults(): Promise<import("@coral-xyz/anchor").ProgramAccount<{
|
|
13
|
+
bump: number;
|
|
14
|
+
authority: PublicKey;
|
|
15
|
+
name: string;
|
|
16
|
+
tokenAccount: PublicKey;
|
|
17
|
+
tickerAddress: PublicKey;
|
|
18
|
+
totalDeposited: BN;
|
|
19
|
+
totalWithdrawn: BN;
|
|
20
|
+
initTs: BN;
|
|
21
|
+
netDeposits: BN;
|
|
22
|
+
netWithdraws: BN;
|
|
23
|
+
longBalance: BN;
|
|
24
|
+
shortBalance: BN;
|
|
25
|
+
longPositionsOpened: BN;
|
|
26
|
+
shortPositionsOpened: BN;
|
|
27
|
+
}>[]>;
|
|
28
|
+
/**
|
|
29
|
+
* Get vault by ticker Address
|
|
30
|
+
*/
|
|
31
|
+
getVaultByTickerAddress(tickerAddress: PublicKey): Promise<{
|
|
32
|
+
tvl: number;
|
|
33
|
+
bump: number;
|
|
34
|
+
authority: PublicKey;
|
|
35
|
+
name: string;
|
|
36
|
+
tokenAccount: PublicKey;
|
|
37
|
+
tickerAddress: PublicKey;
|
|
38
|
+
totalDeposited: BN;
|
|
39
|
+
totalWithdrawn: BN;
|
|
40
|
+
initTs: BN;
|
|
41
|
+
netDeposits: BN;
|
|
42
|
+
netWithdraws: BN;
|
|
43
|
+
longBalance: BN;
|
|
44
|
+
shortBalance: BN;
|
|
45
|
+
longPositionsOpened: BN;
|
|
46
|
+
shortPositionsOpened: BN;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* Open Position
|
|
50
|
+
* @param tickerPDA - Ticker PDA
|
|
51
|
+
* @param amount - The amount to deposit
|
|
52
|
+
* @param position - Long or Short
|
|
53
|
+
* @param mint - Token mint for the vault (e.g. USDC)
|
|
54
|
+
*/
|
|
55
|
+
openPosition({ tickerPDA, amount, position, mint }: {
|
|
56
|
+
tickerPDA: PublicKey;
|
|
57
|
+
amount: string;
|
|
58
|
+
position: 'Long' | 'Short';
|
|
59
|
+
mint: PublicKey;
|
|
60
|
+
}, options?: {
|
|
61
|
+
priorityFee: number;
|
|
62
|
+
}): Promise<string>;
|
|
63
|
+
/**
|
|
64
|
+
* Withdraw from a vault
|
|
65
|
+
* @param tickerPDA - Ticker PDA
|
|
66
|
+
* @param amount - The amount to deposit
|
|
67
|
+
* @param position - Long or Short
|
|
68
|
+
* @param mint - Token mint for the vault (e.g. USDC)
|
|
69
|
+
* @param positionPubkey - The position public key
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
72
|
+
closePosition({ tickerPDA, mint, positionIndex }: {
|
|
73
|
+
tickerPDA: PublicKey;
|
|
74
|
+
mint: PublicKey;
|
|
75
|
+
positionIndex: number;
|
|
76
|
+
}, options?: {
|
|
77
|
+
priorityFee: number;
|
|
78
|
+
}): Promise<string>;
|
|
79
|
+
}
|
package/dist/vault.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
13
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
14
|
+
const helpers_1 = require("./utils/helpers");
|
|
15
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
16
|
+
class Vault {
|
|
17
|
+
constructor(program, provider) {
|
|
18
|
+
this.provider = provider;
|
|
19
|
+
this.program = program;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get all vaults
|
|
23
|
+
*/
|
|
24
|
+
getVaults() {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
return this.program.account.vault.all();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get vault by ticker Address
|
|
31
|
+
*/
|
|
32
|
+
getVaultByTickerAddress(tickerAddress) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerAddress);
|
|
35
|
+
try {
|
|
36
|
+
const vault = yield this.program.account.vault.fetch(VaultPDA);
|
|
37
|
+
const tokenAcc = yield (0, spl_token_1.getAccount)(this.provider.connection, vault.tokenAccount);
|
|
38
|
+
return Object.assign(Object.assign({}, vault), { tvl: (0, helpers_1.formatNumber)(tokenAcc.amount) });
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
throw new Error(e);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Open Position
|
|
47
|
+
* @param tickerPDA - Ticker PDA
|
|
48
|
+
* @param amount - The amount to deposit
|
|
49
|
+
* @param position - Long or Short
|
|
50
|
+
* @param mint - Token mint for the vault (e.g. USDC)
|
|
51
|
+
*/
|
|
52
|
+
openPosition({ tickerPDA, amount, position, mint }, options) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
try {
|
|
55
|
+
const UserPositionPDA = (0, helpers_1.getUserPositionAddressSync)(this.program.programId, this.provider.wallet.publicKey, tickerPDA);
|
|
56
|
+
const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerPDA);
|
|
57
|
+
const userTokenAccount = yield (0, spl_token_1.getAssociatedTokenAddress)(mint, this.provider.wallet.publicKey);
|
|
58
|
+
let hasUserPosition = false;
|
|
59
|
+
try {
|
|
60
|
+
yield this.program.account.userPosition.fetch(UserPositionPDA);
|
|
61
|
+
hasUserPosition = true;
|
|
62
|
+
}
|
|
63
|
+
catch (_a) { }
|
|
64
|
+
const instructions = [];
|
|
65
|
+
if (options === null || options === void 0 ? void 0 : options.priorityFee) {
|
|
66
|
+
instructions.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
67
|
+
microLamports: options.priorityFee
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
if (!hasUserPosition) {
|
|
71
|
+
instructions.push(yield this.program.methods
|
|
72
|
+
.createUserPosition()
|
|
73
|
+
.accounts({
|
|
74
|
+
signer: this.provider.wallet.publicKey,
|
|
75
|
+
ticker: tickerPDA
|
|
76
|
+
})
|
|
77
|
+
.instruction());
|
|
78
|
+
}
|
|
79
|
+
instructions.push(yield this.program.methods
|
|
80
|
+
.openPosition({
|
|
81
|
+
amount: new anchor_1.BN(amount),
|
|
82
|
+
isLong: position === 'Long'
|
|
83
|
+
})
|
|
84
|
+
.accounts({
|
|
85
|
+
userPosition: UserPositionPDA,
|
|
86
|
+
ticker: tickerPDA,
|
|
87
|
+
vault: VaultPDA,
|
|
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
|
+
const hash = yield this.provider.sendAndConfirm(new web3_js_1.VersionedTransaction(message));
|
|
98
|
+
const { blockhash: blockhash2, lastValidBlockHeight } = yield this.provider.connection.getLatestBlockhash();
|
|
99
|
+
const confirmTx = yield this.provider.connection.confirmTransaction({
|
|
100
|
+
signature: hash,
|
|
101
|
+
blockhash: blockhash2,
|
|
102
|
+
lastValidBlockHeight
|
|
103
|
+
}, 'finalized');
|
|
104
|
+
if (confirmTx.value.err) {
|
|
105
|
+
throw new Error('Failed to open position');
|
|
106
|
+
}
|
|
107
|
+
return hash;
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
console.error(error);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Withdraw from a vault
|
|
116
|
+
* @param tickerPDA - Ticker PDA
|
|
117
|
+
* @param amount - The amount to deposit
|
|
118
|
+
* @param position - Long or Short
|
|
119
|
+
* @param mint - Token mint for the vault (e.g. USDC)
|
|
120
|
+
* @param positionPubkey - The position public key
|
|
121
|
+
*
|
|
122
|
+
*/
|
|
123
|
+
closePosition({ tickerPDA, mint, positionIndex }, options) {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
try {
|
|
126
|
+
const UserPositionPDA = (0, helpers_1.getUserPositionAddressSync)(this.program.programId, this.provider.wallet.publicKey, tickerPDA);
|
|
127
|
+
const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerPDA);
|
|
128
|
+
const VaultTokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
|
|
129
|
+
const userTokenAccount = yield (0, spl_token_1.getAssociatedTokenAddress)(mint, this.provider.wallet.publicKey);
|
|
130
|
+
let hasUser = false;
|
|
131
|
+
try {
|
|
132
|
+
yield this.program.account.userPosition.fetch(UserPositionPDA);
|
|
133
|
+
hasUser = true;
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
console.log(e);
|
|
137
|
+
}
|
|
138
|
+
const instructions = [];
|
|
139
|
+
if (options === null || options === void 0 ? void 0 : options.priorityFee) {
|
|
140
|
+
instructions.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
141
|
+
microLamports: options.priorityFee
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
if (!hasUser) {
|
|
145
|
+
instructions.push(yield this.program.methods
|
|
146
|
+
.createUserPosition()
|
|
147
|
+
.accounts({
|
|
148
|
+
signer: this.provider.wallet.publicKey,
|
|
149
|
+
ticker: tickerPDA
|
|
150
|
+
})
|
|
151
|
+
.instruction());
|
|
152
|
+
}
|
|
153
|
+
instructions.push(yield this.program.methods
|
|
154
|
+
.closePosition({ positionIndex })
|
|
155
|
+
.accounts({
|
|
156
|
+
userPosition: UserPositionPDA,
|
|
157
|
+
ticker: tickerPDA,
|
|
158
|
+
vault: VaultPDA,
|
|
159
|
+
vaultTokenAccount: VaultTokenAccountPDA,
|
|
160
|
+
userTokenAccount
|
|
161
|
+
})
|
|
162
|
+
.instruction());
|
|
163
|
+
const { blockhash } = yield this.provider.connection.getLatestBlockhash();
|
|
164
|
+
const message = new web3_js_1.TransactionMessage({
|
|
165
|
+
payerKey: this.provider.wallet.publicKey,
|
|
166
|
+
recentBlockhash: blockhash,
|
|
167
|
+
instructions
|
|
168
|
+
}).compileToV0Message();
|
|
169
|
+
const hash = yield this.provider.sendAndConfirm(new web3_js_1.VersionedTransaction(message));
|
|
170
|
+
const { blockhash: blockhash2, lastValidBlockHeight } = yield this.provider.connection.getLatestBlockhash();
|
|
171
|
+
const confirmTx = yield this.provider.connection.confirmTransaction({
|
|
172
|
+
signature: hash,
|
|
173
|
+
blockhash: blockhash2,
|
|
174
|
+
lastValidBlockHeight
|
|
175
|
+
}, 'finalized');
|
|
176
|
+
if (confirmTx.value.err) {
|
|
177
|
+
throw new Error('Failed to open position');
|
|
178
|
+
}
|
|
179
|
+
return hash;
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
console.error(error);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.default = Vault;
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@triadxyz/triad-protocol",
|
|
3
|
+
"version": "0.0.1-beta",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"clean": "rimraf dist",
|
|
9
|
+
"build": "yarn run clean && tsc",
|
|
10
|
+
"prepublishOnly": "yarn build",
|
|
11
|
+
"start": "yarn build && node ./dist/test.js",
|
|
12
|
+
"test": "rimraf dist && tsc && node ./dist/test.js"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"./dist/**/*"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"solana",
|
|
25
|
+
"blockchain",
|
|
26
|
+
"protocol",
|
|
27
|
+
"triad",
|
|
28
|
+
"trading protocols",
|
|
29
|
+
"dex",
|
|
30
|
+
"SPL 404"
|
|
31
|
+
],
|
|
32
|
+
"author": "Triad Labs",
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@coral-xyz/anchor": "0.30.0",
|
|
36
|
+
"@solana/spl-token": "0.4.6",
|
|
37
|
+
"@solana/web3.js": "1.89.1",
|
|
38
|
+
"axios": "^1.5.1",
|
|
39
|
+
"bn.js": "^5.2.1",
|
|
40
|
+
"bs58": "5.0.0",
|
|
41
|
+
"uuid": "^9.0.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "6.7.3",
|
|
45
|
+
"@typescript-eslint/parser": "6.7.3",
|
|
46
|
+
"eslint": "8.50.0",
|
|
47
|
+
"eslint-config-prettier": "9.0.0",
|
|
48
|
+
"eslint-plugin-prettier": "5.0.0",
|
|
49
|
+
"prettier": "3.2.4",
|
|
50
|
+
"rimraf": "^5.0.5",
|
|
51
|
+
"typedoc": "0.25.1",
|
|
52
|
+
"typescript": "5.2.2"
|
|
53
|
+
}
|
|
54
|
+
}
|