@atomiqlabs/chain-starknet 4.0.0-dev.4 → 4.0.0-dev.40
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/LICENSE +201 -201
- package/dist/index.d.ts +39 -38
- package/dist/index.js +55 -54
- package/dist/starknet/StarknetChainType.d.ts +14 -13
- package/dist/starknet/StarknetChainType.js +2 -2
- package/dist/starknet/StarknetInitializer.d.ts +28 -27
- package/dist/starknet/StarknetInitializer.js +69 -69
- package/dist/starknet/btcrelay/BtcRelayAbi.d.ts +250 -250
- package/dist/starknet/btcrelay/BtcRelayAbi.js +341 -341
- package/dist/starknet/btcrelay/StarknetBtcRelay.d.ts +186 -186
- package/dist/starknet/btcrelay/StarknetBtcRelay.js +379 -379
- package/dist/starknet/btcrelay/headers/StarknetBtcHeader.d.ts +31 -31
- package/dist/starknet/btcrelay/headers/StarknetBtcHeader.js +74 -74
- package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.d.ts +51 -51
- package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.js +113 -113
- package/dist/starknet/chain/StarknetAction.d.ts +19 -19
- package/dist/starknet/chain/StarknetAction.js +73 -73
- package/dist/starknet/chain/StarknetChainInterface.d.ts +60 -52
- package/dist/starknet/chain/StarknetChainInterface.js +116 -91
- package/dist/starknet/chain/StarknetModule.d.ts +9 -14
- package/dist/starknet/chain/StarknetModule.js +13 -13
- package/dist/starknet/chain/modules/ERC20Abi.d.ts +755 -755
- package/dist/starknet/chain/modules/ERC20Abi.js +1032 -1032
- package/dist/starknet/chain/modules/StarknetAccounts.d.ts +6 -6
- package/dist/starknet/chain/modules/StarknetAccounts.js +24 -24
- package/dist/starknet/chain/modules/StarknetAddresses.d.ts +10 -9
- package/dist/starknet/chain/modules/StarknetAddresses.js +27 -26
- package/dist/starknet/chain/modules/StarknetBlocks.d.ts +27 -20
- package/dist/starknet/chain/modules/StarknetBlocks.js +73 -64
- package/dist/starknet/chain/modules/StarknetEvents.d.ts +45 -44
- package/dist/starknet/chain/modules/StarknetEvents.js +90 -88
- package/dist/starknet/chain/modules/StarknetFees.d.ts +82 -77
- package/dist/starknet/chain/modules/StarknetFees.js +121 -114
- package/dist/starknet/chain/modules/StarknetSignatures.d.ts +29 -29
- package/dist/starknet/chain/modules/StarknetSignatures.js +72 -72
- package/dist/starknet/chain/modules/StarknetTokens.d.ts +69 -69
- package/dist/starknet/chain/modules/StarknetTokens.js +102 -98
- package/dist/starknet/chain/modules/StarknetTransactions.d.ts +111 -93
- package/dist/starknet/chain/modules/StarknetTransactions.js +390 -255
- package/dist/starknet/contract/StarknetContractBase.d.ts +13 -13
- package/dist/starknet/contract/StarknetContractBase.js +20 -16
- package/dist/starknet/contract/StarknetContractModule.d.ts +8 -8
- package/dist/starknet/contract/StarknetContractModule.js +11 -11
- package/dist/starknet/contract/modules/StarknetContractEvents.d.ts +52 -51
- package/dist/starknet/contract/modules/StarknetContractEvents.js +98 -97
- package/dist/starknet/events/StarknetChainEvents.d.ts +21 -21
- package/dist/starknet/events/StarknetChainEvents.js +52 -52
- package/dist/starknet/events/StarknetChainEventsBrowser.d.ts +84 -90
- package/dist/starknet/events/StarknetChainEventsBrowser.js +307 -292
- package/dist/starknet/provider/RpcProviderWithRetries.d.ts +41 -21
- package/dist/starknet/provider/RpcProviderWithRetries.js +70 -32
- package/dist/starknet/spv_swap/SpvVaultContractAbi.d.ts +488 -488
- package/dist/starknet/spv_swap/SpvVaultContractAbi.js +656 -656
- package/dist/starknet/spv_swap/StarknetSpvVaultContract.d.ts +92 -66
- package/dist/starknet/spv_swap/StarknetSpvVaultContract.js +483 -382
- package/dist/starknet/spv_swap/StarknetSpvVaultData.d.ts +49 -49
- package/dist/starknet/spv_swap/StarknetSpvVaultData.js +145 -145
- package/dist/starknet/spv_swap/StarknetSpvWithdrawalData.d.ts +25 -25
- package/dist/starknet/spv_swap/StarknetSpvWithdrawalData.js +72 -72
- package/dist/starknet/swaps/EscrowManagerAbi.d.ts +431 -434
- package/dist/starknet/swaps/EscrowManagerAbi.js +583 -587
- package/dist/starknet/swaps/StarknetSwapContract.d.ts +197 -191
- package/dist/starknet/swaps/StarknetSwapContract.js +440 -410
- package/dist/starknet/swaps/StarknetSwapData.d.ts +74 -67
- package/dist/starknet/swaps/StarknetSwapData.js +325 -290
- package/dist/starknet/swaps/StarknetSwapModule.d.ts +10 -10
- package/dist/starknet/swaps/StarknetSwapModule.js +11 -11
- package/dist/starknet/swaps/handlers/IHandler.d.ts +13 -13
- package/dist/starknet/swaps/handlers/IHandler.js +2 -2
- package/dist/starknet/swaps/handlers/claim/ClaimHandlers.d.ts +13 -13
- package/dist/starknet/swaps/handlers/claim/ClaimHandlers.js +13 -13
- package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.d.ts +21 -21
- package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.js +44 -44
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -24
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +48 -48
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -25
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +40 -40
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +20 -20
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +30 -30
- package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +45 -45
- package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +52 -52
- package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -17
- package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.js +27 -27
- package/dist/starknet/swaps/modules/StarknetLpVault.d.ts +69 -69
- package/dist/starknet/swaps/modules/StarknetLpVault.js +122 -122
- package/dist/starknet/swaps/modules/StarknetSwapClaim.d.ts +53 -53
- package/dist/starknet/swaps/modules/StarknetSwapClaim.js +100 -100
- package/dist/starknet/swaps/modules/StarknetSwapInit.d.ts +94 -86
- package/dist/starknet/swaps/modules/StarknetSwapInit.js +235 -224
- package/dist/starknet/swaps/modules/StarknetSwapRefund.d.ts +62 -62
- package/dist/starknet/swaps/modules/StarknetSwapRefund.js +128 -128
- package/dist/starknet/wallet/StarknetBrowserSigner.d.ts +5 -0
- package/dist/starknet/wallet/StarknetBrowserSigner.js +11 -0
- package/dist/starknet/wallet/StarknetKeypairWallet.d.ts +7 -7
- package/dist/starknet/wallet/StarknetKeypairWallet.js +35 -30
- package/dist/starknet/wallet/StarknetPersistentSigner.d.ts +34 -0
- package/dist/starknet/wallet/StarknetPersistentSigner.js +267 -0
- package/dist/starknet/wallet/StarknetSigner.d.ts +19 -12
- package/dist/starknet/wallet/StarknetSigner.js +70 -46
- package/dist/starknet/wallet/accounts/StarknetKeypairWallet.d.ts +7 -0
- package/dist/starknet/wallet/accounts/StarknetKeypairWallet.js +35 -0
- package/dist/utils/Utils.d.ts +39 -37
- package/dist/utils/Utils.js +264 -261
- package/package.json +45 -37
- package/src/index.ts +48 -47
- package/src/starknet/StarknetChainType.ts +30 -28
- package/src/starknet/StarknetInitializer.ts +110 -108
- package/src/starknet/btcrelay/BtcRelayAbi.ts +338 -338
- package/src/starknet/btcrelay/StarknetBtcRelay.ts +494 -494
- package/src/starknet/btcrelay/headers/StarknetBtcHeader.ts +100 -100
- package/src/starknet/btcrelay/headers/StarknetBtcStoredHeader.ts +141 -141
- package/src/starknet/chain/StarknetAction.ts +85 -85
- package/src/starknet/chain/StarknetChainInterface.ts +186 -149
- package/src/starknet/chain/StarknetModule.ts +19 -19
- package/src/starknet/chain/modules/ERC20Abi.ts +1029 -1029
- package/src/starknet/chain/modules/StarknetAccounts.ts +25 -25
- package/src/starknet/chain/modules/StarknetAddresses.ts +23 -22
- package/src/starknet/chain/modules/StarknetBlocks.ts +86 -74
- package/src/starknet/chain/modules/StarknetEvents.ts +107 -104
- package/src/starknet/chain/modules/StarknetFees.ts +162 -154
- package/src/starknet/chain/modules/StarknetSignatures.ts +91 -91
- package/src/starknet/chain/modules/StarknetTokens.ts +120 -116
- package/src/starknet/chain/modules/StarknetTransactions.ts +433 -277
- package/src/starknet/contract/StarknetContractBase.ts +30 -26
- package/src/starknet/contract/StarknetContractModule.ts +16 -16
- package/src/starknet/contract/modules/StarknetContractEvents.ts +136 -134
- package/src/starknet/events/StarknetChainEvents.ts +67 -67
- package/src/starknet/events/StarknetChainEventsBrowser.ts +420 -410
- package/src/starknet/provider/RpcProviderWithRetries.ts +83 -43
- package/src/starknet/spv_swap/SpvVaultContractAbi.ts +656 -656
- package/src/starknet/spv_swap/StarknetSpvVaultContract.ts +601 -483
- package/src/starknet/spv_swap/StarknetSpvVaultData.ts +195 -195
- package/src/starknet/spv_swap/StarknetSpvWithdrawalData.ts +79 -79
- package/src/starknet/swaps/EscrowManagerAbi.ts +582 -586
- package/src/starknet/swaps/StarknetSwapContract.ts +668 -630
- package/src/starknet/swaps/StarknetSwapData.ts +455 -403
- package/src/starknet/swaps/StarknetSwapModule.ts +17 -17
- package/src/starknet/swaps/handlers/IHandler.ts +20 -20
- package/src/starknet/swaps/handlers/claim/ClaimHandlers.ts +23 -23
- package/src/starknet/swaps/handlers/claim/HashlockClaimHandler.ts +53 -53
- package/src/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +73 -73
- package/src/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +67 -67
- package/src/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +50 -50
- package/src/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +102 -102
- package/src/starknet/swaps/handlers/refund/TimelockRefundHandler.ts +38 -38
- package/src/starknet/swaps/modules/StarknetLpVault.ts +147 -147
- package/src/starknet/swaps/modules/StarknetSwapClaim.ts +141 -141
- package/src/starknet/swaps/modules/StarknetSwapInit.ts +300 -286
- package/src/starknet/swaps/modules/StarknetSwapRefund.ts +196 -196
- package/src/starknet/wallet/StarknetBrowserSigner.ts +12 -0
- package/src/starknet/wallet/StarknetPersistentSigner.ts +335 -0
- package/src/starknet/wallet/StarknetSigner.ts +86 -55
- package/src/starknet/wallet/{StarknetKeypairWallet.ts → accounts/StarknetKeypairWallet.ts} +44 -39
- package/src/utils/Utils.ts +262 -252
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import {StarknetSigner} from "./StarknetSigner";
|
|
2
|
+
import {StarknetTransactions, StarknetTx} from "../chain/modules/StarknetTransactions";
|
|
3
|
+
import {StarknetChainInterface} from "../chain/StarknetChainInterface";
|
|
4
|
+
import {bigIntMax, getLogger, LoggerType} from "../../utils/Utils";
|
|
5
|
+
import {Account, BlockTag} from "starknet";
|
|
6
|
+
import {access, readFile, writeFile, mkdir, constants} from "fs/promises";
|
|
7
|
+
import {StarknetFees} from "../chain/modules/StarknetFees";
|
|
8
|
+
import {cloneDeep} from "@scure/btc-signer/transaction";
|
|
9
|
+
import { PromiseQueue } from "promise-queue-ts";
|
|
10
|
+
|
|
11
|
+
const WAIT_BEFORE_BUMP = 15*1000;
|
|
12
|
+
const MIN_FEE_INCREASE_ABSOLUTE = 1n*1_000_000n; //0.001GWei
|
|
13
|
+
const MIN_FEE_INCREASE_PPM = 110_000n; // +11%
|
|
14
|
+
|
|
15
|
+
const MIN_TIP_INCREASE_ABSOLUTE = 1n*1_000_000_000n; //1GWei
|
|
16
|
+
const MIN_TIP_INCREASE_PPM = 110_000n; // +11%
|
|
17
|
+
|
|
18
|
+
export type StarknetPersistentSignerConfig = {
|
|
19
|
+
waitBeforeBump?: number;
|
|
20
|
+
minFeeIncreaseAbsolute?: bigint;
|
|
21
|
+
minFeeIncreasePpm?: bigint
|
|
22
|
+
minTipIncreaseAbsolute?: bigint;
|
|
23
|
+
minTipIncreasePpm?: bigint;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export class StarknetPersistentSigner extends StarknetSigner {
|
|
27
|
+
|
|
28
|
+
private pendingTxs: Map<bigint, {
|
|
29
|
+
txs: StarknetTx[],
|
|
30
|
+
lastBumped: number,
|
|
31
|
+
sending?: boolean //Not saved
|
|
32
|
+
}> = new Map();
|
|
33
|
+
|
|
34
|
+
private confirmedNonce: bigint;
|
|
35
|
+
private pendingNonce: bigint;
|
|
36
|
+
|
|
37
|
+
private feeBumper: any;
|
|
38
|
+
private stopped: boolean = false;
|
|
39
|
+
|
|
40
|
+
private readonly directory: string;
|
|
41
|
+
|
|
42
|
+
private readonly config: StarknetPersistentSignerConfig
|
|
43
|
+
|
|
44
|
+
private readonly chainInterface: StarknetChainInterface;
|
|
45
|
+
|
|
46
|
+
private readonly logger: LoggerType;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
account: Account,
|
|
50
|
+
chainInterface: StarknetChainInterface,
|
|
51
|
+
directory: string,
|
|
52
|
+
config?: StarknetPersistentSignerConfig,
|
|
53
|
+
) {
|
|
54
|
+
super(account, true);
|
|
55
|
+
this.signTransaction = null;
|
|
56
|
+
this.chainInterface = chainInterface;
|
|
57
|
+
this.directory = directory;
|
|
58
|
+
this.config = config ?? {};
|
|
59
|
+
this.config.minFeeIncreaseAbsolute ??= MIN_FEE_INCREASE_ABSOLUTE;
|
|
60
|
+
this.config.minFeeIncreasePpm ??= MIN_FEE_INCREASE_PPM;
|
|
61
|
+
this.config.minTipIncreaseAbsolute ??= MIN_TIP_INCREASE_ABSOLUTE;
|
|
62
|
+
this.config.minTipIncreasePpm ??= MIN_TIP_INCREASE_PPM;
|
|
63
|
+
this.config.waitBeforeBump ??= WAIT_BEFORE_BUMP;
|
|
64
|
+
this.logger = getLogger("StarknetPersistentSigner("+this.account.address+"): ");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private async load() {
|
|
68
|
+
const fileExists = await access(this.directory+"/txs.json", constants.F_OK).then(() => true).catch(() => false);
|
|
69
|
+
if(!fileExists) return;
|
|
70
|
+
const res = await readFile(this.directory+"/txs.json");
|
|
71
|
+
if(res!=null) {
|
|
72
|
+
const pendingTxs: {
|
|
73
|
+
[nonce: string]: {
|
|
74
|
+
txs: string[],
|
|
75
|
+
lastBumped: number
|
|
76
|
+
}
|
|
77
|
+
} = JSON.parse((res as Buffer).toString());
|
|
78
|
+
|
|
79
|
+
for(let nonceStr in pendingTxs) {
|
|
80
|
+
const nonceData = pendingTxs[nonceStr];
|
|
81
|
+
|
|
82
|
+
const nonce = BigInt(nonceStr);
|
|
83
|
+
if(this.confirmedNonce>=nonce) continue; //Already confirmed
|
|
84
|
+
|
|
85
|
+
if(this.pendingNonce<nonce) {
|
|
86
|
+
this.pendingNonce = nonce;
|
|
87
|
+
}
|
|
88
|
+
const parsedPendingTxns = nonceData.txs.map(StarknetTransactions.deserializeTx);
|
|
89
|
+
this.pendingTxs.set(nonce, {
|
|
90
|
+
txs: parsedPendingTxns,
|
|
91
|
+
lastBumped: nonceData.lastBumped
|
|
92
|
+
})
|
|
93
|
+
for(let tx of parsedPendingTxns) {
|
|
94
|
+
this.chainInterface.Transactions._knownTxSet.add(tx.txId);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private priorSavePromise: Promise<void>;
|
|
101
|
+
private saveCount: number = 0;
|
|
102
|
+
|
|
103
|
+
private async save() {
|
|
104
|
+
const pendingTxs: {
|
|
105
|
+
[nonce: string]: {
|
|
106
|
+
txs: string[],
|
|
107
|
+
lastBumped: number
|
|
108
|
+
}
|
|
109
|
+
} = {};
|
|
110
|
+
for(let [nonce, data] of this.pendingTxs) {
|
|
111
|
+
pendingTxs[nonce.toString(10)] = {
|
|
112
|
+
lastBumped: data.lastBumped,
|
|
113
|
+
txs: data.txs.map(StarknetTransactions.serializeTx)
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const requiredSaveCount = ++this.saveCount;
|
|
117
|
+
if(this.priorSavePromise!=null) {
|
|
118
|
+
await this.priorSavePromise;
|
|
119
|
+
}
|
|
120
|
+
if(requiredSaveCount===this.saveCount) {
|
|
121
|
+
this.priorSavePromise = writeFile(this.directory+"/txs.json", JSON.stringify(pendingTxs));
|
|
122
|
+
await this.priorSavePromise;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private async checkPastTransactions() {
|
|
127
|
+
let _gasPrice: {l1GasCost: bigint, l2GasCost: bigint, l1DataGasCost: bigint} = null;
|
|
128
|
+
let _safeBlockNonce: bigint = null;
|
|
129
|
+
|
|
130
|
+
for(let [nonce, data] of this.pendingTxs) {
|
|
131
|
+
if(!data.sending && data.lastBumped<Date.now()-this.config.waitBeforeBump) {
|
|
132
|
+
if(_safeBlockNonce==null) {
|
|
133
|
+
_safeBlockNonce = await this.chainInterface.Transactions.getNonce(this.account.address, BlockTag.LATEST);
|
|
134
|
+
this.confirmedNonce = _safeBlockNonce - 1n;
|
|
135
|
+
}
|
|
136
|
+
if(this.confirmedNonce >= nonce) {
|
|
137
|
+
this.pendingTxs.delete(nonce);
|
|
138
|
+
data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
|
|
139
|
+
this.logger.info("checkPastTransactions(): Tx confirmed, required fee bumps: ", data.txs.length);
|
|
140
|
+
this.save();
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const lastTx = data.txs[data.txs.length-1];
|
|
145
|
+
if(_gasPrice==null) {
|
|
146
|
+
const feeRate = await this.chainInterface.Fees.getFeeRate();
|
|
147
|
+
_gasPrice = StarknetFees.extractFromFeeRateString(feeRate);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let l1GasCost = BigInt(lastTx.details.resourceBounds.l1_gas.max_price_per_unit);
|
|
151
|
+
let l2GasCost = BigInt(lastTx.details.resourceBounds.l2_gas.max_price_per_unit);
|
|
152
|
+
let l1DataGasCost = BigInt(lastTx.details.resourceBounds.l1_data_gas.max_price_per_unit);
|
|
153
|
+
let tip = BigInt(lastTx.details.tip);
|
|
154
|
+
|
|
155
|
+
let feeBumped: boolean = false;
|
|
156
|
+
if(_gasPrice.l1GasCost > l1GasCost) {
|
|
157
|
+
//Bump by minimum allowed or to the actual _gasPrice.l1GasCost
|
|
158
|
+
l1GasCost = bigIntMax(_gasPrice.l1GasCost, this.config.minFeeIncreaseAbsolute + (l1GasCost * (1_000_000n + this.config.minFeeIncreasePpm) / 1_000_000n));
|
|
159
|
+
feeBumped = true;
|
|
160
|
+
}
|
|
161
|
+
if(_gasPrice.l1DataGasCost > l1DataGasCost) {
|
|
162
|
+
//Bump by minimum allowed or to the actual _gasPrice.l1GasCost
|
|
163
|
+
l1DataGasCost = bigIntMax(_gasPrice.l1DataGasCost, this.config.minFeeIncreaseAbsolute + (l1DataGasCost * (1_000_000n + this.config.minFeeIncreasePpm) / 1_000_000n));
|
|
164
|
+
feeBumped = true;
|
|
165
|
+
}
|
|
166
|
+
if(_gasPrice.l2GasCost > l2GasCost || feeBumped) { //In case the fees for l1 and l1Data were bumped, we also need to bump the l2GasFee regardless
|
|
167
|
+
l2GasCost = bigIntMax(_gasPrice.l2GasCost, this.config.minFeeIncreaseAbsolute + (l2GasCost * (1_000_000n + this.config.minFeeIncreasePpm) / 1_000_000n));
|
|
168
|
+
feeBumped = true;
|
|
169
|
+
}
|
|
170
|
+
if(feeBumped) tip = this.config.minTipIncreaseAbsolute + (tip * (1_000_000n + this.config.minTipIncreasePpm) / 1_000_000n);
|
|
171
|
+
|
|
172
|
+
if(!feeBumped) {
|
|
173
|
+
//Not fee bumped
|
|
174
|
+
this.logger.debug("checkPastTransactions(): Tx yet unconfirmed but not increasing fee for ", lastTx.txId);
|
|
175
|
+
//Rebroadcast the tx
|
|
176
|
+
await this.chainInterface.Transactions.sendTransaction(lastTx).catch(e => {
|
|
177
|
+
if(e.baseError?.code === 52) { //Invalid transaction nonce
|
|
178
|
+
this.logger.debug("checkPastTransactions(): Tx re-broadcast success, tx already confirmed: ", lastTx.txId);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if(e.baseError?.code === 59) { //Transaction already in the mempool
|
|
182
|
+
this.logger.debug("checkPastTransactions(): Tx re-broadcast success, tx already known to the RPC: ", lastTx.txId);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
this.logger.error("checkPastTransactions(): Tx re-broadcast error", e)
|
|
186
|
+
});
|
|
187
|
+
data.lastBumped = Date.now();
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const newTx = cloneDeep(lastTx);
|
|
192
|
+
delete newTx.signed;
|
|
193
|
+
delete newTx.txId;
|
|
194
|
+
|
|
195
|
+
newTx.details.tip = tip;
|
|
196
|
+
newTx.details.resourceBounds.l1_gas.max_price_per_unit = l1GasCost;
|
|
197
|
+
newTx.details.resourceBounds.l2_gas.max_price_per_unit = l2GasCost;
|
|
198
|
+
newTx.details.resourceBounds.l1_data_gas.max_price_per_unit = l1DataGasCost;
|
|
199
|
+
|
|
200
|
+
await this._signTransaction(newTx);
|
|
201
|
+
this.logger.info(`checkPastTransactions(): Bump fee for tx ${lastTx.txId} -> ${newTx.txId}`);
|
|
202
|
+
|
|
203
|
+
//Double check pending txns still has nonce after async signTransaction was called
|
|
204
|
+
if(!this.pendingTxs.has(nonce)) continue;
|
|
205
|
+
|
|
206
|
+
for(let callback of this.chainInterface.Transactions._cbksBeforeTxReplace) {
|
|
207
|
+
try {
|
|
208
|
+
await callback(StarknetTransactions.serializeTx(lastTx), lastTx.txId, StarknetTransactions.serializeTx(newTx), newTx.txId)
|
|
209
|
+
} catch (e) {
|
|
210
|
+
this.logger.error("checkPastTransactions(): beforeTxReplace callback error: ", e);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
data.txs.push(newTx);
|
|
215
|
+
data.lastBumped = Date.now();
|
|
216
|
+
this.save();
|
|
217
|
+
|
|
218
|
+
this.chainInterface.Transactions._knownTxSet.add(newTx.txId);
|
|
219
|
+
|
|
220
|
+
//TODO: Better error handling when sending tx
|
|
221
|
+
await this.chainInterface.Transactions.sendTransaction(newTx).catch(e => {
|
|
222
|
+
if(e.baseError?.code === 52) { //Invalid transaction nonce
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
this.logger.error("checkPastTransactions(): Fee-bumped tx broadcast error", e)
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private startFeeBumper() {
|
|
232
|
+
let func: () => Promise<void>;
|
|
233
|
+
func = async () => {
|
|
234
|
+
try {
|
|
235
|
+
await this.checkPastTransactions();
|
|
236
|
+
} catch (e) {
|
|
237
|
+
this.logger.error("startFeeBumper(): Error when check past transactions: ", e);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if(this.stopped) return;
|
|
241
|
+
|
|
242
|
+
this.feeBumper = setTimeout(func, 1000);
|
|
243
|
+
};
|
|
244
|
+
func();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private async syncNonceFromChain() {
|
|
248
|
+
const txCount = await this.chainInterface.Transactions.getNonce(this.account.address, BlockTag.LATEST);
|
|
249
|
+
this.confirmedNonce = txCount-1n;
|
|
250
|
+
if(this.pendingNonce < this.confirmedNonce) {
|
|
251
|
+
this.logger.info(`syncNonceFromChain(): Re-synced latest nonce from chain, adjusting local pending nonce ${this.pendingNonce} -> ${this.confirmedNonce}`);
|
|
252
|
+
this.pendingNonce = this.confirmedNonce;
|
|
253
|
+
for(let [nonce, data] of this.pendingTxs) {
|
|
254
|
+
if(nonce <= this.pendingNonce) {
|
|
255
|
+
this.pendingTxs.delete(nonce);
|
|
256
|
+
data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
|
|
257
|
+
this.logger.info(`syncNonceFromChain(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
this.save();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async init(): Promise<void> {
|
|
265
|
+
try {
|
|
266
|
+
await mkdir(this.directory)
|
|
267
|
+
} catch (e) {}
|
|
268
|
+
|
|
269
|
+
const nonce = await this.chainInterface.Transactions.getNonce(this.account.address, BlockTag.LATEST);
|
|
270
|
+
this.confirmedNonce = nonce - 1n;
|
|
271
|
+
this.pendingNonce = nonce - 1n;
|
|
272
|
+
|
|
273
|
+
await this.load();
|
|
274
|
+
|
|
275
|
+
this.startFeeBumper();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
stop(): Promise<void> {
|
|
279
|
+
this.stopped = true;
|
|
280
|
+
if(this.feeBumper!=null) {
|
|
281
|
+
clearTimeout(this.feeBumper);
|
|
282
|
+
this.feeBumper = null;
|
|
283
|
+
}
|
|
284
|
+
return Promise.resolve();
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private readonly sendTransactionQueue: PromiseQueue = new PromiseQueue();
|
|
288
|
+
|
|
289
|
+
sendTransaction(transaction: StarknetTx, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string> {
|
|
290
|
+
return this.sendTransactionQueue.enqueue(async () => {
|
|
291
|
+
if(transaction.details.nonce!=null) {
|
|
292
|
+
if(transaction.details.nonce !== this.pendingNonce + 1n)
|
|
293
|
+
throw new Error("Invalid transaction nonce!");
|
|
294
|
+
} else {
|
|
295
|
+
transaction.details.nonce = this.pendingNonce + 1n;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const signedTx = await this._signTransaction(transaction);
|
|
299
|
+
|
|
300
|
+
if(onBeforePublish!=null) {
|
|
301
|
+
try {
|
|
302
|
+
await onBeforePublish(signedTx.txId, StarknetTransactions.serializeTx(signedTx));
|
|
303
|
+
} catch (e) {
|
|
304
|
+
this.logger.error("sendTransaction(): Error when calling onBeforePublish function: ", e);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const pendingTxObject = {txs: [signedTx], lastBumped: Date.now(), sending: true};
|
|
309
|
+
this.pendingNonce++;
|
|
310
|
+
this.logger.debug("sendTransaction(): Incrementing pending nonce to: ", this.pendingNonce);
|
|
311
|
+
this.pendingTxs.set(transaction.details.nonce, pendingTxObject);
|
|
312
|
+
this.save();
|
|
313
|
+
|
|
314
|
+
this.chainInterface.Transactions._knownTxSet.add(signedTx.txId);
|
|
315
|
+
|
|
316
|
+
try {
|
|
317
|
+
const result = await this.chainInterface.Transactions.sendTransaction(signedTx);
|
|
318
|
+
pendingTxObject.sending = false;
|
|
319
|
+
return result;
|
|
320
|
+
} catch (e) {
|
|
321
|
+
if(e.baseError?.code === 52) { //Invalid transaction nonce
|
|
322
|
+
//Re-check nonce from on-chain
|
|
323
|
+
this.logger.info("sendTransaction(): Got INVALID_TRANSACTION_NONCE (52) back from backend, re-checking latest nonce from chain!");
|
|
324
|
+
await this.syncNonceFromChain();
|
|
325
|
+
}
|
|
326
|
+
this.chainInterface.Transactions._knownTxSet.delete(signedTx.txId);
|
|
327
|
+
this.pendingTxs.delete(transaction.details.nonce);
|
|
328
|
+
this.pendingNonce--;
|
|
329
|
+
this.logger.debug("sendTransaction(): Error when broadcasting transaction, reverting pending nonce to: ", this.pendingNonce);
|
|
330
|
+
throw e;
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
}
|
|
@@ -1,55 +1,86 @@
|
|
|
1
|
-
import {AbstractSigner} from "@atomiqlabs/base";
|
|
2
|
-
import {Account, DeployAccountContractPayload} from "starknet";
|
|
3
|
-
import {toHex} from "../../utils/Utils";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
1
|
+
import {AbstractSigner} from "@atomiqlabs/base";
|
|
2
|
+
import {Account, DeployAccountContractPayload, BlockTag, Invocation, DeployAccountContractTransaction} from "starknet";
|
|
3
|
+
import {calculateHash, toHex} from "../../utils/Utils";
|
|
4
|
+
import {
|
|
5
|
+
isStarknetTxDeployAccount,
|
|
6
|
+
isStarknetTxInvoke, StarknetTransactions,
|
|
7
|
+
StarknetTx,
|
|
8
|
+
StarknetTxDeployAccount,
|
|
9
|
+
StarknetTxInvoke
|
|
10
|
+
} from "../chain/modules/StarknetTransactions";
|
|
11
|
+
|
|
12
|
+
export class StarknetSigner implements AbstractSigner {
|
|
13
|
+
type = "AtomiqAbstractSigner" as const;
|
|
14
|
+
|
|
15
|
+
public readonly isManagingNoncesInternally: boolean;
|
|
16
|
+
|
|
17
|
+
account: Account;
|
|
18
|
+
|
|
19
|
+
isDeployed: boolean = null;
|
|
20
|
+
|
|
21
|
+
constructor(account: Account, isManagingNoncesInternally: boolean = false) {
|
|
22
|
+
this.account = account;
|
|
23
|
+
this.isManagingNoncesInternally = isManagingNoncesInternally;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getAddress(): string {
|
|
27
|
+
return toHex(this.account.address);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected async _signTransaction(tx: StarknetTx): Promise<StarknetTx> {
|
|
31
|
+
if(isStarknetTxInvoke(tx)) {
|
|
32
|
+
tx.signed = await this.signInvoke(tx);
|
|
33
|
+
} else if(isStarknetTxDeployAccount(tx)) {
|
|
34
|
+
tx.signed = await this.signDeployAccount(tx);
|
|
35
|
+
} else {
|
|
36
|
+
throw new Error("Unsupported transaction type!");
|
|
37
|
+
}
|
|
38
|
+
calculateHash(tx);
|
|
39
|
+
return tx;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
signTransaction?(tx: StarknetTx): Promise<StarknetTx> {
|
|
43
|
+
return this._signTransaction(tx);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected signInvoke(tx: StarknetTxInvoke): Promise<Invocation> {
|
|
47
|
+
return this.account.buildInvocation(tx.tx, tx.details);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
protected signDeployAccount(tx: StarknetTxDeployAccount): Promise<DeployAccountContractTransaction> {
|
|
51
|
+
return this.account.buildAccountDeployPayload(tx.tx, tx.details);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async sendTransaction(tx: StarknetTx, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string> {
|
|
55
|
+
if(isStarknetTxInvoke(tx)) {
|
|
56
|
+
tx.txId = await this.sendInvoke(tx);
|
|
57
|
+
} else if(isStarknetTxDeployAccount(tx)) {
|
|
58
|
+
tx.txId = await this.sendDeployAccount(tx);
|
|
59
|
+
} else {
|
|
60
|
+
throw new Error("Unsupported transaction type!");
|
|
61
|
+
}
|
|
62
|
+
if(onBeforePublish!=null) await onBeforePublish(tx.txId, StarknetTransactions.serializeTx(tx));
|
|
63
|
+
return tx.txId;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected async sendInvoke(tx: StarknetTxInvoke): Promise<string> {
|
|
67
|
+
const result = await this.account.execute(tx.tx, tx.details);
|
|
68
|
+
return result.transaction_hash;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
protected async sendDeployAccount(tx: StarknetTxDeployAccount): Promise<string> {
|
|
72
|
+
const result = await this.account.deployAccount(tx.tx, tx.details);
|
|
73
|
+
return result.transaction_hash;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// isWalletAccount() {
|
|
77
|
+
// return (this.account as any).walletProvider!=null;
|
|
78
|
+
// }
|
|
79
|
+
|
|
80
|
+
async getDeployPayload(): Promise<DeployAccountContractPayload | null> {
|
|
81
|
+
const _account: Account & {getDeploymentData?: () => DeployAccountContractPayload} = this.account;
|
|
82
|
+
if(_account.getDeploymentData==null) return null;
|
|
83
|
+
return _account.getDeploymentData();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
}
|
|
@@ -1,39 +1,44 @@
|
|
|
1
|
-
import {Account, CallData, DeployAccountContractPayload, ec, hash, Provider} from "starknet";
|
|
2
|
-
import {toHex} from "
|
|
3
|
-
import {Buffer} from "buffer";
|
|
4
|
-
|
|
5
|
-
const OZaccountClassHash = '0x00261c293c8084cd79086214176b33e5911677cec55104fddc8d25b0b736dcad';
|
|
6
|
-
|
|
7
|
-
//Openzeppelin Account wallet
|
|
8
|
-
export class StarknetKeypairWallet extends Account {
|
|
9
|
-
|
|
10
|
-
public readonly publicKey: string;
|
|
11
|
-
|
|
12
|
-
constructor(provider: Provider, privateKey: string) {
|
|
13
|
-
const publicKey = ec.starkCurve.getStarkKey(toHex(privateKey));
|
|
14
|
-
// Calculate future address of the account
|
|
15
|
-
const OZaccountConstructorCallData = CallData.compile({ publicKey });
|
|
16
|
-
const OZcontractAddress = hash.calculateContractAddressFromHash(
|
|
17
|
-
publicKey,
|
|
18
|
-
OZaccountClassHash,
|
|
19
|
-
OZaccountConstructorCallData,
|
|
20
|
-
0
|
|
21
|
-
);
|
|
22
|
-
super(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
1
|
+
import {Account, CallData, DeployAccountContractPayload, ec, hash, Provider} from "starknet";
|
|
2
|
+
import {toHex} from "../../../utils/Utils";
|
|
3
|
+
import {Buffer} from "buffer";
|
|
4
|
+
|
|
5
|
+
const OZaccountClassHash = '0x00261c293c8084cd79086214176b33e5911677cec55104fddc8d25b0b736dcad';
|
|
6
|
+
|
|
7
|
+
//Openzeppelin Account wallet
|
|
8
|
+
export class StarknetKeypairWallet extends Account {
|
|
9
|
+
|
|
10
|
+
public readonly publicKey: string;
|
|
11
|
+
|
|
12
|
+
constructor(provider: Provider, privateKey: string) {
|
|
13
|
+
const publicKey = ec.starkCurve.getStarkKey(toHex(privateKey));
|
|
14
|
+
// Calculate future address of the account
|
|
15
|
+
const OZaccountConstructorCallData = CallData.compile({ publicKey });
|
|
16
|
+
const OZcontractAddress = hash.calculateContractAddressFromHash(
|
|
17
|
+
publicKey,
|
|
18
|
+
OZaccountClassHash,
|
|
19
|
+
OZaccountConstructorCallData,
|
|
20
|
+
0
|
|
21
|
+
);
|
|
22
|
+
super({
|
|
23
|
+
provider,
|
|
24
|
+
address: OZcontractAddress,
|
|
25
|
+
signer: privateKey,
|
|
26
|
+
cairoVersion: "1"
|
|
27
|
+
});
|
|
28
|
+
this.publicKey = publicKey;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public getDeploymentData(): DeployAccountContractPayload {
|
|
32
|
+
return {
|
|
33
|
+
classHash: OZaccountClassHash,
|
|
34
|
+
constructorCalldata: CallData.compile({ publicKey: this.publicKey }),
|
|
35
|
+
addressSalt: this.publicKey,
|
|
36
|
+
contractAddress: this.address
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public static generateRandomPrivateKey(): string {
|
|
41
|
+
return "0x"+Buffer.from(ec.starkCurve.utils.randomPrivateKey()).toString("hex");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|