@reyaxyz/sdk 0.1.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.md +1 -0
- package/README.md +10 -0
- package/dist/abis/AccountModule.json +1079 -0
- package/dist/abis/ExecutionModule.json +758 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/services/encode.js +37 -0
- package/dist/services/encode.js.map +1 -0
- package/dist/services/executeTransaction.js +110 -0
- package/dist/services/executeTransaction.js.map +1 -0
- package/dist/services/lp/encode.js +29 -0
- package/dist/services/lp/encode.js.map +1 -0
- package/dist/services/lp/lp.js +70 -0
- package/dist/services/lp/lp.js.map +1 -0
- package/dist/services/lp/types.js +3 -0
- package/dist/services/lp/types.js.map +1 -0
- package/dist/services/margin-accounts/account.js +56 -0
- package/dist/services/margin-accounts/account.js.map +1 -0
- package/dist/services/margin-accounts/deposit.js +56 -0
- package/dist/services/margin-accounts/deposit.js.map +1 -0
- package/dist/services/margin-accounts/encode.js +43 -0
- package/dist/services/margin-accounts/encode.js.map +1 -0
- package/dist/services/margin-accounts/types.js +3 -0
- package/dist/services/margin-accounts/types.js.map +1 -0
- package/dist/services/margin-accounts/withdraw.js +56 -0
- package/dist/services/margin-accounts/withdraw.js.map +1 -0
- package/dist/services/orders/encode.js +25 -0
- package/dist/services/orders/encode.js.map +1 -0
- package/dist/services/orders/order.js +56 -0
- package/dist/services/orders/order.js.map +1 -0
- package/dist/services/orders/types.js +3 -0
- package/dist/services/orders/types.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/services/encode.d.ts +6 -0
- package/dist/types/services/encode.d.ts.map +1 -0
- package/dist/types/services/executeTransaction.d.ts +12 -0
- package/dist/types/services/executeTransaction.d.ts.map +1 -0
- package/dist/types/services/lp/encode.d.ts +4 -0
- package/dist/types/services/lp/encode.d.ts.map +1 -0
- package/dist/types/services/lp/lp.d.ts +4 -0
- package/dist/types/services/lp/lp.d.ts.map +1 -0
- package/dist/types/services/lp/types.d.ts +17 -0
- package/dist/types/services/lp/types.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/account.d.ts +3 -0
- package/dist/types/services/margin-accounts/account.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/deposit.d.ts +3 -0
- package/dist/types/services/margin-accounts/deposit.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/encode.d.ts +5 -0
- package/dist/types/services/margin-accounts/encode.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/types.d.ts +25 -0
- package/dist/types/services/margin-accounts/types.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/withdraw.d.ts +3 -0
- package/dist/types/services/margin-accounts/withdraw.d.ts.map +1 -0
- package/dist/types/services/orders/encode.d.ts +3 -0
- package/dist/types/services/orders/encode.d.ts.map +1 -0
- package/dist/types/services/orders/order.d.ts +3 -0
- package/dist/types/services/orders/order.d.ts.map +1 -0
- package/dist/types/services/orders/types.d.ts +12 -0
- package/dist/types/services/orders/types.d.ts.map +1 -0
- package/dist/types/utils/action.d.ts +27 -0
- package/dist/types/utils/action.d.ts.map +1 -0
- package/dist/types/utils/routerCommands.d.ts +21 -0
- package/dist/types/utils/routerCommands.d.ts.map +1 -0
- package/dist/types/utils/txHelpers.d.ts +2 -0
- package/dist/types/utils/txHelpers.d.ts.map +1 -0
- package/dist/utils/action.js +22 -0
- package/dist/utils/action.js.map +1 -0
- package/dist/utils/routerCommands.js +41 -0
- package/dist/utils/routerCommands.js.map +1 -0
- package/dist/utils/txHelpers.js +8 -0
- package/dist/utils/txHelpers.js.map +1 -0
- package/package.json +36 -0
- package/src/abis/AccountModule.json +1079 -0
- package/src/abis/CoreProxy.json +206 -0
- package/src/abis/ExecutionModule.json +758 -0
- package/src/index.ts +1 -0
- package/src/services/encode.ts +72 -0
- package/src/services/executeTransaction.ts +54 -0
- package/src/services/lp/encode.ts +36 -0
- package/src/services/lp/lp.ts +47 -0
- package/src/services/lp/types.ts +18 -0
- package/src/services/margin-accounts/account.ts +21 -0
- package/src/services/margin-accounts/deposit.ts +23 -0
- package/src/services/margin-accounts/encode.ts +56 -0
- package/src/services/margin-accounts/types.ts +27 -0
- package/src/services/margin-accounts/withdraw.ts +23 -0
- package/src/services/orders/encode.ts +29 -0
- package/src/services/orders/order.ts +24 -0
- package/src/services/orders/types.ts +12 -0
- package/src/utils/action.ts +41 -0
- package/src/utils/routerCommands.ts +58 -0
- package/src/utils/txHelpers.ts +3 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils/action';
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//////////////////// ENCODE SINGLE ////////////////////
|
|
2
|
+
|
|
3
|
+
import { MethodParameters, MultiAction } from '../utils/action';
|
|
4
|
+
import { CommandType, getCommand } from '../utils/routerCommands';
|
|
5
|
+
import { AbiCoder, Interface } from 'ethers';
|
|
6
|
+
import { abi } from '../abis/ExecutionModule.json';
|
|
7
|
+
// const abi = [
|
|
8
|
+
// 'function coreExecute(uint128 poolId, uint128 accountId, Command[] commands, EIP712Signature sig)',
|
|
9
|
+
// ];
|
|
10
|
+
export const encodeSingleDeposit = (
|
|
11
|
+
token: string,
|
|
12
|
+
amount: bigint,
|
|
13
|
+
marketId: number,
|
|
14
|
+
exchangeId: number,
|
|
15
|
+
multiAction: MultiAction,
|
|
16
|
+
): void => {
|
|
17
|
+
multiAction.newAction(
|
|
18
|
+
getCommand(CommandType.DEPOSIT, [token, amount], marketId, exchangeId),
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const encodeSingleWithdraw = (
|
|
23
|
+
token: string,
|
|
24
|
+
amount: bigint,
|
|
25
|
+
marketId: number,
|
|
26
|
+
exchangeId: number,
|
|
27
|
+
multiAction: MultiAction,
|
|
28
|
+
): void => {
|
|
29
|
+
multiAction.newAction(
|
|
30
|
+
getCommand(CommandType.WITHDRAW, [token, amount], marketId, exchangeId),
|
|
31
|
+
);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const encodeSingleTrade = (
|
|
35
|
+
counterpartyAccountIds: number[],
|
|
36
|
+
orderBase: bigint,
|
|
37
|
+
orderPriceLimit: bigint,
|
|
38
|
+
marketId: number,
|
|
39
|
+
exchangeId: number,
|
|
40
|
+
multiAction: MultiAction,
|
|
41
|
+
): void => {
|
|
42
|
+
// (int256 orderBase, UD60x18 orderPriceLimit) = abi.decode(matchOrderInputs.inputs, (int256, UD60x18));
|
|
43
|
+
const encodedBasePrice = AbiCoder.defaultAbiCoder().encode(
|
|
44
|
+
['int256', 'uint256'], // @todo check if uint256 casts to UD60x18
|
|
45
|
+
[orderBase, orderPriceLimit],
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
multiAction.newAction(
|
|
49
|
+
getCommand(
|
|
50
|
+
CommandType.MATCH_ORDER,
|
|
51
|
+
[counterpartyAccountIds, encodedBasePrice],
|
|
52
|
+
marketId,
|
|
53
|
+
exchangeId,
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const encodeRouterCall = (
|
|
59
|
+
multiAction: MultiAction,
|
|
60
|
+
accountId: number,
|
|
61
|
+
nativeCurrencyValue: bigint,
|
|
62
|
+
): MethodParameters => {
|
|
63
|
+
const functionSignature = 'execute';
|
|
64
|
+
const parameters = [accountId, multiAction.commands];
|
|
65
|
+
|
|
66
|
+
const INTERFACE = new Interface(abi);
|
|
67
|
+
const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
68
|
+
return {
|
|
69
|
+
calldata: calldata,
|
|
70
|
+
value: BigInt(nativeCurrencyValue).toString(10),
|
|
71
|
+
};
|
|
72
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Signer } from 'ethers';
|
|
2
|
+
import { getGasBuffer } from '../utils/txHelpers';
|
|
3
|
+
|
|
4
|
+
export type Transaction = {
|
|
5
|
+
from: string;
|
|
6
|
+
to: string;
|
|
7
|
+
data: string;
|
|
8
|
+
value?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export async function estimateGas(
|
|
12
|
+
signer: Signer,
|
|
13
|
+
data: string,
|
|
14
|
+
value: string,
|
|
15
|
+
chainId: number,
|
|
16
|
+
): Promise<Transaction & { gasLimit: bigint }> {
|
|
17
|
+
console.log(chainId); // @todo remove chainId if obsolete
|
|
18
|
+
const accountAddress = await signer.getAddress();
|
|
19
|
+
const contractAddress = `0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4`; // @todo Define after deployment
|
|
20
|
+
const tx = {
|
|
21
|
+
from: accountAddress,
|
|
22
|
+
to: contractAddress,
|
|
23
|
+
data,
|
|
24
|
+
...(value && value !== '0' ? { value: value } : {}),
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let gasLimit: bigint;
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const gasEstimate = await signer.estimateGas(tx);
|
|
31
|
+
gasLimit = getGasBuffer(gasEstimate);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.warn(error);
|
|
34
|
+
throw new Error(error as string);
|
|
35
|
+
}
|
|
36
|
+
return { ...tx, gasLimit };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function executeTransaction(
|
|
40
|
+
signer: Signer,
|
|
41
|
+
data: string,
|
|
42
|
+
value: string,
|
|
43
|
+
chainId: number,
|
|
44
|
+
) {
|
|
45
|
+
const txData = await estimateGas(signer, data, value, chainId);
|
|
46
|
+
try {
|
|
47
|
+
const txResponse = await signer.sendTransaction(txData);
|
|
48
|
+
const txReceipt = await txResponse.wait(); // @todo Check what was reasoning behind this since this function waits until transaction is minted
|
|
49
|
+
return txReceipt;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.warn(error);
|
|
52
|
+
throw new Error('Transaction Execution Error');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { MethodParameters } from '../../utils/action';
|
|
2
|
+
import { Interface } from 'ethers';
|
|
3
|
+
|
|
4
|
+
// @todo Update ABI
|
|
5
|
+
const provideLiquidityAbi = [
|
|
6
|
+
'function addLiquidity(uint128 poolId, address owner, uint256 amount, uint256 minShares) returns (uint256)',
|
|
7
|
+
];
|
|
8
|
+
|
|
9
|
+
// @todo Update ABI
|
|
10
|
+
const removeLiquidityabi = [
|
|
11
|
+
'function removeLiquidity(uint128 poolId, uint256 sharesAmount, uint256 minOut) returns (uint256)',
|
|
12
|
+
];
|
|
13
|
+
export const encodeProvideLiquidityCall = (
|
|
14
|
+
poolId: number,
|
|
15
|
+
accountOwner: string,
|
|
16
|
+
amount: number,
|
|
17
|
+
minShares: number,
|
|
18
|
+
): MethodParameters => {
|
|
19
|
+
const functionSignature = 'addLiquidity';
|
|
20
|
+
const parameters = [poolId, accountOwner, amount, minShares];
|
|
21
|
+
const INTERFACE = new Interface(provideLiquidityAbi);
|
|
22
|
+
const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
23
|
+
return { calldata: calldata, value: BigInt(0).toString(10) };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const encodeRemoveLiquidityCall = (
|
|
27
|
+
poolId: number,
|
|
28
|
+
sharesAmount: number,
|
|
29
|
+
minOut: number,
|
|
30
|
+
): MethodParameters => {
|
|
31
|
+
const functionSignature = 'removeLiquidity';
|
|
32
|
+
const parameters = [poolId, sharesAmount, minOut];
|
|
33
|
+
const INTERFACE = new Interface(removeLiquidityabi);
|
|
34
|
+
const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
35
|
+
return { calldata: calldata, value: BigInt(0).toString(10) };
|
|
36
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { executeTransaction } from '../executeTransaction';
|
|
2
|
+
import {
|
|
3
|
+
encodeProvideLiquidityCall,
|
|
4
|
+
encodeRemoveLiquidityCall,
|
|
5
|
+
} from './encode';
|
|
6
|
+
import { ProvideLiquidityArgs, RemoveLiquidityArgs } from './types';
|
|
7
|
+
|
|
8
|
+
export const provideLiquidity = async (
|
|
9
|
+
params: ProvideLiquidityArgs,
|
|
10
|
+
): Promise<unknown> => {
|
|
11
|
+
// @todo update type once we agree on the structure
|
|
12
|
+
const { calldata: data, value } = encodeProvideLiquidityCall(
|
|
13
|
+
params.poolId,
|
|
14
|
+
params.owner,
|
|
15
|
+
params.amount,
|
|
16
|
+
params.minShares,
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const result = await executeTransaction(
|
|
20
|
+
params.signer,
|
|
21
|
+
data,
|
|
22
|
+
value,
|
|
23
|
+
params.chainId,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const removeLiquidity = async (
|
|
30
|
+
params: RemoveLiquidityArgs,
|
|
31
|
+
): Promise<unknown> => {
|
|
32
|
+
// @todo update type once we agree on the structure
|
|
33
|
+
const { calldata: data, value } = encodeRemoveLiquidityCall(
|
|
34
|
+
params.poolId,
|
|
35
|
+
params.sharesAmount,
|
|
36
|
+
params.minOut,
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const result = await executeTransaction(
|
|
40
|
+
params.signer,
|
|
41
|
+
data,
|
|
42
|
+
value,
|
|
43
|
+
params.chainId,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Signer } from 'ethers';
|
|
2
|
+
|
|
3
|
+
export type ProvideLiquidityArgs = {
|
|
4
|
+
signer: Signer;
|
|
5
|
+
chainId: number;
|
|
6
|
+
poolId: number;
|
|
7
|
+
owner: string;
|
|
8
|
+
amount: number;
|
|
9
|
+
minShares: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type RemoveLiquidityArgs = {
|
|
13
|
+
signer: Signer;
|
|
14
|
+
chainId: number;
|
|
15
|
+
poolId: number;
|
|
16
|
+
sharesAmount: number;
|
|
17
|
+
minOut: number;
|
|
18
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { executeTransaction } from '../executeTransaction';
|
|
2
|
+
import { encodeCreateAccountCall } from './encode';
|
|
3
|
+
import { CreateAccountArgs } from './types';
|
|
4
|
+
|
|
5
|
+
export const createAccount = async (
|
|
6
|
+
params: CreateAccountArgs,
|
|
7
|
+
): Promise<unknown> => {
|
|
8
|
+
// @todo update type once we agree on the structure
|
|
9
|
+
const { calldata: data, value } = encodeCreateAccountCall(
|
|
10
|
+
params.ownerAddress,
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
const result = await executeTransaction(
|
|
14
|
+
params.signer,
|
|
15
|
+
data,
|
|
16
|
+
value,
|
|
17
|
+
params.chainId,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DepositArgs } from './types';
|
|
2
|
+
import { encodeDeposit } from './encode';
|
|
3
|
+
import { executeTransaction } from '../executeTransaction';
|
|
4
|
+
|
|
5
|
+
export const deposit = async (params: DepositArgs): Promise<unknown> => {
|
|
6
|
+
// @todo update type once we agree on the structure
|
|
7
|
+
const { calldata: data, value } = encodeDeposit(
|
|
8
|
+
params.accountId,
|
|
9
|
+
params.token,
|
|
10
|
+
params.amount,
|
|
11
|
+
params.marketId,
|
|
12
|
+
params.exchangeId,
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
const result = await executeTransaction(
|
|
16
|
+
params.signer,
|
|
17
|
+
data,
|
|
18
|
+
value,
|
|
19
|
+
params.chainId,
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { MethodParameters, MultiAction } from '../../utils/action';
|
|
2
|
+
import { Interface } from 'ethers';
|
|
3
|
+
import {
|
|
4
|
+
encodeRouterCall,
|
|
5
|
+
encodeSingleDeposit,
|
|
6
|
+
encodeSingleWithdraw,
|
|
7
|
+
} from '../encode';
|
|
8
|
+
|
|
9
|
+
import { abi } from '../../abis/AccountModule.json';
|
|
10
|
+
export const encodeCreateAccountCall = (
|
|
11
|
+
accountOwner: string,
|
|
12
|
+
): MethodParameters => {
|
|
13
|
+
const functionSignature = 'createAccount';
|
|
14
|
+
const parameters = [accountOwner];
|
|
15
|
+
const INTERFACE = new Interface(abi);
|
|
16
|
+
const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
17
|
+
return { calldata: calldata, value: BigInt(0).toString(10) };
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const encodeDeposit = (
|
|
21
|
+
accountId: number,
|
|
22
|
+
token: string,
|
|
23
|
+
amount: bigint,
|
|
24
|
+
marketId: number,
|
|
25
|
+
exchangeId: number,
|
|
26
|
+
): MethodParameters => {
|
|
27
|
+
const multiAction = new MultiAction();
|
|
28
|
+
|
|
29
|
+
encodeSingleDeposit(token, amount, marketId, exchangeId, multiAction);
|
|
30
|
+
|
|
31
|
+
const value = BigInt(0);
|
|
32
|
+
const call = encodeRouterCall(multiAction, accountId, value);
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
...call,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const encodeWithdraw = (
|
|
40
|
+
accountId: number,
|
|
41
|
+
token: string,
|
|
42
|
+
amount: bigint,
|
|
43
|
+
marketId: number,
|
|
44
|
+
exchangeId: number,
|
|
45
|
+
): MethodParameters => {
|
|
46
|
+
const multiAction = new MultiAction();
|
|
47
|
+
|
|
48
|
+
encodeSingleWithdraw(token, amount, marketId, exchangeId, multiAction);
|
|
49
|
+
|
|
50
|
+
const value = BigInt(0);
|
|
51
|
+
const call = encodeRouterCall(multiAction, accountId, value);
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
...call,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Signer } from 'ethers';
|
|
2
|
+
|
|
3
|
+
export type CreateAccountArgs = {
|
|
4
|
+
signer: Signer;
|
|
5
|
+
chainId: number;
|
|
6
|
+
ownerAddress: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type DepositArgs = {
|
|
10
|
+
signer: Signer;
|
|
11
|
+
chainId: number;
|
|
12
|
+
accountId: number;
|
|
13
|
+
token: string;
|
|
14
|
+
amount: bigint;
|
|
15
|
+
marketId: number;
|
|
16
|
+
exchangeId: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type WithdrawArgs = {
|
|
20
|
+
signer: Signer;
|
|
21
|
+
chainId: number;
|
|
22
|
+
accountId: number;
|
|
23
|
+
token: string;
|
|
24
|
+
amount: bigint;
|
|
25
|
+
marketId: number;
|
|
26
|
+
exchangeId: number;
|
|
27
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { WithdrawArgs } from './types';
|
|
2
|
+
import { encodeWithdraw } from './encode';
|
|
3
|
+
import { executeTransaction } from '../executeTransaction';
|
|
4
|
+
|
|
5
|
+
export const withdraw = async (params: WithdrawArgs): Promise<unknown> => {
|
|
6
|
+
// @todo update type once we agree on the structure
|
|
7
|
+
const { calldata: data, value } = encodeWithdraw(
|
|
8
|
+
params.accountId,
|
|
9
|
+
params.token,
|
|
10
|
+
params.amount,
|
|
11
|
+
params.marketId,
|
|
12
|
+
params.exchangeId,
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
const result = await executeTransaction(
|
|
16
|
+
params.signer,
|
|
17
|
+
data,
|
|
18
|
+
value,
|
|
19
|
+
params.chainId,
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { MethodParameters, MultiAction } from '../../utils/action';
|
|
2
|
+
import { encodeRouterCall, encodeSingleTrade } from '../encode';
|
|
3
|
+
|
|
4
|
+
export const encodeMatchOrder = (
|
|
5
|
+
accountId: number,
|
|
6
|
+
orderBase: bigint,
|
|
7
|
+
orderPriceLimit: bigint,
|
|
8
|
+
counterpartyAccountIds: number[],
|
|
9
|
+
marketId: number,
|
|
10
|
+
exchangeId: number,
|
|
11
|
+
): MethodParameters => {
|
|
12
|
+
const multiAction = new MultiAction();
|
|
13
|
+
|
|
14
|
+
encodeSingleTrade(
|
|
15
|
+
counterpartyAccountIds,
|
|
16
|
+
orderBase,
|
|
17
|
+
orderPriceLimit,
|
|
18
|
+
marketId,
|
|
19
|
+
exchangeId,
|
|
20
|
+
multiAction,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const value = BigInt(0);
|
|
24
|
+
const call = encodeRouterCall(multiAction, accountId, value);
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
...call,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { MatchOrderArgs } from './types';
|
|
2
|
+
import { encodeMatchOrder } from './encode';
|
|
3
|
+
import { executeTransaction } from '../executeTransaction';
|
|
4
|
+
|
|
5
|
+
export const matchOrder = async (params: MatchOrderArgs): Promise<unknown> => {
|
|
6
|
+
// @todo update type once we agree on the structure
|
|
7
|
+
const { calldata: data, value } = encodeMatchOrder(
|
|
8
|
+
params.accountId,
|
|
9
|
+
params.orderBase,
|
|
10
|
+
params.orderPriceLimit,
|
|
11
|
+
params.counterpartyAccountIds,
|
|
12
|
+
params.marketId,
|
|
13
|
+
params.exchangeId,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const result = await executeTransaction(
|
|
17
|
+
params.signer,
|
|
18
|
+
data,
|
|
19
|
+
value,
|
|
20
|
+
params.chainId,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Signer } from 'ethers';
|
|
2
|
+
|
|
3
|
+
export type MatchOrderArgs = {
|
|
4
|
+
signer: Signer;
|
|
5
|
+
chainId: number;
|
|
6
|
+
accountId: number;
|
|
7
|
+
orderBase: bigint;
|
|
8
|
+
orderPriceLimit: bigint;
|
|
9
|
+
counterpartyAccountIds: number[];
|
|
10
|
+
marketId: number;
|
|
11
|
+
exchangeId: number;
|
|
12
|
+
};
|
|
@@ -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,58 @@
|
|
|
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
|
+
MATCH_ORDER,
|
|
13
|
+
PROPAGATE_CACHE_FLOW,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const ABI_DEFINITION: { [key in CommandType]: string[] } = {
|
|
17
|
+
[CommandType.DEPOSIT]: ['address', 'uint256'],
|
|
18
|
+
[CommandType.WITHDRAW]: ['address', 'uint256'],
|
|
19
|
+
[CommandType.MATCH_ORDER]: ['uint128[]', 'bytes'],
|
|
20
|
+
[CommandType.PROPAGATE_CACHE_FLOW]: [], // @todo Check if correct
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type RouterCommand = {
|
|
24
|
+
type: CommandType;
|
|
25
|
+
encodedInput: string;
|
|
26
|
+
marketId: number;
|
|
27
|
+
exchangeId: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function createCommand(
|
|
31
|
+
type: CommandType,
|
|
32
|
+
parameters: unknown[],
|
|
33
|
+
marketId: number,
|
|
34
|
+
exchangeId: number,
|
|
35
|
+
): RouterCommand {
|
|
36
|
+
const encodedInput = AbiCoder.defaultAbiCoder().encode(
|
|
37
|
+
ABI_DEFINITION[type],
|
|
38
|
+
parameters,
|
|
39
|
+
);
|
|
40
|
+
return { type, encodedInput, marketId, exchangeId };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getCommand(
|
|
44
|
+
type: CommandType,
|
|
45
|
+
parameters: unknown[],
|
|
46
|
+
marketId: number,
|
|
47
|
+
exchangeId: number,
|
|
48
|
+
): SingleAction {
|
|
49
|
+
const command = createCommand(type, parameters, marketId, exchangeId);
|
|
50
|
+
return {
|
|
51
|
+
command: {
|
|
52
|
+
commandType: command.type,
|
|
53
|
+
inputs: command.encodedInput,
|
|
54
|
+
marketId: command.marketId,
|
|
55
|
+
exchangeId: command.exchangeId,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|