@triadxyz/triad-protocol 3.5.2-beta → 4.0.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/dist/index.d.ts +16 -48
- package/dist/index.js +27 -187
- package/dist/stake.d.ts +1 -5
- package/dist/stake.js +2 -19
- package/dist/types/idl_triad_protocol.json +265 -1062
- package/dist/types/index.d.ts +9 -72
- package/dist/types/triad_protocol.d.ts +335 -1480
- package/dist/utils/feeCalculator.d.ts +5 -13
- package/dist/utils/feeCalculator.js +13 -29
- package/dist/utils/helpers.d.ts +2 -4
- package/dist/utils/helpers.js +4 -35
- package/package.json +1 -1
- package/dist/claim.d.ts +0 -59
- package/dist/claim.js +0 -193
- package/dist/utils/merkle.d.ts +0 -35
- package/dist/utils/merkle.js +0 -176
- package/dist/utils/swap.d.ts +0 -15
- package/dist/utils/swap.js +0 -93
package/dist/utils/merkle.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.debugMerkleProof = exports.createClaimData = exports.generateMerkleTree = exports.MerkleTree = void 0;
|
|
4
|
-
const sha3_1 = require("@noble/hashes/sha3");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
class MerkleTree {
|
|
7
|
-
constructor(claimData) {
|
|
8
|
-
this.claimData = claimData;
|
|
9
|
-
this.leaves = this.generateLeaves(claimData);
|
|
10
|
-
this.tree = this.buildTree(this.leaves);
|
|
11
|
-
}
|
|
12
|
-
generateLeaves(claimData) {
|
|
13
|
-
return claimData.map((data) => {
|
|
14
|
-
const userBytes = data.user.toBytes();
|
|
15
|
-
const amountBytes = Buffer.alloc(8);
|
|
16
|
-
const amountLamports = BigInt(Math.floor(data.amount * Math.pow(10, 6)));
|
|
17
|
-
amountBytes.writeBigUInt64LE(amountLamports, 0);
|
|
18
|
-
const leafData = Buffer.concat([userBytes, amountBytes]);
|
|
19
|
-
return Buffer.from((0, sha3_1.keccak_256)(leafData));
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
buildTree(leaves) {
|
|
23
|
-
if (leaves.length === 0) {
|
|
24
|
-
throw new Error('Cannot build tree with no leaves');
|
|
25
|
-
}
|
|
26
|
-
let currentLevel = [...leaves];
|
|
27
|
-
const tree = [currentLevel];
|
|
28
|
-
while (currentLevel.length > 1) {
|
|
29
|
-
const nextLevel = [];
|
|
30
|
-
for (let i = 0; i < currentLevel.length; i += 2) {
|
|
31
|
-
const left = currentLevel[i];
|
|
32
|
-
const right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left;
|
|
33
|
-
const combined = left.compare(right) < 0
|
|
34
|
-
? Buffer.concat([left, right])
|
|
35
|
-
: Buffer.concat([right, left]);
|
|
36
|
-
nextLevel.push(Buffer.from((0, sha3_1.keccak_256)(combined)));
|
|
37
|
-
}
|
|
38
|
-
currentLevel = nextLevel;
|
|
39
|
-
tree.push(currentLevel);
|
|
40
|
-
}
|
|
41
|
-
return tree;
|
|
42
|
-
}
|
|
43
|
-
getRoot() {
|
|
44
|
-
if (this.tree.length === 0) {
|
|
45
|
-
throw new Error('Tree not built');
|
|
46
|
-
}
|
|
47
|
-
const root = this.tree[this.tree.length - 1][0];
|
|
48
|
-
return Array.from(root);
|
|
49
|
-
}
|
|
50
|
-
getProof(userPubkey, amount) {
|
|
51
|
-
const targetLeaf = this.generateLeaves([{ user: userPubkey, amount }])[0];
|
|
52
|
-
const leafIndex = this.leaves.findIndex((leaf) => leaf.equals(targetLeaf));
|
|
53
|
-
if (leafIndex === -1) {
|
|
54
|
-
throw new Error('User not found in merkle tree');
|
|
55
|
-
}
|
|
56
|
-
const proof = [];
|
|
57
|
-
let currentIndex = leafIndex;
|
|
58
|
-
for (let level = 0; level < this.tree.length - 1; level++) {
|
|
59
|
-
const isRightNode = currentIndex % 2 === 1;
|
|
60
|
-
const siblingIndex = isRightNode ? currentIndex - 1 : currentIndex + 1;
|
|
61
|
-
if (siblingIndex < this.tree[level].length) {
|
|
62
|
-
proof.push(this.tree[level][siblingIndex]);
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
proof.push(this.tree[level][currentIndex]);
|
|
66
|
-
}
|
|
67
|
-
currentIndex = Math.floor(currentIndex / 2);
|
|
68
|
-
}
|
|
69
|
-
return proof.map((hash) => Array.from(hash));
|
|
70
|
-
}
|
|
71
|
-
getAllProofs() {
|
|
72
|
-
return this.claimData.map((data) => ({
|
|
73
|
-
user: data.user,
|
|
74
|
-
amount: data.amount,
|
|
75
|
-
proof: this.getProof(data.user, data.amount)
|
|
76
|
-
}));
|
|
77
|
-
}
|
|
78
|
-
verifyProof(userPubkey, amount, proof) {
|
|
79
|
-
const leafData = Buffer.concat([
|
|
80
|
-
userPubkey.toBytes(),
|
|
81
|
-
(() => {
|
|
82
|
-
const amountBytes = Buffer.alloc(8);
|
|
83
|
-
const amountLamports = BigInt(Math.floor(amount * Math.pow(10, 6)));
|
|
84
|
-
amountBytes.writeBigUInt64LE(amountLamports, 0);
|
|
85
|
-
return amountBytes;
|
|
86
|
-
})()
|
|
87
|
-
]);
|
|
88
|
-
let currentHash = Buffer.from((0, sha3_1.keccak_256)(leafData));
|
|
89
|
-
for (const proofElement of proof) {
|
|
90
|
-
const proofBuffer = Buffer.from(proofElement);
|
|
91
|
-
const combined = currentHash.compare(proofBuffer) < 0
|
|
92
|
-
? Buffer.concat([currentHash, proofBuffer])
|
|
93
|
-
: Buffer.concat([proofBuffer, currentHash]);
|
|
94
|
-
currentHash = Buffer.from((0, sha3_1.keccak_256)(combined));
|
|
95
|
-
}
|
|
96
|
-
const root = Buffer.from(this.getRoot());
|
|
97
|
-
return currentHash.equals(root);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
exports.MerkleTree = MerkleTree;
|
|
101
|
-
function generateMerkleTree(claimData) {
|
|
102
|
-
const tree = new MerkleTree(claimData);
|
|
103
|
-
return {
|
|
104
|
-
merkleRoot: tree.getRoot(),
|
|
105
|
-
proofs: tree.getAllProofs()
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
exports.generateMerkleTree = generateMerkleTree;
|
|
109
|
-
function createClaimData(data) {
|
|
110
|
-
return data.map((item) => ({
|
|
111
|
-
user: new web3_js_1.PublicKey(item.user),
|
|
112
|
-
amount: item.amount
|
|
113
|
-
}));
|
|
114
|
-
}
|
|
115
|
-
exports.createClaimData = createClaimData;
|
|
116
|
-
function debugMerkleProof(userPubkey, amount, merkleProof, merkleRoot) {
|
|
117
|
-
console.log('🔍 Debug Merkle Proof Validation (Rust Compatible)');
|
|
118
|
-
console.log('User:', userPubkey.toBase58());
|
|
119
|
-
console.log('Amount:', amount);
|
|
120
|
-
console.log('Proof length:', merkleProof.length);
|
|
121
|
-
console.log('Max theoretical elements:', Math.pow(2, merkleProof.length));
|
|
122
|
-
const leafData = Buffer.concat([
|
|
123
|
-
userPubkey.toBytes(),
|
|
124
|
-
(() => {
|
|
125
|
-
const amountBytes = Buffer.alloc(8);
|
|
126
|
-
const amountLamports = BigInt(Math.floor(amount * Math.pow(10, 6)));
|
|
127
|
-
amountBytes.writeBigUInt64LE(amountLamports, 0);
|
|
128
|
-
return amountBytes;
|
|
129
|
-
})()
|
|
130
|
-
]);
|
|
131
|
-
let currentHash = Buffer.from((0, sha3_1.keccak_256)(leafData));
|
|
132
|
-
console.log('Initial leaf hash:', currentHash.toString('hex'));
|
|
133
|
-
const steps = [];
|
|
134
|
-
for (let i = 0; i < merkleProof.length; i++) {
|
|
135
|
-
const proofElement = merkleProof[i];
|
|
136
|
-
const proofBuffer = Buffer.from(proofElement);
|
|
137
|
-
const isCurrentSmaller = currentHash.compare(proofBuffer) < 0;
|
|
138
|
-
let combined;
|
|
139
|
-
if (isCurrentSmaller) {
|
|
140
|
-
combined = Buffer.concat([currentHash, proofBuffer]);
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
combined = Buffer.concat([proofBuffer, currentHash]);
|
|
144
|
-
}
|
|
145
|
-
const newHash = Buffer.from((0, sha3_1.keccak_256)(combined));
|
|
146
|
-
const step = {
|
|
147
|
-
step: i + 1,
|
|
148
|
-
currentHash: currentHash.toString('hex'),
|
|
149
|
-
proofElement: proofBuffer.toString('hex'),
|
|
150
|
-
isCurrentSmaller,
|
|
151
|
-
combined: combined.toString('hex'),
|
|
152
|
-
newHash: newHash.toString('hex')
|
|
153
|
-
};
|
|
154
|
-
steps.push(step);
|
|
155
|
-
console.log(`Step ${i + 1}:`);
|
|
156
|
-
console.log(` Current: ${step.currentHash}`);
|
|
157
|
-
console.log(` Proof: ${step.proofElement}`);
|
|
158
|
-
console.log(` Order: ${isCurrentSmaller ? 'current + proof' : 'proof + current'}`);
|
|
159
|
-
console.log(` Result: ${step.newHash}`);
|
|
160
|
-
currentHash = newHash;
|
|
161
|
-
}
|
|
162
|
-
const rootBuffer = Buffer.from(merkleRoot);
|
|
163
|
-
const finalHash = currentHash.toString('hex');
|
|
164
|
-
const expectedRoot = rootBuffer.toString('hex');
|
|
165
|
-
const isValid = currentHash.equals(rootBuffer);
|
|
166
|
-
console.log('Final hash:', finalHash);
|
|
167
|
-
console.log('Expected root:', expectedRoot);
|
|
168
|
-
console.log('Valid:', isValid);
|
|
169
|
-
return {
|
|
170
|
-
isValid,
|
|
171
|
-
steps,
|
|
172
|
-
finalHash,
|
|
173
|
-
expectedRoot
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
exports.debugMerkleProof = debugMerkleProof;
|
package/dist/utils/swap.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { AddressLookupTableAccount, Connection, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
-
export declare const swap: ({ connection, wallet, inToken, outToken, amount, payer }: {
|
|
3
|
-
connection: Connection;
|
|
4
|
-
wallet: string;
|
|
5
|
-
inToken: string;
|
|
6
|
-
outToken: string;
|
|
7
|
-
amount: number;
|
|
8
|
-
payer: string;
|
|
9
|
-
}) => Promise<{
|
|
10
|
-
swapIxs: TransactionInstruction[];
|
|
11
|
-
addressLookupTableAccounts: AddressLookupTableAccount[];
|
|
12
|
-
setupInstructions: any[];
|
|
13
|
-
outAmount: any;
|
|
14
|
-
}>;
|
|
15
|
-
export declare const getPrice: (token: string) => Promise<any>;
|
package/dist/utils/swap.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
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
|
-
exports.getPrice = exports.swap = void 0;
|
|
16
|
-
const axios_1 = __importDefault(require("axios"));
|
|
17
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
18
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
19
|
-
const pda_1 = require("./pda");
|
|
20
|
-
const constants_1 = require("./constants");
|
|
21
|
-
const swap = ({ connection, wallet, inToken, outToken, amount, payer }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
-
const token = TOKENS[inToken];
|
|
23
|
-
if (!token) {
|
|
24
|
-
throw new Error('Token not found');
|
|
25
|
-
}
|
|
26
|
-
const formattedAmountIn = amount * Math.pow(10, token.decimals);
|
|
27
|
-
const quoteResponse = yield axios_1.default.get(`https://lite-api.jup.ag/swap/v1/quote?inputMint=${inToken}&outputMint=${outToken}&amount=${formattedAmountIn}&slippageBps=20&onlyDirectRoutes=true&platformFeeBps=60`);
|
|
28
|
-
const { data: quoteData } = quoteResponse;
|
|
29
|
-
const swapResponse = yield axios_1.default.post('https://lite-api.jup.ag/swap/v1/swap-instructions', {
|
|
30
|
-
quoteResponse: quoteData,
|
|
31
|
-
userPublicKey: wallet,
|
|
32
|
-
payer,
|
|
33
|
-
feeAccount: inToken === constants_1.TRD_MINT.toString() ? getFeeAccount() : undefined
|
|
34
|
-
});
|
|
35
|
-
const { setupInstructions, swapInstruction, addressLookupTableAddresses } = swapResponse.data;
|
|
36
|
-
return {
|
|
37
|
-
swapIxs: [deserializeInstruction(swapInstruction)],
|
|
38
|
-
addressLookupTableAccounts: yield getAddressLookupTableAccounts(connection, addressLookupTableAddresses),
|
|
39
|
-
setupInstructions: setupInstructions.length > 0
|
|
40
|
-
? [...setupInstructions.map(deserializeInstruction)]
|
|
41
|
-
: [],
|
|
42
|
-
outAmount: quoteData.otherAmountThreshold
|
|
43
|
-
};
|
|
44
|
-
});
|
|
45
|
-
exports.swap = swap;
|
|
46
|
-
const getPrice = (token) => __awaiter(void 0, void 0, void 0, function* () {
|
|
47
|
-
var _a, _b;
|
|
48
|
-
const response = yield axios_1.default.get(`https://lite-api.jup.ag/price/v3?ids=${token}`);
|
|
49
|
-
return (_b = (_a = response.data[token]) === null || _a === void 0 ? void 0 : _a.usdPrice) !== null && _b !== void 0 ? _b : 0;
|
|
50
|
-
});
|
|
51
|
-
exports.getPrice = getPrice;
|
|
52
|
-
const deserializeInstruction = (instruction) => {
|
|
53
|
-
return new web3_js_1.TransactionInstruction({
|
|
54
|
-
programId: new web3_js_1.PublicKey(instruction.programId),
|
|
55
|
-
keys: instruction.accounts.map((key) => ({
|
|
56
|
-
pubkey: new web3_js_1.PublicKey(key.pubkey),
|
|
57
|
-
isSigner: key.isSigner,
|
|
58
|
-
isWritable: key.isWritable
|
|
59
|
-
})),
|
|
60
|
-
data: Buffer.from(instruction.data, 'base64')
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
const getAddressLookupTableAccounts = (connection, keys) => __awaiter(void 0, void 0, void 0, function* () {
|
|
64
|
-
const addressLookupTableAccountInfos = yield connection.getMultipleAccountsInfo(keys.map((key) => new web3_js_1.PublicKey(key)));
|
|
65
|
-
return addressLookupTableAccountInfos.reduce((acc, accountInfo, index) => {
|
|
66
|
-
const addressLookupTableAddress = keys[index];
|
|
67
|
-
if (accountInfo) {
|
|
68
|
-
const addressLookupTableAccount = new web3_js_1.AddressLookupTableAccount({
|
|
69
|
-
key: new web3_js_1.PublicKey(addressLookupTableAddress),
|
|
70
|
-
state: web3_js_1.AddressLookupTableAccount.deserialize(accountInfo.data)
|
|
71
|
-
});
|
|
72
|
-
acc.push(addressLookupTableAccount);
|
|
73
|
-
}
|
|
74
|
-
return acc;
|
|
75
|
-
}, new Array());
|
|
76
|
-
});
|
|
77
|
-
const TOKENS = {
|
|
78
|
-
So11111111111111111111111111111111111111112: {
|
|
79
|
-
mint: 'So11111111111111111111111111111111111111112',
|
|
80
|
-
decimals: 9
|
|
81
|
-
},
|
|
82
|
-
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: {
|
|
83
|
-
mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
|
|
84
|
-
decimals: 6
|
|
85
|
-
},
|
|
86
|
-
t3DohmswhKk94PPbPYwA6ZKACyY3y5kbcqeQerAJjmV: {
|
|
87
|
-
mint: 't3DohmswhKk94PPbPYwA6ZKACyY3y5kbcqeQerAJjmV',
|
|
88
|
-
decimals: 6
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
const getFeeAccount = () => {
|
|
92
|
-
return (0, pda_1.getTokenATA)(new web3_js_1.PublicKey('Hk1r2NUL4LbUhx1agg1w44tyZiNr72mbeLsg6suF5MA4'), constants_1.USDC_MINT, spl_token_1.TOKEN_PROGRAM_ID);
|
|
93
|
-
};
|