@n1xyz/nord-ts 0.0.21 → 0.0.22
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/api/client.d.ts +14 -0
- package/dist/api/client.js +45 -0
- package/dist/bridge/client.d.ts +151 -0
- package/dist/bridge/client.js +434 -0
- package/dist/bridge/const.d.ts +23 -0
- package/dist/bridge/const.js +47 -0
- package/dist/bridge/index.d.ts +4 -0
- package/dist/bridge/index.js +23 -0
- package/dist/bridge/types.d.ts +120 -0
- package/dist/bridge/types.js +18 -0
- package/dist/bridge/utils.d.ts +64 -0
- package/dist/bridge/utils.js +131 -0
- package/dist/gen/common.d.ts +68 -0
- package/dist/gen/common.js +215 -0
- package/dist/gen/nord_pb.d.ts +3651 -0
- package/dist/gen/nord_pb.js +892 -0
- package/dist/gen/openapi.d.ts +241 -2
- package/dist/idl/bridge.d.ts +569 -0
- package/dist/idl/bridge.js +8 -0
- package/dist/idl/bridge.json +1506 -0
- package/dist/idl/index.d.ts +607 -0
- package/dist/idl/index.js +8 -0
- package/dist/nord/api/actions.d.ts +30 -72
- package/dist/nord/api/actions.js +179 -200
- package/dist/nord/api/market.d.ts +36 -0
- package/dist/nord/api/market.js +96 -0
- package/dist/nord/api/queries.d.ts +46 -0
- package/dist/nord/api/queries.js +109 -0
- package/dist/nord/client/Nord.js +3 -3
- package/dist/nord/client/NordUser.d.ts +26 -13
- package/dist/nord/client/NordUser.js +13 -10
- package/dist/types.d.ts +12 -1
- package/dist/types.js +29 -2
- package/dist/utils.d.ts +6 -20
- package/dist/utils.js +17 -35
- package/dist/websocket/NordWebSocketClient.js +2 -6
- package/package.json +26 -23
- package/src/gen/nord_pb.ts +4172 -0
- package/src/gen/openapi.ts +241 -2
- package/src/nord/api/actions.ts +249 -370
- package/src/nord/client/Nord.ts +3 -3
- package/src/nord/client/NordUser.ts +40 -19
- package/src/types.ts +32 -1
- package/src/utils.ts +24 -43
- package/src/websocket/NordWebSocketClient.ts +2 -8
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { paths } from "../gen/openapi";
|
|
2
|
+
export interface ClientConfig {
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
}
|
|
5
|
+
export declare function createApiClient(config: ClientConfig): import("openapi-fetch").Client<paths, `${string}/${string}`>;
|
|
6
|
+
export declare function handleApiResponse<T>(response: Promise<{
|
|
7
|
+
data: T;
|
|
8
|
+
error?: unknown;
|
|
9
|
+
response: Response;
|
|
10
|
+
} | {
|
|
11
|
+
data?: never;
|
|
12
|
+
error?: unknown;
|
|
13
|
+
response: Response;
|
|
14
|
+
}>): Promise<T>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createApiClient = createApiClient;
|
|
7
|
+
exports.handleApiResponse = handleApiResponse;
|
|
8
|
+
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
|
|
9
|
+
const NordError_1 = require("../nord/utils/NordError");
|
|
10
|
+
function createApiClient(config) {
|
|
11
|
+
const client = (0, openapi_fetch_1.default)({
|
|
12
|
+
baseUrl: config.baseUrl,
|
|
13
|
+
});
|
|
14
|
+
client.use({
|
|
15
|
+
onResponse({ response }) {
|
|
16
|
+
if (!response.ok) {
|
|
17
|
+
throw new NordError_1.NordError(`HTTP ${response.status}: ${response.statusText}`, {
|
|
18
|
+
statusCode: response.status,
|
|
19
|
+
details: { url: response.url }
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return response;
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
return client;
|
|
26
|
+
}
|
|
27
|
+
async function handleApiResponse(response) {
|
|
28
|
+
const result = await response;
|
|
29
|
+
if (result.error || !result.response.ok) {
|
|
30
|
+
const errorMessage = result.error
|
|
31
|
+
? `HTTP ${result.response.status}: ${result.response.statusText}`
|
|
32
|
+
: `Request failed: ${result.response.statusText}`;
|
|
33
|
+
throw new NordError_1.NordError(errorMessage, {
|
|
34
|
+
statusCode: result.response.status,
|
|
35
|
+
details: { url: result.response.url }
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (!('data' in result)) {
|
|
39
|
+
throw new NordError_1.NordError("No data in response", {
|
|
40
|
+
statusCode: 500,
|
|
41
|
+
details: { url: result.response.url }
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return result.data;
|
|
45
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
2
|
+
import { AnchorProvider } from "@coral-xyz/anchor";
|
|
3
|
+
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
|
|
4
|
+
import { BlockFacts, DepositSplParams, PdaSeedType, SolanaBridgeConfig, WithdrawalParams } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* Solana Bridge Client for interacting with the bridge program
|
|
7
|
+
*/
|
|
8
|
+
export declare class SolanaBridgeClient {
|
|
9
|
+
/** Anchor program instance */
|
|
10
|
+
program: any;
|
|
11
|
+
/** Solana connection */
|
|
12
|
+
connection: Connection;
|
|
13
|
+
/** Program ID */
|
|
14
|
+
programId: PublicKey;
|
|
15
|
+
/** Configuration */
|
|
16
|
+
config: SolanaBridgeConfig;
|
|
17
|
+
/** Anchor provider */
|
|
18
|
+
provider: AnchorProvider;
|
|
19
|
+
bridge: PublicKey;
|
|
20
|
+
/**
|
|
21
|
+
* Create a new Solana Bridge Client
|
|
22
|
+
*
|
|
23
|
+
* @param config Bridge configuration
|
|
24
|
+
* @param wallet Anchor wallet for signing transactions
|
|
25
|
+
*/
|
|
26
|
+
constructor(config: SolanaBridgeConfig, wallet: anchor.Wallet);
|
|
27
|
+
/**
|
|
28
|
+
* Derive a PDA (Program Derived Address) for the given seeds
|
|
29
|
+
*
|
|
30
|
+
* Seeds can be of type:
|
|
31
|
+
* - Buffer: raw bytes
|
|
32
|
+
* - PublicKey: Solana public key
|
|
33
|
+
* - string: string encoded as UTF-8 bytes
|
|
34
|
+
* - number: u64 encoded as little-endian 8 bytes
|
|
35
|
+
*
|
|
36
|
+
* @param type PDA seed type
|
|
37
|
+
* @param seeds Additional seeds
|
|
38
|
+
* @returns [PDA, bump]
|
|
39
|
+
*/
|
|
40
|
+
findPda(type: PdaSeedType, ...seeds: any[]): Promise<[PublicKey, number]>;
|
|
41
|
+
/**
|
|
42
|
+
* Find the asset config PDA for a token mint
|
|
43
|
+
*
|
|
44
|
+
* @param mint Token mint address
|
|
45
|
+
* @returns [PDA, bump]
|
|
46
|
+
*/
|
|
47
|
+
findAssetConfigPda(mint: PublicKey): Promise<[PublicKey, number]>;
|
|
48
|
+
/**
|
|
49
|
+
* Find the deposit storage PDA for a deposit index
|
|
50
|
+
*
|
|
51
|
+
* @param depositIndex Deposit index
|
|
52
|
+
* @returns [PDA, bump]
|
|
53
|
+
*/
|
|
54
|
+
findDepositStoragePda(depositIndex: number): Promise<[PublicKey, number]>;
|
|
55
|
+
/**
|
|
56
|
+
* Find the block storage PDA for a block ID
|
|
57
|
+
*
|
|
58
|
+
* @param blockId Block ID
|
|
59
|
+
* @returns [PDA, bump]
|
|
60
|
+
*/
|
|
61
|
+
findBlockStoragePda(blockId: number): Promise<[PublicKey, number]>;
|
|
62
|
+
/**
|
|
63
|
+
* Find the withdrawal nullifier PDA
|
|
64
|
+
*
|
|
65
|
+
* @param blockId Block ID
|
|
66
|
+
* @param leafIndex Leaf index
|
|
67
|
+
* @returns [PDA, bump]
|
|
68
|
+
*/
|
|
69
|
+
findWithdrawalNullifierPda(blockId: number, leafIndex: number): Promise<[PublicKey, number]>;
|
|
70
|
+
/**
|
|
71
|
+
* Find the authority PDA
|
|
72
|
+
*
|
|
73
|
+
* @returns [PDA, bump]
|
|
74
|
+
*/
|
|
75
|
+
findAuthorityPda(): Promise<[PublicKey, number]>;
|
|
76
|
+
/**
|
|
77
|
+
* Helper method to sign and send a transaction
|
|
78
|
+
*
|
|
79
|
+
* @param transaction Transaction to sign and send
|
|
80
|
+
* @param signers Additional signers (beyond the provider's wallet)
|
|
81
|
+
* @returns Transaction signature
|
|
82
|
+
*/
|
|
83
|
+
private signAndSendTransaction;
|
|
84
|
+
/**
|
|
85
|
+
* Deposit SPL tokens to the bridge
|
|
86
|
+
*
|
|
87
|
+
* @param params Deposit parameters
|
|
88
|
+
* @param signer Signer keypair
|
|
89
|
+
* @returns Transaction signature
|
|
90
|
+
*/
|
|
91
|
+
depositSpl(params: DepositSplParams): Promise<string>;
|
|
92
|
+
/**
|
|
93
|
+
* Withdraw tokens from the bridge
|
|
94
|
+
*
|
|
95
|
+
* @param params Withdrawal parameters
|
|
96
|
+
* @param signer Signer keypair
|
|
97
|
+
* @returns Transaction signature
|
|
98
|
+
*/
|
|
99
|
+
withdraw(params: WithdrawalParams, signer: Keypair): Promise<string>;
|
|
100
|
+
/**
|
|
101
|
+
* Whitelist an asset (token) for use with the bridge
|
|
102
|
+
*
|
|
103
|
+
* @param mint Token mint address
|
|
104
|
+
* @param signer Operator keypair
|
|
105
|
+
* @returns Transaction signature
|
|
106
|
+
*/
|
|
107
|
+
whitelistAsset(mint: PublicKey, signer: Keypair): Promise<string>;
|
|
108
|
+
/**
|
|
109
|
+
* Propose a new block
|
|
110
|
+
*
|
|
111
|
+
* @param facts Block facts
|
|
112
|
+
* @param signer Operator keypair
|
|
113
|
+
* @returns Transaction signature
|
|
114
|
+
*/
|
|
115
|
+
proposeBlock(facts: BlockFacts, signer: Keypair): Promise<string>;
|
|
116
|
+
/**
|
|
117
|
+
* Finalize a block
|
|
118
|
+
*
|
|
119
|
+
* @param blockId Block ID
|
|
120
|
+
* @param stateUpdateId State update ID
|
|
121
|
+
* @param signer Payer keypair
|
|
122
|
+
* @returns Transaction signature
|
|
123
|
+
*/
|
|
124
|
+
finalizeBlock(blockId: number, stateUpdateId: number, signer: Keypair): Promise<string>;
|
|
125
|
+
/**
|
|
126
|
+
* Finalize a DA fact
|
|
127
|
+
*
|
|
128
|
+
* @param fact DA fact (32-byte array)
|
|
129
|
+
* @param signer Payer keypair
|
|
130
|
+
* @returns Transaction signature
|
|
131
|
+
*/
|
|
132
|
+
finalizeDaFact(fact: Buffer, signer: Keypair): Promise<string>;
|
|
133
|
+
/**
|
|
134
|
+
* Initialize the bridge contract
|
|
135
|
+
*
|
|
136
|
+
* @param operator Operator public key
|
|
137
|
+
* @param initialAppStateCommitment Initial app state commitment (32-byte array)
|
|
138
|
+
* @param signer Payer keypair
|
|
139
|
+
* @returns Transaction signature
|
|
140
|
+
*/
|
|
141
|
+
initialize(operator: PublicKey, initialAppStateCommitment: Buffer, signer: Keypair): Promise<string>;
|
|
142
|
+
/**
|
|
143
|
+
* Create an associated token account if it doesn't exist
|
|
144
|
+
*
|
|
145
|
+
* @param mint Token mint
|
|
146
|
+
* @param owner Account owner
|
|
147
|
+
* @param payer Transaction payer
|
|
148
|
+
* @returns Associated token account address
|
|
149
|
+
*/
|
|
150
|
+
createTokenAccountIfNeeded(mint: PublicKey, owner: PublicKey, payer: Keypair): Promise<PublicKey>;
|
|
151
|
+
}
|
|
@@ -0,0 +1,434 @@
|
|
|
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.SolanaBridgeClient = void 0;
|
|
37
|
+
const anchor = __importStar(require("@coral-xyz/anchor"));
|
|
38
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
39
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
40
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
41
|
+
const types_1 = require("./types");
|
|
42
|
+
const idl_1 = require("../idl");
|
|
43
|
+
/**
|
|
44
|
+
* Solana Bridge Client for interacting with the bridge program
|
|
45
|
+
*/
|
|
46
|
+
class SolanaBridgeClient {
|
|
47
|
+
/**
|
|
48
|
+
* Create a new Solana Bridge Client
|
|
49
|
+
*
|
|
50
|
+
* @param config Bridge configuration
|
|
51
|
+
* @param wallet Anchor wallet for signing transactions
|
|
52
|
+
*/
|
|
53
|
+
constructor(config, wallet) {
|
|
54
|
+
this.config = config;
|
|
55
|
+
this.connection = new web3_js_1.Connection(config.rpcUrl, {
|
|
56
|
+
commitment: config.commitment,
|
|
57
|
+
});
|
|
58
|
+
this.programId = new web3_js_1.PublicKey(config.programId);
|
|
59
|
+
// Create the provider
|
|
60
|
+
const provider = new anchor_1.AnchorProvider(this.connection, wallet, {
|
|
61
|
+
commitment: config.commitment,
|
|
62
|
+
skipPreflight: true, // Skip simulation
|
|
63
|
+
});
|
|
64
|
+
// Set the provider globally
|
|
65
|
+
anchor.setProvider(provider);
|
|
66
|
+
// Store the provider
|
|
67
|
+
this.provider = provider;
|
|
68
|
+
this.program = new anchor.Program({ ...idl_1.BRIDGE_IDL, address: config.programId }, provider);
|
|
69
|
+
this.bridge = new web3_js_1.PublicKey(config.bridgeVk);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Derive a PDA (Program Derived Address) for the given seeds
|
|
73
|
+
*
|
|
74
|
+
* Seeds can be of type:
|
|
75
|
+
* - Buffer: raw bytes
|
|
76
|
+
* - PublicKey: Solana public key
|
|
77
|
+
* - string: string encoded as UTF-8 bytes
|
|
78
|
+
* - number: u64 encoded as little-endian 8 bytes
|
|
79
|
+
*
|
|
80
|
+
* @param type PDA seed type
|
|
81
|
+
* @param seeds Additional seeds
|
|
82
|
+
* @returns [PDA, bump]
|
|
83
|
+
*/
|
|
84
|
+
async findPda(type, ...seeds) {
|
|
85
|
+
const seedBuffers = [
|
|
86
|
+
Buffer.from(type),
|
|
87
|
+
...seeds.map((seed) => {
|
|
88
|
+
if (seed instanceof web3_js_1.PublicKey) {
|
|
89
|
+
return seed.toBuffer();
|
|
90
|
+
}
|
|
91
|
+
else if (typeof seed === "string") {
|
|
92
|
+
return Buffer.from(seed);
|
|
93
|
+
}
|
|
94
|
+
else if (typeof seed === "number") {
|
|
95
|
+
// Convert number to little-endian byte array (8 bytes for u64)
|
|
96
|
+
const buffer = Buffer.alloc(8);
|
|
97
|
+
buffer.writeBigUInt64LE(BigInt(seed), 0);
|
|
98
|
+
return buffer;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
return seed.toBytes();
|
|
102
|
+
}
|
|
103
|
+
}),
|
|
104
|
+
];
|
|
105
|
+
return web3_js_1.PublicKey.findProgramAddressSync(seedBuffers, this.programId);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Find the asset config PDA for a token mint
|
|
109
|
+
*
|
|
110
|
+
* @param mint Token mint address
|
|
111
|
+
* @returns [PDA, bump]
|
|
112
|
+
*/
|
|
113
|
+
async findAssetConfigPda(mint) {
|
|
114
|
+
return this.findPda(types_1.PdaSeedType.AssetConfig, this.bridge, mint);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Find the deposit storage PDA for a deposit index
|
|
118
|
+
*
|
|
119
|
+
* @param depositIndex Deposit index
|
|
120
|
+
* @returns [PDA, bump]
|
|
121
|
+
*/
|
|
122
|
+
async findDepositStoragePda(depositIndex) {
|
|
123
|
+
return this.findPda(types_1.PdaSeedType.DepositStorage, this.bridge, depositIndex);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Find the block storage PDA for a block ID
|
|
127
|
+
*
|
|
128
|
+
* @param blockId Block ID
|
|
129
|
+
* @returns [PDA, bump]
|
|
130
|
+
*/
|
|
131
|
+
async findBlockStoragePda(blockId) {
|
|
132
|
+
return this.findPda(types_1.PdaSeedType.BlockStorage, this.bridge, blockId);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Find the withdrawal nullifier PDA
|
|
136
|
+
*
|
|
137
|
+
* @param blockId Block ID
|
|
138
|
+
* @param leafIndex Leaf index
|
|
139
|
+
* @returns [PDA, bump]
|
|
140
|
+
*/
|
|
141
|
+
async findWithdrawalNullifierPda(blockId, leafIndex) {
|
|
142
|
+
return this.findPda(types_1.PdaSeedType.WithdrawalNullifier, blockId, leafIndex);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Find the authority PDA
|
|
146
|
+
*
|
|
147
|
+
* @returns [PDA, bump]
|
|
148
|
+
*/
|
|
149
|
+
async findAuthorityPda() {
|
|
150
|
+
return this.findPda(types_1.PdaSeedType.Authority);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Helper method to sign and send a transaction
|
|
154
|
+
*
|
|
155
|
+
* @param transaction Transaction to sign and send
|
|
156
|
+
* @param signers Additional signers (beyond the provider's wallet)
|
|
157
|
+
* @returns Transaction signature
|
|
158
|
+
*/
|
|
159
|
+
async signAndSendTransaction(transaction, signers = []) {
|
|
160
|
+
// Use the provider to sign and send the transaction with skipPreflight
|
|
161
|
+
const txSignature = await this.provider.sendAndConfirm(transaction, signers, {
|
|
162
|
+
skipPreflight: true,
|
|
163
|
+
});
|
|
164
|
+
return txSignature;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Deposit SPL tokens to the bridge
|
|
168
|
+
*
|
|
169
|
+
* @param params Deposit parameters
|
|
170
|
+
* @param signer Signer keypair
|
|
171
|
+
* @returns Transaction signature
|
|
172
|
+
*/
|
|
173
|
+
async depositSpl(params) {
|
|
174
|
+
const [assetConfig] = await this.findAssetConfigPda(params.mint);
|
|
175
|
+
// Get the last deposit index from contract storage
|
|
176
|
+
const bridgeAccount = await this.program.account.bridge.fetch(this.bridge);
|
|
177
|
+
const lastDepositIndex = bridgeAccount.lastDepositIndex.toNumber();
|
|
178
|
+
// Find the deposit PDA for this deposit
|
|
179
|
+
const [deposit] = await this.findDepositStoragePda(lastDepositIndex + 1);
|
|
180
|
+
// Find the previous deposit PDA if it exists
|
|
181
|
+
let prevDeposit = undefined;
|
|
182
|
+
if (lastDepositIndex > 0) {
|
|
183
|
+
const [prevDepositPda] = await this.findDepositStoragePda(lastDepositIndex);
|
|
184
|
+
prevDeposit = prevDepositPda;
|
|
185
|
+
}
|
|
186
|
+
const tokenAuthority = (await this.findPda(types_1.PdaSeedType.TokenAuthority, this.bridge))[0];
|
|
187
|
+
const crumbAuthority = (await this.findPda(types_1.PdaSeedType.CrumbAuthority, this.bridge))[0];
|
|
188
|
+
// Get the token program ID from the mint account
|
|
189
|
+
const mintAccount = await this.connection.getAccountInfo(params.mint);
|
|
190
|
+
if (!mintAccount) {
|
|
191
|
+
throw new Error("Mint account not found");
|
|
192
|
+
}
|
|
193
|
+
const tokenProgramId = mintAccount.owner;
|
|
194
|
+
// Validate that the mint is owned by a supported SPL token program
|
|
195
|
+
if (!tokenProgramId.equals(spl_token_1.TOKEN_PROGRAM_ID) &&
|
|
196
|
+
!tokenProgramId.equals(spl_token_1.TOKEN_2022_PROGRAM_ID)) {
|
|
197
|
+
throw new Error("Mint Account is not owned by a supported SPL token program");
|
|
198
|
+
}
|
|
199
|
+
// Get the token program ID from the fromAccount to ensure we're using the same program
|
|
200
|
+
const fromAccountInfo = await this.connection.getAccountInfo(params.fromAccount);
|
|
201
|
+
if (!fromAccountInfo) {
|
|
202
|
+
throw new Error(`From account not found for address: ${params.fromAccount.toBase58()}. ` +
|
|
203
|
+
"This may indicate an invalid, uninitialized, or missing token account.");
|
|
204
|
+
}
|
|
205
|
+
if (!fromAccountInfo.owner.equals(tokenProgramId)) {
|
|
206
|
+
throw new Error(`From account owner (${fromAccountInfo.owner.toBase58()}) does not match mint owner (${tokenProgramId.toBase58()}). ` +
|
|
207
|
+
"This usually means the fromAccount is not a valid token account for the given mint.");
|
|
208
|
+
}
|
|
209
|
+
const toAccount = await (0, spl_token_1.getAssociatedTokenAddress)(params.mint, tokenAuthority, true, tokenProgramId);
|
|
210
|
+
// Build the transaction
|
|
211
|
+
const accounts = {
|
|
212
|
+
payer: this.provider.wallet.publicKey,
|
|
213
|
+
deposit,
|
|
214
|
+
assetConfig,
|
|
215
|
+
bridge: this.bridge,
|
|
216
|
+
program: this.programId,
|
|
217
|
+
fromAccount: params.fromAccount,
|
|
218
|
+
toAccount: toAccount,
|
|
219
|
+
tokenProgram: tokenProgramId,
|
|
220
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
221
|
+
crumbAuthority,
|
|
222
|
+
tokenAuthority,
|
|
223
|
+
};
|
|
224
|
+
const allAccounts = { ...accounts, prevDeposit: prevDeposit };
|
|
225
|
+
// Create the transaction
|
|
226
|
+
const tx = await (await this.program.methods
|
|
227
|
+
.deposit(this.provider.wallet.publicKey, params.amount)
|
|
228
|
+
.accounts(allAccounts)).transaction();
|
|
229
|
+
// Check if toAccount is initialized and add preinstruction if needed
|
|
230
|
+
try {
|
|
231
|
+
await this.connection.getTokenAccountBalance(toAccount);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
// Account doesn't exist, add instruction to create it
|
|
235
|
+
// Get the authority PDA which is the owner of the toAccount
|
|
236
|
+
const [authority] = await this.findAuthorityPda();
|
|
237
|
+
const createTokenAccountIx = (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, // payer
|
|
238
|
+
toAccount, // associated token account address
|
|
239
|
+
authority, // owner of the token account
|
|
240
|
+
params.mint, // token mint
|
|
241
|
+
tokenProgramId);
|
|
242
|
+
// Add the instruction to the beginning of the transaction
|
|
243
|
+
tx.instructions.unshift(createTokenAccountIx);
|
|
244
|
+
}
|
|
245
|
+
// Sign and send the transaction using our helper method
|
|
246
|
+
return await this.signAndSendTransaction(tx);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Withdraw tokens from the bridge
|
|
250
|
+
*
|
|
251
|
+
* @param params Withdrawal parameters
|
|
252
|
+
* @param signer Signer keypair
|
|
253
|
+
* @returns Transaction signature
|
|
254
|
+
*/
|
|
255
|
+
async withdraw(params, signer) {
|
|
256
|
+
const [stateUpdate] = await this.findBlockStoragePda(params.claim.blockId.toNumber());
|
|
257
|
+
const [withdrawalNullifier] = await this.findWithdrawalNullifierPda(params.claim.blockId.toNumber(), params.claim.leafIndex.toNumber());
|
|
258
|
+
const [authority] = await this.findAuthorityPda();
|
|
259
|
+
// Get the token program ID from the fromAccount
|
|
260
|
+
const fromAccountInfo = await this.connection.getAccountInfo(params.fromAccount);
|
|
261
|
+
if (!fromAccountInfo) {
|
|
262
|
+
throw new Error(`From account not found for address: ${params.fromAccount.toBase58()}. ` +
|
|
263
|
+
"This may indicate an invalid, uninitialized, or missing token account.");
|
|
264
|
+
}
|
|
265
|
+
const tokenProgramId = fromAccountInfo.owner;
|
|
266
|
+
// Validate that the account is owned by a supported SPL token program
|
|
267
|
+
if (!tokenProgramId.equals(spl_token_1.TOKEN_PROGRAM_ID) &&
|
|
268
|
+
!tokenProgramId.equals(spl_token_1.TOKEN_2022_PROGRAM_ID)) {
|
|
269
|
+
throw new Error("From Account is not owned by a supported SPL token program");
|
|
270
|
+
}
|
|
271
|
+
// Build the transaction
|
|
272
|
+
const tx = await this.program.methods
|
|
273
|
+
.withdraw(params.claim)
|
|
274
|
+
.accounts({
|
|
275
|
+
payer: signer.publicKey,
|
|
276
|
+
stateUpdate,
|
|
277
|
+
withdrawalNullifier,
|
|
278
|
+
fromAccount: params.fromAccount,
|
|
279
|
+
toAccount: params.toAccount,
|
|
280
|
+
authority,
|
|
281
|
+
tokenProgram: tokenProgramId,
|
|
282
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
283
|
+
})
|
|
284
|
+
.transaction();
|
|
285
|
+
// Sign and send the transaction using our helper method
|
|
286
|
+
return await this.signAndSendTransaction(tx, [signer]);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Whitelist an asset (token) for use with the bridge
|
|
290
|
+
*
|
|
291
|
+
* @param mint Token mint address
|
|
292
|
+
* @param signer Operator keypair
|
|
293
|
+
* @returns Transaction signature
|
|
294
|
+
*/
|
|
295
|
+
async whitelistAsset(mint, signer) {
|
|
296
|
+
const [assetWhitelisted] = await this.findAssetConfigPda(mint);
|
|
297
|
+
// Build the transaction
|
|
298
|
+
const tx = await this.program.methods
|
|
299
|
+
.whitelistAsset(mint)
|
|
300
|
+
.accounts({
|
|
301
|
+
operator: signer.publicKey,
|
|
302
|
+
bridge: this.bridge,
|
|
303
|
+
assetWhitelisted,
|
|
304
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
305
|
+
})
|
|
306
|
+
.transaction();
|
|
307
|
+
// Sign and send the transaction using our helper method
|
|
308
|
+
return await this.signAndSendTransaction(tx, [signer]);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Propose a new block
|
|
312
|
+
*
|
|
313
|
+
* @param facts Block facts
|
|
314
|
+
* @param signer Operator keypair
|
|
315
|
+
* @returns Transaction signature
|
|
316
|
+
*/
|
|
317
|
+
async proposeBlock(facts, signer) {
|
|
318
|
+
// Get the last block ID from contract storage
|
|
319
|
+
const bridgeAccount = await this.program.account.bridge.fetch(this.bridge);
|
|
320
|
+
const [block] = await this.findBlockStoragePda(bridgeAccount.lastBlockId.toNumber() + 1);
|
|
321
|
+
// Find the last deposit PDA
|
|
322
|
+
const [lastDeposit] = await this.findDepositStoragePda(facts.nextStateFacts.lastDepositIndex.toNumber());
|
|
323
|
+
// Find the DA fact state
|
|
324
|
+
const daFactState = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(types_1.PdaSeedType.DaFactStorage), facts.daCommitment], this.programId)[0];
|
|
325
|
+
// Build the transaction
|
|
326
|
+
const tx = await this.program.methods
|
|
327
|
+
.proposeBlock(facts)
|
|
328
|
+
.accounts({
|
|
329
|
+
operator: signer.publicKey,
|
|
330
|
+
block,
|
|
331
|
+
lastDeposit,
|
|
332
|
+
daFactState,
|
|
333
|
+
bridge: this.bridge,
|
|
334
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
335
|
+
})
|
|
336
|
+
.transaction();
|
|
337
|
+
// Sign and send the transaction using our helper method
|
|
338
|
+
return await this.signAndSendTransaction(tx, [signer]);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Finalize a block
|
|
342
|
+
*
|
|
343
|
+
* @param blockId Block ID
|
|
344
|
+
* @param stateUpdateId State update ID
|
|
345
|
+
* @param signer Payer keypair
|
|
346
|
+
* @returns Transaction signature
|
|
347
|
+
*/
|
|
348
|
+
async finalizeBlock(blockId, stateUpdateId, signer) {
|
|
349
|
+
const [block] = await this.findBlockStoragePda(blockId);
|
|
350
|
+
// Build the transaction
|
|
351
|
+
const tx = await this.program.methods
|
|
352
|
+
.finalizeBlock(new anchor.BN(stateUpdateId))
|
|
353
|
+
.accounts({
|
|
354
|
+
payer: signer.publicKey,
|
|
355
|
+
block,
|
|
356
|
+
bridge: this.bridge,
|
|
357
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
358
|
+
})
|
|
359
|
+
.transaction();
|
|
360
|
+
// Sign and send the transaction using our helper method
|
|
361
|
+
return await this.signAndSendTransaction(tx, [signer]);
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Finalize a DA fact
|
|
365
|
+
*
|
|
366
|
+
* @param fact DA fact (32-byte array)
|
|
367
|
+
* @param signer Payer keypair
|
|
368
|
+
* @returns Transaction signature
|
|
369
|
+
*/
|
|
370
|
+
async finalizeDaFact(fact, signer) {
|
|
371
|
+
const factStateStorage = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(types_1.PdaSeedType.DaFactStorage), fact], this.programId)[0];
|
|
372
|
+
// Build the transaction
|
|
373
|
+
const tx = await this.program.methods
|
|
374
|
+
.finalizeDaFact(Array.from(fact))
|
|
375
|
+
.accounts({
|
|
376
|
+
payer: signer.publicKey,
|
|
377
|
+
factStateStorage,
|
|
378
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
379
|
+
})
|
|
380
|
+
.transaction();
|
|
381
|
+
// Sign and send the transaction using our helper method
|
|
382
|
+
return await this.signAndSendTransaction(tx, [signer]);
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Initialize the bridge contract
|
|
386
|
+
*
|
|
387
|
+
* @param operator Operator public key
|
|
388
|
+
* @param initialAppStateCommitment Initial app state commitment (32-byte array)
|
|
389
|
+
* @param signer Payer keypair
|
|
390
|
+
* @returns Transaction signature
|
|
391
|
+
*/
|
|
392
|
+
async initialize(operator, initialAppStateCommitment, signer) {
|
|
393
|
+
// Build the transaction
|
|
394
|
+
const tx = await this.program.methods
|
|
395
|
+
.initialize(operator, Array.from(initialAppStateCommitment))
|
|
396
|
+
.accounts({
|
|
397
|
+
payer: signer.publicKey,
|
|
398
|
+
program: this.programId,
|
|
399
|
+
bridge: this.bridge,
|
|
400
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
401
|
+
})
|
|
402
|
+
.transaction();
|
|
403
|
+
// Sign and send the transaction using our helper method
|
|
404
|
+
return await this.signAndSendTransaction(tx, [signer]);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Create an associated token account if it doesn't exist
|
|
408
|
+
*
|
|
409
|
+
* @param mint Token mint
|
|
410
|
+
* @param owner Account owner
|
|
411
|
+
* @param payer Transaction payer
|
|
412
|
+
* @returns Associated token account address
|
|
413
|
+
*/
|
|
414
|
+
async createTokenAccountIfNeeded(mint, owner, payer) {
|
|
415
|
+
// Get the token program ID from the mint account
|
|
416
|
+
const mintAccount = await this.connection.getAccountInfo(mint);
|
|
417
|
+
if (!mintAccount) {
|
|
418
|
+
throw new Error("Mint account not found");
|
|
419
|
+
}
|
|
420
|
+
const tokenProgramId = mintAccount.owner;
|
|
421
|
+
const associatedTokenAddress = await (0, spl_token_1.getAssociatedTokenAddress)(mint, owner, false, tokenProgramId);
|
|
422
|
+
try {
|
|
423
|
+
await this.connection.getTokenAccountBalance(associatedTokenAddress);
|
|
424
|
+
}
|
|
425
|
+
catch {
|
|
426
|
+
// Account doesn't exist, create it
|
|
427
|
+
const transaction = new web3_js_1.Transaction().add((0, spl_token_1.createAssociatedTokenAccountInstruction)(payer.publicKey, associatedTokenAddress, owner, mint, tokenProgramId));
|
|
428
|
+
// Sign and send the transaction using our helper method
|
|
429
|
+
await this.signAndSendTransaction(transaction, [payer]);
|
|
430
|
+
}
|
|
431
|
+
return associatedTokenAddress;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
exports.SolanaBridgeClient = SolanaBridgeClient;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SPLTokenInfo } from "./types";
|
|
2
|
+
import { Commitment } from "@solana/web3.js";
|
|
3
|
+
export declare const SOLANA_MAINNET_URL = "https://api.mainnet-beta.solana.com";
|
|
4
|
+
export declare const SOLANA_DEVNET_URL = "https://api.devnet.solana.com";
|
|
5
|
+
export declare const SOLANA_TESTNET_URL = "https://api.testnet.solana.com";
|
|
6
|
+
export declare const SOLANA_LOCALNET_URL = "http://localhost:8899";
|
|
7
|
+
export declare const DEFAULT_COMMITMENT: Commitment;
|
|
8
|
+
export declare const DEV_URL = "http://localhost";
|
|
9
|
+
export declare const WEBSERVER_DEV_URL: string;
|
|
10
|
+
export declare const SOLANA_PROGRAM_ID = "CVDFLCAjXhVWiPXH9nTCTpCgVzmDVoiPzNJYuccr1dqB";
|
|
11
|
+
export declare const DEV_TOKEN_INFOS: SPLTokenInfo[];
|
|
12
|
+
export declare const DEFAULT_FUNDING_AMOUNTS: {
|
|
13
|
+
[key: string]: [string, number];
|
|
14
|
+
};
|
|
15
|
+
export declare const PDA_SEEDS: {
|
|
16
|
+
CONTRACT_STORAGE: string;
|
|
17
|
+
ASSET_WHITELISTED: string;
|
|
18
|
+
DEPOSIT_STORAGE: string;
|
|
19
|
+
BLOCK_STORAGE: string;
|
|
20
|
+
WITHDRAWAL_NULLIFIER: string;
|
|
21
|
+
AUTHORITY: string;
|
|
22
|
+
DA_FACT_STORAGE: string;
|
|
23
|
+
};
|