@atomiqlabs/chain-starknet 1.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/LICENSE +201 -0
- package/dist/get_serialized_block.d.ts +1 -0
- package/dist/get_serialized_block.js +28 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +50 -0
- package/dist/starknet/StarknetChainType.d.ts +9 -0
- package/dist/starknet/StarknetChainType.js +2 -0
- package/dist/starknet/StarknetInitializer.d.ts +18 -0
- package/dist/starknet/StarknetInitializer.js +49 -0
- package/dist/starknet/base/StarknetAction.d.ts +27 -0
- package/dist/starknet/base/StarknetAction.js +73 -0
- package/dist/starknet/base/StarknetBase.d.ts +34 -0
- package/dist/starknet/base/StarknetBase.js +29 -0
- package/dist/starknet/base/StarknetModule.d.ts +14 -0
- package/dist/starknet/base/StarknetModule.js +13 -0
- package/dist/starknet/base/modules/ERC20Abi.d.ts +755 -0
- package/dist/starknet/base/modules/ERC20Abi.js +1032 -0
- package/dist/starknet/base/modules/StarknetAccounts.d.ts +6 -0
- package/dist/starknet/base/modules/StarknetAccounts.js +24 -0
- package/dist/starknet/base/modules/StarknetAddresses.d.ts +9 -0
- package/dist/starknet/base/modules/StarknetAddresses.js +26 -0
- package/dist/starknet/base/modules/StarknetBlocks.d.ts +19 -0
- package/dist/starknet/base/modules/StarknetBlocks.js +49 -0
- package/dist/starknet/base/modules/StarknetEvents.d.ts +44 -0
- package/dist/starknet/base/modules/StarknetEvents.js +88 -0
- package/dist/starknet/base/modules/StarknetFees.d.ts +55 -0
- package/dist/starknet/base/modules/StarknetFees.js +102 -0
- package/dist/starknet/base/modules/StarknetSignatures.d.ts +30 -0
- package/dist/starknet/base/modules/StarknetSignatures.js +71 -0
- package/dist/starknet/base/modules/StarknetTokens.d.ts +67 -0
- package/dist/starknet/base/modules/StarknetTokens.js +97 -0
- package/dist/starknet/base/modules/StarknetTransactions.d.ts +87 -0
- package/dist/starknet/base/modules/StarknetTransactions.js +226 -0
- package/dist/starknet/btcrelay/BtcRelayAbi.d.ts +250 -0
- package/dist/starknet/btcrelay/BtcRelayAbi.js +341 -0
- package/dist/starknet/btcrelay/StarknetBtcRelay.d.ts +166 -0
- package/dist/starknet/btcrelay/StarknetBtcRelay.js +323 -0
- package/dist/starknet/btcrelay/headers/StarknetBtcHeader.d.ts +32 -0
- package/dist/starknet/btcrelay/headers/StarknetBtcHeader.js +74 -0
- package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.d.ts +52 -0
- package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.js +113 -0
- package/dist/starknet/contract/StarknetContractBase.d.ts +13 -0
- package/dist/starknet/contract/StarknetContractBase.js +18 -0
- package/dist/starknet/contract/modules/StarknetContractEvents.d.ts +40 -0
- package/dist/starknet/contract/modules/StarknetContractEvents.js +77 -0
- package/dist/starknet/events/StarknetChainEvents.d.ts +19 -0
- package/dist/starknet/events/StarknetChainEvents.js +51 -0
- package/dist/starknet/events/StarknetChainEventsBrowser.d.ts +73 -0
- package/dist/starknet/events/StarknetChainEventsBrowser.js +210 -0
- package/dist/starknet/swaps/EscrowManagerAbi.d.ts +445 -0
- package/dist/starknet/swaps/EscrowManagerAbi.js +601 -0
- package/dist/starknet/swaps/StarknetSwapContract.d.ts +215 -0
- package/dist/starknet/swaps/StarknetSwapContract.js +452 -0
- package/dist/starknet/swaps/StarknetSwapData.d.ts +74 -0
- package/dist/starknet/swaps/StarknetSwapData.js +316 -0
- package/dist/starknet/swaps/StarknetSwapModule.d.ts +9 -0
- package/dist/starknet/swaps/StarknetSwapModule.js +12 -0
- package/dist/starknet/swaps/handlers/IHandler.d.ts +13 -0
- package/dist/starknet/swaps/handlers/IHandler.js +2 -0
- package/dist/starknet/swaps/handlers/claim/ClaimHandlers.d.ts +13 -0
- package/dist/starknet/swaps/handlers/claim/ClaimHandlers.js +13 -0
- package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.d.ts +22 -0
- package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.js +44 -0
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +25 -0
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +48 -0
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +26 -0
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +40 -0
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +20 -0
- package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +29 -0
- package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +64 -0
- package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +86 -0
- package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -0
- package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.js +27 -0
- package/dist/starknet/swaps/modules/StarknetLpVault.d.ts +69 -0
- package/dist/starknet/swaps/modules/StarknetLpVault.js +122 -0
- package/dist/starknet/swaps/modules/StarknetSwapClaim.d.ts +53 -0
- package/dist/starknet/swaps/modules/StarknetSwapClaim.js +100 -0
- package/dist/starknet/swaps/modules/StarknetSwapInit.d.ts +84 -0
- package/dist/starknet/swaps/modules/StarknetSwapInit.js +164 -0
- package/dist/starknet/swaps/modules/StarknetSwapRefund.d.ts +64 -0
- package/dist/starknet/swaps/modules/StarknetSwapRefund.js +131 -0
- package/dist/starknet/swaps/modules/SwapClaim.d.ts +54 -0
- package/dist/starknet/swaps/modules/SwapClaim.js +115 -0
- package/dist/starknet/swaps/modules/SwapInit.d.ts +79 -0
- package/dist/starknet/swaps/modules/SwapInit.js +174 -0
- package/dist/starknet/swaps/modules/SwapRefund.d.ts +63 -0
- package/dist/starknet/swaps/modules/SwapRefund.js +149 -0
- package/dist/starknet/wallet/StarknetKeypairWallet.d.ts +6 -0
- package/dist/starknet/wallet/StarknetKeypairWallet.js +26 -0
- package/dist/starknet/wallet/StarknetSigner.d.ts +12 -0
- package/dist/starknet/wallet/StarknetSigner.js +46 -0
- package/dist/utils/Utils.d.ts +38 -0
- package/dist/utils/Utils.js +255 -0
- package/package.json +39 -0
- package/src/index.ts +41 -0
- package/src/starknet/StarknetChainType.ts +20 -0
- package/src/starknet/StarknetInitializer.ts +75 -0
- package/src/starknet/base/StarknetAction.ts +90 -0
- package/src/starknet/base/StarknetBase.ts +56 -0
- package/src/starknet/base/StarknetModule.ts +20 -0
- package/src/starknet/base/modules/ERC20Abi.ts +1029 -0
- package/src/starknet/base/modules/StarknetAccounts.ts +26 -0
- package/src/starknet/base/modules/StarknetAddresses.ts +23 -0
- package/src/starknet/base/modules/StarknetBlocks.ts +59 -0
- package/src/starknet/base/modules/StarknetEvents.ts +105 -0
- package/src/starknet/base/modules/StarknetFees.ts +136 -0
- package/src/starknet/base/modules/StarknetSignatures.ts +91 -0
- package/src/starknet/base/modules/StarknetTokens.ts +116 -0
- package/src/starknet/base/modules/StarknetTransactions.ts +254 -0
- package/src/starknet/btcrelay/BtcRelayAbi.ts +338 -0
- package/src/starknet/btcrelay/StarknetBtcRelay.ts +415 -0
- package/src/starknet/btcrelay/headers/StarknetBtcHeader.ts +101 -0
- package/src/starknet/btcrelay/headers/StarknetBtcStoredHeader.ts +142 -0
- package/src/starknet/contract/StarknetContractBase.ts +29 -0
- package/src/starknet/contract/modules/StarknetContractEvents.ts +108 -0
- package/src/starknet/events/StarknetChainEvents.ts +63 -0
- package/src/starknet/events/StarknetChainEventsBrowser.ts +289 -0
- package/src/starknet/swaps/EscrowManagerAbi.ts +600 -0
- package/src/starknet/swaps/StarknetSwapContract.ts +694 -0
- package/src/starknet/swaps/StarknetSwapData.ts +441 -0
- package/src/starknet/swaps/StarknetSwapModule.ts +17 -0
- package/src/starknet/swaps/handlers/IHandler.ts +20 -0
- package/src/starknet/swaps/handlers/claim/ClaimHandlers.ts +23 -0
- package/src/starknet/swaps/handlers/claim/HashlockClaimHandler.ts +54 -0
- package/src/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +73 -0
- package/src/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +67 -0
- package/src/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +49 -0
- package/src/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +151 -0
- package/src/starknet/swaps/handlers/refund/TimelockRefundHandler.ts +39 -0
- package/src/starknet/swaps/modules/StarknetLpVault.ts +148 -0
- package/src/starknet/swaps/modules/StarknetSwapClaim.ts +142 -0
- package/src/starknet/swaps/modules/StarknetSwapInit.ts +226 -0
- package/src/starknet/swaps/modules/StarknetSwapRefund.ts +202 -0
- package/src/starknet/wallet/StarknetKeypairWallet.ts +34 -0
- package/src/starknet/wallet/StarknetSigner.ts +55 -0
- package/src/utils/Utils.ts +247 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StarknetTokens = void 0;
|
|
4
|
+
const StarknetModule_1 = require("../StarknetModule");
|
|
5
|
+
const StarknetAction_1 = require("../StarknetAction");
|
|
6
|
+
const ERC20Abi_1 = require("./ERC20Abi");
|
|
7
|
+
const starknet_1 = require("starknet");
|
|
8
|
+
const Utils_1 = require("../../../utils/Utils");
|
|
9
|
+
class StarknetTokens extends StarknetModule_1.StarknetModule {
|
|
10
|
+
getContract(address) {
|
|
11
|
+
return new starknet_1.Contract(ERC20Abi_1.ERC20Abi, address, this.root.provider).typedv2(ERC20Abi_1.ERC20Abi);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Action for transferring the erc20 token
|
|
15
|
+
*
|
|
16
|
+
* @param signer
|
|
17
|
+
* @param recipient
|
|
18
|
+
* @param token
|
|
19
|
+
* @param amount
|
|
20
|
+
* @constructor
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
23
|
+
Transfer(signer, recipient, token, amount) {
|
|
24
|
+
const erc20 = this.getContract(token);
|
|
25
|
+
return new StarknetAction_1.StarknetAction(signer, this.root, erc20.populateTransaction.transfer(recipient, amount), StarknetTokens.GasCosts.TRANSFER);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Approves spend of tokens for a specific spender
|
|
29
|
+
*
|
|
30
|
+
* @param signer
|
|
31
|
+
* @param spender
|
|
32
|
+
* @param token
|
|
33
|
+
* @param amount
|
|
34
|
+
* @constructor
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
Approve(signer, spender, token, amount) {
|
|
38
|
+
const erc20 = this.getContract(token);
|
|
39
|
+
return new StarknetAction_1.StarknetAction(signer, this.root, erc20.populateTransaction.approve(spender, amount), StarknetTokens.GasCosts.APPROVE);
|
|
40
|
+
}
|
|
41
|
+
///////////////////
|
|
42
|
+
//// Tokens
|
|
43
|
+
/**
|
|
44
|
+
* Checks if the provided string is a valid starknet token
|
|
45
|
+
*
|
|
46
|
+
* @param token
|
|
47
|
+
*/
|
|
48
|
+
isValidToken(token) {
|
|
49
|
+
return this.root.Addresses.isValidAddress(token);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns the token balance of the address
|
|
53
|
+
*
|
|
54
|
+
* @param address
|
|
55
|
+
* @param token
|
|
56
|
+
*/
|
|
57
|
+
async getTokenBalance(address, token) {
|
|
58
|
+
const erc20 = this.getContract(token);
|
|
59
|
+
const balance = await erc20.balance_of(address);
|
|
60
|
+
const balanceBN = (0, Utils_1.toBigInt)(balance);
|
|
61
|
+
this.logger.debug("getTokenBalance(): token balance fetched, token: " + token +
|
|
62
|
+
" address: " + address + " amount: " + balanceBN.toString());
|
|
63
|
+
return balanceBN;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Returns the native currency address, return the default used by the fee module
|
|
67
|
+
*/
|
|
68
|
+
getNativeCurrencyAddress() {
|
|
69
|
+
return this.root.Fees.getDefaultGasToken();
|
|
70
|
+
}
|
|
71
|
+
///////////////////
|
|
72
|
+
//// Transfers
|
|
73
|
+
/**
|
|
74
|
+
* Creates transactions for sending the over the tokens
|
|
75
|
+
*
|
|
76
|
+
* @param signer
|
|
77
|
+
* @param token token to send
|
|
78
|
+
* @param amount amount of the token to send
|
|
79
|
+
* @param recipient recipient's address
|
|
80
|
+
* @param feeRate fee rate to use for the transactions
|
|
81
|
+
* @private
|
|
82
|
+
*/
|
|
83
|
+
async txsTransfer(signer, token, amount, recipient, feeRate) {
|
|
84
|
+
const action = this.Transfer(signer, recipient, token, amount);
|
|
85
|
+
feeRate = feeRate ?? await this.root.Fees.getFeeRate();
|
|
86
|
+
this.logger.debug("txsTransfer(): transfer TX created, recipient: " + recipient.toString() +
|
|
87
|
+
" token: " + token.toString() + " amount: " + amount.toString(10));
|
|
88
|
+
return [await action.tx(feeRate)];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.StarknetTokens = StarknetTokens;
|
|
92
|
+
StarknetTokens.ERC20_ETH = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
|
|
93
|
+
StarknetTokens.ERC20_STRK = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d";
|
|
94
|
+
StarknetTokens.GasCosts = {
|
|
95
|
+
TRANSFER: { l1: 400, l2: 0 },
|
|
96
|
+
APPROVE: { l1: 400, l2: 0 }
|
|
97
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { StarknetModule } from "../StarknetModule";
|
|
2
|
+
import { Call, DeployAccountContractPayload, DeployAccountContractTransaction, Invocation, InvocationsSignerDetails, BigNumberish } from "starknet";
|
|
3
|
+
import { StarknetSigner } from "../../wallet/StarknetSigner";
|
|
4
|
+
export type StarknetTx = ({
|
|
5
|
+
type: "DEPLOY_ACCOUNT";
|
|
6
|
+
tx: DeployAccountContractPayload;
|
|
7
|
+
signed?: DeployAccountContractTransaction;
|
|
8
|
+
} | {
|
|
9
|
+
type: "INVOKE";
|
|
10
|
+
tx: Array<Call>;
|
|
11
|
+
signed?: Invocation;
|
|
12
|
+
}) & {
|
|
13
|
+
details: InvocationsSignerDetails & {
|
|
14
|
+
maxFee?: BigNumberish;
|
|
15
|
+
};
|
|
16
|
+
txId?: string;
|
|
17
|
+
};
|
|
18
|
+
export declare class StarknetTransactions extends StarknetModule {
|
|
19
|
+
private cbkBeforeTxSigned;
|
|
20
|
+
/**
|
|
21
|
+
* Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
|
|
22
|
+
* the transaction at regular interval
|
|
23
|
+
*
|
|
24
|
+
* @param tx starknet transaction to wait for confirmation for & keep re-sending until it confirms
|
|
25
|
+
* @param abortSignal signal to abort waiting for tx confirmation
|
|
26
|
+
* @private
|
|
27
|
+
*/
|
|
28
|
+
private confirmTransaction;
|
|
29
|
+
/**
|
|
30
|
+
* Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
|
|
31
|
+
*
|
|
32
|
+
* @param signer
|
|
33
|
+
* @param txs
|
|
34
|
+
* @private
|
|
35
|
+
*/
|
|
36
|
+
private prepareTransactions;
|
|
37
|
+
/**
|
|
38
|
+
* Sends out a signed transaction to the RPC
|
|
39
|
+
*
|
|
40
|
+
* @param tx Starknet tx to send
|
|
41
|
+
* @param onBeforePublish a callback called before every transaction is published
|
|
42
|
+
* @param signer
|
|
43
|
+
* @param retryOnSubmissionFailure
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
private sendSignedTransaction;
|
|
47
|
+
/**
|
|
48
|
+
* Prepares, signs , sends (in parallel or sequentially) & optionally waits for confirmation
|
|
49
|
+
* of a batch of starknet transactions
|
|
50
|
+
*
|
|
51
|
+
* @param signer
|
|
52
|
+
* @param txs transactions to send
|
|
53
|
+
* @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
|
|
54
|
+
* are re-sent at regular intervals)
|
|
55
|
+
* @param abortSignal abort signal to abort waiting for transaction confirmations
|
|
56
|
+
* @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
|
|
57
|
+
* are executed in order)
|
|
58
|
+
* @param onBeforePublish a callback called before every transaction is published
|
|
59
|
+
*/
|
|
60
|
+
sendAndConfirm(signer: StarknetSigner, txs: StarknetTx[], waitForConfirmation?: boolean, abortSignal?: AbortSignal, parallel?: boolean, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]>;
|
|
61
|
+
/**
|
|
62
|
+
* Serializes the solana transaction, saves the transaction, signers & last valid blockheight
|
|
63
|
+
*
|
|
64
|
+
* @param tx
|
|
65
|
+
*/
|
|
66
|
+
serializeTx(tx: StarknetTx): Promise<string>;
|
|
67
|
+
/**
|
|
68
|
+
* Deserializes saved solana transaction, extracting the transaction, signers & last valid blockheight
|
|
69
|
+
*
|
|
70
|
+
* @param txData
|
|
71
|
+
*/
|
|
72
|
+
deserializeTx(txData: string): Promise<StarknetTx>;
|
|
73
|
+
/**
|
|
74
|
+
* Gets the status of the raw starknet transaction
|
|
75
|
+
*
|
|
76
|
+
* @param tx
|
|
77
|
+
*/
|
|
78
|
+
getTxStatus(tx: string): Promise<"pending" | "success" | "not_found" | "reverted">;
|
|
79
|
+
/**
|
|
80
|
+
* Gets the status of the starknet transaction with a specific txId
|
|
81
|
+
*
|
|
82
|
+
* @param txId
|
|
83
|
+
*/
|
|
84
|
+
getTxIdStatus(txId: string): Promise<"pending" | "success" | "not_found" | "reverted">;
|
|
85
|
+
onBeforeTxSigned(callback: (tx: StarknetTx) => Promise<void>): void;
|
|
86
|
+
offBeforeTxSigned(callback: (tx: StarknetTx) => Promise<void>): boolean;
|
|
87
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StarknetTransactions = void 0;
|
|
4
|
+
const StarknetModule_1 = require("../StarknetModule");
|
|
5
|
+
const Utils_1 = require("../../../utils/Utils");
|
|
6
|
+
class StarknetTransactions extends StarknetModule_1.StarknetModule {
|
|
7
|
+
/**
|
|
8
|
+
* Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
|
|
9
|
+
* the transaction at regular interval
|
|
10
|
+
*
|
|
11
|
+
* @param tx starknet transaction to wait for confirmation for & keep re-sending until it confirms
|
|
12
|
+
* @param abortSignal signal to abort waiting for tx confirmation
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
async confirmTransaction(tx, abortSignal) {
|
|
16
|
+
let state = "pending";
|
|
17
|
+
while (state === "pending" || state === "not_found") {
|
|
18
|
+
await (0, Utils_1.timeoutPromise)(3, abortSignal);
|
|
19
|
+
state = await this.getTxIdStatus(tx.txId);
|
|
20
|
+
if (state === "not_found" && tx.signed != null)
|
|
21
|
+
await this.sendSignedTransaction(tx, undefined, undefined, false).catch(e => {
|
|
22
|
+
if (e.message != null && e.message.includes("59: A transaction with the same hash already exists in the mempool"))
|
|
23
|
+
return;
|
|
24
|
+
console.error("Error on transaction re-send: ", e);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (state === "reverted")
|
|
28
|
+
throw new Error("Transaction reverted!");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
|
|
32
|
+
*
|
|
33
|
+
* @param signer
|
|
34
|
+
* @param txs
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
async prepareTransactions(signer, txs) {
|
|
38
|
+
let nonce = await signer.getNonce();
|
|
39
|
+
if (nonce === BigInt(0) && signer.isWalletAccount()) {
|
|
40
|
+
//Just increment the nonce by one and hope the wallet is smart enough to deploy account first
|
|
41
|
+
nonce = BigInt(1);
|
|
42
|
+
}
|
|
43
|
+
const deployPayload = await signer.checkAndGetDeployPayload(nonce);
|
|
44
|
+
if (deployPayload != null) {
|
|
45
|
+
txs.unshift(await this.root.Accounts.getAccountDeployTransaction(deployPayload));
|
|
46
|
+
}
|
|
47
|
+
for (let i = 0; i < txs.length; i++) {
|
|
48
|
+
const tx = txs[i];
|
|
49
|
+
if (tx.details.nonce != null)
|
|
50
|
+
nonce = BigInt(tx.details.nonce); //Take the nonce from last tx
|
|
51
|
+
if (nonce == null)
|
|
52
|
+
nonce = BigInt(await this.root.provider.getNonceForAddress(signer.getAddress())); //Fetch the nonce
|
|
53
|
+
if (tx.details.nonce == null)
|
|
54
|
+
tx.details.nonce = nonce;
|
|
55
|
+
this.logger.debug("sendAndConfirm(): transaction prepared (" + (i + 1) + "/" + txs.length + "), nonce: " + tx.details.nonce);
|
|
56
|
+
nonce += BigInt(1);
|
|
57
|
+
if (this.cbkBeforeTxSigned != null)
|
|
58
|
+
await this.cbkBeforeTxSigned(tx);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Sends out a signed transaction to the RPC
|
|
63
|
+
*
|
|
64
|
+
* @param tx Starknet tx to send
|
|
65
|
+
* @param onBeforePublish a callback called before every transaction is published
|
|
66
|
+
* @param signer
|
|
67
|
+
* @param retryOnSubmissionFailure
|
|
68
|
+
* @private
|
|
69
|
+
*/
|
|
70
|
+
async sendSignedTransaction(tx, onBeforePublish, signer, retryOnSubmissionFailure = true) {
|
|
71
|
+
if (onBeforePublish != null)
|
|
72
|
+
await onBeforePublish(tx.txId, await this.serializeTx(tx));
|
|
73
|
+
this.logger.debug("sendSignedTransaction(): sending transaction: ", tx);
|
|
74
|
+
if (tx.signed == null) {
|
|
75
|
+
let txHash;
|
|
76
|
+
switch (tx.type) {
|
|
77
|
+
case "INVOKE":
|
|
78
|
+
txHash = (await signer.account.execute(tx.tx, tx.details)).transaction_hash;
|
|
79
|
+
break;
|
|
80
|
+
case "DEPLOY_ACCOUNT":
|
|
81
|
+
txHash = (await signer.account.deployAccount(tx.tx, tx.details)).transaction_hash;
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
throw new Error("Unsupported tx type!");
|
|
85
|
+
}
|
|
86
|
+
tx.txId = txHash;
|
|
87
|
+
return txHash;
|
|
88
|
+
}
|
|
89
|
+
const txResult = await (0, Utils_1.tryWithRetries)(() => {
|
|
90
|
+
switch (tx.type) {
|
|
91
|
+
case "INVOKE":
|
|
92
|
+
return this.provider.channel.invoke(tx.signed, tx.details).then(res => res.transaction_hash);
|
|
93
|
+
case "DEPLOY_ACCOUNT":
|
|
94
|
+
return this.provider.channel.deployAccount(tx.signed, tx.details).then((res) => res.transaction_hash);
|
|
95
|
+
default:
|
|
96
|
+
throw new Error("Unsupported tx type!");
|
|
97
|
+
}
|
|
98
|
+
}, retryOnSubmissionFailure ? this.retryPolicy : { maxRetries: 1 });
|
|
99
|
+
if (tx.txId !== txResult)
|
|
100
|
+
this.logger.warn("sendSignedTransaction(): sent tx hash not matching the precomputed hash!");
|
|
101
|
+
this.logger.info("sendSignedTransaction(): tx sent, expected txHash: " + tx.txId + ", txHash: " + txResult);
|
|
102
|
+
return txResult;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Prepares, signs , sends (in parallel or sequentially) & optionally waits for confirmation
|
|
106
|
+
* of a batch of starknet transactions
|
|
107
|
+
*
|
|
108
|
+
* @param signer
|
|
109
|
+
* @param txs transactions to send
|
|
110
|
+
* @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
|
|
111
|
+
* are re-sent at regular intervals)
|
|
112
|
+
* @param abortSignal abort signal to abort waiting for transaction confirmations
|
|
113
|
+
* @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
|
|
114
|
+
* are executed in order)
|
|
115
|
+
* @param onBeforePublish a callback called before every transaction is published
|
|
116
|
+
*/
|
|
117
|
+
async sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
|
|
118
|
+
await this.prepareTransactions(signer, txs);
|
|
119
|
+
if (!signer.isWalletAccount()) {
|
|
120
|
+
for (let i = 0; i < txs.length; i++) {
|
|
121
|
+
const tx = txs[i];
|
|
122
|
+
switch (tx.type) {
|
|
123
|
+
case "INVOKE":
|
|
124
|
+
tx.signed = await signer.account.buildInvocation(tx.tx, tx.details);
|
|
125
|
+
(0, Utils_1.calculateHash)(tx);
|
|
126
|
+
break;
|
|
127
|
+
case "DEPLOY_ACCOUNT":
|
|
128
|
+
tx.signed = await signer.account.buildAccountDeployPayload(tx.tx, tx.details);
|
|
129
|
+
(0, Utils_1.calculateHash)(tx);
|
|
130
|
+
break;
|
|
131
|
+
default:
|
|
132
|
+
throw new Error("Unsupported tx type!");
|
|
133
|
+
}
|
|
134
|
+
this.logger.debug("sendAndConfirm(): transaction signed (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
this.logger.debug("sendAndConfirm(): sending transactions, count: " + txs.length +
|
|
138
|
+
" waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
|
|
139
|
+
const txIds = [];
|
|
140
|
+
if (parallel) {
|
|
141
|
+
const promises = [];
|
|
142
|
+
for (let i = 0; i < txs.length; i++) {
|
|
143
|
+
const signedTx = txs[i];
|
|
144
|
+
const txId = await this.sendSignedTransaction(signedTx, onBeforePublish, signer);
|
|
145
|
+
if (waitForConfirmation)
|
|
146
|
+
promises.push(this.confirmTransaction(signedTx, abortSignal));
|
|
147
|
+
txIds.push(txId);
|
|
148
|
+
this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
|
|
149
|
+
}
|
|
150
|
+
if (promises.length > 0)
|
|
151
|
+
await Promise.all(promises);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
for (let i = 0; i < txs.length; i++) {
|
|
155
|
+
const signedTx = txs[i];
|
|
156
|
+
const txId = await this.sendSignedTransaction(signedTx, onBeforePublish, signer);
|
|
157
|
+
const confirmPromise = this.confirmTransaction(signedTx, abortSignal);
|
|
158
|
+
this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
|
|
159
|
+
//Don't await the last promise when !waitForConfirmation
|
|
160
|
+
if (i < txs.length - 1 || waitForConfirmation)
|
|
161
|
+
await confirmPromise;
|
|
162
|
+
txIds.push(txId);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
this.logger.info("sendAndConfirm(): sent transactions, count: " + txs.length +
|
|
166
|
+
" waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
|
|
167
|
+
return txIds;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Serializes the solana transaction, saves the transaction, signers & last valid blockheight
|
|
171
|
+
*
|
|
172
|
+
* @param tx
|
|
173
|
+
*/
|
|
174
|
+
serializeTx(tx) {
|
|
175
|
+
return Promise.resolve(JSON.stringify(tx, (key, value) => {
|
|
176
|
+
if (typeof (value) === "bigint")
|
|
177
|
+
return (0, Utils_1.toHex)(value);
|
|
178
|
+
return value;
|
|
179
|
+
}));
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Deserializes saved solana transaction, extracting the transaction, signers & last valid blockheight
|
|
183
|
+
*
|
|
184
|
+
* @param txData
|
|
185
|
+
*/
|
|
186
|
+
deserializeTx(txData) {
|
|
187
|
+
return Promise.resolve(JSON.parse(txData));
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Gets the status of the raw starknet transaction
|
|
191
|
+
*
|
|
192
|
+
* @param tx
|
|
193
|
+
*/
|
|
194
|
+
async getTxStatus(tx) {
|
|
195
|
+
const parsedTx = await this.deserializeTx(tx);
|
|
196
|
+
return await this.getTxIdStatus(parsedTx.txId);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Gets the status of the starknet transaction with a specific txId
|
|
200
|
+
*
|
|
201
|
+
* @param txId
|
|
202
|
+
*/
|
|
203
|
+
async getTxIdStatus(txId) {
|
|
204
|
+
const status = await this.provider.getTransactionStatus(txId).catch(e => {
|
|
205
|
+
if (e.message != null && e.message.includes("29: Transaction hash not found"))
|
|
206
|
+
return null;
|
|
207
|
+
throw e;
|
|
208
|
+
});
|
|
209
|
+
if (status == null)
|
|
210
|
+
return "not_found";
|
|
211
|
+
if (status.finality_status === "RECEIVED")
|
|
212
|
+
return "pending";
|
|
213
|
+
if (status.finality_status !== "REJECTED" && status.execution_status === "SUCCEEDED") {
|
|
214
|
+
return "success";
|
|
215
|
+
}
|
|
216
|
+
return "reverted";
|
|
217
|
+
}
|
|
218
|
+
onBeforeTxSigned(callback) {
|
|
219
|
+
this.cbkBeforeTxSigned = callback;
|
|
220
|
+
}
|
|
221
|
+
offBeforeTxSigned(callback) {
|
|
222
|
+
this.cbkBeforeTxSigned = null;
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
exports.StarknetTransactions = StarknetTransactions;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
export declare const BtcRelayAbi: readonly [{
|
|
2
|
+
readonly type: "impl";
|
|
3
|
+
readonly name: "BtcRelayImpl";
|
|
4
|
+
readonly interface_name: "btc_relay::IBtcRelay";
|
|
5
|
+
}, {
|
|
6
|
+
readonly type: "struct";
|
|
7
|
+
readonly name: "btc_relay::structs::blockheader::BlockHeader";
|
|
8
|
+
readonly members: readonly [{
|
|
9
|
+
readonly name: "reversed_version";
|
|
10
|
+
readonly type: "core::integer::u32";
|
|
11
|
+
}, {
|
|
12
|
+
readonly name: "previous_blockhash";
|
|
13
|
+
readonly type: "[core::integer::u32; 8]";
|
|
14
|
+
}, {
|
|
15
|
+
readonly name: "merkle_root";
|
|
16
|
+
readonly type: "[core::integer::u32; 8]";
|
|
17
|
+
}, {
|
|
18
|
+
readonly name: "reversed_timestamp";
|
|
19
|
+
readonly type: "core::integer::u32";
|
|
20
|
+
}, {
|
|
21
|
+
readonly name: "nbits";
|
|
22
|
+
readonly type: "core::integer::u32";
|
|
23
|
+
}, {
|
|
24
|
+
readonly name: "nonce";
|
|
25
|
+
readonly type: "core::integer::u32";
|
|
26
|
+
}];
|
|
27
|
+
}, {
|
|
28
|
+
readonly type: "struct";
|
|
29
|
+
readonly name: "core::array::Span::<btc_relay::structs::blockheader::BlockHeader>";
|
|
30
|
+
readonly members: readonly [{
|
|
31
|
+
readonly name: "snapshot";
|
|
32
|
+
readonly type: "@core::array::Array::<btc_relay::structs::blockheader::BlockHeader>";
|
|
33
|
+
}];
|
|
34
|
+
}, {
|
|
35
|
+
readonly type: "struct";
|
|
36
|
+
readonly name: "core::integer::u256";
|
|
37
|
+
readonly members: readonly [{
|
|
38
|
+
readonly name: "low";
|
|
39
|
+
readonly type: "core::integer::u128";
|
|
40
|
+
}, {
|
|
41
|
+
readonly name: "high";
|
|
42
|
+
readonly type: "core::integer::u128";
|
|
43
|
+
}];
|
|
44
|
+
}, {
|
|
45
|
+
readonly type: "struct";
|
|
46
|
+
readonly name: "btc_relay::structs::stored_blockheader::StoredBlockHeader";
|
|
47
|
+
readonly members: readonly [{
|
|
48
|
+
readonly name: "blockheader";
|
|
49
|
+
readonly type: "btc_relay::structs::blockheader::BlockHeader";
|
|
50
|
+
}, {
|
|
51
|
+
readonly name: "block_hash";
|
|
52
|
+
readonly type: "[core::integer::u32; 8]";
|
|
53
|
+
}, {
|
|
54
|
+
readonly name: "chain_work";
|
|
55
|
+
readonly type: "core::integer::u256";
|
|
56
|
+
}, {
|
|
57
|
+
readonly name: "block_height";
|
|
58
|
+
readonly type: "core::integer::u32";
|
|
59
|
+
}, {
|
|
60
|
+
readonly name: "last_diff_adjustment";
|
|
61
|
+
readonly type: "core::integer::u32";
|
|
62
|
+
}, {
|
|
63
|
+
readonly name: "prev_block_timestamps";
|
|
64
|
+
readonly type: "[core::integer::u32; 10]";
|
|
65
|
+
}];
|
|
66
|
+
}, {
|
|
67
|
+
readonly type: "interface";
|
|
68
|
+
readonly name: "btc_relay::IBtcRelay";
|
|
69
|
+
readonly items: readonly [{
|
|
70
|
+
readonly type: "function";
|
|
71
|
+
readonly name: "submit_main_blockheaders";
|
|
72
|
+
readonly inputs: readonly [{
|
|
73
|
+
readonly name: "block_headers";
|
|
74
|
+
readonly type: "core::array::Span::<btc_relay::structs::blockheader::BlockHeader>";
|
|
75
|
+
}, {
|
|
76
|
+
readonly name: "stored_header";
|
|
77
|
+
readonly type: "btc_relay::structs::stored_blockheader::StoredBlockHeader";
|
|
78
|
+
}];
|
|
79
|
+
readonly outputs: readonly [];
|
|
80
|
+
readonly state_mutability: "external";
|
|
81
|
+
}, {
|
|
82
|
+
readonly type: "function";
|
|
83
|
+
readonly name: "submit_short_fork_blockheaders";
|
|
84
|
+
readonly inputs: readonly [{
|
|
85
|
+
readonly name: "block_headers";
|
|
86
|
+
readonly type: "core::array::Span::<btc_relay::structs::blockheader::BlockHeader>";
|
|
87
|
+
}, {
|
|
88
|
+
readonly name: "stored_header";
|
|
89
|
+
readonly type: "btc_relay::structs::stored_blockheader::StoredBlockHeader";
|
|
90
|
+
}];
|
|
91
|
+
readonly outputs: readonly [];
|
|
92
|
+
readonly state_mutability: "external";
|
|
93
|
+
}, {
|
|
94
|
+
readonly type: "function";
|
|
95
|
+
readonly name: "submit_fork_blockheaders";
|
|
96
|
+
readonly inputs: readonly [{
|
|
97
|
+
readonly name: "fork_id";
|
|
98
|
+
readonly type: "core::felt252";
|
|
99
|
+
}, {
|
|
100
|
+
readonly name: "block_headers";
|
|
101
|
+
readonly type: "core::array::Span::<btc_relay::structs::blockheader::BlockHeader>";
|
|
102
|
+
}, {
|
|
103
|
+
readonly name: "stored_header";
|
|
104
|
+
readonly type: "btc_relay::structs::stored_blockheader::StoredBlockHeader";
|
|
105
|
+
}];
|
|
106
|
+
readonly outputs: readonly [];
|
|
107
|
+
readonly state_mutability: "external";
|
|
108
|
+
}];
|
|
109
|
+
}, {
|
|
110
|
+
readonly type: "impl";
|
|
111
|
+
readonly name: "BtcRelayReadOnlyImpl";
|
|
112
|
+
readonly interface_name: "btc_relay::IBtcRelayReadOnly";
|
|
113
|
+
}, {
|
|
114
|
+
readonly type: "interface";
|
|
115
|
+
readonly name: "btc_relay::IBtcRelayReadOnly";
|
|
116
|
+
readonly items: readonly [{
|
|
117
|
+
readonly type: "function";
|
|
118
|
+
readonly name: "get_chainwork";
|
|
119
|
+
readonly inputs: readonly [];
|
|
120
|
+
readonly outputs: readonly [{
|
|
121
|
+
readonly type: "core::integer::u256";
|
|
122
|
+
}];
|
|
123
|
+
readonly state_mutability: "view";
|
|
124
|
+
}, {
|
|
125
|
+
readonly type: "function";
|
|
126
|
+
readonly name: "get_blockheight";
|
|
127
|
+
readonly inputs: readonly [];
|
|
128
|
+
readonly outputs: readonly [{
|
|
129
|
+
readonly type: "core::integer::u32";
|
|
130
|
+
}];
|
|
131
|
+
readonly state_mutability: "view";
|
|
132
|
+
}, {
|
|
133
|
+
readonly type: "function";
|
|
134
|
+
readonly name: "verify_blockheader";
|
|
135
|
+
readonly inputs: readonly [{
|
|
136
|
+
readonly name: "stored_header";
|
|
137
|
+
readonly type: "btc_relay::structs::stored_blockheader::StoredBlockHeader";
|
|
138
|
+
}];
|
|
139
|
+
readonly outputs: readonly [{
|
|
140
|
+
readonly type: "core::integer::u32";
|
|
141
|
+
}];
|
|
142
|
+
readonly state_mutability: "view";
|
|
143
|
+
}, {
|
|
144
|
+
readonly type: "function";
|
|
145
|
+
readonly name: "get_commit_hash";
|
|
146
|
+
readonly inputs: readonly [{
|
|
147
|
+
readonly name: "height";
|
|
148
|
+
readonly type: "core::integer::u32";
|
|
149
|
+
}];
|
|
150
|
+
readonly outputs: readonly [{
|
|
151
|
+
readonly type: "core::felt252";
|
|
152
|
+
}];
|
|
153
|
+
readonly state_mutability: "view";
|
|
154
|
+
}, {
|
|
155
|
+
readonly type: "function";
|
|
156
|
+
readonly name: "get_tip_commit_hash";
|
|
157
|
+
readonly inputs: readonly [];
|
|
158
|
+
readonly outputs: readonly [{
|
|
159
|
+
readonly type: "core::felt252";
|
|
160
|
+
}];
|
|
161
|
+
readonly state_mutability: "view";
|
|
162
|
+
}];
|
|
163
|
+
}, {
|
|
164
|
+
readonly type: "constructor";
|
|
165
|
+
readonly name: "constructor";
|
|
166
|
+
readonly inputs: readonly [{
|
|
167
|
+
readonly name: "stored_header";
|
|
168
|
+
readonly type: "btc_relay::structs::stored_blockheader::StoredBlockHeader";
|
|
169
|
+
}];
|
|
170
|
+
}, {
|
|
171
|
+
readonly type: "event";
|
|
172
|
+
readonly name: "btc_relay::events::StoreHeader";
|
|
173
|
+
readonly kind: "struct";
|
|
174
|
+
readonly members: readonly [{
|
|
175
|
+
readonly name: "commit_hash";
|
|
176
|
+
readonly type: "core::felt252";
|
|
177
|
+
readonly kind: "key";
|
|
178
|
+
}, {
|
|
179
|
+
readonly name: "block_hash_poseidon";
|
|
180
|
+
readonly type: "core::felt252";
|
|
181
|
+
readonly kind: "key";
|
|
182
|
+
}, {
|
|
183
|
+
readonly name: "header";
|
|
184
|
+
readonly type: "btc_relay::structs::stored_blockheader::StoredBlockHeader";
|
|
185
|
+
readonly kind: "data";
|
|
186
|
+
}];
|
|
187
|
+
}, {
|
|
188
|
+
readonly type: "event";
|
|
189
|
+
readonly name: "btc_relay::events::StoreForkHeader";
|
|
190
|
+
readonly kind: "struct";
|
|
191
|
+
readonly members: readonly [{
|
|
192
|
+
readonly name: "commit_hash";
|
|
193
|
+
readonly type: "core::felt252";
|
|
194
|
+
readonly kind: "key";
|
|
195
|
+
}, {
|
|
196
|
+
readonly name: "block_hash_poseidon";
|
|
197
|
+
readonly type: "core::felt252";
|
|
198
|
+
readonly kind: "key";
|
|
199
|
+
}, {
|
|
200
|
+
readonly name: "fork_id";
|
|
201
|
+
readonly type: "core::felt252";
|
|
202
|
+
readonly kind: "key";
|
|
203
|
+
}, {
|
|
204
|
+
readonly name: "header";
|
|
205
|
+
readonly type: "btc_relay::structs::stored_blockheader::StoredBlockHeader";
|
|
206
|
+
readonly kind: "data";
|
|
207
|
+
}];
|
|
208
|
+
}, {
|
|
209
|
+
readonly type: "event";
|
|
210
|
+
readonly name: "btc_relay::events::ChainReorg";
|
|
211
|
+
readonly kind: "struct";
|
|
212
|
+
readonly members: readonly [{
|
|
213
|
+
readonly name: "fork_submitter";
|
|
214
|
+
readonly type: "core::starknet::contract_address::ContractAddress";
|
|
215
|
+
readonly kind: "key";
|
|
216
|
+
}, {
|
|
217
|
+
readonly name: "fork_id";
|
|
218
|
+
readonly type: "core::felt252";
|
|
219
|
+
readonly kind: "key";
|
|
220
|
+
}, {
|
|
221
|
+
readonly name: "tip_block_hash_poseidon";
|
|
222
|
+
readonly type: "core::felt252";
|
|
223
|
+
readonly kind: "key";
|
|
224
|
+
}, {
|
|
225
|
+
readonly name: "tip_commit_hash";
|
|
226
|
+
readonly type: "core::felt252";
|
|
227
|
+
readonly kind: "key";
|
|
228
|
+
}, {
|
|
229
|
+
readonly name: "start_height";
|
|
230
|
+
readonly type: "core::felt252";
|
|
231
|
+
readonly kind: "data";
|
|
232
|
+
}];
|
|
233
|
+
}, {
|
|
234
|
+
readonly type: "event";
|
|
235
|
+
readonly name: "btc_relay::BtcRelay::Event";
|
|
236
|
+
readonly kind: "enum";
|
|
237
|
+
readonly variants: readonly [{
|
|
238
|
+
readonly name: "StoreHeader";
|
|
239
|
+
readonly type: "btc_relay::events::StoreHeader";
|
|
240
|
+
readonly kind: "nested";
|
|
241
|
+
}, {
|
|
242
|
+
readonly name: "StoreForkHeader";
|
|
243
|
+
readonly type: "btc_relay::events::StoreForkHeader";
|
|
244
|
+
readonly kind: "nested";
|
|
245
|
+
}, {
|
|
246
|
+
readonly name: "ChainReorg";
|
|
247
|
+
readonly type: "btc_relay::events::ChainReorg";
|
|
248
|
+
readonly kind: "nested";
|
|
249
|
+
}];
|
|
250
|
+
}];
|