@triadxyz/triad-protocol 0.1.0-alpha.2

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,70 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.formatNumber = exports.getUserAddressSync = exports.getTokenVaultAddressSync = exports.getVaultAddressSync = exports.decodeString = exports.encodeString = exports.getTickerAddressSync = void 0;
27
+ const web3_js_1 = require("@solana/web3.js");
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);
31
+ return TickerPDA;
32
+ };
33
+ exports.getTickerAddressSync = getTickerAddressSync;
34
+ const encodeString = (value) => {
35
+ const buffer = Buffer.alloc(32);
36
+ buffer.fill(value);
37
+ buffer.fill(' ', value.length);
38
+ return Array(...buffer);
39
+ };
40
+ exports.encodeString = encodeString;
41
+ const decodeString = (bytes) => {
42
+ const buffer = Buffer.from(bytes);
43
+ return buffer.toString('utf8').trim();
44
+ };
45
+ exports.decodeString = decodeString;
46
+ const getVaultAddressSync = (programId, tickerAddress) => {
47
+ const [VaultPDA] = web3_js_1.PublicKey.findProgramAddressSync([
48
+ Buffer.from(anchor.utils.bytes.utf8.encode('vault')),
49
+ tickerAddress.toBuffer()
50
+ ], programId);
51
+ return VaultPDA;
52
+ };
53
+ exports.getVaultAddressSync = getVaultAddressSync;
54
+ const getTokenVaultAddressSync = (programId, vault) => {
55
+ const [VaultTokenPDA] = web3_js_1.PublicKey.findProgramAddressSync([
56
+ Buffer.from(anchor.utils.bytes.utf8.encode('vault_token_account')),
57
+ vault.toBuffer()
58
+ ], programId);
59
+ return VaultTokenPDA;
60
+ };
61
+ exports.getTokenVaultAddressSync = getTokenVaultAddressSync;
62
+ function getUserAddressSync(programId, authority) {
63
+ const [UserPDA] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(anchor.utils.bytes.utf8.encode('user')), authority.toBuffer()], programId);
64
+ return UserPDA;
65
+ }
66
+ exports.getUserAddressSync = getUserAddressSync;
67
+ const formatNumber = (number, decimals = 6) => {
68
+ return Number(number.toString()) / Math.pow(10, decimals);
69
+ };
70
+ exports.formatNumber = formatNumber;
@@ -0,0 +1,89 @@
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
+ * Create a new vault
11
+ * @param protocolProgramId - The program ID of the protocol
12
+ * @param mint - Token mint for the vault (e.g. USDC)
13
+ *
14
+ */
15
+ createVault({ protocolProgramId, mint }: {
16
+ protocolProgramId: PublicKey;
17
+ mint: PublicKey;
18
+ }): Promise<string>;
19
+ /**
20
+ * Get all vaults
21
+ */
22
+ getVaults(): Promise<import("@coral-xyz/anchor").ProgramAccount<{
23
+ bump: number;
24
+ authority: PublicKey;
25
+ name: string;
26
+ tokenAccount: PublicKey;
27
+ tickerAddress: PublicKey;
28
+ totalDeposits: BN;
29
+ totalWithdraws: BN;
30
+ initTs: BN;
31
+ netDeposits: BN;
32
+ netWithdraws: BN;
33
+ longBalance: BN;
34
+ shortBalance: BN;
35
+ longPositionsOpened: BN;
36
+ shortPositionsOpened: BN;
37
+ ticker: PublicKey;
38
+ }>[]>;
39
+ /**
40
+ * Get vault by ticker Address
41
+ */
42
+ getVaultByTickerAddress(tickerAddress: PublicKey): Promise<{
43
+ tvl: number;
44
+ bump: number;
45
+ authority: PublicKey;
46
+ name: string;
47
+ tokenAccount: PublicKey;
48
+ tickerAddress: PublicKey;
49
+ totalDeposits: BN;
50
+ totalWithdraws: BN;
51
+ initTs: BN;
52
+ netDeposits: BN;
53
+ netWithdraws: BN;
54
+ longBalance: BN;
55
+ shortBalance: BN;
56
+ longPositionsOpened: BN;
57
+ shortPositionsOpened: BN;
58
+ ticker: PublicKey;
59
+ }>;
60
+ /**
61
+ * Open Position
62
+ * @param tickerPDA - Ticker PDA
63
+ * @param amount - The amount to deposit
64
+ * @param position - Long or Short
65
+ * @param mint - Token mint for the vault (e.g. USDC)
66
+ */
67
+ openPosition({ tickerPDA, amount, position, mint }: {
68
+ tickerPDA: PublicKey;
69
+ amount: string;
70
+ position: 'Long' | 'Short';
71
+ mint: PublicKey;
72
+ }): Promise<string>;
73
+ /**
74
+ * Withdraw from a vault
75
+ * @param tickerPDA - Ticker PDA
76
+ * @param amount - The amount to deposit
77
+ * @param position - Long or Short
78
+ * @param mint - Token mint for the vault (e.g. USDC)
79
+ * @param positionPubkey - The position public key
80
+ *
81
+ */
82
+ withdraw({ tickerPDA, amount, position, mint, positionPubkey }: {
83
+ tickerPDA: PublicKey;
84
+ amount: string;
85
+ position: 'Long' | 'Short';
86
+ mint: PublicKey;
87
+ positionPubkey: PublicKey;
88
+ }): Promise<string>;
89
+ }
package/dist/vault.js ADDED
@@ -0,0 +1,125 @@
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 helpers_1 = require("./utils/helpers");
14
+ const spl_token_1 = require("@solana/spl-token");
15
+ class Vault {
16
+ constructor(program, provider) {
17
+ this.provider = provider;
18
+ this.program = program;
19
+ }
20
+ /**
21
+ * Create a new vault
22
+ * @param protocolProgramId - The program ID of the protocol
23
+ * @param mint - Token mint for the vault (e.g. USDC)
24
+ *
25
+ */
26
+ createVault({ protocolProgramId, mint }) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const TickerPDA = (0, helpers_1.getTickerAddressSync)(this.program.programId, protocolProgramId);
29
+ const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, TickerPDA);
30
+ const TokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
31
+ return this.program.methods
32
+ .createVault()
33
+ .accounts({
34
+ signer: this.provider.wallet.publicKey,
35
+ vault: VaultPDA,
36
+ ticker: TickerPDA,
37
+ tokenAccount: TokenAccountPDA,
38
+ payerTokenMint: mint
39
+ })
40
+ .rpc();
41
+ });
42
+ }
43
+ /**
44
+ * Get all vaults
45
+ */
46
+ getVaults() {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ return this.program.account.vault.all();
49
+ });
50
+ }
51
+ /**
52
+ * Get vault by ticker Address
53
+ */
54
+ getVaultByTickerAddress(tickerAddress) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerAddress);
57
+ try {
58
+ const vault = yield this.program.account.vault.fetch(VaultPDA);
59
+ const tokenAcc = yield (0, spl_token_1.getAccount)(this.provider.connection, vault.tokenAccount);
60
+ return Object.assign(Object.assign({}, vault), { tvl: (0, helpers_1.formatNumber)(tokenAcc.amount) });
61
+ }
62
+ catch (e) {
63
+ throw new Error(e);
64
+ }
65
+ });
66
+ }
67
+ /**
68
+ * Open Position
69
+ * @param tickerPDA - Ticker PDA
70
+ * @param amount - The amount to deposit
71
+ * @param position - Long or Short
72
+ * @param mint - Token mint for the vault (e.g. USDC)
73
+ */
74
+ openPosition({ tickerPDA, amount, position, mint }) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const UserPDA = (0, helpers_1.getUserAddressSync)(this.program.programId, this.provider.wallet.publicKey);
77
+ const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerPDA);
78
+ const VaultTokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
79
+ const userTokenAccount = yield (0, spl_token_1.getAssociatedTokenAddress)(mint, this.provider.wallet.publicKey);
80
+ return this.program.methods
81
+ .openPosition({
82
+ amount: new anchor_1.BN(amount),
83
+ isLong: position === 'Long'
84
+ })
85
+ .accounts({
86
+ user: UserPDA,
87
+ vault: VaultPDA,
88
+ vaultTokenAccount: VaultTokenAccountPDA,
89
+ userTokenAccount
90
+ })
91
+ .rpc();
92
+ });
93
+ }
94
+ /**
95
+ * Withdraw from a vault
96
+ * @param tickerPDA - Ticker PDA
97
+ * @param amount - The amount to deposit
98
+ * @param position - Long or Short
99
+ * @param mint - Token mint for the vault (e.g. USDC)
100
+ * @param positionPubkey - The position public key
101
+ *
102
+ */
103
+ withdraw({ tickerPDA, amount, position, mint, positionPubkey }) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const UserPDA = (0, helpers_1.getUserAddressSync)(this.program.programId, this.provider.wallet.publicKey);
106
+ const VaultPDA = (0, helpers_1.getVaultAddressSync)(this.program.programId, tickerPDA);
107
+ const VaultTokenAccountPDA = (0, helpers_1.getTokenVaultAddressSync)(this.program.programId, VaultPDA);
108
+ const userTokenAccount = yield (0, spl_token_1.getAssociatedTokenAddress)(mint, this.provider.wallet.publicKey);
109
+ return this.program.methods
110
+ .closePosition({
111
+ amount: new anchor_1.BN(amount),
112
+ isLong: position === 'Long',
113
+ pubkey: positionPubkey
114
+ })
115
+ .accounts({
116
+ user: UserPDA,
117
+ vault: VaultPDA,
118
+ vaultTokenAccount: VaultTokenAccountPDA,
119
+ userTokenAccount
120
+ })
121
+ .rpc();
122
+ });
123
+ }
124
+ }
125
+ exports.default = Vault;
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@triadxyz/triad-protocol",
3
+ "version": "0.1.0-alpha.2",
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/index.js"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "exports": {
17
+ ".": "./dist/index.js"
18
+ },
19
+ "files": [
20
+ "./dist/**/*"
21
+ ],
22
+ "keywords": [
23
+ "solana",
24
+ "blockchain",
25
+ "protocol",
26
+ "triad",
27
+ "trading protocols"
28
+ ],
29
+ "author": "Lineup",
30
+ "license": "ISC",
31
+ "dependencies": {
32
+ "@coral-xyz/anchor": "0.28.0",
33
+ "@firethreexyz/firethree-protocol": "^0.19.1",
34
+ "@pythnetwork/client": "^2.19.0",
35
+ "@shadow-drive/sdk": "5.0.0",
36
+ "@solana/spl-token": "^0.3.11",
37
+ "@solana/web3.js": "1.89.1",
38
+ "axios": "^1.5.1",
39
+ "bs58": "5.0.0",
40
+ "uuid": "^9.0.1"
41
+ },
42
+ "devDependencies": {
43
+ "@typescript-eslint/eslint-plugin": "6.7.3",
44
+ "@typescript-eslint/parser": "6.7.3",
45
+ "eslint": "8.50.0",
46
+ "eslint-config-prettier": "9.0.0",
47
+ "eslint-plugin-prettier": "5.0.0",
48
+ "prettier": "3.2.4",
49
+ "rimraf": "^5.0.5",
50
+ "typedoc": "0.25.1",
51
+ "typescript": "5.2.2"
52
+ }
53
+ }