@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,49 @@
|
|
|
1
|
+
import {StarknetSwapData} from "../../../StarknetSwapData";
|
|
2
|
+
import {StarknetGas} from "../../../../base/StarknetAction";
|
|
3
|
+
import {ChainSwapType} from "@atomiqlabs/base";
|
|
4
|
+
import {BigNumberish} from "starknet";
|
|
5
|
+
import {StarknetTx} from "../../../../base/modules/StarknetTransactions";
|
|
6
|
+
import {bufferToU32Array, getLogger} from "../../../../../utils/Utils";
|
|
7
|
+
import {BitcoinCommitmentData, BitcoinWitnessData, IBitcoinClaimHandler} from "./IBitcoinClaimHandler";
|
|
8
|
+
|
|
9
|
+
export type BitcoinTxIdCommitmentData = {
|
|
10
|
+
txId: string
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const logger = getLogger("BitcoinTxIdClaimHandler: ");
|
|
14
|
+
|
|
15
|
+
export class BitcoinTxIdClaimHandler extends IBitcoinClaimHandler<BitcoinTxIdCommitmentData, BitcoinWitnessData> {
|
|
16
|
+
|
|
17
|
+
public static readonly type: ChainSwapType = ChainSwapType.CHAIN_TXID;
|
|
18
|
+
public static readonly gas: StarknetGas = {l1: 20000};
|
|
19
|
+
|
|
20
|
+
protected serializeCommitment(data: BitcoinTxIdCommitmentData & BitcoinCommitmentData): BigNumberish[] {
|
|
21
|
+
return [
|
|
22
|
+
...bufferToU32Array(Buffer.from(data.txId, "hex").reverse()),
|
|
23
|
+
...super.serializeCommitment(data)
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getWitness(
|
|
28
|
+
signer: string,
|
|
29
|
+
swapData: StarknetSwapData,
|
|
30
|
+
witnessData: BitcoinWitnessData,
|
|
31
|
+
feeRate?: string
|
|
32
|
+
): Promise<{
|
|
33
|
+
initialTxns: StarknetTx[];
|
|
34
|
+
witness: BigNumberish[]
|
|
35
|
+
}> {
|
|
36
|
+
if(!swapData.isClaimHandler(this.address)) throw new Error("Invalid claim handler");
|
|
37
|
+
|
|
38
|
+
return this._getWitness(signer, swapData, witnessData, {txId: witnessData.tx.txid});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getGas(data: StarknetSwapData): StarknetGas {
|
|
42
|
+
return BitcoinTxIdClaimHandler.gas;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getType(): ChainSwapType {
|
|
46
|
+
return BitcoinTxIdClaimHandler.type;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {IClaimHandler} from "../ClaimHandlers";
|
|
2
|
+
import {StarknetSwapData} from "../../../StarknetSwapData";
|
|
3
|
+
import {StarknetGas} from "../../../../base/StarknetAction";
|
|
4
|
+
import {ChainSwapType, RelaySynchronizer} from "@atomiqlabs/base";
|
|
5
|
+
import {BigNumberish, hash} from "starknet";
|
|
6
|
+
import {StarknetBtcStoredHeader} from "../../../../btcrelay/headers/StarknetBtcStoredHeader";
|
|
7
|
+
import {StarknetTx} from "../../../../base/modules/StarknetTransactions";
|
|
8
|
+
import {StarknetBtcRelay} from "../../../../btcrelay/StarknetBtcRelay";
|
|
9
|
+
import {bufferToU32Array, getLogger, toHex, tryWithRetries, u32ArrayToBuffer} from "../../../../../utils/Utils";
|
|
10
|
+
|
|
11
|
+
export type BitcoinCommitmentData = {
|
|
12
|
+
btcRelay: StarknetBtcRelay<any>,
|
|
13
|
+
confirmations: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type BitcoinWitnessData = {
|
|
17
|
+
tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
|
|
18
|
+
requiredConfirmations: number,
|
|
19
|
+
commitedHeader?: StarknetBtcStoredHeader,
|
|
20
|
+
btcRelay?: StarknetBtcRelay<any>,
|
|
21
|
+
synchronizer?: RelaySynchronizer<StarknetBtcStoredHeader, StarknetTx, any>
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const logger = getLogger("IBitcoinClaimHandler: ");
|
|
25
|
+
|
|
26
|
+
export abstract class IBitcoinClaimHandler<C, W extends BitcoinWitnessData> implements IClaimHandler<C & BitcoinCommitmentData, W> {
|
|
27
|
+
|
|
28
|
+
public readonly address: string;
|
|
29
|
+
|
|
30
|
+
constructor(address: string) {
|
|
31
|
+
this.address = address;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Gets committed header, identified by blockhash & blockheight, determines required BTC relay blockheight based on
|
|
36
|
+
* requiredConfirmations
|
|
37
|
+
* If synchronizer is passed & blockhash is not found, it produces transactions to sync up the btc relay to the
|
|
38
|
+
* current chain tip & adds them to the txs array
|
|
39
|
+
*
|
|
40
|
+
* @param signer
|
|
41
|
+
* @param btcRelay
|
|
42
|
+
* @param txBlockheight transaction blockheight
|
|
43
|
+
* @param requiredConfirmations required confirmation for the swap to be claimable with that TX
|
|
44
|
+
* @param blockhash blockhash of the block which includes the transaction
|
|
45
|
+
* @param txs solana transaction array, in case we need to synchronize the btc relay ourselves the synchronization
|
|
46
|
+
* txns are added here
|
|
47
|
+
* @param synchronizer optional synchronizer to use to synchronize the btc relay in case it is not yet synchronized
|
|
48
|
+
* to the required blockheight
|
|
49
|
+
* @param feeRate Fee rate to use for synchronization transactions
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
protected async getCommitedHeaderAndSynchronize(
|
|
53
|
+
signer: string,
|
|
54
|
+
btcRelay: StarknetBtcRelay<any>,
|
|
55
|
+
txBlockheight: number,
|
|
56
|
+
requiredConfirmations: number,
|
|
57
|
+
blockhash: string,
|
|
58
|
+
txs: StarknetTx[],
|
|
59
|
+
synchronizer?: RelaySynchronizer<StarknetBtcStoredHeader, StarknetTx, any>,
|
|
60
|
+
feeRate?: string
|
|
61
|
+
): Promise<StarknetBtcStoredHeader> {
|
|
62
|
+
const requiredBlockheight = txBlockheight+requiredConfirmations-1;
|
|
63
|
+
|
|
64
|
+
const result = await tryWithRetries(
|
|
65
|
+
() => btcRelay.retrieveLogAndBlockheight({
|
|
66
|
+
blockhash: blockhash
|
|
67
|
+
}, requiredBlockheight)
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
if(result!=null) return result.header;
|
|
71
|
+
|
|
72
|
+
//Need to synchronize
|
|
73
|
+
if(synchronizer==null) return null;
|
|
74
|
+
|
|
75
|
+
//TODO: We don't have to synchronize to tip, only to our required blockheight
|
|
76
|
+
const resp = await synchronizer.syncToLatestTxs(signer.toString(), feeRate);
|
|
77
|
+
logger.debug("getCommitedHeaderAndSynchronize(): BTC Relay not synchronized to required blockheight, "+
|
|
78
|
+
"synchronizing ourselves in "+resp.txs.length+" txs");
|
|
79
|
+
logger.debug("getCommitedHeaderAndSynchronize(): BTC Relay computed header map: ",resp.computedHeaderMap);
|
|
80
|
+
resp.txs.forEach(tx => txs.push(tx));
|
|
81
|
+
|
|
82
|
+
//Retrieve computed header
|
|
83
|
+
return resp.computedHeaderMap[txBlockheight];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public static readonly address = "";
|
|
87
|
+
public static readonly type: ChainSwapType = ChainSwapType.CHAIN_TXID;
|
|
88
|
+
public static readonly gas: StarknetGas = {l1: 20000};
|
|
89
|
+
|
|
90
|
+
protected serializeCommitment(data: BitcoinCommitmentData): BigNumberish[] {
|
|
91
|
+
return [
|
|
92
|
+
data.confirmations,
|
|
93
|
+
data.btcRelay.contract.address
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getCommitment(data: C & BitcoinCommitmentData): BigNumberish {
|
|
98
|
+
return hash.computePoseidonHashOnElements(this.serializeCommitment(data));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
protected async _getWitness(
|
|
102
|
+
signer: string,
|
|
103
|
+
swapData: StarknetSwapData,
|
|
104
|
+
{tx, btcRelay, commitedHeader, synchronizer, requiredConfirmations}: BitcoinWitnessData,
|
|
105
|
+
commitment: C,
|
|
106
|
+
feeRate?: string
|
|
107
|
+
): Promise<{
|
|
108
|
+
initialTxns: StarknetTx[];
|
|
109
|
+
witness: BigNumberish[]
|
|
110
|
+
}> {
|
|
111
|
+
const serializedData: BigNumberish[] = this.serializeCommitment({
|
|
112
|
+
...commitment,
|
|
113
|
+
btcRelay,
|
|
114
|
+
confirmations: requiredConfirmations
|
|
115
|
+
});
|
|
116
|
+
const commitmentHash = hash.computePoseidonHashOnElements(serializedData);
|
|
117
|
+
|
|
118
|
+
if(!swapData.isClaimData(commitmentHash)) throw new Error("Invalid commit data");
|
|
119
|
+
|
|
120
|
+
const merkleProof = await btcRelay.bitcoinRpc.getMerkleProof(tx.txid, tx.blockhash);
|
|
121
|
+
logger.debug("getWitness(): merkle proof computed: ", merkleProof);
|
|
122
|
+
|
|
123
|
+
const txs: StarknetTx[] = [];
|
|
124
|
+
if(commitedHeader==null) commitedHeader = await this.getCommitedHeaderAndSynchronize(
|
|
125
|
+
signer, btcRelay, tx.height, requiredConfirmations,
|
|
126
|
+
tx.blockhash, txs, synchronizer, feeRate
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
if(commitedHeader==null) throw new Error("Cannot fetch committed header!");
|
|
130
|
+
|
|
131
|
+
serializedData.push(...commitedHeader.serialize());
|
|
132
|
+
serializedData.push(merkleProof.merkle.length, ...merkleProof.merkle.map(bufferToU32Array).flat());
|
|
133
|
+
serializedData.push(merkleProof.pos);
|
|
134
|
+
|
|
135
|
+
return {initialTxns: txs, witness: serializedData};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
abstract getWitness(signer: string, data: StarknetSwapData, witnessData: W, feeRate?: string): Promise<{
|
|
139
|
+
initialTxns: StarknetTx[];
|
|
140
|
+
witness: BigNumberish[]
|
|
141
|
+
}>;
|
|
142
|
+
|
|
143
|
+
abstract getGas(data: StarknetSwapData): StarknetGas;
|
|
144
|
+
|
|
145
|
+
abstract getType(): ChainSwapType;
|
|
146
|
+
|
|
147
|
+
parseWitnessResult(result: BigNumberish[]): string {
|
|
148
|
+
return u32ArrayToBuffer(result).toString("hex");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {StarknetTx} from "../../../base/modules/StarknetTransactions";
|
|
2
|
+
import {StarknetSwapData} from "../../StarknetSwapData";
|
|
3
|
+
import {bigNumberishToBuffer, toBigInt} from "../../../../utils/Utils";
|
|
4
|
+
import {BigNumberish} from "starknet";
|
|
5
|
+
import {IHandler} from "../IHandler";
|
|
6
|
+
import {StarknetGas} from "../../../base/StarknetAction";
|
|
7
|
+
|
|
8
|
+
export class TimelockRefundHandler implements IHandler<bigint, never> {
|
|
9
|
+
|
|
10
|
+
public readonly address: string;
|
|
11
|
+
public static readonly gas: StarknetGas = {l1: 500};
|
|
12
|
+
|
|
13
|
+
constructor(address: string) {
|
|
14
|
+
this.address = address;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public getCommitment(data: bigint): BigNumberish {
|
|
18
|
+
return data;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public getWitness(signer: string, data: StarknetSwapData): Promise<{
|
|
22
|
+
initialTxns: StarknetTx[],
|
|
23
|
+
witness: BigNumberish[]
|
|
24
|
+
}> {
|
|
25
|
+
const expiry = TimelockRefundHandler.getExpiry(data);
|
|
26
|
+
const currentTimestamp = BigInt(Math.floor(Date.now()/1000));
|
|
27
|
+
if(expiry > currentTimestamp) throw new Error("Not expired yet!");
|
|
28
|
+
return Promise.resolve({initialTxns: [], witness: []});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getGas(): StarknetGas {
|
|
32
|
+
return TimelockRefundHandler.gas;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public static getExpiry(data: StarknetSwapData): bigint {
|
|
36
|
+
return bigNumberishToBuffer(data.refundData, 32).readBigUInt64BE(24);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import {toBigInt} from "../../../utils/Utils";
|
|
2
|
+
import { IntermediaryReputationType } from "@atomiqlabs/base";
|
|
3
|
+
import {StarknetSwapModule} from "../StarknetSwapModule";
|
|
4
|
+
import {StarknetAction} from "../../base/StarknetAction";
|
|
5
|
+
import {cairo} from "starknet";
|
|
6
|
+
import {StarknetTx} from "../../base/modules/StarknetTransactions";
|
|
7
|
+
|
|
8
|
+
export class StarknetLpVault extends StarknetSwapModule {
|
|
9
|
+
|
|
10
|
+
private static readonly GasCosts = {
|
|
11
|
+
WITHDRAW: {l1: 750, l2: 0},
|
|
12
|
+
DEPOSIT: {l1: 750, l2: 0}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Action for withdrawing funds from the LP vault
|
|
17
|
+
*
|
|
18
|
+
* @param signer
|
|
19
|
+
* @param token
|
|
20
|
+
* @param amount
|
|
21
|
+
* @constructor
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
private Withdraw(signer: string, token: string, amount: bigint): StarknetAction {
|
|
25
|
+
return new StarknetAction(signer, this.root,
|
|
26
|
+
this.contract.populateTransaction.withdraw(token, cairo.uint256(amount), signer),
|
|
27
|
+
StarknetLpVault.GasCosts.WITHDRAW
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Action for depositing funds to the LP vault
|
|
33
|
+
*
|
|
34
|
+
* @param signer
|
|
35
|
+
* @param token
|
|
36
|
+
* @param amount
|
|
37
|
+
* @constructor
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
private Deposit(signer: string, token: string, amount: bigint): StarknetAction {
|
|
41
|
+
return new StarknetAction(signer, this.root,
|
|
42
|
+
this.contract.populateTransaction.deposit(token, cairo.uint256(amount)),
|
|
43
|
+
StarknetLpVault.GasCosts.WITHDRAW
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns intermediary's reputation & vault balance for a specific token
|
|
49
|
+
*
|
|
50
|
+
* @param address
|
|
51
|
+
* @param token
|
|
52
|
+
*/
|
|
53
|
+
public async getIntermediaryData(address: string, token: string): Promise<{
|
|
54
|
+
balance: bigint,
|
|
55
|
+
reputation: IntermediaryReputationType
|
|
56
|
+
}> {
|
|
57
|
+
const [balance, reputation] = await Promise.all([
|
|
58
|
+
this.getIntermediaryBalance(address, token),
|
|
59
|
+
this.getIntermediaryReputation(address, token)
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
return {balance, reputation};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Returns intermediary's reputation for a specific token
|
|
67
|
+
*
|
|
68
|
+
* @param address
|
|
69
|
+
* @param token
|
|
70
|
+
*/
|
|
71
|
+
public async getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType> {
|
|
72
|
+
const filter = Object.keys(this.root.claimHandlersByAddress).map(claimHandler => cairo.tuple(address, token, claimHandler));
|
|
73
|
+
const rawReputation = await this.provider.callContract(this.contract.populateTransaction.get_reputation(filter));
|
|
74
|
+
const length = toBigInt(rawReputation.shift());
|
|
75
|
+
if(Number(length)!==filter.length) throw new Error("getIntermediaryReputation(): Invalid response length");
|
|
76
|
+
|
|
77
|
+
const result: any = {};
|
|
78
|
+
Object.keys(this.root.claimHandlersByAddress).forEach((address) => {
|
|
79
|
+
const handler = this.root.claimHandlersByAddress[address];
|
|
80
|
+
result[handler.getType()] = {
|
|
81
|
+
successVolume: toBigInt({low: rawReputation.shift(), high: rawReputation.shift()}),
|
|
82
|
+
successCount: toBigInt(rawReputation.shift()),
|
|
83
|
+
coopCloseVolume: toBigInt({low: rawReputation.shift(), high: rawReputation.shift()}),
|
|
84
|
+
coopCloseCount: toBigInt(rawReputation.shift()),
|
|
85
|
+
failVolume: toBigInt({low: rawReputation.shift(), high: rawReputation.shift()}),
|
|
86
|
+
failCount: toBigInt(rawReputation.shift()),
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
return result as any;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Returns the balance of the token an intermediary has in his LP vault
|
|
94
|
+
*
|
|
95
|
+
* @param address
|
|
96
|
+
* @param token
|
|
97
|
+
*/
|
|
98
|
+
public async getIntermediaryBalance(address: string, token: string): Promise<bigint> {
|
|
99
|
+
const balance = toBigInt((await this.contract.get_balance([cairo.tuple(address, token)]))[0]);
|
|
100
|
+
|
|
101
|
+
this.logger.debug("getIntermediaryBalance(): token LP balance fetched, token: "+token.toString()+
|
|
102
|
+
" address: "+address+" amount: "+(balance==null ? "null" : balance.toString()));
|
|
103
|
+
|
|
104
|
+
return balance;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Creates transactions for withdrawing funds from the LP vault, creates ATA if it doesn't exist and unwraps
|
|
109
|
+
* WSOL to SOL if required
|
|
110
|
+
*
|
|
111
|
+
* @param signer
|
|
112
|
+
* @param token
|
|
113
|
+
* @param amount
|
|
114
|
+
* @param feeRate
|
|
115
|
+
*/
|
|
116
|
+
public async txsWithdraw(signer: string, token: string, amount: bigint, feeRate?: string): Promise<StarknetTx[]> {
|
|
117
|
+
const action = await this.Withdraw(signer, token, amount);
|
|
118
|
+
|
|
119
|
+
feeRate ??= await this.root.Fees.getFeeRate();
|
|
120
|
+
|
|
121
|
+
this.logger.debug("txsWithdraw(): withdraw TX created, token: "+token.toString()+
|
|
122
|
+
" amount: "+amount.toString(10));
|
|
123
|
+
|
|
124
|
+
return [await action.tx(feeRate)];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Creates transaction for depositing funds into the LP vault, wraps SOL to WSOL if required
|
|
129
|
+
*
|
|
130
|
+
* @param signer
|
|
131
|
+
* @param token
|
|
132
|
+
* @param amount
|
|
133
|
+
* @param feeRate
|
|
134
|
+
*/
|
|
135
|
+
public async txsDeposit(signer: string, token: string, amount: bigint, feeRate?: string): Promise<StarknetTx[]> {
|
|
136
|
+
//Approve first
|
|
137
|
+
const action = await this.root.Tokens.Approve(signer, this.contract.address, token, amount);
|
|
138
|
+
action.add(this.Deposit(signer, token, amount));
|
|
139
|
+
|
|
140
|
+
feeRate ??= await this.root.Fees.getFeeRate();
|
|
141
|
+
|
|
142
|
+
this.logger.debug("txsDeposit(): deposit TX created, token: "+token.toString()+
|
|
143
|
+
" amount: "+amount.toString(10));
|
|
144
|
+
|
|
145
|
+
return [await action.tx(feeRate)];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {ChainSwapType, RelaySynchronizer, SwapDataVerificationError} from "@atomiqlabs/base";
|
|
2
|
+
import {toHex} from "../../../utils/Utils";
|
|
3
|
+
import {StarknetSwapModule} from "../StarknetSwapModule";
|
|
4
|
+
import {StarknetSwapData} from "../StarknetSwapData";
|
|
5
|
+
import {StarknetAction, StarknetGas, sumStarknetGas} from "../../base/StarknetAction";
|
|
6
|
+
import {BigNumberish} from "starknet";
|
|
7
|
+
import {IClaimHandler} from "../handlers/claim/ClaimHandlers";
|
|
8
|
+
import {StarknetTx} from "../../base/modules/StarknetTransactions";
|
|
9
|
+
import {StarknetFees} from "../../base/modules/StarknetFees";
|
|
10
|
+
import {StarknetBtcStoredHeader} from "../../btcrelay/headers/StarknetBtcStoredHeader";
|
|
11
|
+
import {BitcoinOutputWitnessData} from "../handlers/claim/btc/BitcoinOutputClaimHandler";
|
|
12
|
+
import {BitcoinWitnessData} from "../handlers/claim/btc/IBitcoinClaimHandler";
|
|
13
|
+
import {Buffer} from "buffer";
|
|
14
|
+
|
|
15
|
+
export class StarknetSwapClaim extends StarknetSwapModule {
|
|
16
|
+
|
|
17
|
+
private static readonly GasCosts = {
|
|
18
|
+
CLAIM: {l1: 500, l2: 0},
|
|
19
|
+
CLAIM_PAY_OUT: {l1: 1000, l2: 0}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Claim action which uses the provided witness for claiming the swap
|
|
24
|
+
*
|
|
25
|
+
* @param signer
|
|
26
|
+
* @param swapData
|
|
27
|
+
* @param witness
|
|
28
|
+
* @param claimHandlerGas
|
|
29
|
+
* @constructor
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
private Claim(
|
|
33
|
+
signer: string,
|
|
34
|
+
swapData: StarknetSwapData,
|
|
35
|
+
witness: BigNumberish[],
|
|
36
|
+
claimHandlerGas?: StarknetGas
|
|
37
|
+
): StarknetAction {
|
|
38
|
+
return new StarknetAction(signer, this.root,
|
|
39
|
+
this.contract.populateTransaction.claim(swapData.toEscrowStruct(), witness),
|
|
40
|
+
sumStarknetGas(swapData.payOut ? StarknetSwapClaim.GasCosts.CLAIM_PAY_OUT : StarknetSwapClaim.GasCosts.CLAIM, claimHandlerGas)
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates transactions claiming the swap using a secret (for HTLC swaps)
|
|
46
|
+
*
|
|
47
|
+
* @param signer
|
|
48
|
+
* @param swapData swap to claim
|
|
49
|
+
* @param secret hex encoded secret pre-image to the HTLC hash
|
|
50
|
+
* @param checkExpiry whether to check if the swap is already expired (trying to claim an expired swap with a secret
|
|
51
|
+
* is dangerous because we might end up revealing the secret to the counterparty without being able to claim the swap)
|
|
52
|
+
* @param feeRate fee rate to use for the transaction
|
|
53
|
+
*/
|
|
54
|
+
async txsClaimWithSecret(
|
|
55
|
+
signer: string,
|
|
56
|
+
swapData: StarknetSwapData,
|
|
57
|
+
secret: string,
|
|
58
|
+
checkExpiry?: boolean,
|
|
59
|
+
feeRate?: string
|
|
60
|
+
): Promise<StarknetTx[]> {
|
|
61
|
+
//We need to be sure that this transaction confirms in time, otherwise we reveal the secret to the counterparty
|
|
62
|
+
// and won't claim the funds
|
|
63
|
+
if(checkExpiry && await this.root.isExpired(swapData.claimer.toString(), swapData)) {
|
|
64
|
+
throw new SwapDataVerificationError("Not enough time to reliably pay the invoice");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const claimHandler: IClaimHandler<Buffer, string> = this.root.claimHandlersByAddress[swapData.claimHandler.toLowerCase()];
|
|
68
|
+
if(claimHandler==null) throw new SwapDataVerificationError("Unknown claim handler!");
|
|
69
|
+
if(claimHandler.getType()!==ChainSwapType.HTLC) throw new SwapDataVerificationError("Invalid claim handler!");
|
|
70
|
+
|
|
71
|
+
feeRate ??= await this.root.Fees.getFeeRate();
|
|
72
|
+
|
|
73
|
+
const {initialTxns, witness} = await claimHandler.getWitness(signer, swapData, secret, feeRate);
|
|
74
|
+
const action = this.Claim(signer, swapData, witness, claimHandler.getGas(swapData));
|
|
75
|
+
await action.addToTxs(initialTxns, feeRate);
|
|
76
|
+
|
|
77
|
+
this.logger.debug("txsClaimWithSecret(): creating claim transaction, swap: "+swapData.getClaimHash()+" witness: ", witness.map(toHex));
|
|
78
|
+
|
|
79
|
+
return initialTxns;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Creates transaction claiming the swap using a confirmed transaction data (for BTC on-chain swaps)
|
|
84
|
+
*
|
|
85
|
+
* @param signer
|
|
86
|
+
* @param swapData swap to claim
|
|
87
|
+
* @param tx bitcoin transaction that satisfies the swap condition
|
|
88
|
+
* @param requiredConfirmations
|
|
89
|
+
* @param vout vout of the bitcoin transaction that satisfies the swap condition
|
|
90
|
+
* @param commitedHeader commited header data from btc relay (fetched internally if null)
|
|
91
|
+
* @param synchronizer optional synchronizer to use in case we need to sync up the btc relay ourselves
|
|
92
|
+
* @param feeRate fee rate to be used for the transactions
|
|
93
|
+
*/
|
|
94
|
+
async txsClaimWithTxData(
|
|
95
|
+
signer: string,
|
|
96
|
+
swapData: StarknetSwapData,
|
|
97
|
+
tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
|
|
98
|
+
requiredConfirmations: number,
|
|
99
|
+
vout: number,
|
|
100
|
+
commitedHeader?: StarknetBtcStoredHeader,
|
|
101
|
+
synchronizer?: RelaySynchronizer<StarknetBtcStoredHeader, StarknetTx, any>,
|
|
102
|
+
feeRate?: string
|
|
103
|
+
): Promise<StarknetTx[] | null> {
|
|
104
|
+
const claimHandler: IClaimHandler<any, BitcoinOutputWitnessData | BitcoinWitnessData> = this.root.claimHandlersByAddress[swapData.claimHandler.toLowerCase()];
|
|
105
|
+
if(claimHandler==null) throw new SwapDataVerificationError("Unknown claim handler!");
|
|
106
|
+
if(
|
|
107
|
+
claimHandler.getType()!==ChainSwapType.CHAIN_NONCED &&
|
|
108
|
+
claimHandler.getType()!==ChainSwapType.CHAIN_TXID &&
|
|
109
|
+
claimHandler.getType()!==ChainSwapType.CHAIN
|
|
110
|
+
) throw new SwapDataVerificationError("Invalid claim handler!");
|
|
111
|
+
|
|
112
|
+
feeRate ??= await this.root.Fees.getFeeRate();
|
|
113
|
+
|
|
114
|
+
const {initialTxns, witness} = await claimHandler.getWitness(signer, swapData, {
|
|
115
|
+
tx,
|
|
116
|
+
vout,
|
|
117
|
+
requiredConfirmations,
|
|
118
|
+
commitedHeader,
|
|
119
|
+
btcRelay: this.root.btcRelay,
|
|
120
|
+
synchronizer,
|
|
121
|
+
}, feeRate);
|
|
122
|
+
const action = this.Claim(signer, swapData, witness, claimHandler.getGas(swapData));
|
|
123
|
+
await action.addToTxs(initialTxns, feeRate);
|
|
124
|
+
|
|
125
|
+
return initialTxns;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Get the estimated starknet transaction fee of the claim transaction
|
|
130
|
+
*/
|
|
131
|
+
public async getClaimFee(swapData: StarknetSwapData, feeRate?: string): Promise<bigint> {
|
|
132
|
+
feeRate ??= await this.root.Fees.getFeeRate();
|
|
133
|
+
|
|
134
|
+
let gasRequired = swapData.payOut ? StarknetSwapClaim.GasCosts.CLAIM_PAY_OUT : StarknetSwapClaim.GasCosts.CLAIM;
|
|
135
|
+
|
|
136
|
+
const claimHandler: IClaimHandler<any, any> = this.root.claimHandlersByAddress[swapData.claimHandler.toLowerCase()];
|
|
137
|
+
if(claimHandler!=null) gasRequired = sumStarknetGas(gasRequired, claimHandler.getGas(swapData));
|
|
138
|
+
|
|
139
|
+
return StarknetFees.getGasFee(gasRequired.l1, feeRate);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
}
|