@atomiqlabs/chain-solana 7.3.8 → 8.0.0-beta.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 +1 -0
- package/dist/index.js +1 -0
- package/dist/solana/SolanaChains.d.ts +14 -0
- package/dist/solana/SolanaChains.js +18 -0
- package/dist/solana/SolanaInitializer.d.ts +18 -0
- package/dist/solana/SolanaInitializer.js +59 -0
- package/dist/solana/base/SolanaAction.js +25 -38
- package/dist/solana/base/modules/SolanaBlocks.js +18 -29
- package/dist/solana/base/modules/SolanaEvents.js +15 -26
- package/dist/solana/base/modules/SolanaFees.d.ts +4 -5
- package/dist/solana/base/modules/SolanaFees.js +168 -182
- package/dist/solana/base/modules/SolanaSlots.js +16 -29
- package/dist/solana/base/modules/SolanaTokens.d.ts +6 -7
- package/dist/solana/base/modules/SolanaTokens.js +63 -84
- package/dist/solana/base/modules/SolanaTransactions.d.ts +2 -2
- package/dist/solana/base/modules/SolanaTransactions.js +143 -165
- package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +2 -3
- package/dist/solana/btcrelay/SolanaBtcRelay.js +185 -221
- package/dist/solana/events/SolanaChainEvents.js +113 -138
- package/dist/solana/events/SolanaChainEventsBrowser.js +56 -65
- package/dist/solana/program/modules/SolanaProgramEvents.js +13 -24
- package/dist/solana/swaps/SolanaSwapData.d.ts +15 -11
- package/dist/solana/swaps/SolanaSwapData.js +29 -13
- package/dist/solana/swaps/SolanaSwapProgram.d.ts +40 -41
- package/dist/solana/swaps/SolanaSwapProgram.js +212 -221
- package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +2 -3
- package/dist/solana/swaps/modules/SolanaDataAccount.js +117 -141
- package/dist/solana/swaps/modules/SolanaLpVault.d.ts +4 -5
- package/dist/solana/swaps/modules/SolanaLpVault.js +87 -110
- package/dist/solana/swaps/modules/SwapClaim.d.ts +4 -4
- package/dist/solana/swaps/modules/SwapClaim.js +122 -134
- package/dist/solana/swaps/modules/SwapInit.d.ts +2 -3
- package/dist/solana/swaps/modules/SwapInit.js +254 -285
- package/dist/solana/swaps/modules/SwapRefund.d.ts +2 -3
- package/dist/solana/swaps/modules/SwapRefund.js +103 -113
- package/dist/utils/Utils.d.ts +10 -0
- package/dist/utils/Utils.js +62 -35
- package/package.json +9 -6
- package/src/index.ts +1 -0
- package/src/solana/SolanaChains.ts +16 -0
- package/src/solana/SolanaInitializer.ts +98 -0
- package/src/solana/base/SolanaBase.ts +1 -2
- package/src/solana/base/modules/SolanaEvents.ts +0 -1
- package/src/solana/base/modules/SolanaFees.ts +35 -31
- package/src/solana/base/modules/SolanaTokens.ts +26 -27
- package/src/solana/base/modules/SolanaTransactions.ts +33 -28
- package/src/solana/btcrelay/SolanaBtcRelay.ts +6 -10
- package/src/solana/events/SolanaChainEventsBrowser.ts +14 -10
- package/src/solana/program/modules/SolanaProgramEvents.ts +1 -1
- package/src/solana/swaps/SolanaSwapData.ts +38 -19
- package/src/solana/swaps/SolanaSwapProgram.ts +133 -105
- package/src/solana/swaps/modules/SolanaDataAccount.ts +4 -6
- package/src/solana/swaps/modules/SolanaLpVault.ts +18 -18
- package/src/solana/swaps/modules/SwapClaim.ts +14 -17
- package/src/solana/swaps/modules/SwapInit.ts +35 -38
- package/src/solana/swaps/modules/SwapRefund.ts +20 -23
- package/src/utils/Utils.ts +35 -0
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.SolanaBtcRelay = void 0;
|
|
13
4
|
const web3_js_1 = require("@solana/web3.js");
|
|
@@ -16,11 +7,10 @@ const SolanaBtcHeader_1 = require("./headers/SolanaBtcHeader");
|
|
|
16
7
|
const programIdl = require("./program/programIdl.json");
|
|
17
8
|
const base_1 = require("@atomiqlabs/base");
|
|
18
9
|
const SolanaProgramBase_1 = require("../program/SolanaProgramBase");
|
|
19
|
-
const BN = require("bn.js");
|
|
20
10
|
const SolanaAction_1 = require("../base/SolanaAction");
|
|
21
11
|
const buffer_1 = require("buffer");
|
|
22
12
|
const SolanaFees_1 = require("../base/modules/SolanaFees");
|
|
23
|
-
const
|
|
13
|
+
const BN = require("bn.js");
|
|
24
14
|
const MAX_CLOSE_IX_PER_TX = 10;
|
|
25
15
|
function serializeBlockHeader(e) {
|
|
26
16
|
return new SolanaBtcHeader_1.SolanaBtcHeader({
|
|
@@ -45,19 +35,17 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
45
35
|
* @constructor
|
|
46
36
|
* @private
|
|
47
37
|
*/
|
|
48
|
-
Initialize(signer, header, epochStart, pastBlocksTimestamps) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
.instruction());
|
|
60
|
-
});
|
|
38
|
+
async Initialize(signer, header, epochStart, pastBlocksTimestamps) {
|
|
39
|
+
const serializedBlock = serializeBlockHeader(header);
|
|
40
|
+
return new SolanaAction_1.SolanaAction(signer, this, await this.program.methods
|
|
41
|
+
.initialize(serializedBlock, header.getHeight(), header.getChainWork(), epochStart, pastBlocksTimestamps)
|
|
42
|
+
.accounts({
|
|
43
|
+
signer,
|
|
44
|
+
mainState: this.BtcRelayMainState,
|
|
45
|
+
headerTopic: this.BtcRelayHeader(serializedBlock.hash),
|
|
46
|
+
systemProgram: web3_js_1.SystemProgram.programId
|
|
47
|
+
})
|
|
48
|
+
.instruction());
|
|
61
49
|
}
|
|
62
50
|
/**
|
|
63
51
|
* Creates verify action to be used with the swap program, specifies the action to be firstIxBeforeComputeBudget,
|
|
@@ -71,28 +59,24 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
71
59
|
* @param reversedMerkleProof
|
|
72
60
|
* @param committedHeader
|
|
73
61
|
*/
|
|
74
|
-
Verify(signer, reversedTxId, confirmations, position, reversedMerkleProof, committedHeader) {
|
|
75
|
-
return
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
.instruction(), null, null, null, true);
|
|
83
|
-
});
|
|
62
|
+
async Verify(signer, reversedTxId, confirmations, position, reversedMerkleProof, committedHeader) {
|
|
63
|
+
return new SolanaAction_1.SolanaAction(signer, this, await this.program.methods
|
|
64
|
+
.verifyTransaction(reversedTxId, confirmations, position, reversedMerkleProof, committedHeader)
|
|
65
|
+
.accounts({
|
|
66
|
+
signer,
|
|
67
|
+
mainState: this.BtcRelayMainState
|
|
68
|
+
})
|
|
69
|
+
.instruction(), null, null, null, true);
|
|
84
70
|
}
|
|
85
|
-
CloseForkAccount(signer, forkId) {
|
|
86
|
-
return
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
.instruction(), 20000);
|
|
95
|
-
});
|
|
71
|
+
async CloseForkAccount(signer, forkId) {
|
|
72
|
+
return new SolanaAction_1.SolanaAction(signer, this, await this.program.methods
|
|
73
|
+
.closeForkAccount(new BN(forkId))
|
|
74
|
+
.accounts({
|
|
75
|
+
signer,
|
|
76
|
+
forkState: this.BtcRelayFork(forkId, signer),
|
|
77
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
78
|
+
})
|
|
79
|
+
.instruction(), 20000);
|
|
96
80
|
}
|
|
97
81
|
constructor(connection, bitcoinRpc, programAddress, solanaFeeEstimator = new SolanaFees_1.SolanaFees(connection)) {
|
|
98
82
|
super(connection, programIdl, programAddress, null, solanaFeeEstimator);
|
|
@@ -144,53 +128,49 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
144
128
|
* @param createTx transaction generator function
|
|
145
129
|
* @private
|
|
146
130
|
*/
|
|
147
|
-
_saveHeaders(signer, headers, storedHeader, tipWork, forkId, feeRate, createTx) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
.remainingAccounts(blockHeaderObj.map(e => {
|
|
152
|
-
return {
|
|
153
|
-
pubkey: this.BtcRelayHeader(e.hash),
|
|
154
|
-
isSigner: false,
|
|
155
|
-
isWritable: false
|
|
156
|
-
};
|
|
157
|
-
}))
|
|
158
|
-
.transaction();
|
|
159
|
-
tx.feePayer = signer;
|
|
160
|
-
this.Fees.applyFeeRateBegin(tx, null, feeRate);
|
|
161
|
-
this.Fees.applyFeeRateEnd(tx, null, feeRate);
|
|
162
|
-
const computedCommitedHeaders = this.computeCommitedHeaders(storedHeader, blockHeaderObj);
|
|
163
|
-
const lastStoredHeader = computedCommitedHeaders[computedCommitedHeaders.length - 1];
|
|
164
|
-
if (forkId !== 0 && base_1.StatePredictorUtils.gtBuffer(buffer_1.Buffer.from(lastStoredHeader.chainWork), tipWork)) {
|
|
165
|
-
//Fork's work is higher than main chain's work, this fork will become a main chain
|
|
166
|
-
forkId = 0;
|
|
167
|
-
}
|
|
131
|
+
async _saveHeaders(signer, headers, storedHeader, tipWork, forkId, feeRate, createTx) {
|
|
132
|
+
const blockHeaderObj = headers.map(serializeBlockHeader);
|
|
133
|
+
const tx = await createTx(blockHeaderObj)
|
|
134
|
+
.remainingAccounts(blockHeaderObj.map(e => {
|
|
168
135
|
return {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
tx,
|
|
173
|
-
signers: []
|
|
174
|
-
},
|
|
175
|
-
computedCommitedHeaders
|
|
136
|
+
pubkey: this.BtcRelayHeader(e.hash),
|
|
137
|
+
isSigner: false,
|
|
138
|
+
isWritable: false
|
|
176
139
|
};
|
|
177
|
-
})
|
|
140
|
+
}))
|
|
141
|
+
.transaction();
|
|
142
|
+
tx.feePayer = signer;
|
|
143
|
+
this.Fees.applyFeeRateBegin(tx, null, feeRate);
|
|
144
|
+
this.Fees.applyFeeRateEnd(tx, null, feeRate);
|
|
145
|
+
const computedCommitedHeaders = this.computeCommitedHeaders(storedHeader, blockHeaderObj);
|
|
146
|
+
const lastStoredHeader = computedCommitedHeaders[computedCommitedHeaders.length - 1];
|
|
147
|
+
if (forkId !== 0 && base_1.StatePredictorUtils.gtBuffer(buffer_1.Buffer.from(lastStoredHeader.chainWork), tipWork)) {
|
|
148
|
+
//Fork's work is higher than main chain's work, this fork will become a main chain
|
|
149
|
+
forkId = 0;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
forkId: forkId,
|
|
153
|
+
lastStoredHeader,
|
|
154
|
+
tx: {
|
|
155
|
+
tx,
|
|
156
|
+
signers: []
|
|
157
|
+
},
|
|
158
|
+
computedCommitedHeaders
|
|
159
|
+
};
|
|
178
160
|
}
|
|
179
161
|
/**
|
|
180
162
|
* Returns data about current main chain tip stored in the btc relay
|
|
181
163
|
*/
|
|
182
|
-
getTipData() {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
};
|
|
193
|
-
});
|
|
164
|
+
async getTipData() {
|
|
165
|
+
const data = await this.program.account.mainState.fetchNullable(this.BtcRelayMainState);
|
|
166
|
+
if (data == null)
|
|
167
|
+
return null;
|
|
168
|
+
return {
|
|
169
|
+
blockheight: data.blockHeight,
|
|
170
|
+
commitHash: buffer_1.Buffer.from(data.tipCommitHash).toString("hex"),
|
|
171
|
+
blockhash: buffer_1.Buffer.from(data.tipBlockHash).reverse().toString("hex"),
|
|
172
|
+
chainWork: buffer_1.Buffer.from(data.chainWork)
|
|
173
|
+
};
|
|
194
174
|
}
|
|
195
175
|
/**
|
|
196
176
|
* Retrieves blockheader with a specific blockhash, returns null if requiredBlockheight is provided and
|
|
@@ -199,32 +179,30 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
199
179
|
* @param blockData
|
|
200
180
|
* @param requiredBlockheight
|
|
201
181
|
*/
|
|
202
|
-
retrieveLogAndBlockheight(blockData, requiredBlockheight) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
182
|
+
async retrieveLogAndBlockheight(blockData, requiredBlockheight) {
|
|
183
|
+
const mainState = await this.program.account.mainState.fetch(this.BtcRelayMainState);
|
|
184
|
+
if (requiredBlockheight != null && mainState.blockHeight < requiredBlockheight) {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
const storedCommitments = this.getBlockCommitmentsSet(mainState);
|
|
188
|
+
const blockHashBuffer = buffer_1.Buffer.from(blockData.blockhash, 'hex').reverse();
|
|
189
|
+
const topicKey = this.BtcRelayHeader(blockHashBuffer);
|
|
190
|
+
const data = await this.Events.findInEvents(topicKey, async (event) => {
|
|
191
|
+
if (event.name === "StoreFork" || event.name === "StoreHeader") {
|
|
192
|
+
const eventData = event.data;
|
|
193
|
+
const commitHash = buffer_1.Buffer.from(eventData.commitHash).toString("hex");
|
|
194
|
+
if (blockHashBuffer.equals(buffer_1.Buffer.from(eventData.blockHash)) && storedCommitments.has(commitHash))
|
|
195
|
+
return {
|
|
196
|
+
header: new SolanaBtcStoredHeader_1.SolanaBtcStoredHeader(eventData.header),
|
|
197
|
+
height: mainState.blockHeight,
|
|
198
|
+
commitHash
|
|
199
|
+
};
|
|
207
200
|
}
|
|
208
|
-
const storedCommitments = this.getBlockCommitmentsSet(mainState);
|
|
209
|
-
const blockHashBuffer = buffer_1.Buffer.from(blockData.blockhash, 'hex').reverse();
|
|
210
|
-
const topicKey = this.BtcRelayHeader(blockHashBuffer);
|
|
211
|
-
const data = yield this.Events.findInEvents(topicKey, (event) => __awaiter(this, void 0, void 0, function* () {
|
|
212
|
-
if (event.name === "StoreFork" || event.name === "StoreHeader") {
|
|
213
|
-
const eventData = event.data;
|
|
214
|
-
const commitHash = buffer_1.Buffer.from(eventData.commitHash).toString("hex");
|
|
215
|
-
if (blockHashBuffer.equals(buffer_1.Buffer.from(eventData.blockHash)) && storedCommitments.has(commitHash))
|
|
216
|
-
return {
|
|
217
|
-
header: new SolanaBtcStoredHeader_1.SolanaBtcStoredHeader(eventData.header),
|
|
218
|
-
height: mainState.blockHeight,
|
|
219
|
-
commitHash
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
}));
|
|
223
|
-
if (data != null)
|
|
224
|
-
this.logger.debug("retrieveLogAndBlockheight(): block found," +
|
|
225
|
-
" commit hash: " + data.commitHash + " blockhash: " + blockData.blockhash + " height: " + data.height);
|
|
226
|
-
return data;
|
|
227
201
|
});
|
|
202
|
+
if (data != null)
|
|
203
|
+
this.logger.debug("retrieveLogAndBlockheight(): block found," +
|
|
204
|
+
" commit hash: " + data.commitHash + " blockhash: " + blockData.blockhash + " height: " + data.height);
|
|
205
|
+
return data;
|
|
228
206
|
}
|
|
229
207
|
/**
|
|
230
208
|
* Retrieves blockheader data by blockheader's commit hash,
|
|
@@ -232,52 +210,48 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
232
210
|
* @param commitmentHashStr
|
|
233
211
|
* @param blockData
|
|
234
212
|
*/
|
|
235
|
-
retrieveLogByCommitHash(commitmentHashStr, blockData) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
}));
|
|
247
|
-
if (data != null)
|
|
248
|
-
this.logger.debug("retrieveLogByCommitHash(): block found," +
|
|
249
|
-
" commit hash: " + commitmentHashStr + " blockhash: " + blockData.blockhash + " height: " + data.blockheight);
|
|
250
|
-
return data;
|
|
213
|
+
async retrieveLogByCommitHash(commitmentHashStr, blockData) {
|
|
214
|
+
const blockHashBuffer = buffer_1.Buffer.from(blockData.blockhash, "hex").reverse();
|
|
215
|
+
const topicKey = this.BtcRelayHeader(blockHashBuffer);
|
|
216
|
+
const data = await this.Events.findInEvents(topicKey, async (event) => {
|
|
217
|
+
if (event.name === "StoreFork" || event.name === "StoreHeader") {
|
|
218
|
+
const eventData = event.data;
|
|
219
|
+
const commitHash = buffer_1.Buffer.from(eventData.commitHash).toString("hex");
|
|
220
|
+
if (commitmentHashStr === commitHash)
|
|
221
|
+
return new SolanaBtcStoredHeader_1.SolanaBtcStoredHeader(eventData.header);
|
|
222
|
+
}
|
|
251
223
|
});
|
|
224
|
+
if (data != null)
|
|
225
|
+
this.logger.debug("retrieveLogByCommitHash(): block found," +
|
|
226
|
+
" commit hash: " + commitmentHashStr + " blockhash: " + blockData.blockhash + " height: " + data.blockheight);
|
|
227
|
+
return data;
|
|
252
228
|
}
|
|
253
229
|
/**
|
|
254
230
|
* Retrieves latest known stored blockheader & blockheader from bitcoin RPC that is in the main chain
|
|
255
231
|
*/
|
|
256
|
-
retrieveLatestKnownBlockLog() {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
return data;
|
|
280
|
-
});
|
|
232
|
+
async retrieveLatestKnownBlockLog() {
|
|
233
|
+
const mainState = await this.program.account.mainState.fetch(this.BtcRelayMainState);
|
|
234
|
+
const storedCommitments = this.getBlockCommitmentsSet(mainState);
|
|
235
|
+
const data = await this.Events.findInEvents(this.program.programId, async (event) => {
|
|
236
|
+
if (event.name === "StoreFork" || event.name === "StoreHeader") {
|
|
237
|
+
const eventData = event.data;
|
|
238
|
+
const blockHashHex = buffer_1.Buffer.from(eventData.blockHash).reverse().toString("hex");
|
|
239
|
+
const isInMainChain = await this.bitcoinRpc.isInMainChain(blockHashHex).catch(() => false);
|
|
240
|
+
const commitHash = buffer_1.Buffer.from(eventData.commitHash).toString("hex");
|
|
241
|
+
//Check if this fork is part of main chain
|
|
242
|
+
if (isInMainChain && storedCommitments.has(commitHash))
|
|
243
|
+
return {
|
|
244
|
+
resultStoredHeader: new SolanaBtcStoredHeader_1.SolanaBtcStoredHeader(eventData.header),
|
|
245
|
+
resultBitcoinHeader: await this.bitcoinRpc.getBlockHeader(blockHashHex),
|
|
246
|
+
commitHash: commitHash
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
}, null, 10);
|
|
250
|
+
if (data != null)
|
|
251
|
+
this.logger.debug("retrieveLatestKnownBlockLog(): block found," +
|
|
252
|
+
" commit hash: " + data.commitHash + " blockhash: " + data.resultBitcoinHeader.getHash() +
|
|
253
|
+
" height: " + data.resultStoredHeader.blockheight);
|
|
254
|
+
return data;
|
|
281
255
|
}
|
|
282
256
|
/**
|
|
283
257
|
* Saves initial block header when the btc relay is in uninitialized state
|
|
@@ -288,15 +262,13 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
288
262
|
* @param pastBlocksTimestamps timestamp of the past 10 blocks
|
|
289
263
|
* @param feeRate fee rate to use for the transaction
|
|
290
264
|
*/
|
|
291
|
-
saveInitialHeader(signer, header, epochStart, pastBlocksTimestamps, feeRate) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
return yield action.tx(feeRate);
|
|
299
|
-
});
|
|
265
|
+
async saveInitialHeader(signer, header, epochStart, pastBlocksTimestamps, feeRate) {
|
|
266
|
+
if (pastBlocksTimestamps.length !== 10)
|
|
267
|
+
throw new Error("Invalid prevBlocksTimestamps");
|
|
268
|
+
const action = await this.Initialize(new web3_js_1.PublicKey(signer), header, epochStart, pastBlocksTimestamps);
|
|
269
|
+
this.logger.debug("saveInitialHeader(): saving initial header, blockhash: " + header.getHash() +
|
|
270
|
+
" blockheight: " + header.getHeight() + " epochStart: " + epochStart + " past block timestamps: " + pastBlocksTimestamps.join());
|
|
271
|
+
return await action.tx(feeRate);
|
|
300
272
|
}
|
|
301
273
|
/**
|
|
302
274
|
* Saves blockheaders as a bitcoin main chain to the btc relay
|
|
@@ -325,22 +297,20 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
325
297
|
* @param tipWork
|
|
326
298
|
* @param feeRate
|
|
327
299
|
*/
|
|
328
|
-
saveNewForkHeaders(signer, forkHeaders, storedHeader, tipWork, feeRate) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
}));
|
|
343
|
-
});
|
|
300
|
+
async saveNewForkHeaders(signer, forkHeaders, storedHeader, tipWork, feeRate) {
|
|
301
|
+
const mainState = await this.program.account.mainState.fetch(this.BtcRelayMainState);
|
|
302
|
+
let forkId = mainState.forkCounter;
|
|
303
|
+
const _signer = new web3_js_1.PublicKey(signer);
|
|
304
|
+
this.logger.debug("saveNewForkHeaders(): submitting new fork & blockheaders," +
|
|
305
|
+
" count: " + forkHeaders.length + " forkId: " + forkId.toString(10));
|
|
306
|
+
return await this._saveHeaders(_signer, forkHeaders, storedHeader, tipWork, forkId.toNumber(), feeRate, (blockHeaders) => this.program.methods
|
|
307
|
+
.submitForkHeaders(blockHeaders, storedHeader, forkId, true)
|
|
308
|
+
.accounts({
|
|
309
|
+
signer: _signer,
|
|
310
|
+
mainState: this.BtcRelayMainState,
|
|
311
|
+
forkState: this.BtcRelayFork(forkId.toNumber(), _signer),
|
|
312
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
313
|
+
}));
|
|
344
314
|
}
|
|
345
315
|
/**
|
|
346
316
|
* Continues submitting blockheaders to a given fork
|
|
@@ -392,35 +362,33 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
392
362
|
* @param lastSweepId lastCheckedId returned from the previous sweepForkData() call
|
|
393
363
|
* @returns {number} lastCheckedId that should be passed to the next call of sweepForkData()
|
|
394
364
|
*/
|
|
395
|
-
sweepForkData(signer, lastSweepId) {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
action.add(yield this.CloseForkAccount(signer.getPublicKey(), i));
|
|
410
|
-
if (action.ixsLength() >= MAX_CLOSE_IX_PER_TX) {
|
|
411
|
-
yield action.addToTxs(txs);
|
|
412
|
-
action = new SolanaAction_1.SolanaAction(signer.getPublicKey(), this);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
365
|
+
async sweepForkData(signer, lastSweepId) {
|
|
366
|
+
const mainState = await this.program.account.mainState.fetch(this.BtcRelayMainState);
|
|
367
|
+
let forkId = mainState.forkCounter.toNumber();
|
|
368
|
+
const txs = [];
|
|
369
|
+
let action = new SolanaAction_1.SolanaAction(signer.getPublicKey(), this);
|
|
370
|
+
let lastCheckedId = lastSweepId;
|
|
371
|
+
for (let i = lastSweepId == null ? 0 : lastSweepId + 1; i <= forkId; i++) {
|
|
372
|
+
lastCheckedId = i;
|
|
373
|
+
const accountAddr = this.BtcRelayFork(i, signer.getPublicKey());
|
|
374
|
+
let forkState = await this.program.account.forkState.fetchNullable(accountAddr);
|
|
375
|
+
if (forkState == null)
|
|
376
|
+
continue;
|
|
377
|
+
this.logger.info("sweepForkData(): sweeping forkId: " + i);
|
|
378
|
+
action.add(await this.CloseForkAccount(signer.getPublicKey(), i));
|
|
415
379
|
if (action.ixsLength() >= MAX_CLOSE_IX_PER_TX) {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
if (txs.length > 0) {
|
|
419
|
-
const signatures = yield this.Transactions.sendAndConfirm(signer, txs, true);
|
|
420
|
-
this.logger.info("sweepForkData(): forks swept, signatures: " + signatures.join());
|
|
380
|
+
await action.addToTxs(txs);
|
|
381
|
+
action = new SolanaAction_1.SolanaAction(signer.getPublicKey(), this);
|
|
421
382
|
}
|
|
422
|
-
|
|
423
|
-
|
|
383
|
+
}
|
|
384
|
+
if (action.ixsLength() >= MAX_CLOSE_IX_PER_TX) {
|
|
385
|
+
await action.addToTxs(txs);
|
|
386
|
+
}
|
|
387
|
+
if (txs.length > 0) {
|
|
388
|
+
const signatures = await this.Transactions.sendAndConfirm(signer, txs, true);
|
|
389
|
+
this.logger.info("sweepForkData(): forks swept, signatures: " + signatures.join());
|
|
390
|
+
}
|
|
391
|
+
return lastCheckedId;
|
|
424
392
|
}
|
|
425
393
|
/**
|
|
426
394
|
* Estimate required synchronization fee (worst case) to synchronize btc relay to the required blockheight
|
|
@@ -428,30 +396,26 @@ class SolanaBtcRelay extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
428
396
|
* @param requiredBlockheight
|
|
429
397
|
* @param feeRate
|
|
430
398
|
*/
|
|
431
|
-
estimateSynchronizeFee(requiredBlockheight, feeRate) {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
return synchronizationFee;
|
|
442
|
-
});
|
|
399
|
+
async estimateSynchronizeFee(requiredBlockheight, feeRate) {
|
|
400
|
+
const tipData = await this.getTipData();
|
|
401
|
+
const currBlockheight = tipData.blockheight;
|
|
402
|
+
const blockheightDelta = requiredBlockheight - currBlockheight;
|
|
403
|
+
if (blockheightDelta <= 0)
|
|
404
|
+
return 0n;
|
|
405
|
+
const synchronizationFee = BigInt(blockheightDelta) * await this.getFeePerBlock(feeRate);
|
|
406
|
+
this.logger.debug("estimateSynchronizeFee(): required blockheight: " + requiredBlockheight +
|
|
407
|
+
" blockheight delta: " + blockheightDelta + " fee: " + synchronizationFee.toString(10));
|
|
408
|
+
return synchronizationFee;
|
|
443
409
|
}
|
|
444
410
|
/**
|
|
445
411
|
* Returns fee required (in SOL) to synchronize a single block to btc relay
|
|
446
412
|
*
|
|
447
413
|
* @param feeRate
|
|
448
414
|
*/
|
|
449
|
-
getFeePerBlock(feeRate) {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
return new BN(50000);
|
|
454
|
-
});
|
|
415
|
+
async getFeePerBlock(feeRate) {
|
|
416
|
+
// feeRate = feeRate || await this.getMainFeeRate(null);
|
|
417
|
+
// return BASE_FEE_SOL_PER_BLOCKHEADER.add(this.Fees.getPriorityFee(200000, feeRate, false));
|
|
418
|
+
return 50000n;
|
|
455
419
|
}
|
|
456
420
|
/**
|
|
457
421
|
* Gets fee rate required for submitting blockheaders to the main chain
|