@reyaxyz/common 0.174.0 → 0.175.1

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.
@@ -5645,6 +5645,125 @@
5645
5645
  ],
5646
5646
  "outputs": [],
5647
5647
  "stateMutability": "nonpayable"
5648
- }
5648
+ },
5649
+ {
5650
+ "type": "function",
5651
+ "name": "tryAggregate",
5652
+ "inputs": [
5653
+ {
5654
+ "name": "requireSuccess",
5655
+ "type": "bool",
5656
+ "internalType": "bool"
5657
+ },
5658
+ {
5659
+ "name": "calls",
5660
+ "type": "bytes[]",
5661
+ "internalType": "bytes[]"
5662
+ }
5663
+ ],
5664
+ "outputs": [
5665
+ {
5666
+ "name": "returnData",
5667
+ "type": "tuple[]",
5668
+ "internalType": "struct MulticallResult[]",
5669
+ "components": [
5670
+ {
5671
+ "name": "success",
5672
+ "type": "bool",
5673
+ "internalType": "bool"
5674
+ },
5675
+ {
5676
+ "name": "returnData",
5677
+ "type": "bytes",
5678
+ "internalType": "bytes"
5679
+ }
5680
+ ]
5681
+ }
5682
+ ],
5683
+ "stateMutability": "payable"
5684
+ },
5685
+ {
5686
+ "type": "error",
5687
+ "name": "MulticallFailed",
5688
+ "inputs": [
5689
+ {
5690
+ "name": "index",
5691
+ "type": "uint256",
5692
+ "internalType": "uint256"
5693
+ },
5694
+ {
5695
+ "name": "inputData",
5696
+ "type": "bytes",
5697
+ "internalType": "bytes"
5698
+ },
5699
+ {
5700
+ "name": "returnData",
5701
+ "type": "bytes",
5702
+ "internalType": "bytes"
5703
+ }
5704
+ ]
5705
+ },
5706
+ {
5707
+ "type": "event",
5708
+ "name": "FailedCall",
5709
+ "inputs": [
5710
+ {
5711
+ "name": "index",
5712
+ "type": "uint256",
5713
+ "indexed": false,
5714
+ "internalType": "uint256"
5715
+ },
5716
+ {
5717
+ "name": "inputData",
5718
+ "type": "bytes",
5719
+ "indexed": false,
5720
+ "internalType": "bytes"
5721
+ },
5722
+ {
5723
+ "name": "returnData",
5724
+ "type": "bytes",
5725
+ "indexed": false,
5726
+ "internalType": "bytes"
5727
+ },
5728
+ {
5729
+ "name": "blockTimestamp",
5730
+ "type": "uint256",
5731
+ "indexed": false,
5732
+ "internalType": "uint256"
5733
+ }
5734
+ ],
5735
+ "anonymous": false
5736
+ },
5737
+ {
5738
+ "type": "event",
5739
+ "name": "SuccessfulCall",
5740
+ "inputs": [
5741
+ {
5742
+ "name": "index",
5743
+ "type": "uint256",
5744
+ "indexed": false,
5745
+ "internalType": "uint256"
5746
+ },
5747
+ {
5748
+ "name": "inputData",
5749
+ "type": "bytes",
5750
+ "indexed": false,
5751
+ "internalType": "bytes"
5752
+ },
5753
+ {
5754
+ "name": "returnData",
5755
+ "type": "bytes",
5756
+ "indexed": false,
5757
+ "internalType": "bytes"
5758
+ },
5759
+ {
5760
+ "name": "blockTimestamp",
5761
+ "type": "uint256",
5762
+ "indexed": false,
5763
+ "internalType": "uint256"
5764
+ }
5765
+ ],
5766
+ "anonymous": false
5767
+ }
5649
5768
  ]
5650
5769
  }
@@ -3586,6 +3586,27 @@
3586
3586
  "type": "error",
3587
3587
  "name": "MismatchedLength",
3588
3588
  "inputs": []
3589
+ },
3590
+ {
3591
+ "type": "error",
3592
+ "name": "MulticallFailed",
3593
+ "inputs": [
3594
+ {
3595
+ "name": "index",
3596
+ "type": "uint256",
3597
+ "internalType": "uint256"
3598
+ },
3599
+ {
3600
+ "name": "inputData",
3601
+ "type": "bytes",
3602
+ "internalType": "bytes"
3603
+ },
3604
+ {
3605
+ "name": "returnData",
3606
+ "type": "bytes",
3607
+ "internalType": "bytes"
3608
+ }
3609
+ ]
3589
3610
  }
3590
3611
  ]
3591
3612
  }
@@ -1,4 +1,5 @@
1
1
  import { ethers } from 'ethers';
2
+ import { abi as CoreAbi } from './abis/CoreProxy.json';
2
3
  import { abi as MulticallAbi } from './abis/MulticallV2.json';
3
4
 
4
5
  export type Call = {
@@ -6,6 +7,14 @@ export type Call = {
6
7
  callData: string;
7
8
  };
8
9
 
10
+ export const encodeCoreMulticall = (calls: Call[]): string => {
11
+ const functionSignature = 'tryAggregate';
12
+ const parameters: [boolean, string[]] = [false, calls.map((c) => c.callData)];
13
+ const INTERFACE = new ethers.Interface(CoreAbi);
14
+ const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
15
+ return calldata;
16
+ };
17
+
9
18
  const encodeMulticall = (requireSuccess: boolean, calls: Call[]): string => {
10
19
  const functionSignature = 'tryAggregate';
11
20
  const parameters: [boolean, Call[]] = [requireSuccess, calls];
@@ -90,6 +90,57 @@ export async function estimateGasFromTxAndChainId(
90
90
  throw new Error('All RPC URLs failed; last error: ' + lastError);
91
91
  }
92
92
 
93
+ export async function estimateGasWithoutHardcoding(
94
+ signer: Signer | JsonRpcSigner,
95
+ data: string,
96
+ value: string,
97
+ chainId: number,
98
+ targetContract: ContractType | string,
99
+ ): Promise<
100
+ Transaction & {
101
+ gasLimit: bigint;
102
+ maxPriorityFeePerGas?: bigint;
103
+ maxFeePerGas?: bigint;
104
+ }
105
+ > {
106
+ const accountAddress = await signer.getAddress();
107
+
108
+ const contractAddress = Object.values(ContractType).includes(
109
+ targetContract as ContractType,
110
+ )
111
+ ? getAddress(chainId, targetContract as ContractType)
112
+ : targetContract;
113
+ const tx = {
114
+ from: accountAddress, // @todo Update with relayer address
115
+ to: contractAddress,
116
+ data,
117
+ ...(value && value !== '0' ? { value: value } : {}),
118
+ };
119
+
120
+ let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate
121
+
122
+ try {
123
+ const provider = ethers.getDefaultProvider(reyaChainIdRPCMapper[chainId]);
124
+ const estimation = await provider.estimateGas(tx);
125
+ gasLimit = addGasBuffer(estimation);
126
+ } catch (error) {
127
+ const decodedError = await errorDecoder.decode(error);
128
+ const reason = customReasonMapper(decodedError);
129
+ console.error(
130
+ `Error simulating transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,
131
+ );
132
+ throw new Error(reason);
133
+ }
134
+
135
+ if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {
136
+ const maxPriorityFeePerGas: bigint = BigInt('0');
137
+ const maxFeePerGas: bigint = BigInt('100000000');
138
+ return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };
139
+ }
140
+
141
+ return { ...tx, gasLimit };
142
+ }
143
+
93
144
  async function estimateGasExternalChains(
94
145
  signer: Signer | JsonRpcSigner,
95
146
  data: string,
@@ -184,3 +235,10 @@ const customReasonMapper = ({ name, reason }: DecodedError): string => {
184
235
  function addGasBuffer(estimation: bigint): bigint {
185
236
  return (estimation * BigInt(120)) / BigInt(100);
186
237
  }
238
+
239
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
240
+ export async function decodeError(returnData: any): Promise<string> {
241
+ const decodedError = await errorDecoder.decode(returnData);
242
+ const reason = customReasonMapper(decodedError);
243
+ return reason;
244
+ }
package/src/types.ts CHANGED
@@ -90,6 +90,7 @@ export type CollateralEntity = {
90
90
  percentage: number;
91
91
  balance: number;
92
92
  balanceRUSD: number;
93
+ balanceWithHaircutRUSD: number;
93
94
  exchangeRate: number;
94
95
  exchangeRateChange24HPercentage: number;
95
96
  };
@@ -106,6 +107,7 @@ export type MarginAccountEntity = {
106
107
  totalBalance: number;
107
108
  totalBalanceUnderlyingAsset: string;
108
109
  totalBalanceChange24HPercentage: number;
110
+ totalBalanceWithHaircut: number;
109
111
  livePnL: number;
110
112
  livePnLUnderlyingAsset: string;
111
113
  realizedPnL: number;