Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP-4308: cross-chain swap #238

Draft
wants to merge 87 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
c654778
add getCrossChainAssetsByNetwork method
TheJuze Jan 10, 2024
49c5294
update getCrossChainAssetsByNetwork prop types
TheJuze Jan 10, 2024
782b39c
update getCrossChainAssetsByNetwork return type
TheJuze Jan 10, 2024
2eeae06
Merge branch 'main' into feat/OP-4308-cross-chain-swap
TheJuze Jan 16, 2024
12cbd5a
update lock file
TheJuze Jan 16, 2024
72e1e94
add lock order support
TheJuze Jan 18, 2024
330fc51
add to signOrder secretHash only if targetChainId exist
TheJuze Jan 19, 2024
fcc3017
make fields in signOrder optional
TheJuze Jan 19, 2024
0d9d19b
Merge branch 'main' into feat/OP-4308-cross-chain-swap
TheJuze Jan 19, 2024
2079e49
fix build and types
TheJuze Jan 19, 2024
ac865f4
add signLockOrder to imports
TheJuze Jan 23, 2024
aac63a6
Merge branch 'main' into feat/OP-4308-cross-chain-swap
TheJuze Jan 23, 2024
a2cdadf
update lockOrder types
TheJuze Jan 23, 2024
74b2e44
update lockOrder types
TheJuze Jan 23, 2024
009f4f9
fix version
TheJuze Jan 23, 2024
a5ab496
add logger
TheJuze Jan 23, 2024
c6be1c7
fix signLockOrder
TheJuze Jan 24, 2024
e815373
fix signLockOrder
TheJuze Jan 24, 2024
62825cb
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
TheJuze May 6, 2024
6948b1b
bump v
TheJuze May 6, 2024
22b2602
bump v
TheJuze May 7, 2024
9a60f97
bump v
TheJuze May 8, 2024
82117a3
extend swap info
TheJuze May 13, 2024
6ed39b9
bump stf
TheJuze May 13, 2024
3860c76
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/OP-…
TheJuze May 15, 2024
ef099f3
update package-lock.json
TheJuze May 15, 2024
14c2f3b
update swapinfo schema
TheJuze May 16, 2024
5a98172
make props as object
TheJuze May 20, 2024
b1ad837
add types export
TheJuze May 20, 2024
29150c2
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/OP-…
TheJuze May 21, 2024
b1ea82e
update signOrder function
TheJuze May 21, 2024
a0c5bf4
update signLockOrder request
TheJuze May 21, 2024
132399c
update signOrder function
TheJuze May 22, 2024
cc928e4
update signLockOrder
TheJuze May 22, 2024
7c7f2f1
update hash forming
TheJuze May 22, 2024
9cd077f
bump sdk
TheJuze May 22, 2024
ee50522
mock signOrder hashing
TheJuze May 23, 2024
39cacef
remove user from signLockOrder
TheJuze May 23, 2024
6001d75
update lockOrderTypes.ts
TheJuze May 23, 2024
c262535
update lockOrderTypes.ts
TheJuze May 23, 2024
fd81062
update lockOrderTypes.ts
TheJuze May 23, 2024
15da8fe
update domainData chainId type
TheJuze May 23, 2024
5d9900c
update secret generation
TheJuze May 27, 2024
5f15147
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
TheJuze May 27, 2024
e6ec278
bump sdk
TheJuze May 27, 2024
6a7caf4
bump sdk
TheJuze May 27, 2024
b47a81f
update signOrder
TheJuze May 28, 2024
7cbf81e
fix placeLockOrder same secret and secretHash
TheJuze May 28, 2024
ef5ef4f
update signOrder types
TheJuze May 28, 2024
3229845
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
TheJuze May 28, 2024
3cb95bb
update signOrder types
TheJuze May 28, 2024
677f99a
🗿
TheJuze May 28, 2024
0c99c52
update orderTypes.ts
TheJuze May 29, 2024
f76b493
update signOrder
TheJuze May 29, 2024
af8eb67
add new order status: "TRANSFER"
TheJuze May 29, 2024
5f4e540
add lock order expiration = 4 days
TheJuze May 30, 2024
c76597e
change placeOrder body
TheJuze May 30, 2024
afb8049
change lockOrderExpiration
TheJuze May 30, 2024
761a9cc
massive refactor
TheJuze May 30, 2024
2613e4a
au update schema
TheJuze May 30, 2024
0c2b95a
au update schema
TheJuze May 30, 2024
517f127
change chainId to targetChainId
TheJuze May 31, 2024
ab4a674
update sign cross-chain order types
TheJuze Jun 3, 2024
11a5ba6
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
TheJuze Jun 3, 2024
551a3ff
bump v
TheJuze Jun 5, 2024
c73a41b
fix typo
TheJuze Jun 5, 2024
d36d616
remove ledger signature
TheJuze Jun 6, 2024
296d644
change lockOrderExpiration (mock)
TheJuze Jun 7, 2024
543a74e
change lockOrderExpiration (mock)
TheJuze Jun 10, 2024
6017224
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
TheJuze Jun 11, 2024
c07e672
add secret hash field
TheJuze Jun 11, 2024
1c12a4b
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
TheJuze Jun 11, 2024
1b045b7
bump contracts
TheJuze Jun 11, 2024
f515d88
update au schema
TheJuze Jun 12, 2024
5dce6e5
add get atomic fee method
TheJuze Jun 17, 2024
ad6de65
move atomic fee method to blockchain service
TheJuze Jun 17, 2024
7a8f555
update getAtomicSwapFee url
TheJuze Jun 17, 2024
87f2eed
Revert "change lockOrderExpiration (mock)"
TheJuze Jun 18, 2024
cfa389d
Revert "change lockOrderExpiration (mock)"
TheJuze Jun 18, 2024
de2219a
change atomic expiration to 4 days
TheJuze Jun 18, 2024
722ca75
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
cheshirrrcat Jul 10, 2024
bec2775
feat: added getMinLockAmountForCCS method
cheshirrrcat Jul 18, 2024
606188b
Merge branch 'refs/heads/main' into feat/OP-4308-cross-chain-swap
cheshirrrcat Jul 18, 2024
d2fe0d3
fix: fix small typo
cheshirrrcat Jul 18, 2024
9711930
feat: updated order scheme
cheshirrrcat Jul 23, 2024
8d28302
feat: updated SDK version
cheshirrrcat Jul 24, 2024
b5d700d
feat: updated SDK version
cheshirrrcat Jul 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.22.14",
"version": "0.23.1-rc0",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
Expand Down Expand Up @@ -86,9 +86,8 @@
},
"dependencies": {
"@babel/runtime": "^7.21.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@orionprotocol/contracts": "^1.23.9",
"@orionprotocol/contracts": "1.23.9",
"@types/lodash.clonedeep": "^4.5.9",
"bignumber.js": "^9.1.1",
"bson-objectid": "^2.0.4",
Expand All @@ -102,7 +101,7 @@
"merge-anything": "^5.1.7",
"neverthrow": "^6.0.0",
"patch-package": "^8.0.0",
"simple-typed-fetch": "0.2.3",
"simple-typed-fetch": "0.2.5",
"stream-browserify": "^3.0.0",
"tiny-invariant": "^1.3.1",
"ts-is-present": "^1.2.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Orion/bridge/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
LOCKATOMIC_GAS_LIMIT,
REDEEMATOMIC_GAS_LIMIT,
WITHDRAW_GAS_LIMIT
} from '../../constants/index.js';
} from '../../constants';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
import { denormalizeNumber, generateSecret, normalizeNumber, toUpperCase } from '../../utils/index.js';
import type { SupportedChainId } from '../../types.js';
Expand Down
36 changes: 20 additions & 16 deletions src/Unit/Exchange/swapLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import getBalances from '../../utils/getBalances.js';
import BalanceGuard from '../../BalanceGuard.js';
import getAvailableSources from '../../utils/getAvailableFundsSources.js';
import type Unit from '../index.js';
import { INTERNAL_PROTOCOL_PRECISION, NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '../../constants/index.js';
import {
INTERNAL_PROTOCOL_PRECISION,
NATIVE_CURRENCY_PRECISION,
SWAP_THROUGH_ORION_POOL_GAS_LIMIT
} from '../../constants';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
import { calculateFeeInFeeAsset, denormalizeNumber, normalizeNumber } from '../../utils/index.js';
import { signOrder } from '../../crypt/index.js';
import { signOrder } from '../../crypt';
import type orderSchema from '../../services/Aggregator/schemas/orderSchema.js';
import type { z } from 'zod';
import { simpleFetch } from 'simple-typed-fetch';
Expand Down Expand Up @@ -212,7 +216,7 @@ export default async function swapLimit({
if (options.developer.route === 'pool' && !priceIsBTEMP) {
throw new Error(
'CONFLICT: Pool execution is not available for this swap.' +
' Price is worse than market price. Please unset "route" option or set it to "aggregator"'
' Price is worse than market price. Please unset "route" option or set it to "aggregator"'
);
}
route = options.developer.route;
Expand All @@ -221,17 +225,17 @@ export default async function swapLimit({
if (!priceIsBTEMP) {
throw new Error(
'CONFLICT: Pool execution is not available for this swap.' +
' Price is worse than market price. Please disable "poolOnly" option'
' Price is worse than market price. Please disable "poolOnly" option'
);
}
options.logger?.('Swap is through pool (because "poolOnly" option is true)');
route = 'pool';
} else if (
poolExchangesList.length > 0 &&
swapExchanges.length === 1 &&
firstSwapExchange !== undefined &&
poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange) &&
priceIsBTEMP
swapExchanges.length === 1 &&
firstSwapExchange !== undefined &&
poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange) &&
priceIsBTEMP
) {
options?.logger?.(`Swap is through pool [via ${firstSwapExchange}] (detected by "exchanges" field)`);
route = 'pool';
Expand Down Expand Up @@ -439,19 +443,19 @@ export default async function swapLimit({

await balanceGuard.check(options?.autoApprove);

const signedOrder = await signOrder(
const signedOrder = await signOrder({
baseAssetAddress,
quoteAssetAddress,
swapInfo.orderInfo.side,
safePriceWithAppliedPrecision.toString(),
swapInfo.orderInfo.amount,
totalFeeInFeeAsset,
walletAddress,
side: swapInfo.orderInfo.side,
price: safePriceWithAppliedPrecision.toString(),
amount: swapInfo.orderInfo.amount,
matcherFee: totalFeeInFeeAsset,
senderAddress: walletAddress,
matcherAddress,
feeAssetAddress,
serviceFeeAssetAddress: feeAssetAddress,
signer,
chainId,
);
});
const orderIsOk = await exchangeContract.validateOrder(signedOrder);
if (!orderIsOk) throw new Error('Order is not valid');

Expand Down
30 changes: 17 additions & 13 deletions src/Unit/Exchange/swapMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { Exchange__factory } from '@orionprotocol/contracts/lib/ethers-v6/index.
import getBalances from '../../utils/getBalances.js';
import BalanceGuard from '../../BalanceGuard.js';
import getAvailableSources from '../../utils/getAvailableFundsSources.js';
import { INTERNAL_PROTOCOL_PRECISION, NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '../../constants/index.js';
import {
INTERNAL_PROTOCOL_PRECISION,
NATIVE_CURRENCY_PRECISION,
SWAP_THROUGH_ORION_POOL_GAS_LIMIT
} from '../../constants';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
import { calculateFeeInFeeAsset, denormalizeNumber, normalizeNumber } from '../../utils/index.js';
import { signOrder } from '../../crypt/index.js';
import { signOrder } from '../../crypt';
import type orderSchema from '../../services/Aggregator/schemas/orderSchema.js';
import type { z } from 'zod';
import type { SwapLimitParams } from './swapLimit.js';
Expand Down Expand Up @@ -178,9 +182,9 @@ export default async function swapMarket({
route = 'pool';
} else if (
poolExchangesList.length > 0 &&
swapExchanges.length === 1 &&
firstSwapExchange !== undefined &&
poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange)
swapExchanges.length === 1 &&
firstSwapExchange !== undefined &&
poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange)
) {
options?.logger?.(`Swap is through pool [via ${firstSwapExchange}] (detected by "exchanges" field)`);
route = 'pool';
Expand Down Expand Up @@ -398,19 +402,19 @@ export default async function swapMarket({

await balanceGuard.check(options?.autoApprove);

const signedOrder = await signOrder(
const signedOrder = await signOrder({
baseAssetAddress,
quoteAssetAddress,
swapInfo.orderInfo.side,
safePriceWithAppliedPrecision.toString(),
swapInfo.orderInfo.amount,
totalFeeInFeeAsset,
walletAddress,
side: swapInfo.orderInfo.side,
price: safePriceWithAppliedPrecision.toString(),
amount: swapInfo.orderInfo.amount,
matcherFee: totalFeeInFeeAsset,
senderAddress: walletAddress,
matcherAddress,
feeAssetAddress,
serviceFeeAssetAddress: feeAssetAddress,
signer,
chainId,
);
});
const orderIsOk = await exchangeContract.validateOrder(signedOrder);
if (!orderIsOk) throw new Error('Order is not valid');

Expand Down
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export { default as cancelOrderTypes } from './cancelOrderTypes.js';
export { default as orderStatuses } from './orderStatuses.js';
export { default as orderTypes } from './orderTypes.js';
export { default as subOrderStatuses } from './subOrderStatuses.js';
export { default as networkCodes } from './networkCodes.js';
export { default as exchanges } from './exchanges.js';
export { default as exchangesMap } from './exchangesMap.js';

export * from './orderTypes';
export * from './chains.js';
export * from './precisions.js';
export * from './gasLimits.js';
Expand Down
2 changes: 2 additions & 0 deletions src/constants/orderStatuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import subOrderStatuses from './subOrderStatuses.js';
const orderStatuses = [
...subOrderStatuses,
'ROUTING', // order got sub orders, but not all of them have status ACCEPTED
'TRANSFER', // TX_PENDING > TRANSFER > SETTLED
'REFUNDED' // TX_PENDING > TRANSFER > CANCELED > REFUNDED
] as const;

export default orderStatuses;
17 changes: 0 additions & 17 deletions src/constants/orderTypes.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/constants/orderTypes/crossChainOrderTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {ORDER_TYPE} from './orderTypes';

export const CROSS_CHAIN_ORDER_TYPES = {
Order: ORDER_TYPE,
CrossChainOrder: [
{name: 'limitOrder', type: 'Order'},
{name: 'chainId', type: 'uint24'},
{name: 'secretHash', type: 'bytes32'},
{name: 'lockOrderExpiration', type: 'uint64'},
]
}
3 changes: 3 additions & 0 deletions src/constants/orderTypes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './crossChainOrderTypes';
export * from './lockOrderTypes';
export { ORDER_TYPES } from './orderTypes';
10 changes: 10 additions & 0 deletions src/constants/orderTypes/lockOrderTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const LOCK_ORDER_TYPES = {
LockOrder: [
{ name: 'sender', type: 'address' },
{ name: 'expiration', type: 'uint64' },
{ name: 'asset', type: 'address' },
{ name: 'amount', type: 'uint64' },
{ name: 'targetChainId', type: 'uint24' },
{ name: 'secretHash', type: 'bytes32' },
],
};
27 changes: 27 additions & 0 deletions src/constants/orderTypes/orderTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const ORDER_TYPE = [
{ name: 'senderAddress', type: 'address' },
{ name: 'matcherAddress', type: 'address' },
{ name: 'baseAsset', type: 'address' },
{ name: 'quoteAsset', type: 'address' },
{ name: 'matcherFeeAsset', type: 'address' },
{ name: 'amount', type: 'uint64' },
{ name: 'price', type: 'uint64' },
{ name: 'matcherFee', type: 'uint64' },
{ name: 'nonce', type: 'uint64' },
{ name: 'expiration', type: 'uint64' },
{ name: 'buySide', type: 'uint8' },
];

export const ORDER_TYPES = {
Order: ORDER_TYPE
}

export const CROSS_CHAIN_ORDER_TYPES = {
Order: ORDER_TYPE,
CrossChainOrder: [
{ name: 'limitOrder', type: 'Order' },
{ name: 'targetChainId', type: 'uint24' },
{ name: 'secretHash', type: 'bytes32' },
{ name: 'lockOrderExpiration', type: 'uint64' },
]
}
4 changes: 2 additions & 2 deletions src/crypt/getDomainData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SupportedChainId } from '../types.js';
import eip712DomainData from '../config/eip712DomainData.json' assert { type: 'json' };
import eip712DomainData from '../config/eip712DomainData.json' assert {type: 'json'};
import eip712DomainSchema from '../config/schemas/eip712DomainSchema.js';

const EIP712Domain = eip712DomainSchema.parse(eip712DomainData);
Expand All @@ -19,7 +19,7 @@ function removeUndefined<T>(obj: Record<string, T | undefined>) {
*/
const getDomainData = (chainId: SupportedChainId) => ({
...removeUndefined(EIP712Domain),
chainId,
chainId: Number(chainId),
});

export default getDomainData;
16 changes: 0 additions & 16 deletions src/crypt/hashOrder.ts

This file was deleted.

Loading
Loading