@n1xyz/nord-ts 0.0.16 → 0.0.18-8121ed05.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/bridge/client.js +45 -6
- package/dist/gen/nord.d.ts +48 -4
- package/dist/gen/nord.js +634 -34
- package/dist/idl/bridge.d.ts +569 -0
- package/dist/idl/bridge.js +8 -0
- package/dist/idl/bridge.json +75 -62
- package/dist/idl/index.d.ts +71 -49
- package/dist/index.d.ts +1 -3
- package/dist/index.js +6 -13
- package/dist/nord/api/actions.d.ts +7 -2
- package/dist/nord/api/actions.js +7 -1
- package/dist/nord/api/core.d.ts +1 -1
- package/dist/nord/api/core.js +3 -3
- package/dist/nord/api/market.js +1 -3
- package/dist/nord/api/queries.js +2 -2
- package/dist/nord/client/Nord.d.ts +9 -14
- package/dist/nord/client/Nord.js +27 -38
- package/dist/nord/client/NordUser.d.ts +7 -23
- package/dist/nord/client/NordUser.js +67 -143
- package/dist/nord/utils/NordError.js +5 -2
- package/dist/types.d.ts +34 -50
- package/dist/utils.d.ts +2 -9
- package/dist/utils.js +13 -18
- package/package.json +17 -18
- package/protoc-generate.sh +0 -0
- package/src/idl/bridge.json +75 -62
- package/src/index.ts +9 -12
- package/src/nord/api/actions.ts +14 -3
- package/src/nord/api/core.ts +4 -2
- package/src/nord/api/market.ts +1 -4
- package/src/nord/api/queries.ts +2 -2
- package/src/nord/client/Nord.ts +45 -50
- package/src/nord/client/NordUser.ts +90 -171
- package/src/nord/utils/NordError.ts +6 -2
- package/src/types.ts +34 -49
- package/src/utils.ts +16 -19
- package/codebase.md +0 -18886
- package/docs/.nojekyll +0 -1
- package/docs/assets/hierarchy.js +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/icons.js +0 -18
- package/docs/assets/icons.svg +0 -1
- package/docs/assets/main.js +0 -60
- package/docs/assets/navigation.js +0 -1
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1611
- package/docs/classes/Nord.html +0 -189
- package/docs/classes/NordError.html +0 -24
- package/docs/classes/NordUser.html +0 -115
- package/docs/classes/NordWebSocketClient.html +0 -330
- package/docs/classes/SolanaBridgeClient.html +0 -91
- package/docs/classes/Subscriber.html +0 -10
- package/docs/enums/FillMode.html +0 -5
- package/docs/enums/KeyType.html +0 -4
- package/docs/enums/MetricPeriod.html +0 -9
- package/docs/enums/PdaSeedType.html +0 -11
- package/docs/enums/PeakTpsPeriodUnit.html +0 -15
- package/docs/enums/Side.html +0 -3
- package/docs/enums/WebSocketMessageType.html +0 -7
- package/docs/functions/actionQueryRollman.html +0 -6
- package/docs/functions/actionsQueryRollman.html +0 -6
- package/docs/functions/aggregateMetrics-1.html +0 -7
- package/docs/functions/assert.html +0 -1
- package/docs/functions/bigIntToProtoU128.html +0 -4
- package/docs/functions/bridgeToBN.html +0 -5
- package/docs/functions/bufferToHex.html +0 -4
- package/docs/functions/cancelOrder.html +0 -1
- package/docs/functions/checkPubKeyLength.html +0 -1
- package/docs/functions/checkedFetch.html +0 -6
- package/docs/functions/createSession.html +0 -1
- package/docs/functions/decodeLengthDelimited.html +0 -11
- package/docs/functions/encodeLengthDelimited.html +0 -6
- package/docs/functions/fillModeToProtoFillMode.html +0 -5
- package/docs/functions/findMarket.html +0 -1
- package/docs/functions/findPda.html +0 -6
- package/docs/functions/findToken.html +0 -1
- package/docs/functions/fromBN.html +0 -5
- package/docs/functions/getAccount.html +0 -6
- package/docs/functions/getActionNonce.html +0 -5
- package/docs/functions/getCurrentTps.html +0 -6
- package/docs/functions/getInfo.html +0 -5
- package/docs/functions/getLastActionId.html +0 -5
- package/docs/functions/getMedianLatency.html +0 -6
- package/docs/functions/getOrderbook.html +0 -6
- package/docs/functions/getPeakTps.html +0 -6
- package/docs/functions/getTimestamp.html +0 -5
- package/docs/functions/getTotalTransactions.html +0 -5
- package/docs/functions/getTrades.html +0 -6
- package/docs/functions/getUserAccountIds.html +0 -6
- package/docs/functions/hexToBuffer.html +0 -4
- package/docs/functions/initWebSocketClient.html +0 -12
- package/docs/functions/keypairFromPrivateKey.html +0 -4
- package/docs/functions/makeSigningFunction.html +0 -4
- package/docs/functions/makeWalletSignFn.html +0 -6
- package/docs/functions/marketsStats.html +0 -5
- package/docs/functions/optExpect.html +0 -5
- package/docs/functions/optMap.html +0 -5
- package/docs/functions/optUnwrap.html +0 -2
- package/docs/functions/panic.html +0 -1
- package/docs/functions/placeOrder.html +0 -1
- package/docs/functions/queryAction.html +0 -6
- package/docs/functions/queryPrometheus.html +0 -6
- package/docs/functions/queryRecentActions.html +0 -7
- package/docs/functions/revokeSession.html +0 -1
- package/docs/functions/shortenPublicKey.html +0 -5
- package/docs/functions/signAction.html +0 -6
- package/docs/functions/toBN.html +0 -5
- package/docs/functions/toScaledU128.html +0 -8
- package/docs/functions/toScaledU64.html +0 -8
- package/docs/functions/transfer.html +0 -1
- package/docs/functions/withdraw.html +0 -1
- package/docs/hierarchy.html +0 -1
- package/docs/index.html +0 -40
- package/docs/interfaces/Account.html +0 -8
- package/docs/interfaces/ActionInfo.html +0 -8
- package/docs/interfaces/ActionNonceResponse.html +0 -3
- package/docs/interfaces/ActionQuery.html +0 -4
- package/docs/interfaces/ActionResponse.html +0 -8
- package/docs/interfaces/ActionsExtendedInfo.html +0 -10
- package/docs/interfaces/ActionsQuery.html +0 -5
- package/docs/interfaces/ActionsResponse.html +0 -6
- package/docs/interfaces/AggregateMetrics.html +0 -12
- package/docs/interfaces/BlockFacts.html +0 -10
- package/docs/interfaces/BlockQuery.html +0 -6
- package/docs/interfaces/BlockResponse.html +0 -6
- package/docs/interfaces/BlockSummary.html +0 -8
- package/docs/interfaces/BlockSummaryResponse.html +0 -6
- package/docs/interfaces/DeltaEvent.html +0 -6
- package/docs/interfaces/DepositSplParams.html +0 -8
- package/docs/interfaces/Info.html +0 -3
- package/docs/interfaces/Market.html +0 -8
- package/docs/interfaces/MarketStats.html +0 -7
- package/docs/interfaces/MarketsStatsResponse.html +0 -2
- package/docs/interfaces/NordConfig.html +0 -24
- package/docs/interfaces/NordWebSocketClientEvents.html +0 -4
- package/docs/interfaces/NordWebSocketEvents.html +0 -8
- package/docs/interfaces/Order.html +0 -6
- package/docs/interfaces/OrderInfo.html +0 -6
- package/docs/interfaces/OrderbookEntry.html +0 -4
- package/docs/interfaces/OrderbookQuery.html +0 -6
- package/docs/interfaces/OrderbookResponse.html +0 -6
- package/docs/interfaces/OrderbookSubscription.html +0 -159
- package/docs/interfaces/PerpMarketStats.html +0 -5
- package/docs/interfaces/RollmanActionExtendedInfo.html +0 -4
- package/docs/interfaces/RollmanActionInfo.html +0 -4
- package/docs/interfaces/RollmanActionResponse.html +0 -4
- package/docs/interfaces/RollmanActionsResponse.html +0 -2
- package/docs/interfaces/RollmanBlockResponse.html +0 -3
- package/docs/interfaces/SPLTokenInfo.html +0 -10
- package/docs/interfaces/SolanaBridgeConfig.html +0 -12
- package/docs/interfaces/StateFacts.html +0 -10
- package/docs/interfaces/SubscriberConfig.html +0 -3
- package/docs/interfaces/TimestampResponse.html +0 -3
- package/docs/interfaces/Token.html +0 -5
- package/docs/interfaces/TokenInfo.html +0 -6
- package/docs/interfaces/Trade.html +0 -5
- package/docs/interfaces/TradeSubscription.html +0 -159
- package/docs/interfaces/Trades.html +0 -5
- package/docs/interfaces/TradesQuery.html +0 -6
- package/docs/interfaces/TradesResponse.html +0 -7
- package/docs/interfaces/TransferParams.html +0 -8
- package/docs/interfaces/UserAccountIdsQuery.html +0 -3
- package/docs/interfaces/UserAccountIdsResponse.html +0 -3
- package/docs/interfaces/WebSocketAccountUpdate.html +0 -6
- package/docs/interfaces/WebSocketDeltaUpdate.html +0 -9
- package/docs/interfaces/WebSocketSubscription.html +0 -4
- package/docs/interfaces/WebSocketTradeUpdate.html +0 -6
- package/docs/interfaces/WithdrawalClaim.html +0 -14
- package/docs/interfaces/WithdrawalParams.html +0 -8
- package/docs/modules.html +0 -1
- package/docs/types/BigIntValue.html +0 -2
- package/docs/types/SubscriptionPattern.html +0 -4
- package/docs/types/SubscriptionType.html +0 -2
- package/docs/types/WebSocketMessage.html +0 -1
- package/docs/variables/DEBUG_KEYS.html +0 -1
- package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +0 -1
- package/docs/variables/DEV_TOKEN_INFOS.html +0 -1
- package/docs/variables/DEV_URL.html +0 -1
- package/docs/variables/MAX_BUFFER_LEN.html +0 -1
- package/docs/variables/SESSION_TTL.html +0 -1
- package/docs/variables/WEBSERVER_DEV_URL.html +0 -1
- package/docs/variables/ZERO_DECIMAL.html +0 -1
- package/docs/variables/_private.html +0 -2
- package/idl-generate.sh +0 -4
- package/src/bridge/client.ts +0 -498
- package/src/bridge/const.ts +0 -53
- package/src/bridge/index.ts +0 -6
- package/src/bridge/types.ts +0 -129
- package/src/bridge/utils.ts +0 -140
- package/src/gen/common.ts +0 -297
- package/src/gen/nord.ts +0 -6716
- package/src/idl/index.ts +0 -2
package/src/bridge/client.ts
DELETED
|
@@ -1,498 +0,0 @@
|
|
|
1
|
-
import * as anchor from "@coral-xyz/anchor";
|
|
2
|
-
import { AnchorProvider, Idl } from "@coral-xyz/anchor";
|
|
3
|
-
import {
|
|
4
|
-
createAssociatedTokenAccountInstruction,
|
|
5
|
-
getAssociatedTokenAddress,
|
|
6
|
-
TOKEN_2022_PROGRAM_ID,
|
|
7
|
-
} from "@solana/spl-token";
|
|
8
|
-
import {
|
|
9
|
-
Connection,
|
|
10
|
-
Keypair,
|
|
11
|
-
PublicKey,
|
|
12
|
-
SystemProgram,
|
|
13
|
-
Transaction,
|
|
14
|
-
} from "@solana/web3.js";
|
|
15
|
-
import {
|
|
16
|
-
BlockFacts,
|
|
17
|
-
DepositSplParams,
|
|
18
|
-
PdaSeedType,
|
|
19
|
-
SolanaBridgeConfig,
|
|
20
|
-
WithdrawalParams,
|
|
21
|
-
} from "./types";
|
|
22
|
-
import { BRIDGE_IDL } from "../idl";
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Solana Bridge Client for interacting with the bridge program
|
|
26
|
-
*/
|
|
27
|
-
export class SolanaBridgeClient {
|
|
28
|
-
/** Anchor program instance */
|
|
29
|
-
program: any;
|
|
30
|
-
/** Solana connection */
|
|
31
|
-
connection: Connection;
|
|
32
|
-
/** Program ID */
|
|
33
|
-
programId: PublicKey;
|
|
34
|
-
/** Configuration */
|
|
35
|
-
config: SolanaBridgeConfig;
|
|
36
|
-
/** Anchor provider */
|
|
37
|
-
provider: AnchorProvider;
|
|
38
|
-
|
|
39
|
-
bridge: PublicKey;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Create a new Solana Bridge Client
|
|
43
|
-
*
|
|
44
|
-
* @param config Bridge configuration
|
|
45
|
-
* @param wallet Anchor wallet for signing transactions
|
|
46
|
-
*/
|
|
47
|
-
constructor(config: SolanaBridgeConfig, wallet: anchor.Wallet) {
|
|
48
|
-
this.config = config;
|
|
49
|
-
this.connection = new Connection(config.rpcUrl, {
|
|
50
|
-
commitment: config.commitment,
|
|
51
|
-
});
|
|
52
|
-
this.programId = new PublicKey(config.programId);
|
|
53
|
-
|
|
54
|
-
// Create the provider
|
|
55
|
-
const provider = new AnchorProvider(this.connection, wallet, {
|
|
56
|
-
commitment: config.commitment,
|
|
57
|
-
skipPreflight: true, // Skip simulation
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
// Set the provider globally
|
|
61
|
-
anchor.setProvider(provider);
|
|
62
|
-
|
|
63
|
-
// Store the provider
|
|
64
|
-
this.provider = provider;
|
|
65
|
-
|
|
66
|
-
this.program = new anchor.Program(
|
|
67
|
-
{ ...(BRIDGE_IDL as Idl), address: config.programId },
|
|
68
|
-
provider,
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
this.bridge = new PublicKey(config.bridgeVk);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Derive a PDA (Program Derived Address) for the given seeds
|
|
76
|
-
*
|
|
77
|
-
* Seeds can be of type:
|
|
78
|
-
* - Buffer: raw bytes
|
|
79
|
-
* - PublicKey: Solana public key
|
|
80
|
-
* - string: string encoded as UTF-8 bytes
|
|
81
|
-
* - number: u64 encoded as little-endian 8 bytes
|
|
82
|
-
*
|
|
83
|
-
* @param type PDA seed type
|
|
84
|
-
* @param seeds Additional seeds
|
|
85
|
-
* @returns [PDA, bump]
|
|
86
|
-
*/
|
|
87
|
-
async findPda(
|
|
88
|
-
type: PdaSeedType,
|
|
89
|
-
...seeds: any[]
|
|
90
|
-
): Promise<[PublicKey, number]> {
|
|
91
|
-
const seedBuffers = [
|
|
92
|
-
Buffer.from(type),
|
|
93
|
-
...seeds.map((seed) => {
|
|
94
|
-
if (seed instanceof PublicKey) {
|
|
95
|
-
return seed.toBuffer();
|
|
96
|
-
} else if (typeof seed === "string") {
|
|
97
|
-
return Buffer.from(seed);
|
|
98
|
-
} else if (typeof seed === "number") {
|
|
99
|
-
// Convert number to little-endian byte array (8 bytes for u64)
|
|
100
|
-
const buffer = Buffer.alloc(8);
|
|
101
|
-
buffer.writeBigUInt64LE(BigInt(seed), 0);
|
|
102
|
-
return buffer;
|
|
103
|
-
} else {
|
|
104
|
-
return seed.toBytes();
|
|
105
|
-
}
|
|
106
|
-
}),
|
|
107
|
-
];
|
|
108
|
-
|
|
109
|
-
return PublicKey.findProgramAddressSync(seedBuffers, this.programId);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Find the asset config PDA for a token mint
|
|
114
|
-
*
|
|
115
|
-
* @param mint Token mint address
|
|
116
|
-
* @returns [PDA, bump]
|
|
117
|
-
*/
|
|
118
|
-
async findAssetConfigPda(mint: PublicKey): Promise<[PublicKey, number]> {
|
|
119
|
-
return this.findPda(PdaSeedType.AssetConfig, this.bridge, mint);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Find the deposit storage PDA for a deposit index
|
|
124
|
-
*
|
|
125
|
-
* @param depositIndex Deposit index
|
|
126
|
-
* @returns [PDA, bump]
|
|
127
|
-
*/
|
|
128
|
-
async findDepositStoragePda(
|
|
129
|
-
depositIndex: number,
|
|
130
|
-
): Promise<[PublicKey, number]> {
|
|
131
|
-
return this.findPda(PdaSeedType.DepositStorage, this.bridge, depositIndex);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Find the block storage PDA for a block ID
|
|
136
|
-
*
|
|
137
|
-
* @param blockId Block ID
|
|
138
|
-
* @returns [PDA, bump]
|
|
139
|
-
*/
|
|
140
|
-
async findBlockStoragePda(blockId: number): Promise<[PublicKey, number]> {
|
|
141
|
-
return this.findPda(PdaSeedType.BlockStorage, this.bridge, blockId);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Find the withdrawal nullifier PDA
|
|
146
|
-
*
|
|
147
|
-
* @param blockId Block ID
|
|
148
|
-
* @param leafIndex Leaf index
|
|
149
|
-
* @returns [PDA, bump]
|
|
150
|
-
*/
|
|
151
|
-
async findWithdrawalNullifierPda(
|
|
152
|
-
blockId: number,
|
|
153
|
-
leafIndex: number,
|
|
154
|
-
): Promise<[PublicKey, number]> {
|
|
155
|
-
return this.findPda(PdaSeedType.WithdrawalNullifier, blockId, leafIndex);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Find the authority PDA
|
|
160
|
-
*
|
|
161
|
-
* @returns [PDA, bump]
|
|
162
|
-
*/
|
|
163
|
-
async findAuthorityPda(): Promise<[PublicKey, number]> {
|
|
164
|
-
return this.findPda(PdaSeedType.Authority);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Helper method to sign and send a transaction
|
|
169
|
-
*
|
|
170
|
-
* @param transaction Transaction to sign and send
|
|
171
|
-
* @param signers Additional signers (beyond the provider's wallet)
|
|
172
|
-
* @returns Transaction signature
|
|
173
|
-
*/
|
|
174
|
-
private async signAndSendTransaction(
|
|
175
|
-
transaction: Transaction,
|
|
176
|
-
signers: Keypair[] = [],
|
|
177
|
-
): Promise<string> {
|
|
178
|
-
// Use the provider to sign and send the transaction with skipPreflight
|
|
179
|
-
const txSignature = await this.provider.sendAndConfirm(
|
|
180
|
-
transaction,
|
|
181
|
-
signers,
|
|
182
|
-
{
|
|
183
|
-
skipPreflight: true,
|
|
184
|
-
},
|
|
185
|
-
);
|
|
186
|
-
return txSignature;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Deposit SPL tokens to the bridge
|
|
191
|
-
*
|
|
192
|
-
* @param params Deposit parameters
|
|
193
|
-
* @param signer Signer keypair
|
|
194
|
-
* @returns Transaction signature
|
|
195
|
-
*/
|
|
196
|
-
async depositSpl(params: DepositSplParams): Promise<string> {
|
|
197
|
-
const [assetConfig] = await this.findAssetConfigPda(params.mint);
|
|
198
|
-
|
|
199
|
-
// Get the last deposit index from contract storage
|
|
200
|
-
const bridgeAccount = await this.program.account.bridge.fetch(this.bridge);
|
|
201
|
-
const lastDepositIndex = bridgeAccount.lastDepositIndex.toNumber();
|
|
202
|
-
|
|
203
|
-
// Find the deposit PDA for this deposit
|
|
204
|
-
const [deposit] = await this.findDepositStoragePda(lastDepositIndex + 1);
|
|
205
|
-
|
|
206
|
-
// Find the previous deposit PDA if it exists
|
|
207
|
-
let prevDeposit: PublicKey | undefined = undefined;
|
|
208
|
-
if (lastDepositIndex > 0) {
|
|
209
|
-
const [prevDepositPda] =
|
|
210
|
-
await this.findDepositStoragePda(lastDepositIndex);
|
|
211
|
-
prevDeposit = prevDepositPda;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const tokenAuthority = (
|
|
215
|
-
await this.findPda(PdaSeedType.TokenAuthority, this.bridge)
|
|
216
|
-
)[0];
|
|
217
|
-
const crumbAuthority = (
|
|
218
|
-
await this.findPda(PdaSeedType.CrumbAuthority, this.bridge)
|
|
219
|
-
)[0];
|
|
220
|
-
|
|
221
|
-
const toAccount = await getAssociatedTokenAddress(
|
|
222
|
-
params.mint,
|
|
223
|
-
tokenAuthority,
|
|
224
|
-
true,
|
|
225
|
-
TOKEN_2022_PROGRAM_ID,
|
|
226
|
-
);
|
|
227
|
-
|
|
228
|
-
// Build the transaction
|
|
229
|
-
const accounts: any = {
|
|
230
|
-
payer: this.provider.wallet.publicKey,
|
|
231
|
-
deposit,
|
|
232
|
-
assetConfig,
|
|
233
|
-
bridge: this.bridge,
|
|
234
|
-
program: this.programId,
|
|
235
|
-
fromAccount: params.fromAccount,
|
|
236
|
-
toAccount: toAccount,
|
|
237
|
-
tokenProgram: TOKEN_2022_PROGRAM_ID,
|
|
238
|
-
systemProgram: SystemProgram.programId,
|
|
239
|
-
crumbAuthority,
|
|
240
|
-
tokenAuthority,
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
const allAccounts = { ...accounts, prevDeposit: prevDeposit };
|
|
244
|
-
|
|
245
|
-
// Create the transaction
|
|
246
|
-
const tx = await (
|
|
247
|
-
await this.program.methods
|
|
248
|
-
.deposit(this.provider.wallet.publicKey, params.amount)
|
|
249
|
-
.accounts(allAccounts)
|
|
250
|
-
).transaction();
|
|
251
|
-
|
|
252
|
-
// Check if toAccount is initialized and add preinstruction if needed
|
|
253
|
-
try {
|
|
254
|
-
await this.connection.getTokenAccountBalance(toAccount);
|
|
255
|
-
} catch {
|
|
256
|
-
// Account doesn't exist, add instruction to create it
|
|
257
|
-
// Get the authority PDA which is the owner of the toAccount
|
|
258
|
-
const [authority] = await this.findAuthorityPda();
|
|
259
|
-
|
|
260
|
-
const createTokenAccountIx = createAssociatedTokenAccountInstruction(
|
|
261
|
-
this.provider.wallet.publicKey, // payer
|
|
262
|
-
toAccount, // associated token account address
|
|
263
|
-
authority, // owner of the token account
|
|
264
|
-
params.mint, // token mint
|
|
265
|
-
TOKEN_2022_PROGRAM_ID,
|
|
266
|
-
);
|
|
267
|
-
|
|
268
|
-
// Add the instruction to the beginning of the transaction
|
|
269
|
-
tx.instructions.unshift(createTokenAccountIx);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// Sign and send the transaction using our helper method
|
|
273
|
-
return await this.signAndSendTransaction(tx);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Withdraw tokens from the bridge
|
|
278
|
-
*
|
|
279
|
-
* @param params Withdrawal parameters
|
|
280
|
-
* @param signer Signer keypair
|
|
281
|
-
* @returns Transaction signature
|
|
282
|
-
*/
|
|
283
|
-
async withdraw(params: WithdrawalParams, signer: Keypair): Promise<string> {
|
|
284
|
-
const [stateUpdate] = await this.findBlockStoragePda(
|
|
285
|
-
params.claim.blockId.toNumber(),
|
|
286
|
-
);
|
|
287
|
-
const [withdrawalNullifier] = await this.findWithdrawalNullifierPda(
|
|
288
|
-
params.claim.blockId.toNumber(),
|
|
289
|
-
params.claim.leafIndex.toNumber(),
|
|
290
|
-
);
|
|
291
|
-
const [authority] = await this.findAuthorityPda();
|
|
292
|
-
|
|
293
|
-
// Build the transaction
|
|
294
|
-
const tx = await this.program.methods
|
|
295
|
-
.withdraw(params.claim)
|
|
296
|
-
.accounts({
|
|
297
|
-
payer: signer.publicKey,
|
|
298
|
-
stateUpdate,
|
|
299
|
-
withdrawalNullifier,
|
|
300
|
-
fromAccount: params.fromAccount,
|
|
301
|
-
toAccount: params.toAccount,
|
|
302
|
-
authority,
|
|
303
|
-
tokenProgram: TOKEN_2022_PROGRAM_ID,
|
|
304
|
-
systemProgram: SystemProgram.programId,
|
|
305
|
-
})
|
|
306
|
-
.transaction();
|
|
307
|
-
|
|
308
|
-
// Sign and send the transaction using our helper method
|
|
309
|
-
return await this.signAndSendTransaction(tx, [signer]);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Whitelist an asset (token) for use with the bridge
|
|
314
|
-
*
|
|
315
|
-
* @param mint Token mint address
|
|
316
|
-
* @param signer Operator keypair
|
|
317
|
-
* @returns Transaction signature
|
|
318
|
-
*/
|
|
319
|
-
async whitelistAsset(mint: PublicKey, signer: Keypair): Promise<string> {
|
|
320
|
-
const [assetWhitelisted] = await this.findAssetConfigPda(mint);
|
|
321
|
-
|
|
322
|
-
// Build the transaction
|
|
323
|
-
const tx = await this.program.methods
|
|
324
|
-
.whitelistAsset(mint)
|
|
325
|
-
.accounts({
|
|
326
|
-
operator: signer.publicKey,
|
|
327
|
-
bridge: this.bridge,
|
|
328
|
-
assetWhitelisted,
|
|
329
|
-
systemProgram: SystemProgram.programId,
|
|
330
|
-
})
|
|
331
|
-
.transaction();
|
|
332
|
-
|
|
333
|
-
// Sign and send the transaction using our helper method
|
|
334
|
-
return await this.signAndSendTransaction(tx, [signer]);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Propose a new block
|
|
339
|
-
*
|
|
340
|
-
* @param facts Block facts
|
|
341
|
-
* @param signer Operator keypair
|
|
342
|
-
* @returns Transaction signature
|
|
343
|
-
*/
|
|
344
|
-
async proposeBlock(facts: BlockFacts, signer: Keypair): Promise<string> {
|
|
345
|
-
// Get the last block ID from contract storage
|
|
346
|
-
const bridgeAccount = await this.program.account.bridge.fetch(this.bridge);
|
|
347
|
-
const [block] = await this.findBlockStoragePda(
|
|
348
|
-
bridgeAccount.lastBlockId.toNumber() + 1,
|
|
349
|
-
);
|
|
350
|
-
|
|
351
|
-
// Find the last deposit PDA
|
|
352
|
-
const [lastDeposit] = await this.findDepositStoragePda(
|
|
353
|
-
facts.nextStateFacts.lastDepositIndex.toNumber(),
|
|
354
|
-
);
|
|
355
|
-
|
|
356
|
-
// Find the DA fact state
|
|
357
|
-
const daFactState = PublicKey.findProgramAddressSync(
|
|
358
|
-
[Buffer.from(PdaSeedType.DaFactStorage), facts.daCommitment],
|
|
359
|
-
this.programId,
|
|
360
|
-
)[0];
|
|
361
|
-
|
|
362
|
-
// Build the transaction
|
|
363
|
-
const tx = await this.program.methods
|
|
364
|
-
.proposeBlock(facts)
|
|
365
|
-
.accounts({
|
|
366
|
-
operator: signer.publicKey,
|
|
367
|
-
block,
|
|
368
|
-
lastDeposit,
|
|
369
|
-
daFactState,
|
|
370
|
-
bridge: this.bridge,
|
|
371
|
-
systemProgram: SystemProgram.programId,
|
|
372
|
-
})
|
|
373
|
-
.transaction();
|
|
374
|
-
|
|
375
|
-
// Sign and send the transaction using our helper method
|
|
376
|
-
return await this.signAndSendTransaction(tx, [signer]);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* Finalize a block
|
|
381
|
-
*
|
|
382
|
-
* @param blockId Block ID
|
|
383
|
-
* @param stateUpdateId State update ID
|
|
384
|
-
* @param signer Payer keypair
|
|
385
|
-
* @returns Transaction signature
|
|
386
|
-
*/
|
|
387
|
-
async finalizeBlock(
|
|
388
|
-
blockId: number,
|
|
389
|
-
stateUpdateId: number,
|
|
390
|
-
signer: Keypair,
|
|
391
|
-
): Promise<string> {
|
|
392
|
-
const [block] = await this.findBlockStoragePda(blockId);
|
|
393
|
-
|
|
394
|
-
// Build the transaction
|
|
395
|
-
const tx = await this.program.methods
|
|
396
|
-
.finalizeBlock(new anchor.BN(stateUpdateId))
|
|
397
|
-
.accounts({
|
|
398
|
-
payer: signer.publicKey,
|
|
399
|
-
block,
|
|
400
|
-
bridge: this.bridge,
|
|
401
|
-
systemProgram: SystemProgram.programId,
|
|
402
|
-
})
|
|
403
|
-
.transaction();
|
|
404
|
-
|
|
405
|
-
// Sign and send the transaction using our helper method
|
|
406
|
-
return await this.signAndSendTransaction(tx, [signer]);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* Finalize a DA fact
|
|
411
|
-
*
|
|
412
|
-
* @param fact DA fact (32-byte array)
|
|
413
|
-
* @param signer Payer keypair
|
|
414
|
-
* @returns Transaction signature
|
|
415
|
-
*/
|
|
416
|
-
async finalizeDaFact(fact: Buffer, signer: Keypair): Promise<string> {
|
|
417
|
-
const factStateStorage = PublicKey.findProgramAddressSync(
|
|
418
|
-
[Buffer.from(PdaSeedType.DaFactStorage), fact],
|
|
419
|
-
this.programId,
|
|
420
|
-
)[0];
|
|
421
|
-
|
|
422
|
-
// Build the transaction
|
|
423
|
-
const tx = await this.program.methods
|
|
424
|
-
.finalizeDaFact(Array.from(fact))
|
|
425
|
-
.accounts({
|
|
426
|
-
payer: signer.publicKey,
|
|
427
|
-
factStateStorage,
|
|
428
|
-
systemProgram: SystemProgram.programId,
|
|
429
|
-
})
|
|
430
|
-
.transaction();
|
|
431
|
-
|
|
432
|
-
// Sign and send the transaction using our helper method
|
|
433
|
-
return await this.signAndSendTransaction(tx, [signer]);
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* Initialize the bridge contract
|
|
438
|
-
*
|
|
439
|
-
* @param operator Operator public key
|
|
440
|
-
* @param initialAppStateCommitment Initial app state commitment (32-byte array)
|
|
441
|
-
* @param signer Payer keypair
|
|
442
|
-
* @returns Transaction signature
|
|
443
|
-
*/
|
|
444
|
-
async initialize(
|
|
445
|
-
operator: PublicKey,
|
|
446
|
-
initialAppStateCommitment: Buffer,
|
|
447
|
-
signer: Keypair,
|
|
448
|
-
): Promise<string> {
|
|
449
|
-
// Build the transaction
|
|
450
|
-
const tx = await this.program.methods
|
|
451
|
-
.initialize(operator, Array.from(initialAppStateCommitment))
|
|
452
|
-
.accounts({
|
|
453
|
-
payer: signer.publicKey,
|
|
454
|
-
program: this.programId,
|
|
455
|
-
bridge: this.bridge,
|
|
456
|
-
systemProgram: SystemProgram.programId,
|
|
457
|
-
})
|
|
458
|
-
.transaction();
|
|
459
|
-
|
|
460
|
-
// Sign and send the transaction using our helper method
|
|
461
|
-
return await this.signAndSendTransaction(tx, [signer]);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
/**
|
|
465
|
-
* Create an associated token account if it doesn't exist
|
|
466
|
-
*
|
|
467
|
-
* @param mint Token mint
|
|
468
|
-
* @param owner Account owner
|
|
469
|
-
* @param payer Transaction payer
|
|
470
|
-
* @returns Associated token account address
|
|
471
|
-
*/
|
|
472
|
-
async createTokenAccountIfNeeded(
|
|
473
|
-
mint: PublicKey,
|
|
474
|
-
owner: PublicKey,
|
|
475
|
-
payer: Keypair,
|
|
476
|
-
): Promise<PublicKey> {
|
|
477
|
-
const associatedTokenAddress = await getAssociatedTokenAddress(mint, owner);
|
|
478
|
-
|
|
479
|
-
try {
|
|
480
|
-
await this.connection.getTokenAccountBalance(associatedTokenAddress);
|
|
481
|
-
} catch {
|
|
482
|
-
// Account doesn't exist, create it
|
|
483
|
-
const transaction = new Transaction().add(
|
|
484
|
-
createAssociatedTokenAccountInstruction(
|
|
485
|
-
payer.publicKey,
|
|
486
|
-
associatedTokenAddress,
|
|
487
|
-
owner,
|
|
488
|
-
mint,
|
|
489
|
-
),
|
|
490
|
-
);
|
|
491
|
-
|
|
492
|
-
// Sign and send the transaction using our helper method
|
|
493
|
-
await this.signAndSendTransaction(transaction, [payer]);
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
return associatedTokenAddress;
|
|
497
|
-
}
|
|
498
|
-
}
|
package/src/bridge/const.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { SPLTokenInfo } from "./types";
|
|
2
|
-
import { Commitment } from "@solana/web3.js";
|
|
3
|
-
|
|
4
|
-
// Solana RPC endpoints
|
|
5
|
-
export const SOLANA_MAINNET_URL = "https://api.mainnet-beta.solana.com";
|
|
6
|
-
export const SOLANA_DEVNET_URL = "https://api.devnet.solana.com";
|
|
7
|
-
export const SOLANA_TESTNET_URL = "https://api.testnet.solana.com";
|
|
8
|
-
export const SOLANA_LOCALNET_URL = "http://localhost:8899";
|
|
9
|
-
|
|
10
|
-
// Default commitment level
|
|
11
|
-
export const DEFAULT_COMMITMENT: Commitment = "confirmed";
|
|
12
|
-
|
|
13
|
-
// Webserver configuration
|
|
14
|
-
const WEBSERVER_PORT = "80";
|
|
15
|
-
export const DEV_URL = "http://localhost";
|
|
16
|
-
export const WEBSERVER_DEV_URL = DEV_URL + ":" + WEBSERVER_PORT;
|
|
17
|
-
|
|
18
|
-
// Program ID (will be supplied externally)
|
|
19
|
-
export const SOLANA_PROGRAM_ID = "CVDFLCAjXhVWiPXH9nTCTpCgVzmDVoiPzNJYuccr1dqB";
|
|
20
|
-
|
|
21
|
-
// Sample token information for development
|
|
22
|
-
export const DEV_TOKEN_INFOS: SPLTokenInfo[] = [
|
|
23
|
-
{
|
|
24
|
-
mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC on devnet
|
|
25
|
-
precision: 6,
|
|
26
|
-
tokenId: 0,
|
|
27
|
-
name: "usdc",
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
mint: "So11111111111111111111111111111111111111112", // Wrapped SOL
|
|
31
|
-
precision: 9,
|
|
32
|
-
tokenId: 1,
|
|
33
|
-
name: "sol",
|
|
34
|
-
},
|
|
35
|
-
];
|
|
36
|
-
|
|
37
|
-
// Default funding amounts for development
|
|
38
|
-
export const DEFAULT_FUNDING_AMOUNTS: { [key: string]: [string, number] } = {
|
|
39
|
-
SOL: ["0.2", 9],
|
|
40
|
-
// USDC mint address
|
|
41
|
-
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: ["10000", 6],
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
// PDA seed constants
|
|
45
|
-
export const PDA_SEEDS = {
|
|
46
|
-
CONTRACT_STORAGE: "contract_storage",
|
|
47
|
-
ASSET_WHITELISTED: "asset_whitelisted",
|
|
48
|
-
DEPOSIT_STORAGE: "deposit_storage",
|
|
49
|
-
BLOCK_STORAGE: "block_storage",
|
|
50
|
-
WITHDRAWAL_NULLIFIER: "withdrawal_nullifier",
|
|
51
|
-
AUTHORITY: "authority",
|
|
52
|
-
DA_FACT_STORAGE: "da_fact_storage",
|
|
53
|
-
};
|
package/src/bridge/index.ts
DELETED
package/src/bridge/types.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { PublicKey } from "@solana/web3.js";
|
|
2
|
-
import { BN } from "@coral-xyz/anchor";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Configuration for Solana bridge
|
|
6
|
-
*/
|
|
7
|
-
export interface SolanaBridgeConfig {
|
|
8
|
-
/** Solana RPC URL */
|
|
9
|
-
rpcUrl: string;
|
|
10
|
-
/** Program ID for the bridge program */
|
|
11
|
-
programId: string;
|
|
12
|
-
/** Bridge Verifying Key */
|
|
13
|
-
bridgeVk: string;
|
|
14
|
-
/** Commitment level for transactions */
|
|
15
|
-
commitment: "processed" | "confirmed" | "finalized";
|
|
16
|
-
/** Token information for supported SPL tokens */
|
|
17
|
-
tokenInfos: SPLTokenInfo[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Information about an SPL token
|
|
22
|
-
*/
|
|
23
|
-
export interface SPLTokenInfo {
|
|
24
|
-
/** Token mint address */
|
|
25
|
-
mint: string;
|
|
26
|
-
/** Token decimals */
|
|
27
|
-
precision: number;
|
|
28
|
-
/** Token ID in the Layer-N system */
|
|
29
|
-
tokenId: number;
|
|
30
|
-
/** Token name */
|
|
31
|
-
name: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Parameters for depositing SPL tokens
|
|
36
|
-
*/
|
|
37
|
-
export interface DepositSplParams {
|
|
38
|
-
/** Amount to deposit (in token's smallest unit) */
|
|
39
|
-
amount: BN;
|
|
40
|
-
/** Mint address of the token to deposit */
|
|
41
|
-
mint: PublicKey;
|
|
42
|
-
/** User's token account to deposit from */
|
|
43
|
-
fromAccount: PublicKey;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Parameters for withdrawing tokens
|
|
48
|
-
*/
|
|
49
|
-
export interface WithdrawalParams {
|
|
50
|
-
/** Withdrawal claim data */
|
|
51
|
-
claim: WithdrawalClaim;
|
|
52
|
-
/** User's token account to receive tokens */
|
|
53
|
-
toAccount: PublicKey;
|
|
54
|
-
/** Bridge's token account to withdraw from */
|
|
55
|
-
fromAccount: PublicKey;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Withdrawal claim data
|
|
60
|
-
*/
|
|
61
|
-
export interface WithdrawalClaim {
|
|
62
|
-
/** Block ID containing the withdrawal */
|
|
63
|
-
blockId: BN;
|
|
64
|
-
/** Leaf index in the Merkle tree */
|
|
65
|
-
leafIndex: BN;
|
|
66
|
-
/** Amount to withdraw */
|
|
67
|
-
amount: BN;
|
|
68
|
-
/** Recipient address */
|
|
69
|
-
recipient: PublicKey;
|
|
70
|
-
/** Token mint address */
|
|
71
|
-
mint: PublicKey;
|
|
72
|
-
/** Merkle proof for the withdrawal */
|
|
73
|
-
proof: Buffer[];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Block facts data structure
|
|
78
|
-
*/
|
|
79
|
-
export interface BlockFacts {
|
|
80
|
-
/** Previous state facts */
|
|
81
|
-
prevStateFacts: StateFacts;
|
|
82
|
-
/** Next state facts */
|
|
83
|
-
nextStateFacts: StateFacts;
|
|
84
|
-
/** Data availability commitment */
|
|
85
|
-
daCommitment: Buffer;
|
|
86
|
-
/** Withdrawal Merkle root */
|
|
87
|
-
withdrawalRoot: Buffer;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* State facts data structure
|
|
92
|
-
*/
|
|
93
|
-
export interface StateFacts {
|
|
94
|
-
/** Application state commitment */
|
|
95
|
-
appStateCommitment: Buffer;
|
|
96
|
-
/** Deposit root */
|
|
97
|
-
depositRoot: Buffer;
|
|
98
|
-
/** Last deposit index */
|
|
99
|
-
lastDepositIndex: BN;
|
|
100
|
-
/** Last action ID */
|
|
101
|
-
lastActionId: BN;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Transfer parameters
|
|
106
|
-
*/
|
|
107
|
-
export interface TransferParams {
|
|
108
|
-
/** User public key */
|
|
109
|
-
user: PublicKey;
|
|
110
|
-
/** Token mint */
|
|
111
|
-
mint: PublicKey;
|
|
112
|
-
/** Amount to transfer */
|
|
113
|
-
amount: BN;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* PDA seed types
|
|
118
|
-
*/
|
|
119
|
-
export enum PdaSeedType {
|
|
120
|
-
Bridge = "bridge",
|
|
121
|
-
TokenAuthority = "token_authority",
|
|
122
|
-
CrumbAuthority = "crumb_authority",
|
|
123
|
-
AssetConfig = "asset_config",
|
|
124
|
-
DepositStorage = "deposit_storage",
|
|
125
|
-
BlockStorage = "block_storage",
|
|
126
|
-
WithdrawalNullifier = "withdrawal_nullifier",
|
|
127
|
-
Authority = "authority",
|
|
128
|
-
DaFactStorage = "da_fact_storage",
|
|
129
|
-
}
|