@atomiqlabs/chain-evm 1.0.0-dev.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/chains/citrea/CitreaChainType.d.ts +13 -0
- package/dist/chains/citrea/CitreaChainType.js +2 -0
- package/dist/chains/citrea/CitreaInitializer.d.ts +30 -0
- package/dist/chains/citrea/CitreaInitializer.js +120 -0
- package/dist/evm/btcrelay/BtcRelayAbi.d.ts +198 -0
- package/dist/evm/btcrelay/BtcRelayAbi.js +261 -0
- package/dist/evm/btcrelay/BtcRelayTypechain.d.ts +172 -0
- package/dist/evm/btcrelay/BtcRelayTypechain.js +2 -0
- package/dist/evm/btcrelay/EVMBtcRelay.d.ts +188 -0
- package/dist/evm/btcrelay/EVMBtcRelay.js +419 -0
- package/dist/evm/btcrelay/headers/EVMBtcHeader.d.ts +33 -0
- package/dist/evm/btcrelay/headers/EVMBtcHeader.js +84 -0
- package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.d.ts +56 -0
- package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.js +123 -0
- package/dist/evm/chain/EVMChainInterface.d.ts +51 -0
- package/dist/evm/chain/EVMChainInterface.js +90 -0
- package/dist/evm/chain/EVMModule.d.ts +9 -0
- package/dist/evm/chain/EVMModule.js +13 -0
- package/dist/evm/chain/modules/ERC20Abi.d.ts +168 -0
- package/dist/evm/chain/modules/ERC20Abi.js +225 -0
- package/dist/evm/chain/modules/EVMAddresses.d.ts +9 -0
- package/dist/evm/chain/modules/EVMAddresses.js +26 -0
- package/dist/evm/chain/modules/EVMBlocks.d.ts +20 -0
- package/dist/evm/chain/modules/EVMBlocks.js +64 -0
- package/dist/evm/chain/modules/EVMEvents.d.ts +36 -0
- package/dist/evm/chain/modules/EVMEvents.js +122 -0
- package/dist/evm/chain/modules/EVMFees.d.ts +35 -0
- package/dist/evm/chain/modules/EVMFees.js +73 -0
- package/dist/evm/chain/modules/EVMSignatures.d.ts +29 -0
- package/dist/evm/chain/modules/EVMSignatures.js +68 -0
- package/dist/evm/chain/modules/EVMTokens.d.ts +49 -0
- package/dist/evm/chain/modules/EVMTokens.js +105 -0
- package/dist/evm/chain/modules/EVMTransactions.d.ts +89 -0
- package/dist/evm/chain/modules/EVMTransactions.js +216 -0
- package/dist/evm/contract/EVMContractBase.d.ts +22 -0
- package/dist/evm/contract/EVMContractBase.js +34 -0
- package/dist/evm/contract/EVMContractModule.d.ts +8 -0
- package/dist/evm/contract/EVMContractModule.js +11 -0
- package/dist/evm/contract/modules/EVMContractEvents.d.ts +42 -0
- package/dist/evm/contract/modules/EVMContractEvents.js +75 -0
- package/dist/evm/events/EVMChainEvents.d.ts +22 -0
- package/dist/evm/events/EVMChainEvents.js +67 -0
- package/dist/evm/events/EVMChainEventsBrowser.d.ts +86 -0
- package/dist/evm/events/EVMChainEventsBrowser.js +294 -0
- package/dist/evm/spv_swap/EVMSpvVaultContract.d.ts +64 -0
- package/dist/evm/spv_swap/EVMSpvVaultContract.js +410 -0
- package/dist/evm/spv_swap/EVMSpvVaultData.d.ts +38 -0
- package/dist/evm/spv_swap/EVMSpvVaultData.js +159 -0
- package/dist/evm/spv_swap/EVMSpvWithdrawalData.d.ts +19 -0
- package/dist/evm/spv_swap/EVMSpvWithdrawalData.js +55 -0
- package/dist/evm/spv_swap/SpvVaultContractAbi.d.ts +91 -0
- package/dist/evm/spv_swap/SpvVaultContractAbi.js +849 -0
- package/dist/evm/spv_swap/SpvVaultContractTypechain.d.ts +450 -0
- package/dist/evm/spv_swap/SpvVaultContractTypechain.js +2 -0
- package/dist/evm/swaps/EVMSwapContract.d.ts +192 -0
- package/dist/evm/swaps/EVMSwapContract.js +373 -0
- package/dist/evm/swaps/EVMSwapData.d.ts +64 -0
- package/dist/evm/swaps/EVMSwapData.js +254 -0
- package/dist/evm/swaps/EVMSwapModule.d.ts +9 -0
- package/dist/evm/swaps/EVMSwapModule.js +11 -0
- package/dist/evm/swaps/EscrowManagerAbi.d.ts +120 -0
- package/dist/evm/swaps/EscrowManagerAbi.js +985 -0
- package/dist/evm/swaps/EscrowManagerTypechain.d.ts +475 -0
- package/dist/evm/swaps/EscrowManagerTypechain.js +2 -0
- package/dist/evm/swaps/handlers/IHandler.d.ts +13 -0
- package/dist/evm/swaps/handlers/IHandler.js +2 -0
- package/dist/evm/swaps/handlers/claim/ClaimHandlers.d.ts +10 -0
- package/dist/evm/swaps/handlers/claim/ClaimHandlers.js +13 -0
- package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.d.ts +20 -0
- package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.js +39 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +59 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +51 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +21 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +28 -0
- package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +48 -0
- package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +63 -0
- package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -0
- package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.js +28 -0
- package/dist/evm/swaps/modules/EVMLpVault.d.ts +69 -0
- package/dist/evm/swaps/modules/EVMLpVault.js +131 -0
- package/dist/evm/swaps/modules/EVMSwapClaim.d.ts +53 -0
- package/dist/evm/swaps/modules/EVMSwapClaim.js +101 -0
- package/dist/evm/swaps/modules/EVMSwapInit.d.ts +88 -0
- package/dist/evm/swaps/modules/EVMSwapInit.js +241 -0
- package/dist/evm/swaps/modules/EVMSwapRefund.d.ts +62 -0
- package/dist/evm/swaps/modules/EVMSwapRefund.js +132 -0
- package/dist/evm/typechain/common.d.ts +50 -0
- package/dist/evm/typechain/common.js +2 -0
- package/dist/evm/wallet/EVMSigner.d.ts +9 -0
- package/dist/evm/wallet/EVMSigner.js +16 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +53 -0
- package/dist/utils/Utils.d.ts +15 -0
- package/dist/utils/Utils.js +71 -0
- package/package.json +37 -0
- package/src/chains/citrea/CitreaChainType.ts +28 -0
- package/src/chains/citrea/CitreaInitializer.ts +167 -0
- package/src/evm/btcrelay/BtcRelayAbi.ts +258 -0
- package/src/evm/btcrelay/BtcRelayTypechain.ts +371 -0
- package/src/evm/btcrelay/EVMBtcRelay.ts +517 -0
- package/src/evm/btcrelay/headers/EVMBtcHeader.ts +110 -0
- package/src/evm/btcrelay/headers/EVMBtcStoredHeader.ts +153 -0
- package/src/evm/chain/EVMChainInterface.ts +157 -0
- package/src/evm/chain/EVMModule.ts +21 -0
- package/src/evm/chain/modules/ERC20Abi.ts +222 -0
- package/src/evm/chain/modules/EVMAddresses.ts +24 -0
- package/src/evm/chain/modules/EVMBlocks.ts +75 -0
- package/src/evm/chain/modules/EVMEvents.ts +139 -0
- package/src/evm/chain/modules/EVMFees.ts +105 -0
- package/src/evm/chain/modules/EVMSignatures.ts +76 -0
- package/src/evm/chain/modules/EVMTokens.ts +115 -0
- package/src/evm/chain/modules/EVMTransactions.ts +246 -0
- package/src/evm/contract/EVMContractBase.ts +63 -0
- package/src/evm/contract/EVMContractModule.ts +16 -0
- package/src/evm/contract/modules/EVMContractEvents.ts +102 -0
- package/src/evm/events/EVMChainEvents.ts +81 -0
- package/src/evm/events/EVMChainEventsBrowser.ts +390 -0
- package/src/evm/spv_swap/EVMSpvVaultContract.ts +533 -0
- package/src/evm/spv_swap/EVMSpvVaultData.ts +201 -0
- package/src/evm/spv_swap/EVMSpvWithdrawalData.ts +70 -0
- package/src/evm/spv_swap/SpvVaultContractAbi.ts +846 -0
- package/src/evm/spv_swap/SpvVaultContractTypechain.ts +685 -0
- package/src/evm/swaps/EVMSwapContract.ts +590 -0
- package/src/evm/swaps/EVMSwapData.ts +367 -0
- package/src/evm/swaps/EVMSwapModule.ts +16 -0
- package/src/evm/swaps/EscrowManagerAbi.ts +982 -0
- package/src/evm/swaps/EscrowManagerTypechain.ts +723 -0
- package/src/evm/swaps/handlers/IHandler.ts +17 -0
- package/src/evm/swaps/handlers/claim/ClaimHandlers.ts +20 -0
- package/src/evm/swaps/handlers/claim/HashlockClaimHandler.ts +47 -0
- package/src/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +82 -0
- package/src/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +76 -0
- package/src/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +46 -0
- package/src/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +115 -0
- package/src/evm/swaps/handlers/refund/TimelockRefundHandler.ts +38 -0
- package/src/evm/swaps/modules/EVMLpVault.ts +153 -0
- package/src/evm/swaps/modules/EVMSwapClaim.ts +141 -0
- package/src/evm/swaps/modules/EVMSwapInit.ts +292 -0
- package/src/evm/swaps/modules/EVMSwapRefund.ts +198 -0
- package/src/evm/typechain/common.ts +131 -0
- package/src/evm/wallet/EVMSigner.ts +23 -0
- package/src/index.ts +44 -0
- package/src/utils/Utils.ts +81 -0
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EVMSpvVaultContract = exports.unpackOwnerAndVaultId = exports.packOwnerAndVaultId = void 0;
|
|
4
|
+
const base_1 = require("@atomiqlabs/base");
|
|
5
|
+
const buffer_1 = require("buffer");
|
|
6
|
+
const EVMContractBase_1 = require("../contract/EVMContractBase");
|
|
7
|
+
const SpvVaultContractAbi_1 = require("./SpvVaultContractAbi");
|
|
8
|
+
const EVMBtcRelay_1 = require("../btcrelay/EVMBtcRelay");
|
|
9
|
+
const Utils_1 = require("../../utils/Utils");
|
|
10
|
+
const ethers_1 = require("ethers");
|
|
11
|
+
const EVMAddresses_1 = require("../chain/modules/EVMAddresses");
|
|
12
|
+
const EVMSpvVaultData_1 = require("./EVMSpvVaultData");
|
|
13
|
+
const EVMSpvWithdrawalData_1 = require("./EVMSpvWithdrawalData");
|
|
14
|
+
const EVMFees_1 = require("../chain/modules/EVMFees");
|
|
15
|
+
function decodeUtxo(utxo) {
|
|
16
|
+
const [txId, vout] = utxo.split(":");
|
|
17
|
+
return {
|
|
18
|
+
txHash: "0x" + buffer_1.Buffer.from(txId, "hex").reverse().toString("hex"),
|
|
19
|
+
vout: BigInt(vout)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function packOwnerAndVaultId(owner, vaultId) {
|
|
23
|
+
if (owner.length !== 42)
|
|
24
|
+
throw new Error("Invalid owner address");
|
|
25
|
+
return owner.toLowerCase() + base_1.BigIntBufferUtils.toBuffer(vaultId, "be", 12).toString("hex");
|
|
26
|
+
}
|
|
27
|
+
exports.packOwnerAndVaultId = packOwnerAndVaultId;
|
|
28
|
+
function unpackOwnerAndVaultId(data) {
|
|
29
|
+
return [(0, ethers_1.getAddress)(data.substring(0, 42)), BigInt("0x" + data.substring(42, 66))];
|
|
30
|
+
}
|
|
31
|
+
exports.unpackOwnerAndVaultId = unpackOwnerAndVaultId;
|
|
32
|
+
class EVMSpvVaultContract extends EVMContractBase_1.EVMContractBase {
|
|
33
|
+
constructor(chainInterface, btcRelay, bitcoinRpc, contractAddress, contractDeploymentHeight) {
|
|
34
|
+
super(chainInterface, contractAddress, SpvVaultContractAbi_1.SpvVaultContractAbi, contractDeploymentHeight);
|
|
35
|
+
this.claimTimeout = 180;
|
|
36
|
+
this.logger = (0, Utils_1.getLogger)("EVMSpvVaultContract: ");
|
|
37
|
+
this.btcRelay = btcRelay;
|
|
38
|
+
this.bitcoinRpc = bitcoinRpc;
|
|
39
|
+
}
|
|
40
|
+
//Transactions
|
|
41
|
+
async Open(signer, vault, feeRate) {
|
|
42
|
+
const { txHash, vout } = decodeUtxo(vault.getUtxo());
|
|
43
|
+
const tokens = vault.getTokenData();
|
|
44
|
+
if (tokens.length !== 2)
|
|
45
|
+
throw new Error("Must specify exactly 2 tokens for vault!");
|
|
46
|
+
const tx = await this.contract.open.populateTransaction(vault.vaultId, vault.getVaultParamsStruct(), txHash, vout);
|
|
47
|
+
tx.from = signer;
|
|
48
|
+
EVMFees_1.EVMFees.applyFeeRate(tx, EVMSpvVaultContract.GasCosts.OPEN, feeRate);
|
|
49
|
+
return tx;
|
|
50
|
+
}
|
|
51
|
+
async Deposit(signer, vault, rawAmounts, feeRate) {
|
|
52
|
+
let value = 0n;
|
|
53
|
+
if (vault.token0.token.toLowerCase() === this.Chain.getNativeCurrencyAddress().toLowerCase())
|
|
54
|
+
value += rawAmounts[0] * vault.token0.multiplier;
|
|
55
|
+
if (vault.token1.token.toLowerCase() === this.Chain.getNativeCurrencyAddress().toLowerCase())
|
|
56
|
+
value += (rawAmounts[1] ?? 0n) * vault.token1.multiplier;
|
|
57
|
+
const tx = await this.contract.deposit.populateTransaction(vault.owner, vault.vaultId, vault.getVaultParamsStruct(), rawAmounts[0], rawAmounts[1] ?? 0n, { value });
|
|
58
|
+
tx.from = signer;
|
|
59
|
+
EVMFees_1.EVMFees.applyFeeRate(tx, EVMSpvVaultContract.GasCosts.DEPOSIT, feeRate);
|
|
60
|
+
return tx;
|
|
61
|
+
}
|
|
62
|
+
async Front(signer, vault, data, withdrawalSequence, feeRate) {
|
|
63
|
+
let value = 0n;
|
|
64
|
+
const frontingAmount = data.getFrontingAmount();
|
|
65
|
+
if (vault.token0.token.toLowerCase() === this.Chain.getNativeCurrencyAddress().toLowerCase())
|
|
66
|
+
value += frontingAmount[0] * vault.token0.multiplier;
|
|
67
|
+
if (vault.token1.token.toLowerCase() === this.Chain.getNativeCurrencyAddress().toLowerCase())
|
|
68
|
+
value += (frontingAmount[1] ?? 0n) * vault.token1.multiplier;
|
|
69
|
+
const tx = await this.contract.front.populateTransaction(vault.owner, vault.vaultId, vault.getVaultParamsStruct(), withdrawalSequence, data.getTxHash(), data.serializeToStruct(), { value });
|
|
70
|
+
tx.from = signer;
|
|
71
|
+
EVMFees_1.EVMFees.applyFeeRate(tx, EVMSpvVaultContract.GasCosts.FRONT, feeRate);
|
|
72
|
+
return tx;
|
|
73
|
+
}
|
|
74
|
+
async Claim(signer, vault, data, blockheader, merkle, position, feeRate) {
|
|
75
|
+
const tx = await this.contract.claim.populateTransaction(vault.owner, vault.vaultId, vault.getVaultParamsStruct(), "0x" + data.btcTx.hex, blockheader.serializeToStruct(), merkle, position);
|
|
76
|
+
tx.from = signer;
|
|
77
|
+
EVMFees_1.EVMFees.applyFeeRate(tx, EVMSpvVaultContract.GasCosts.CLAIM, feeRate);
|
|
78
|
+
return tx;
|
|
79
|
+
}
|
|
80
|
+
async checkWithdrawalTx(tx) {
|
|
81
|
+
const result = await this.contract.parseBitcoinTx(buffer_1.Buffer.from(tx.btcTx.hex, "hex"));
|
|
82
|
+
if (result == null)
|
|
83
|
+
throw new Error("Failed to parse transaction!");
|
|
84
|
+
}
|
|
85
|
+
createVaultData(owner, vaultId, utxo, confirmations, tokenData) {
|
|
86
|
+
if (tokenData.length !== 2)
|
|
87
|
+
throw new Error("Must specify 2 tokens in tokenData!");
|
|
88
|
+
const vaultParams = {
|
|
89
|
+
btcRelayContract: this.btcRelay.contractAddress,
|
|
90
|
+
token0: tokenData[0].token,
|
|
91
|
+
token1: tokenData[1].token,
|
|
92
|
+
token0Multiplier: tokenData[0].multiplier,
|
|
93
|
+
token1Multiplier: tokenData[1].multiplier,
|
|
94
|
+
confirmations: BigInt(confirmations)
|
|
95
|
+
};
|
|
96
|
+
const spvVaultParametersCommitment = (0, ethers_1.keccak256)(ethers_1.AbiCoder.defaultAbiCoder().encode(["address", "address", "address", "uint192", "uint192", "uint256"], [vaultParams.btcRelayContract, vaultParams.token0, vaultParams.token1, vaultParams.token0Multiplier, vaultParams.token1Multiplier, vaultParams.confirmations]));
|
|
97
|
+
return Promise.resolve(new EVMSpvVaultData_1.EVMSpvVaultData(owner, vaultId, {
|
|
98
|
+
spvVaultParametersCommitment,
|
|
99
|
+
utxoTxHash: ethers_1.ZeroHash,
|
|
100
|
+
utxoVout: 0n,
|
|
101
|
+
openBlockheight: 0n,
|
|
102
|
+
withdrawCount: 0n,
|
|
103
|
+
depositCount: 0n,
|
|
104
|
+
token0Amount: 0n,
|
|
105
|
+
token1Amount: 0n
|
|
106
|
+
}, vaultParams, utxo));
|
|
107
|
+
}
|
|
108
|
+
//Getters
|
|
109
|
+
async getFronterAddress(owner, vaultId, withdrawal) {
|
|
110
|
+
const frontingAddress = await this.contract.getFronterById(owner, vaultId, "0x" + withdrawal.getFrontingId());
|
|
111
|
+
if (frontingAddress === ethers_1.ZeroAddress)
|
|
112
|
+
return null;
|
|
113
|
+
return frontingAddress;
|
|
114
|
+
}
|
|
115
|
+
async getVaultData(owner, vaultId) {
|
|
116
|
+
const vaultState = await this.contract.getVault(owner, vaultId);
|
|
117
|
+
const blockheight = Number(vaultState.openBlockheight);
|
|
118
|
+
const events = await this.Events.getContractBlockEvents(["Opened"], [
|
|
119
|
+
"0x" + owner.substring(2).padStart(64, "0"),
|
|
120
|
+
(0, ethers_1.hexlify)(base_1.BigIntBufferUtils.toBuffer(vaultId, "be", 32))
|
|
121
|
+
], blockheight);
|
|
122
|
+
const foundEvent = events.find(event => (0, EVMSpvVaultData_1.getVaultParamsCommitment)(event.args.params) === vaultState.spvVaultParametersCommitment);
|
|
123
|
+
if (foundEvent == null)
|
|
124
|
+
throw new Error("Valid open event not found!");
|
|
125
|
+
const vaultParams = foundEvent.args.params;
|
|
126
|
+
if (vaultParams.btcRelayContract.toLowerCase() !== this.btcRelay.contractAddress.toLowerCase())
|
|
127
|
+
return null;
|
|
128
|
+
return new EVMSpvVaultData_1.EVMSpvVaultData(owner, vaultId, vaultState, vaultParams);
|
|
129
|
+
}
|
|
130
|
+
async getAllVaults(owner) {
|
|
131
|
+
const openedVaults = new Map();
|
|
132
|
+
await this.Events.findInContractEventsForward(["Opened", "Closed"], owner == null ? null : [
|
|
133
|
+
"0x" + owner.substring(2).padStart(64, "0")
|
|
134
|
+
], (event) => {
|
|
135
|
+
const vaultIdentifier = event.args.owner + ":" + event.args.vaultId.toString(10);
|
|
136
|
+
if (event.eventName === "Opened") {
|
|
137
|
+
const _event = event;
|
|
138
|
+
openedVaults.set(vaultIdentifier, _event.args.params);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
openedVaults.delete(vaultIdentifier);
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
});
|
|
145
|
+
const vaults = [];
|
|
146
|
+
for (let [identifier, vaultParams] of openedVaults.entries()) {
|
|
147
|
+
const [owner, vaultIdStr] = identifier.split(":");
|
|
148
|
+
const vaultState = await this.contract.getVault(owner, BigInt(vaultIdStr));
|
|
149
|
+
if (vaultState.spvVaultParametersCommitment === (0, EVMSpvVaultData_1.getVaultParamsCommitment)(vaultParams)) {
|
|
150
|
+
vaults.push(new EVMSpvVaultData_1.EVMSpvVaultData(owner, BigInt(vaultIdStr), vaultState, vaultParams));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return vaults;
|
|
154
|
+
}
|
|
155
|
+
async getWithdrawalState(btcTxId) {
|
|
156
|
+
const txHash = buffer_1.Buffer.from(btcTxId, "hex").reverse();
|
|
157
|
+
let result = await this.Events.findInContractEvents(["Fronted", "Claimed", "Closed"], [
|
|
158
|
+
null,
|
|
159
|
+
null,
|
|
160
|
+
(0, ethers_1.hexlify)(txHash)
|
|
161
|
+
], async (event) => {
|
|
162
|
+
switch (event.eventName) {
|
|
163
|
+
case "Fronted":
|
|
164
|
+
const frontedEvent = event;
|
|
165
|
+
const [ownerFront, vaultIdFront] = unpackOwnerAndVaultId(frontedEvent.args.ownerAndVaultId);
|
|
166
|
+
return {
|
|
167
|
+
type: base_1.SpvWithdrawalStateType.FRONTED,
|
|
168
|
+
txId: event.transactionHash,
|
|
169
|
+
owner: ownerFront,
|
|
170
|
+
vaultId: vaultIdFront,
|
|
171
|
+
recipient: frontedEvent.args.recipient,
|
|
172
|
+
fronter: frontedEvent.args.caller
|
|
173
|
+
};
|
|
174
|
+
case "Claimed":
|
|
175
|
+
const claimedEvent = event;
|
|
176
|
+
const [ownerClaim, vaultIdClaim] = unpackOwnerAndVaultId(claimedEvent.args.ownerAndVaultId);
|
|
177
|
+
return {
|
|
178
|
+
type: base_1.SpvWithdrawalStateType.CLAIMED,
|
|
179
|
+
txId: event.transactionHash,
|
|
180
|
+
owner: ownerClaim,
|
|
181
|
+
vaultId: vaultIdClaim,
|
|
182
|
+
recipient: claimedEvent.args.recipient,
|
|
183
|
+
claimer: claimedEvent.args.caller,
|
|
184
|
+
fronter: claimedEvent.args.frontingAddress
|
|
185
|
+
};
|
|
186
|
+
case "Closed":
|
|
187
|
+
const closedEvent = event;
|
|
188
|
+
return {
|
|
189
|
+
type: base_1.SpvWithdrawalStateType.CLOSED,
|
|
190
|
+
txId: event.transactionHash,
|
|
191
|
+
owner: closedEvent.args.owner,
|
|
192
|
+
vaultId: closedEvent.args.vaultId,
|
|
193
|
+
error: closedEvent.args.error
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
result ?? (result = {
|
|
198
|
+
type: base_1.SpvWithdrawalStateType.NOT_FOUND
|
|
199
|
+
});
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
getWithdrawalData(btcTx) {
|
|
203
|
+
return Promise.resolve(new EVMSpvWithdrawalData_1.EVMSpvWithdrawalData(btcTx));
|
|
204
|
+
}
|
|
205
|
+
//OP_RETURN data encoding/decoding
|
|
206
|
+
fromOpReturnData(data) {
|
|
207
|
+
return EVMSpvVaultContract.fromOpReturnData(data);
|
|
208
|
+
}
|
|
209
|
+
static fromOpReturnData(data) {
|
|
210
|
+
let rawAmount0 = 0n;
|
|
211
|
+
let rawAmount1 = 0n;
|
|
212
|
+
let executionHash = null;
|
|
213
|
+
if (data.length === 28) {
|
|
214
|
+
rawAmount0 = data.readBigInt64BE(20).valueOf();
|
|
215
|
+
}
|
|
216
|
+
else if (data.length === 36) {
|
|
217
|
+
rawAmount0 = data.readBigInt64BE(20).valueOf();
|
|
218
|
+
rawAmount1 = data.readBigInt64BE(28).valueOf();
|
|
219
|
+
}
|
|
220
|
+
else if (data.length === 60) {
|
|
221
|
+
rawAmount0 = data.readBigInt64BE(20).valueOf();
|
|
222
|
+
executionHash = data.slice(28, 60).toString("hex");
|
|
223
|
+
}
|
|
224
|
+
else if (data.length === 68) {
|
|
225
|
+
rawAmount0 = data.readBigInt64BE(20).valueOf();
|
|
226
|
+
rawAmount1 = data.readBigInt64BE(28).valueOf();
|
|
227
|
+
executionHash = data.slice(36, 68).toString("hex");
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
throw new Error("Invalid OP_RETURN data length!");
|
|
231
|
+
}
|
|
232
|
+
const recipient = "0x" + data.slice(0, 20).toString("hex");
|
|
233
|
+
if (!EVMAddresses_1.EVMAddresses.isValidAddress(recipient))
|
|
234
|
+
throw new Error("Invalid recipient specified");
|
|
235
|
+
return { executionHash, rawAmounts: [rawAmount0, rawAmount1], recipient: (0, ethers_1.getAddress)(recipient) };
|
|
236
|
+
}
|
|
237
|
+
toOpReturnData(recipient, rawAmounts, executionHash) {
|
|
238
|
+
return EVMSpvVaultContract.toOpReturnData(recipient, rawAmounts, executionHash);
|
|
239
|
+
}
|
|
240
|
+
static toOpReturnData(recipient, rawAmounts, executionHash) {
|
|
241
|
+
if (!EVMAddresses_1.EVMAddresses.isValidAddress(recipient))
|
|
242
|
+
throw new Error("Invalid recipient specified");
|
|
243
|
+
if (rawAmounts.length < 1)
|
|
244
|
+
throw new Error("At least 1 amount needs to be specified");
|
|
245
|
+
if (rawAmounts.length > 2)
|
|
246
|
+
throw new Error("At most 2 amounts need to be specified");
|
|
247
|
+
rawAmounts.forEach(val => {
|
|
248
|
+
if (val < 0n)
|
|
249
|
+
throw new Error("Negative raw amount specified");
|
|
250
|
+
if (val >= 2n ** 64n)
|
|
251
|
+
throw new Error("Raw amount overflow");
|
|
252
|
+
});
|
|
253
|
+
if (executionHash != null) {
|
|
254
|
+
if (buffer_1.Buffer.from(executionHash, "hex").length !== 32)
|
|
255
|
+
throw new Error("Invalid execution hash");
|
|
256
|
+
}
|
|
257
|
+
const recipientBuffer = buffer_1.Buffer.from(recipient.substring(2).padStart(40, "0"), "hex");
|
|
258
|
+
const amount0Buffer = base_1.BigIntBufferUtils.toBuffer(rawAmounts[0], "be", 8);
|
|
259
|
+
const amount1Buffer = rawAmounts[1] == null || rawAmounts[1] === 0n ? buffer_1.Buffer.alloc(0) : base_1.BigIntBufferUtils.toBuffer(rawAmounts[1], "be", 8);
|
|
260
|
+
const executionHashBuffer = executionHash == null ? buffer_1.Buffer.alloc(0) : buffer_1.Buffer.from(executionHash, "hex");
|
|
261
|
+
return buffer_1.Buffer.concat([
|
|
262
|
+
recipientBuffer,
|
|
263
|
+
amount0Buffer,
|
|
264
|
+
amount1Buffer,
|
|
265
|
+
executionHashBuffer
|
|
266
|
+
]);
|
|
267
|
+
}
|
|
268
|
+
//Actions
|
|
269
|
+
async claim(signer, vault, txs, synchronizer, initAta, txOptions) {
|
|
270
|
+
const result = await this.txsClaim(signer.getAddress(), vault, txs, synchronizer, initAta, txOptions?.feeRate);
|
|
271
|
+
const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
272
|
+
return signature;
|
|
273
|
+
}
|
|
274
|
+
async deposit(signer, vault, rawAmounts, txOptions) {
|
|
275
|
+
const result = await this.txsDeposit(signer.getAddress(), vault, rawAmounts, txOptions?.feeRate);
|
|
276
|
+
const txHashes = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
277
|
+
return txHashes[txHashes.length - 1];
|
|
278
|
+
}
|
|
279
|
+
async frontLiquidity(signer, vault, realWithdrawalTx, withdrawSequence, txOptions) {
|
|
280
|
+
const result = await this.txsFrontLiquidity(signer.getAddress(), vault, realWithdrawalTx, withdrawSequence, txOptions?.feeRate);
|
|
281
|
+
const txHashes = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
282
|
+
return txHashes[txHashes.length - 1];
|
|
283
|
+
}
|
|
284
|
+
async open(signer, vault, txOptions) {
|
|
285
|
+
const result = await this.txsOpen(signer.getAddress(), vault, txOptions?.feeRate);
|
|
286
|
+
const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
287
|
+
return signature;
|
|
288
|
+
}
|
|
289
|
+
//Transactions
|
|
290
|
+
async txsClaim(signer, vault, txs, synchronizer, initAta, feeRate) {
|
|
291
|
+
if (!vault.isOpened())
|
|
292
|
+
throw new Error("Cannot claim from a closed vault!");
|
|
293
|
+
feeRate ?? (feeRate = await this.Chain.Fees.getFeeRate());
|
|
294
|
+
const txsWithMerkleProofs = [];
|
|
295
|
+
for (let tx of txs) {
|
|
296
|
+
const merkleProof = await this.bitcoinRpc.getMerkleProof(tx.tx.btcTx.txid, tx.tx.btcTx.blockhash);
|
|
297
|
+
this.logger.debug("txsClaim(): merkle proof computed: ", merkleProof);
|
|
298
|
+
txsWithMerkleProofs.push({
|
|
299
|
+
...merkleProof,
|
|
300
|
+
...tx
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
const evmTxs = [];
|
|
304
|
+
const storedHeaders = await EVMBtcRelay_1.EVMBtcRelay.getCommitedHeadersAndSynchronize(signer, this.btcRelay, txsWithMerkleProofs.filter(tx => tx.storedHeader == null).map(tx => {
|
|
305
|
+
return {
|
|
306
|
+
blockhash: tx.tx.btcTx.blockhash,
|
|
307
|
+
blockheight: tx.blockheight,
|
|
308
|
+
requiredConfirmations: vault.getConfirmations()
|
|
309
|
+
};
|
|
310
|
+
}), evmTxs, synchronizer, feeRate);
|
|
311
|
+
if (storedHeaders == null)
|
|
312
|
+
throw new Error("Cannot fetch committed header!");
|
|
313
|
+
for (let tx of txsWithMerkleProofs) {
|
|
314
|
+
evmTxs.push(await this.Claim(signer, vault, tx.tx, tx.storedHeader ?? storedHeaders[tx.tx.btcTx.blockhash], tx.merkle, tx.pos, feeRate));
|
|
315
|
+
}
|
|
316
|
+
this.logger.debug("txsClaim(): " + evmTxs.length + " claim TXs created claiming " + txs.length + " txs, owner: " + vault.getOwner() +
|
|
317
|
+
" vaultId: " + vault.getVaultId().toString(10));
|
|
318
|
+
return evmTxs;
|
|
319
|
+
}
|
|
320
|
+
async txsDeposit(signer, vault, rawAmounts, feeRate) {
|
|
321
|
+
var _a;
|
|
322
|
+
if (!vault.isOpened())
|
|
323
|
+
throw new Error("Cannot deposit to a closed vault!");
|
|
324
|
+
feeRate ?? (feeRate = await this.Chain.Fees.getFeeRate());
|
|
325
|
+
const txs = [];
|
|
326
|
+
let realAmount0 = 0n;
|
|
327
|
+
let realAmount1 = 0n;
|
|
328
|
+
//Approve first
|
|
329
|
+
const requiredApprovals = {};
|
|
330
|
+
if (rawAmounts[0] != null && rawAmounts[0] !== 0n) {
|
|
331
|
+
if (vault.token0.token.toLowerCase() !== this.Chain.getNativeCurrencyAddress().toLowerCase()) {
|
|
332
|
+
realAmount0 = rawAmounts[0] * vault.token0.multiplier;
|
|
333
|
+
requiredApprovals[vault.token0.token.toLowerCase()] = realAmount0;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (rawAmounts[1] != null && rawAmounts[1] !== 0n) {
|
|
337
|
+
if (vault.token1.token.toLowerCase() !== this.Chain.getNativeCurrencyAddress().toLowerCase()) {
|
|
338
|
+
realAmount1 = rawAmounts[1] * vault.token1.multiplier;
|
|
339
|
+
requiredApprovals[_a = vault.token1.token.toLowerCase()] ?? (requiredApprovals[_a] = 0n);
|
|
340
|
+
requiredApprovals[vault.token1.token.toLowerCase()] += realAmount1;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
for (let tokenAddress in requiredApprovals) {
|
|
344
|
+
txs.push(await this.Chain.Tokens.Approve(signer, tokenAddress, requiredApprovals[tokenAddress], this.contractAddress, feeRate));
|
|
345
|
+
}
|
|
346
|
+
txs.push(await this.Deposit(signer, vault, rawAmounts, feeRate));
|
|
347
|
+
this.logger.debug("txsDeposit(): deposit TX created," +
|
|
348
|
+
" token0: " + vault.token0.token + " rawAmount0: " + rawAmounts[0].toString(10) + " amount0: " + realAmount0.toString(10) +
|
|
349
|
+
" token1: " + vault.token1.token + " rawAmount1: " + (rawAmounts[1] ?? 0n).toString(10) + " amount1: " + realAmount1.toString(10));
|
|
350
|
+
return txs;
|
|
351
|
+
}
|
|
352
|
+
async txsFrontLiquidity(signer, vault, realWithdrawalTx, withdrawSequence, feeRate) {
|
|
353
|
+
var _a;
|
|
354
|
+
if (!vault.isOpened())
|
|
355
|
+
throw new Error("Cannot front on a closed vault!");
|
|
356
|
+
feeRate ?? (feeRate = await this.Chain.Fees.getFeeRate());
|
|
357
|
+
const txs = [];
|
|
358
|
+
let realAmount0 = 0n;
|
|
359
|
+
let realAmount1 = 0n;
|
|
360
|
+
//Approve first
|
|
361
|
+
const rawAmounts = realWithdrawalTx.getFrontingAmount();
|
|
362
|
+
//Approve first
|
|
363
|
+
const requiredApprovals = {};
|
|
364
|
+
if (rawAmounts[0] != null && rawAmounts[0] !== 0n) {
|
|
365
|
+
if (vault.token0.token.toLowerCase() !== this.Chain.getNativeCurrencyAddress().toLowerCase()) {
|
|
366
|
+
realAmount0 = rawAmounts[0] * vault.token0.multiplier;
|
|
367
|
+
requiredApprovals[vault.token0.token.toLowerCase()] = realAmount0;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (rawAmounts[1] != null && rawAmounts[1] !== 0n) {
|
|
371
|
+
if (vault.token1.token.toLowerCase() !== this.Chain.getNativeCurrencyAddress().toLowerCase()) {
|
|
372
|
+
realAmount1 = rawAmounts[1] * vault.token1.multiplier;
|
|
373
|
+
requiredApprovals[_a = vault.token1.token.toLowerCase()] ?? (requiredApprovals[_a] = 0n);
|
|
374
|
+
requiredApprovals[vault.token1.token.toLowerCase()] += realAmount1;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
for (let tokenAddress in requiredApprovals) {
|
|
378
|
+
txs.push(await this.Chain.Tokens.Approve(signer, tokenAddress, requiredApprovals[tokenAddress], this.contractAddress, feeRate));
|
|
379
|
+
}
|
|
380
|
+
txs.push(await this.Front(signer, vault, realWithdrawalTx, withdrawSequence, feeRate));
|
|
381
|
+
this.logger.debug("txsFrontLiquidity(): front TX created," +
|
|
382
|
+
" token0: " + vault.token0.token + " rawAmount0: " + rawAmounts[0].toString(10) + " amount0: " + realAmount0.toString(10) +
|
|
383
|
+
" token1: " + vault.token1.token + " rawAmount1: " + (rawAmounts[1] ?? 0n).toString(10) + " amount1: " + realAmount1.toString(10));
|
|
384
|
+
return txs;
|
|
385
|
+
}
|
|
386
|
+
async txsOpen(signer, vault, feeRate) {
|
|
387
|
+
if (vault.isOpened())
|
|
388
|
+
throw new Error("Cannot open an already opened vault!");
|
|
389
|
+
feeRate ?? (feeRate = await this.Chain.Fees.getFeeRate());
|
|
390
|
+
const tx = await this.Open(signer, vault, feeRate);
|
|
391
|
+
this.logger.debug("txsOpen(): open TX created, owner: " + vault.getOwner() +
|
|
392
|
+
" vaultId: " + vault.getVaultId().toString(10));
|
|
393
|
+
return [tx];
|
|
394
|
+
}
|
|
395
|
+
async getClaimFee(signer, withdrawalData, feeRate) {
|
|
396
|
+
feeRate ?? (feeRate = await this.Chain.Fees.getFeeRate());
|
|
397
|
+
return EVMFees_1.EVMFees.getGasFee(EVMSpvVaultContract.GasCosts.CLAIM, feeRate);
|
|
398
|
+
}
|
|
399
|
+
async getFrontFee(signer, withdrawalData, feeRate) {
|
|
400
|
+
feeRate ?? (feeRate = await this.Chain.Fees.getFeeRate());
|
|
401
|
+
return EVMFees_1.EVMFees.getGasFee(EVMSpvVaultContract.GasCosts.FRONT, feeRate);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
exports.EVMSpvVaultContract = EVMSpvVaultContract;
|
|
405
|
+
EVMSpvVaultContract.GasCosts = {
|
|
406
|
+
DEPOSIT: 150000,
|
|
407
|
+
OPEN: 100000,
|
|
408
|
+
FRONT: 250000,
|
|
409
|
+
CLAIM: 250000
|
|
410
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { SpvVaultClaimEvent, SpvVaultCloseEvent, SpvVaultData, SpvVaultDepositEvent, SpvVaultOpenEvent, SpvVaultTokenBalance, SpvVaultTokenData } from "@atomiqlabs/base";
|
|
2
|
+
import { EVMSpvWithdrawalData } from "./EVMSpvWithdrawalData";
|
|
3
|
+
import { SpvVaultParametersStruct, SpvVaultStateStruct } from "./SpvVaultContractTypechain";
|
|
4
|
+
export declare function getVaultParamsCommitment(vaultParams: SpvVaultParametersStruct): string;
|
|
5
|
+
export declare class EVMSpvVaultData extends SpvVaultData<EVMSpvWithdrawalData> {
|
|
6
|
+
readonly owner: string;
|
|
7
|
+
readonly vaultId: bigint;
|
|
8
|
+
readonly relayContract: string;
|
|
9
|
+
readonly token0: {
|
|
10
|
+
token: string;
|
|
11
|
+
multiplier: bigint;
|
|
12
|
+
rawAmount: bigint;
|
|
13
|
+
};
|
|
14
|
+
readonly token1: {
|
|
15
|
+
token: string;
|
|
16
|
+
multiplier: bigint;
|
|
17
|
+
rawAmount: bigint;
|
|
18
|
+
};
|
|
19
|
+
readonly initialUtxo: string;
|
|
20
|
+
utxo: string;
|
|
21
|
+
readonly confirmations: number;
|
|
22
|
+
withdrawCount: number;
|
|
23
|
+
depositCount: number;
|
|
24
|
+
constructor(owner: string, vaultId: bigint, state: SpvVaultStateStruct, params: SpvVaultParametersStruct, initialUtxo?: string);
|
|
25
|
+
constructor(serializedObj: any);
|
|
26
|
+
getBalances(): SpvVaultTokenBalance[];
|
|
27
|
+
getConfirmations(): number;
|
|
28
|
+
getOwner(): string;
|
|
29
|
+
getTokenData(): SpvVaultTokenData[];
|
|
30
|
+
getUtxo(): string;
|
|
31
|
+
getVaultId(): bigint;
|
|
32
|
+
getWithdrawalCount(): number;
|
|
33
|
+
isOpened(): boolean;
|
|
34
|
+
serialize(): any;
|
|
35
|
+
updateState(withdrawalTxOrEvent: SpvVaultClaimEvent | SpvVaultCloseEvent | SpvVaultOpenEvent | SpvVaultDepositEvent | EVMSpvWithdrawalData): void;
|
|
36
|
+
getDepositCount(): number;
|
|
37
|
+
getVaultParamsStruct(): SpvVaultParametersStruct;
|
|
38
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EVMSpvVaultData = exports.getVaultParamsCommitment = void 0;
|
|
4
|
+
const base_1 = require("@atomiqlabs/base");
|
|
5
|
+
const buffer_1 = require("buffer");
|
|
6
|
+
const EVMSpvWithdrawalData_1 = require("./EVMSpvWithdrawalData");
|
|
7
|
+
const ethers_1 = require("ethers");
|
|
8
|
+
const ethers_2 = require("ethers");
|
|
9
|
+
function getVaultParamsCommitment(vaultParams) {
|
|
10
|
+
return (0, ethers_2.keccak256)(ethers_2.AbiCoder.defaultAbiCoder().encode(["address", "address", "address", "uint192", "uint192", "uint256"], [vaultParams.btcRelayContract, vaultParams.token0, vaultParams.token1, vaultParams.token0Multiplier, vaultParams.token1Multiplier, vaultParams.confirmations]));
|
|
11
|
+
}
|
|
12
|
+
exports.getVaultParamsCommitment = getVaultParamsCommitment;
|
|
13
|
+
class EVMSpvVaultData extends base_1.SpvVaultData {
|
|
14
|
+
constructor(ownerOrObj, vaultId, state, params, initialUtxo) {
|
|
15
|
+
super();
|
|
16
|
+
if (typeof (ownerOrObj) === "string") {
|
|
17
|
+
this.owner = ownerOrObj;
|
|
18
|
+
this.vaultId = vaultId;
|
|
19
|
+
this.relayContract = params.btcRelayContract;
|
|
20
|
+
this.token0 = {
|
|
21
|
+
token: params.token0,
|
|
22
|
+
multiplier: BigInt(params.token0Multiplier),
|
|
23
|
+
rawAmount: BigInt(state.token0Amount)
|
|
24
|
+
};
|
|
25
|
+
this.token1 = {
|
|
26
|
+
token: params.token1,
|
|
27
|
+
multiplier: BigInt(params.token1Multiplier),
|
|
28
|
+
rawAmount: BigInt(state.token1Amount)
|
|
29
|
+
};
|
|
30
|
+
const txHash = buffer_1.Buffer.from((0, ethers_1.hexlify)(state.utxoTxHash).substring(2), "hex");
|
|
31
|
+
this.utxo = txHash.reverse().toString("hex") + ":" + BigInt(state.utxoVout).toString(10);
|
|
32
|
+
this.confirmations = Number(params.confirmations);
|
|
33
|
+
this.withdrawCount = Number(state.withdrawCount);
|
|
34
|
+
this.depositCount = Number(state.depositCount);
|
|
35
|
+
this.initialUtxo = initialUtxo;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
this.owner = ownerOrObj.owner;
|
|
39
|
+
this.vaultId = BigInt(ownerOrObj.vaultId);
|
|
40
|
+
this.relayContract = ownerOrObj.relayContract;
|
|
41
|
+
this.token0 = {
|
|
42
|
+
token: ownerOrObj.token0.token,
|
|
43
|
+
multiplier: BigInt(ownerOrObj.token0.multiplier),
|
|
44
|
+
rawAmount: BigInt(ownerOrObj.token0.rawAmount)
|
|
45
|
+
};
|
|
46
|
+
this.token1 = {
|
|
47
|
+
token: ownerOrObj.token1.token,
|
|
48
|
+
multiplier: BigInt(ownerOrObj.token1.multiplier),
|
|
49
|
+
rawAmount: BigInt(ownerOrObj.token1.rawAmount)
|
|
50
|
+
};
|
|
51
|
+
this.utxo = ownerOrObj.utxo;
|
|
52
|
+
this.confirmations = ownerOrObj.confirmations;
|
|
53
|
+
this.withdrawCount = ownerOrObj.withdrawCount;
|
|
54
|
+
this.depositCount = ownerOrObj.depositCount;
|
|
55
|
+
this.initialUtxo = ownerOrObj.initialUtxo;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
getBalances() {
|
|
59
|
+
return [
|
|
60
|
+
{ ...this.token0, scaledAmount: this.token0.rawAmount * this.token0.multiplier },
|
|
61
|
+
{ ...this.token1, scaledAmount: this.token1.rawAmount * this.token1.multiplier }
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
getConfirmations() {
|
|
65
|
+
return this.confirmations;
|
|
66
|
+
}
|
|
67
|
+
getOwner() {
|
|
68
|
+
return this.owner;
|
|
69
|
+
}
|
|
70
|
+
getTokenData() {
|
|
71
|
+
return [this.token0, this.token1];
|
|
72
|
+
}
|
|
73
|
+
getUtxo() {
|
|
74
|
+
return this.isOpened() ? this.utxo : this.initialUtxo;
|
|
75
|
+
}
|
|
76
|
+
getVaultId() {
|
|
77
|
+
return this.vaultId;
|
|
78
|
+
}
|
|
79
|
+
getWithdrawalCount() {
|
|
80
|
+
return this.withdrawCount;
|
|
81
|
+
}
|
|
82
|
+
isOpened() {
|
|
83
|
+
return this.utxo !== "0000000000000000000000000000000000000000000000000000000000000000:0";
|
|
84
|
+
}
|
|
85
|
+
serialize() {
|
|
86
|
+
return {
|
|
87
|
+
type: "EVM",
|
|
88
|
+
owner: this.owner,
|
|
89
|
+
vaultId: this.vaultId.toString(10),
|
|
90
|
+
relayContract: this.relayContract,
|
|
91
|
+
token0: {
|
|
92
|
+
token: this.token0.token,
|
|
93
|
+
multiplier: this.token0.multiplier.toString(10),
|
|
94
|
+
rawAmount: this.token0.rawAmount.toString(10)
|
|
95
|
+
},
|
|
96
|
+
token1: {
|
|
97
|
+
token: this.token1.token,
|
|
98
|
+
multiplier: this.token1.multiplier.toString(10),
|
|
99
|
+
rawAmount: this.token1.rawAmount.toString(10)
|
|
100
|
+
},
|
|
101
|
+
utxo: this.utxo,
|
|
102
|
+
confirmations: this.confirmations,
|
|
103
|
+
withdrawCount: this.withdrawCount,
|
|
104
|
+
depositCount: this.depositCount,
|
|
105
|
+
initialUtxo: this.initialUtxo
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
updateState(withdrawalTxOrEvent) {
|
|
109
|
+
if (withdrawalTxOrEvent instanceof base_1.SpvVaultClaimEvent) {
|
|
110
|
+
if (withdrawalTxOrEvent.withdrawCount <= this.withdrawCount)
|
|
111
|
+
return;
|
|
112
|
+
this.token0.rawAmount -= withdrawalTxOrEvent.amounts[0];
|
|
113
|
+
this.token1.rawAmount -= withdrawalTxOrEvent.amounts[1];
|
|
114
|
+
this.withdrawCount = withdrawalTxOrEvent.withdrawCount;
|
|
115
|
+
this.utxo = withdrawalTxOrEvent.btcTxId + ":0";
|
|
116
|
+
}
|
|
117
|
+
if (withdrawalTxOrEvent instanceof base_1.SpvVaultCloseEvent) {
|
|
118
|
+
this.token0.rawAmount = 0n;
|
|
119
|
+
this.token1.rawAmount = 0n;
|
|
120
|
+
this.utxo = "0000000000000000000000000000000000000000000000000000000000000000:0";
|
|
121
|
+
}
|
|
122
|
+
if (withdrawalTxOrEvent instanceof base_1.SpvVaultOpenEvent) {
|
|
123
|
+
if (this.isOpened())
|
|
124
|
+
return;
|
|
125
|
+
this.utxo = withdrawalTxOrEvent.btcTxId + ":" + withdrawalTxOrEvent.vout;
|
|
126
|
+
}
|
|
127
|
+
if (withdrawalTxOrEvent instanceof base_1.SpvVaultDepositEvent) {
|
|
128
|
+
if (withdrawalTxOrEvent.depositCount <= this.depositCount)
|
|
129
|
+
return;
|
|
130
|
+
this.token0.rawAmount += withdrawalTxOrEvent.amounts[0];
|
|
131
|
+
this.token1.rawAmount += withdrawalTxOrEvent.amounts[1];
|
|
132
|
+
this.depositCount = withdrawalTxOrEvent.depositCount;
|
|
133
|
+
}
|
|
134
|
+
if (withdrawalTxOrEvent instanceof EVMSpvWithdrawalData_1.EVMSpvWithdrawalData) {
|
|
135
|
+
if (withdrawalTxOrEvent.getSpentVaultUtxo() !== this.utxo)
|
|
136
|
+
return;
|
|
137
|
+
const amounts = withdrawalTxOrEvent.getTotalOutput();
|
|
138
|
+
this.token0.rawAmount -= amounts[0];
|
|
139
|
+
this.token1.rawAmount -= amounts[1];
|
|
140
|
+
this.withdrawCount++;
|
|
141
|
+
this.utxo = withdrawalTxOrEvent.btcTx.txid + ":0";
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
getDepositCount() {
|
|
145
|
+
return this.depositCount;
|
|
146
|
+
}
|
|
147
|
+
getVaultParamsStruct() {
|
|
148
|
+
return {
|
|
149
|
+
btcRelayContract: this.relayContract,
|
|
150
|
+
token0: this.token0.token,
|
|
151
|
+
token1: this.token1.token,
|
|
152
|
+
token0Multiplier: this.token0.multiplier,
|
|
153
|
+
token1Multiplier: this.token1.multiplier,
|
|
154
|
+
confirmations: this.confirmations
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
exports.EVMSpvVaultData = EVMSpvVaultData;
|
|
159
|
+
base_1.SpvVaultData.deserializers["EVM"] = EVMSpvVaultData;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { SpvWithdrawalTransactionData } from "@atomiqlabs/base";
|
|
4
|
+
import { Buffer } from "buffer";
|
|
5
|
+
import { BitcoinVaultTransactionDataStruct } from "./SpvVaultContractTypechain";
|
|
6
|
+
export declare class EVMSpvWithdrawalData extends SpvWithdrawalTransactionData {
|
|
7
|
+
private getExecutionHashWith0x;
|
|
8
|
+
protected fromOpReturnData(data: Buffer): {
|
|
9
|
+
recipient: string;
|
|
10
|
+
rawAmounts: bigint[];
|
|
11
|
+
executionHash: string;
|
|
12
|
+
};
|
|
13
|
+
isRecipient(address: string): boolean;
|
|
14
|
+
getFrontingId(): string;
|
|
15
|
+
getTxHash(): string;
|
|
16
|
+
getFrontingAmount(): bigint[];
|
|
17
|
+
serialize(): any;
|
|
18
|
+
serializeToStruct(): BitcoinVaultTransactionDataStruct;
|
|
19
|
+
}
|