@atomiqlabs/chain-evm 1.0.0-dev.77 → 1.0.0-dev.78

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.
@@ -0,0 +1,180 @@
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
+ const EVMAddresses_1 = require("../chain/modules/EVMAddresses");
10
+ function getVaultParamsCommitment(vaultParams) {
11
+ 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]));
12
+ }
13
+ exports.getVaultParamsCommitment = getVaultParamsCommitment;
14
+ class EVMSpvVaultData extends base_1.SpvVaultData {
15
+ constructor(ownerOrObj, vaultId, state, params, initialUtxo) {
16
+ super();
17
+ if (typeof (ownerOrObj) === "string") {
18
+ this.owner = ownerOrObj;
19
+ this.vaultId = vaultId;
20
+ this.relayContract = params.btcRelayContract;
21
+ this.token0 = {
22
+ token: params.token0,
23
+ multiplier: BigInt(params.token0Multiplier),
24
+ rawAmount: BigInt(state.token0Amount)
25
+ };
26
+ this.token1 = {
27
+ token: params.token1,
28
+ multiplier: BigInt(params.token1Multiplier),
29
+ rawAmount: BigInt(state.token1Amount)
30
+ };
31
+ const txHash = buffer_1.Buffer.from((0, ethers_1.hexlify)(state.utxoTxHash).substring(2), "hex");
32
+ this.utxo = txHash.reverse().toString("hex") + ":" + BigInt(state.utxoVout).toString(10);
33
+ this.confirmations = Number(params.confirmations);
34
+ this.withdrawCount = Number(state.withdrawCount);
35
+ this.depositCount = Number(state.depositCount);
36
+ this.initialUtxo = initialUtxo;
37
+ }
38
+ else {
39
+ this.owner = ownerOrObj.owner;
40
+ this.vaultId = BigInt(ownerOrObj.vaultId);
41
+ this.relayContract = ownerOrObj.relayContract;
42
+ this.token0 = {
43
+ token: ownerOrObj.token0.token,
44
+ multiplier: BigInt(ownerOrObj.token0.multiplier),
45
+ rawAmount: BigInt(ownerOrObj.token0.rawAmount)
46
+ };
47
+ this.token1 = {
48
+ token: ownerOrObj.token1.token,
49
+ multiplier: BigInt(ownerOrObj.token1.multiplier),
50
+ rawAmount: BigInt(ownerOrObj.token1.rawAmount)
51
+ };
52
+ this.utxo = ownerOrObj.utxo;
53
+ this.confirmations = ownerOrObj.confirmations;
54
+ this.withdrawCount = ownerOrObj.withdrawCount;
55
+ this.depositCount = ownerOrObj.depositCount;
56
+ this.initialUtxo = ownerOrObj.initialUtxo;
57
+ }
58
+ }
59
+ getBalances() {
60
+ return [
61
+ { ...this.token0, scaledAmount: this.token0.rawAmount * this.token0.multiplier },
62
+ { ...this.token1, scaledAmount: this.token1.rawAmount * this.token1.multiplier }
63
+ ];
64
+ }
65
+ getConfirmations() {
66
+ return this.confirmations;
67
+ }
68
+ getOwner() {
69
+ return this.owner;
70
+ }
71
+ getTokenData() {
72
+ return [this.token0, this.token1];
73
+ }
74
+ getUtxo() {
75
+ return this.isOpened() ? this.utxo : this.initialUtxo;
76
+ }
77
+ getVaultId() {
78
+ return this.vaultId;
79
+ }
80
+ getWithdrawalCount() {
81
+ return this.withdrawCount;
82
+ }
83
+ isOpened() {
84
+ return this.utxo !== "0000000000000000000000000000000000000000000000000000000000000000:0";
85
+ }
86
+ serialize() {
87
+ return {
88
+ type: "EVM",
89
+ owner: this.owner,
90
+ vaultId: this.vaultId.toString(10),
91
+ relayContract: this.relayContract,
92
+ token0: {
93
+ token: this.token0.token,
94
+ multiplier: this.token0.multiplier.toString(10),
95
+ rawAmount: this.token0.rawAmount.toString(10)
96
+ },
97
+ token1: {
98
+ token: this.token1.token,
99
+ multiplier: this.token1.multiplier.toString(10),
100
+ rawAmount: this.token1.rawAmount.toString(10)
101
+ },
102
+ utxo: this.utxo,
103
+ confirmations: this.confirmations,
104
+ withdrawCount: this.withdrawCount,
105
+ depositCount: this.depositCount,
106
+ initialUtxo: this.initialUtxo
107
+ };
108
+ }
109
+ updateState(withdrawalTxOrEvent) {
110
+ if (withdrawalTxOrEvent instanceof base_1.SpvVaultClaimEvent) {
111
+ if (withdrawalTxOrEvent.withdrawCount <= this.withdrawCount)
112
+ return;
113
+ this.token0.rawAmount -= withdrawalTxOrEvent.amounts[0];
114
+ this.token1.rawAmount -= withdrawalTxOrEvent.amounts[1];
115
+ this.withdrawCount = withdrawalTxOrEvent.withdrawCount;
116
+ this.utxo = withdrawalTxOrEvent.btcTxId + ":0";
117
+ }
118
+ if (withdrawalTxOrEvent instanceof base_1.SpvVaultCloseEvent) {
119
+ this.token0.rawAmount = 0n;
120
+ this.token1.rawAmount = 0n;
121
+ this.utxo = "0000000000000000000000000000000000000000000000000000000000000000:0";
122
+ }
123
+ if (withdrawalTxOrEvent instanceof base_1.SpvVaultOpenEvent) {
124
+ if (this.isOpened())
125
+ return;
126
+ this.utxo = withdrawalTxOrEvent.btcTxId + ":" + withdrawalTxOrEvent.vout;
127
+ }
128
+ if (withdrawalTxOrEvent instanceof base_1.SpvVaultDepositEvent) {
129
+ if (withdrawalTxOrEvent.depositCount <= this.depositCount)
130
+ return;
131
+ this.token0.rawAmount += withdrawalTxOrEvent.amounts[0];
132
+ this.token1.rawAmount += withdrawalTxOrEvent.amounts[1];
133
+ this.depositCount = withdrawalTxOrEvent.depositCount;
134
+ }
135
+ if (withdrawalTxOrEvent instanceof EVMSpvWithdrawalData_1.EVMSpvWithdrawalData) {
136
+ if (withdrawalTxOrEvent.getSpentVaultUtxo() !== this.utxo)
137
+ return;
138
+ const amounts = withdrawalTxOrEvent.getTotalOutput();
139
+ this.token0.rawAmount -= amounts[0];
140
+ this.token1.rawAmount -= amounts[1];
141
+ this.withdrawCount++;
142
+ this.utxo = withdrawalTxOrEvent.btcTx.txid + ":0";
143
+ }
144
+ }
145
+ getDepositCount() {
146
+ return this.depositCount;
147
+ }
148
+ getVaultParamsStruct() {
149
+ return {
150
+ btcRelayContract: this.relayContract,
151
+ token0: this.token0.token,
152
+ token1: this.token1.token,
153
+ token0Multiplier: this.token0.multiplier,
154
+ token1Multiplier: this.token1.multiplier,
155
+ confirmations: this.confirmations
156
+ };
157
+ }
158
+ static randomVault() {
159
+ const spvVaultParams = {
160
+ btcRelayContract: EVMAddresses_1.EVMAddresses.randomAddress(),
161
+ token0: EVMAddresses_1.EVMAddresses.randomAddress(),
162
+ token1: EVMAddresses_1.EVMAddresses.randomAddress(),
163
+ token0Multiplier: 1n,
164
+ token1Multiplier: 1n,
165
+ confirmations: 3n,
166
+ };
167
+ return new EVMSpvVaultData(EVMAddresses_1.EVMAddresses.randomAddress(), 0n, {
168
+ spvVaultParametersCommitment: getVaultParamsCommitment(spvVaultParams),
169
+ utxoTxHash: (0, ethers_1.randomBytes)(32),
170
+ utxoVout: 0n,
171
+ openBlockheight: 0n,
172
+ withdrawCount: 0n,
173
+ depositCount: 0n,
174
+ token0Amount: 0n,
175
+ token1Amount: 0n
176
+ }, spvVaultParams);
177
+ }
178
+ }
179
+ exports.EVMSpvVaultData = EVMSpvVaultData;
180
+ base_1.SpvVaultData.deserializers["EVM"] = EVMSpvVaultData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-evm",
3
- "version": "1.0.0-dev.77",
3
+ "version": "1.0.0-dev.78",
4
4
  "description": "EVM specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -27,7 +27,7 @@
27
27
  "@noble/hashes": "^1.8.0",
28
28
  "@scure/btc-signer": "1.6.0",
29
29
  "buffer": "6.0.3",
30
- "promise-cache-ts": "0.0.4",
30
+ "promise-cache-ts": "0.0.6",
31
31
  "promise-queue-ts": "1.0.0"
32
32
  },
33
33
  "peerDependencies": {