@reyaxyz/common 0.97.0 → 0.98.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.
Files changed (61) hide show
  1. package/README.md +1 -1
  2. package/dist/index.js +1 -0
  3. package/dist/index.js.map +1 -1
  4. package/dist/services/getSocketBridgeTime.js +5 -5
  5. package/dist/services/getSocketBridgeTime.js.map +1 -1
  6. package/dist/transactions/abis/CoreProxy.json +5600 -0
  7. package/dist/transactions/abis/Periphery.json +1464 -0
  8. package/dist/transactions/abis/socket/VaultWithPayload.json +40 -0
  9. package/dist/transactions/action.js +22 -0
  10. package/dist/transactions/action.js.map +1 -0
  11. package/dist/transactions/consts.js +7 -0
  12. package/dist/transactions/consts.js.map +1 -0
  13. package/dist/transactions/contractAddresses.js +36 -0
  14. package/dist/transactions/contractAddresses.js.map +1 -0
  15. package/dist/transactions/executeTransaction.js +120 -0
  16. package/dist/transactions/executeTransaction.js.map +1 -0
  17. package/dist/transactions/index.js +32 -0
  18. package/dist/transactions/index.js.map +1 -0
  19. package/dist/transactions/routerCommands.js +45 -0
  20. package/dist/transactions/routerCommands.js.map +1 -0
  21. package/dist/transactions/sign.js +146 -0
  22. package/dist/transactions/sign.js.map +1 -0
  23. package/dist/transactions/trade.js +24 -0
  24. package/dist/transactions/trade.js.map +1 -0
  25. package/dist/transactions/txHelpers.js +8 -0
  26. package/dist/transactions/txHelpers.js.map +1 -0
  27. package/dist/types/index.d.ts +1 -0
  28. package/dist/types/index.d.ts.map +1 -1
  29. package/dist/types/transactions/action.d.ts +27 -0
  30. package/dist/types/transactions/action.d.ts.map +1 -0
  31. package/dist/types/transactions/consts.d.ts +4 -0
  32. package/dist/types/transactions/consts.d.ts.map +1 -0
  33. package/dist/types/transactions/contractAddresses.d.ts +8 -0
  34. package/dist/types/transactions/contractAddresses.d.ts.map +1 -0
  35. package/dist/types/transactions/executeTransaction.d.ts +15 -0
  36. package/dist/types/transactions/executeTransaction.d.ts.map +1 -0
  37. package/dist/types/transactions/index.d.ts +12 -0
  38. package/dist/types/transactions/index.d.ts.map +1 -0
  39. package/dist/types/transactions/routerCommands.d.ts +23 -0
  40. package/dist/types/transactions/routerCommands.d.ts.map +1 -0
  41. package/dist/types/transactions/sign.d.ts +12 -0
  42. package/dist/types/transactions/sign.d.ts.map +1 -0
  43. package/dist/types/transactions/trade.d.ts +2 -0
  44. package/dist/types/transactions/trade.d.ts.map +1 -0
  45. package/dist/types/transactions/txHelpers.d.ts +2 -0
  46. package/dist/types/transactions/txHelpers.d.ts.map +1 -0
  47. package/package.json +2 -2
  48. package/src/index.ts +1 -0
  49. package/src/services/getSocketBridgeTime.ts +5 -5
  50. package/src/transactions/abis/CoreProxy.json +5600 -0
  51. package/src/transactions/abis/Periphery.json +1464 -0
  52. package/src/transactions/abis/socket/VaultWithPayload.json +40 -0
  53. package/src/transactions/action.ts +41 -0
  54. package/src/transactions/consts.ts +3 -0
  55. package/src/transactions/contractAddresses.ts +41 -0
  56. package/src/transactions/executeTransaction.ts +83 -0
  57. package/src/transactions/index.ts +11 -0
  58. package/src/transactions/routerCommands.ts +62 -0
  59. package/src/transactions/sign.ts +135 -0
  60. package/src/transactions/trade.ts +25 -0
  61. package/src/transactions/txHelpers.ts +3 -0
@@ -0,0 +1,40 @@
1
+ {
2
+ "abi": [
3
+ {
4
+ "type": "function",
5
+ "name": "bridge",
6
+ "inputs": [
7
+ {
8
+ "name": "receiver_",
9
+ "type": "address",
10
+ "internalType": "address"
11
+ },
12
+ {
13
+ "name": "amount_",
14
+ "type": "uint256",
15
+ "internalType": "uint256"
16
+ },
17
+ {
18
+ "name": "msgGasLimit_",
19
+ "type": "uint256",
20
+ "internalType": "uint256"
21
+ },
22
+ {
23
+ "name": "connector_",
24
+ "type": "address",
25
+ "internalType": "address"
26
+ },
27
+ {
28
+ "name": "execPayload_",
29
+ "type": "bytes",
30
+ "internalType": "bytes"
31
+ },
32
+ {
33
+ "name": "options_",
34
+ "type": "bytes",
35
+ "internalType": "bytes"
36
+ }
37
+ ]
38
+ }
39
+ ]
40
+ }
@@ -0,0 +1,41 @@
1
+ // ENCODED ACTIONS
2
+
3
+ import { CommandType } from './routerCommands';
4
+
5
+ export type MethodParameters = {
6
+ /**
7
+ * The hex encoded calldata to perform the given operation
8
+ */
9
+ calldata: string;
10
+ /**
11
+ * The amount of ether (wei) to send in hex.
12
+ */
13
+ value: string;
14
+ };
15
+
16
+ export type Command = {
17
+ commandType: CommandType;
18
+ inputs: string;
19
+ marketId: number;
20
+ exchangeId: number;
21
+ };
22
+
23
+ export type SingleAction = {
24
+ command: Command;
25
+ };
26
+
27
+ export class MultiAction {
28
+ public commands: Command[];
29
+
30
+ constructor() {
31
+ this.commands = [];
32
+ }
33
+
34
+ public get length(): number {
35
+ return this.commands.length;
36
+ }
37
+
38
+ public newAction(singleAction: SingleAction): void {
39
+ this.commands.push(singleAction.command);
40
+ }
41
+ }
@@ -0,0 +1,3 @@
1
+ export const BRIDGE_DEADLINE_IN_SECONDS = 15 * 60; // 15 minutes
2
+ export const CORE_DEADLINE_IN_SECONDS = 60; // 1 minute
3
+ export const RELAYER_ACCOUNT = '0xb43c317c9c9215d268881d7d6b59c2d3d0247da1'; // @todo update value
@@ -0,0 +1,41 @@
1
+ import { Address, ReyaChainId } from '../types';
2
+
3
+ export enum ContractType {
4
+ CORE_PROXY = 'core_proxy',
5
+ PASSIVE_POOL_PROXY = 'passive_pool_proxy',
6
+ PERIPHERY_PROXY = 'periphery_proxy',
7
+ }
8
+
9
+ const addresses: Record<ReyaChainId, Record<ContractType, Address>> = {
10
+ [ReyaChainId.reyaCronos]: {
11
+ [ContractType.CORE_PROXY]: '0x20da8f7aa44f858a3a3e3f03ba6ffad039d1eb28',
12
+ [ContractType.PASSIVE_POOL_PROXY]:
13
+ '0x9afc30bed29d636e566fd9d7dd89d51773724be6',
14
+ [ContractType.PERIPHERY_PROXY]:
15
+ '0xa45d0566144956b6012d461ad509dad2bacacaaf',
16
+ },
17
+ [ReyaChainId.reyaNetwork]: {
18
+ [ContractType.CORE_PROXY]: '0xa763b6a5e09378434406c003dae6487fbbdc1a80',
19
+ [ContractType.PASSIVE_POOL_PROXY]:
20
+ '0xb4b77d6180cc14472a9a7bdff01cc2459368d413',
21
+ [ContractType.PERIPHERY_PROXY]:
22
+ '0xcd2869d1eb1bc8991bc55de9e9b779e912faf736',
23
+ },
24
+ };
25
+ export const getAddress = (
26
+ chainId: number,
27
+ contractName: ContractType,
28
+ ): Address => {
29
+ const keyChainId = chainId.toString();
30
+
31
+ if (!Object.keys(addresses).includes(keyChainId)) {
32
+ throw new Error(`Unspecified addresses for chain id ${keyChainId}`);
33
+ }
34
+
35
+ const networkAddresses = addresses[chainId as ReyaChainId];
36
+ if (!Object.keys(networkAddresses).includes(contractName)) {
37
+ throw new Error(`Unspecified address for ${contractName} contract`);
38
+ }
39
+
40
+ return networkAddresses[contractName];
41
+ };
@@ -0,0 +1,83 @@
1
+ import { JsonRpcSigner, Signer } from 'ethers';
2
+ import { ReyaChainId } from '../types';
3
+ import { ContractType, getAddress } from './contractAddresses';
4
+ import { getGasBuffer } from './txHelpers';
5
+
6
+ export type Transaction = {
7
+ from: string;
8
+ to: string;
9
+ data: string;
10
+ value?: string;
11
+ };
12
+
13
+ export async function estimateGas(
14
+ signer: Signer | JsonRpcSigner,
15
+ data: string,
16
+ value: string,
17
+ chainId: number,
18
+ targetContract: ContractType | string,
19
+ ): Promise<
20
+ Transaction & {
21
+ gasLimit: bigint;
22
+ maxPriorityFeePerGas?: bigint;
23
+ maxFeePerGas?: bigint;
24
+ }
25
+ > {
26
+ const accountAddress = await signer.getAddress();
27
+
28
+ const contractAddress = Object.values(ContractType).includes(
29
+ targetContract as ContractType,
30
+ )
31
+ ? getAddress(chainId, targetContract as ContractType)
32
+ : targetContract;
33
+ const tx = {
34
+ from: accountAddress, // @todo Update with relayer address
35
+ to: contractAddress,
36
+ data,
37
+ ...(value && value !== '0' ? { value: value } : {}),
38
+ };
39
+
40
+ let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate
41
+
42
+ try {
43
+ const gasEstimate = await signer.estimateGas(tx);
44
+ gasLimit = getGasBuffer(gasEstimate);
45
+ } catch (error) {
46
+ // sentry error & thorw
47
+ console.warn(error);
48
+ throw new Error('Error executing transaction'); // @todo introduce error parsing
49
+ }
50
+
51
+ if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {
52
+ const maxPriorityFeePerGas: bigint = BigInt('0');
53
+ const maxFeePerGas: bigint = BigInt('100000000');
54
+ return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };
55
+ }
56
+
57
+ return { ...tx, gasLimit };
58
+ }
59
+
60
+ export async function executeTransaction(
61
+ signer: Signer | JsonRpcSigner,
62
+ data: string,
63
+ value: string,
64
+ chainId: number,
65
+ targetContract: ContractType | string,
66
+ ) {
67
+ const txData = await estimateGas(
68
+ signer,
69
+ data,
70
+ value,
71
+ chainId,
72
+ targetContract,
73
+ );
74
+
75
+ try {
76
+ const txResponse = await signer.sendTransaction(txData);
77
+ const txReceipt = await txResponse.wait();
78
+ return txReceipt;
79
+ } catch (error) {
80
+ console.warn(error);
81
+ throw new Error('Transaction Execution Error');
82
+ }
83
+ }
@@ -0,0 +1,11 @@
1
+ export * from './action';
2
+ export { abi as CoreAbi } from './abis/CoreProxy.json';
3
+ export { abi as PeripheryAbi } from './abis/Periphery.json';
4
+ export { abi as Socket_VaultWithPayloadAbi } from './abis/socket/VaultWithPayload.json';
5
+ export * from './contractAddresses';
6
+ export * from './routerCommands';
7
+ export * from './sign';
8
+ export * from './trade';
9
+ export * from './txHelpers';
10
+ export * from './consts';
11
+ export * from './executeTransaction';
@@ -0,0 +1,62 @@
1
+ import { AbiCoder } from 'ethers';
2
+ import { SingleAction } from './action';
3
+
4
+ /**
5
+ * CommandTypes
6
+ * @description Flags that modify a command's execution
7
+ * @enum {number}
8
+ */
9
+ export enum CommandType {
10
+ DEPOSIT,
11
+ WITHDRAW,
12
+ DUTCH_LIQUIDATION,
13
+ MATCH_ORDER,
14
+ PROPAGATE_CACHE_FLOW,
15
+ TRANSFER_MARGIN_ACCOUNT,
16
+ }
17
+
18
+ const ABI_DEFINITION: { [key in CommandType]: string[] } = {
19
+ [CommandType.DEPOSIT]: ['address', 'uint256'],
20
+ [CommandType.WITHDRAW]: ['address', 'uint256'],
21
+ [CommandType.MATCH_ORDER]: ['uint128[]', 'bytes'],
22
+ [CommandType.PROPAGATE_CACHE_FLOW]: [], // @todo Check if correct
23
+ [CommandType.DUTCH_LIQUIDATION]: ['tuple(uint128,address,uint128[],bytes)'],
24
+ [CommandType.TRANSFER_MARGIN_ACCOUNT]: ['tuple(uint128,address,uint256)'], // @todo update once needed
25
+ };
26
+
27
+ export type RouterCommand = {
28
+ type: CommandType;
29
+ encodedInput: string;
30
+ marketId: number;
31
+ exchangeId: number;
32
+ };
33
+
34
+ export function createCommand(
35
+ type: CommandType,
36
+ parameters: unknown[],
37
+ marketId: number,
38
+ exchangeId: number,
39
+ ): RouterCommand {
40
+ const encodedInput = AbiCoder.defaultAbiCoder().encode(
41
+ ABI_DEFINITION[type],
42
+ parameters,
43
+ );
44
+ return { type, encodedInput, marketId, exchangeId };
45
+ }
46
+
47
+ export function getCommand(
48
+ type: CommandType,
49
+ parameters: unknown[],
50
+ marketId: number,
51
+ exchangeId: number,
52
+ ): SingleAction {
53
+ const command = createCommand(type, parameters, marketId, exchangeId);
54
+ return {
55
+ command: {
56
+ commandType: command.type,
57
+ inputs: command.encodedInput,
58
+ marketId: command.marketId,
59
+ exchangeId: command.exchangeId,
60
+ },
61
+ };
62
+ }
@@ -0,0 +1,135 @@
1
+ import { JsonRpcSigner, Signature, Signer, TypedDataField } from 'ethers';
2
+ import { ContractType, getAddress } from './contractAddresses';
3
+ import { Command } from './action';
4
+ import { ethers } from 'ethers';
5
+ import { Address, ReyaChainId } from '../types';
6
+
7
+ export type EIP712Signature = {
8
+ v: number;
9
+ r: string;
10
+ s: string;
11
+ deadline: number;
12
+ };
13
+
14
+ function convertEthersSignatureToEIP712Signature(
15
+ signature: Signature,
16
+ deadline: number,
17
+ ): EIP712Signature {
18
+ return {
19
+ v: signature.v,
20
+ r: signature.r,
21
+ s: signature.s,
22
+ deadline: deadline,
23
+ };
24
+ }
25
+
26
+ export async function signCoreCommands(
27
+ signer: Signer | JsonRpcSigner,
28
+ reyaChainId: ReyaChainId,
29
+ caller: Address,
30
+ accountId: number,
31
+ commands: Command[],
32
+ nonce: number,
33
+ deadline: number,
34
+ extraSignatureData: string,
35
+ ): Promise<EIP712Signature> {
36
+ const signature = await signReyaTypedData(
37
+ signer,
38
+ getAddress(reyaChainId, ContractType.CORE_PROXY),
39
+ {
40
+ ExecuteBySig: [
41
+ { name: 'verifyingChainId', type: 'uint256' },
42
+ { name: 'caller', type: 'address' },
43
+ { name: 'accountId', type: 'uint128' },
44
+ { name: 'commands', type: 'Command[]' },
45
+ { name: 'nonce', type: 'uint256' },
46
+ { name: 'deadline', type: 'uint256' },
47
+ { name: 'extraSignatureData', type: 'bytes' },
48
+ ],
49
+ Command: [
50
+ { name: 'commandType', type: 'uint8' },
51
+ { name: 'inputs', type: 'bytes' },
52
+ { name: 'marketId', type: 'uint128' },
53
+ { name: 'exchangeId', type: 'uint128' },
54
+ ],
55
+ },
56
+ {
57
+ verifyingChainId: reyaChainId,
58
+ caller: caller,
59
+ accountId: accountId,
60
+ commands: commands,
61
+ nonce: nonce,
62
+ deadline: deadline,
63
+ extraSignatureData: extraSignatureData,
64
+ },
65
+ );
66
+
67
+ return convertEthersSignatureToEIP712Signature(signature, deadline);
68
+ }
69
+
70
+ export async function signPoolRemoveLiquidity(
71
+ signer: Signer | JsonRpcSigner,
72
+ reyaChainId: ReyaChainId,
73
+ caller: Address,
74
+ owner: Address,
75
+ poolId: number,
76
+ sharesAmount: bigint,
77
+ minOut: bigint,
78
+ nonce: number,
79
+ deadline: number,
80
+ socketMsgGasLimit: bigint,
81
+ chainId: number,
82
+ receiver: string,
83
+ ): Promise<EIP712Signature> {
84
+ const signature = await signReyaTypedData(
85
+ signer,
86
+ getAddress(reyaChainId, ContractType.PASSIVE_POOL_PROXY),
87
+ {
88
+ RemoveLiquidityBySig: [
89
+ { name: 'verifyingChainId', type: 'uint256' },
90
+ { name: 'caller', type: 'address' },
91
+ { name: 'owner', type: 'address' },
92
+ { name: 'poolId', type: 'uint128' },
93
+ { name: 'sharesAmount', type: 'uint256' },
94
+ { name: 'minOut', type: 'uint256' },
95
+ { name: 'nonce', type: 'uint256' },
96
+ { name: 'deadline', type: 'uint256' },
97
+ { name: 'extraSignatureData', type: 'bytes' },
98
+ ],
99
+ },
100
+ {
101
+ verifyingChainId: reyaChainId,
102
+ caller: caller,
103
+ owner: owner,
104
+ poolId: poolId,
105
+ sharesAmount: sharesAmount,
106
+ minOut: minOut,
107
+ nonce: nonce,
108
+ deadline: deadline,
109
+ extraSignatureData: ethers.AbiCoder.defaultAbiCoder().encode(
110
+ ['address', 'uint256', 'uint256'],
111
+ [receiver, chainId, socketMsgGasLimit],
112
+ ),
113
+ },
114
+ );
115
+
116
+ return convertEthersSignatureToEIP712Signature(signature, deadline);
117
+ }
118
+
119
+ async function signReyaTypedData(
120
+ signer: Signer | JsonRpcSigner,
121
+ verifyingContract: string,
122
+ types: Record<string, Array<TypedDataField>>,
123
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
+ value: Record<string, any>,
125
+ ): Promise<Signature> {
126
+ const domain = {
127
+ name: 'Reya',
128
+ version: '1',
129
+ verifyingContract: verifyingContract,
130
+ };
131
+
132
+ const signatureString = await signer.signTypedData(domain, types, value);
133
+
134
+ return Signature.from(signatureString);
135
+ }
@@ -0,0 +1,25 @@
1
+ import BigNumber from 'bignumber.js';
2
+
3
+ // todo: need the ability to supply custom slippage or use a more conservative default for the slippage tolerance
4
+ export function calculatePriceLimitForTrade(
5
+ orderPrice: number,
6
+ amount: number,
7
+ ): bigint {
8
+ // Ensure orderPrice is BigInt
9
+ const orderPriceBigInt = BigNumber(orderPrice);
10
+
11
+ // Calculate 50% of the order price
12
+ const offset = orderPriceBigInt.div(2);
13
+
14
+ // Determine if the amount is positive or negative and adjust the price accordingly
15
+ if (amount > 0) {
16
+ // Trade is long, so add
17
+ return BigInt(
18
+ BigNumber(orderPriceBigInt).plus(offset).times(1e18).toFixed(0),
19
+ );
20
+ } else {
21
+ return BigInt(
22
+ BigNumber(orderPriceBigInt).minus(offset).times(1e18).toFixed(0),
23
+ );
24
+ }
25
+ }
@@ -0,0 +1,3 @@
1
+ export function getGasBuffer(value: bigint): bigint {
2
+ return (value * BigInt(120)) / BigInt(100);
3
+ }