@gearbox-protocol/sdk 14.12.0-next.81 → 14.12.0-next.82
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/dist/cjs/model/filters.js +0 -4
- package/dist/cjs/model/filters.schema.js +10 -1
- package/dist/cjs/model/index.js +3 -0
- package/dist/cjs/model/liquidations.js +1 -1
- package/dist/cjs/model/liquidations.schema.js +1 -1
- package/dist/cjs/model/opportunities.js +1 -1
- package/dist/cjs/model/opportunities.schema.js +6 -13
- package/dist/cjs/model/positions.js +1 -1
- package/dist/cjs/model/positions.schema.js +33 -1
- package/dist/cjs/model/response.schema.js +9 -6
- package/dist/cjs/new-sdk/AbstractNamespace.js +46 -128
- package/dist/cjs/new-sdk/GearboxSDK.js +55 -42
- package/dist/cjs/new-sdk/errors/AllSourcesFailedError.js +20 -0
- package/dist/cjs/new-sdk/errors/MissingSourceError.js +25 -0
- package/dist/cjs/new-sdk/errors/NoSourceServedError.js +20 -0
- package/dist/cjs/new-sdk/errors/SourceChainMismatchError.js +34 -0
- package/dist/cjs/new-sdk/errors/SourceUnavailableError.js +25 -0
- package/dist/cjs/new-sdk/errors/assertSameChains.js +12 -0
- package/dist/cjs/new-sdk/errors/everyChainFailed.js +11 -0
- package/dist/cjs/new-sdk/errors/index.js +15 -0
- package/dist/cjs/new-sdk/index.js +23 -3
- package/dist/cjs/new-sdk/opportunities/OpportunitiesNamespace.js +37 -89
- package/dist/cjs/new-sdk/positions/PositionsNamespace.js +27 -94
- package/dist/cjs/new-sdk/types.js +0 -21
- package/dist/cjs/new-sdk/utils/filterResponse.js +21 -0
- package/dist/cjs/new-sdk/utils/index.js +6 -0
- package/dist/cjs/new-sdk/utils/mergeChains.js +81 -0
- package/dist/cjs/new-sdk/utils/types.js +1 -0
- package/dist/cjs/offchain/AbstractOffchainNamespace.js +42 -65
- package/dist/cjs/offchain/GearboxAPI.js +12 -8
- package/dist/cjs/offchain/errors/OffchainInvalidJsonError.js +19 -0
- package/dist/cjs/offchain/errors/OffchainNotConfiguredError.js +20 -0
- package/dist/cjs/offchain/errors/OffchainNotImplementedError.js +19 -0
- package/dist/cjs/offchain/errors/OffchainRequestFailedError.js +24 -0
- package/dist/cjs/offchain/errors/OffchainStatusError.js +20 -0
- package/dist/cjs/offchain/errors/OffchainTransportError.js +31 -0
- package/dist/cjs/offchain/errors/OffchainValidationError.js +26 -0
- package/dist/cjs/offchain/errors/backendMessage.js +28 -0
- package/dist/cjs/offchain/errors/errorCause.js +12 -0
- package/dist/cjs/offchain/errors/index.js +21 -0
- package/dist/cjs/offchain/errors/readResponseBody.js +15 -0
- package/dist/cjs/offchain/index.js +22 -5
- package/dist/cjs/offchain/opportunities/OffchainOpportunities.js +6 -3
- package/dist/cjs/offchain/positions/OffchainPositions.js +15 -21
- package/dist/cjs/offchain/types.js +0 -75
- package/dist/cjs/sdk/MultichainSDK.js +1 -1
- package/dist/cjs/sdk/OnchainSDK.js +1 -1
- package/dist/cjs/sdk/accounts/liquidations/MultichainLiquidationsService.js +38 -14
- package/dist/cjs/sdk/base/MultichainConstruct.js +73 -34
- package/dist/cjs/sdk/chain/chains.js +11 -0
- package/dist/cjs/sdk/chain/index.js +1 -0
- package/dist/cjs/sdk/core/index.js +1 -1
- package/dist/cjs/sdk/index.js +2 -1
- package/dist/cjs/sdk/opportunities/MultichainOpportunitiesService.js +18 -32
- package/dist/cjs/sdk/opportunities/OpportunitiesService.js +1 -4
- package/dist/cjs/sdk/positions/MultichainPositionsService.js +6 -29
- package/dist/cjs/sdk/positions/PositionsService.js +1 -2
- package/dist/esm/dev/AccountOpener.js +1 -1
- package/dist/esm/dev/withdrawalUtils.js +1 -1
- package/dist/esm/model/filters.js +0 -4
- package/dist/esm/model/filters.schema.js +10 -2
- package/dist/esm/model/index.js +3 -3
- package/dist/esm/model/liquidations.js +1 -1
- package/dist/esm/model/liquidations.schema.js +1 -1
- package/dist/esm/model/opportunities.js +1 -1
- package/dist/esm/model/opportunities.schema.js +4 -11
- package/dist/esm/model/positions.js +1 -1
- package/dist/esm/model/positions.schema.js +33 -3
- package/dist/esm/model/response.schema.js +9 -6
- package/dist/esm/new-sdk/AbstractNamespace.js +46 -128
- package/dist/esm/new-sdk/GearboxSDK.js +55 -42
- package/dist/esm/new-sdk/errors/AllSourcesFailedError.js +19 -0
- package/dist/esm/new-sdk/errors/MissingSourceError.js +24 -0
- package/dist/esm/new-sdk/errors/NoSourceServedError.js +19 -0
- package/dist/esm/new-sdk/errors/SourceChainMismatchError.js +33 -0
- package/dist/esm/new-sdk/errors/SourceUnavailableError.js +24 -0
- package/dist/esm/new-sdk/errors/assertSameChains.js +11 -0
- package/dist/esm/new-sdk/errors/everyChainFailed.js +10 -0
- package/dist/esm/new-sdk/errors/index.js +8 -0
- package/dist/esm/new-sdk/index.js +13 -3
- package/dist/esm/new-sdk/opportunities/OpportunitiesNamespace.js +38 -90
- package/dist/esm/new-sdk/positions/PositionsNamespace.js +28 -95
- package/dist/esm/new-sdk/types.js +1 -21
- package/dist/esm/new-sdk/utils/filterResponse.js +20 -0
- package/dist/esm/new-sdk/utils/index.js +3 -1
- package/dist/esm/new-sdk/utils/mergeChains.js +78 -0
- package/dist/esm/new-sdk/utils/types.js +1 -0
- package/dist/esm/offchain/AbstractOffchainNamespace.js +41 -64
- package/dist/esm/offchain/GearboxAPI.js +12 -8
- package/dist/esm/offchain/errors/OffchainInvalidJsonError.js +18 -0
- package/dist/esm/offchain/errors/OffchainNotConfiguredError.js +19 -0
- package/dist/esm/offchain/errors/OffchainNotImplementedError.js +18 -0
- package/dist/esm/offchain/errors/OffchainRequestFailedError.js +23 -0
- package/dist/esm/offchain/errors/OffchainStatusError.js +19 -0
- package/dist/esm/offchain/errors/OffchainTransportError.js +30 -0
- package/dist/esm/offchain/errors/OffchainValidationError.js +25 -0
- package/dist/esm/offchain/errors/backendMessage.js +27 -0
- package/dist/esm/offchain/errors/errorCause.js +11 -0
- package/dist/esm/offchain/errors/index.js +11 -0
- package/dist/esm/offchain/errors/readResponseBody.js +14 -0
- package/dist/esm/offchain/index.js +13 -2
- package/dist/esm/offchain/opportunities/OffchainOpportunities.js +6 -3
- package/dist/esm/offchain/positions/OffchainPositions.js +15 -21
- package/dist/esm/offchain/types.js +1 -72
- package/dist/esm/plugins/adapters/contracts/ERC4626AdapterContract.js +1 -1
- package/dist/esm/preview/simulate/simulatePoolOperation.js +1 -1
- package/dist/esm/preview/trace/extractTransfers.js +1 -1
- package/dist/esm/sdk/MultichainSDK.js +1 -1
- package/dist/esm/sdk/OnchainSDK.js +1 -1
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +2 -2
- package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +1 -1
- package/dist/esm/sdk/accounts/liquidations/MultichainLiquidationsService.js +38 -14
- package/dist/esm/sdk/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -1
- package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -1
- package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -1
- package/dist/esm/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
- package/dist/esm/sdk/base/MultichainConstruct.js +73 -34
- package/dist/esm/sdk/base/TokensMeta.js +2 -2
- package/dist/esm/sdk/chain/chains.js +11 -1
- package/dist/esm/sdk/chain/detectNetwork.js +1 -1
- package/dist/esm/sdk/chain/index.js +2 -2
- package/dist/esm/sdk/core/createAddressProvider.js +1 -1
- package/dist/esm/sdk/core/index.js +1 -1
- package/dist/esm/sdk/index.js +3 -3
- package/dist/esm/sdk/market/credit/CreditFacadeV310BaseContract.js +1 -1
- package/dist/esm/sdk/market/pool/PoolV310Contract.js +1 -1
- package/dist/esm/sdk/market/zapper/IETHZapperContract.js +1 -1
- package/dist/esm/sdk/market/zapper/ZapperContract.js +1 -1
- package/dist/esm/sdk/opportunities/MultichainOpportunitiesService.js +18 -32
- package/dist/esm/sdk/opportunities/OpportunitiesService.js +1 -4
- package/dist/esm/sdk/pools/PoolService.js +1 -1
- package/dist/esm/sdk/positions/MultichainPositionsService.js +6 -29
- package/dist/esm/sdk/positions/PositionsService.js +1 -2
- package/dist/esm/sdk/utils/viem/simulateWithPriceUpdates.js +1 -1
- package/dist/types/model/filters.d.ts +9 -3
- package/dist/types/model/filters.schema.d.ts +7 -1
- package/dist/types/model/index.d.ts +5 -5
- package/dist/types/model/liquidations.d.ts +2 -6
- package/dist/types/model/liquidations.schema.d.ts +1 -1
- package/dist/types/model/opportunities.d.ts +3 -7
- package/dist/types/model/opportunities.schema.d.ts +2 -2
- package/dist/types/model/positions.d.ts +2 -6
- package/dist/types/model/positions.schema.d.ts +32 -2
- package/dist/types/model/response.d.ts +7 -6
- package/dist/types/model/response.schema.d.ts +41 -32
- package/dist/types/new-sdk/AbstractNamespace.d.ts +33 -98
- package/dist/types/new-sdk/GearboxSDK.d.ts +10 -25
- package/dist/types/new-sdk/errors/AllSourcesFailedError.d.ts +11 -0
- package/dist/types/new-sdk/errors/MissingSourceError.d.ts +23 -0
- package/dist/types/new-sdk/errors/NoSourceServedError.d.ts +17 -0
- package/dist/types/new-sdk/errors/SourceChainMismatchError.d.ts +35 -0
- package/dist/types/new-sdk/errors/SourceUnavailableError.d.ts +22 -0
- package/dist/types/new-sdk/errors/assertSameChains.d.ts +11 -0
- package/dist/types/new-sdk/errors/everyChainFailed.d.ts +10 -0
- package/dist/types/new-sdk/errors/index.d.ts +8 -0
- package/dist/types/new-sdk/index.d.ts +17 -6
- package/dist/types/new-sdk/opportunities/OpportunitiesNamespace.d.ts +19 -40
- package/dist/types/new-sdk/opportunities/index.d.ts +2 -2
- package/dist/types/new-sdk/opportunities/types.d.ts +80 -26
- package/dist/types/new-sdk/positions/PositionsNamespace.d.ts +17 -38
- package/dist/types/new-sdk/positions/index.d.ts +2 -2
- package/dist/types/new-sdk/positions/types.d.ts +74 -24
- package/dist/types/new-sdk/types.d.ts +24 -77
- package/dist/types/new-sdk/utils/filterResponse.d.ts +16 -0
- package/dist/types/new-sdk/utils/history.d.ts +6 -36
- package/dist/types/new-sdk/utils/index.d.ts +5 -2
- package/dist/types/new-sdk/utils/mergeChains.d.ts +20 -0
- package/dist/types/new-sdk/utils/types.d.ts +11 -0
- package/dist/types/offchain/AbstractOffchainNamespace.d.ts +21 -28
- package/dist/types/offchain/GearboxAPI.d.ts +13 -9
- package/dist/types/offchain/errors/OffchainInvalidJsonError.d.ts +16 -0
- package/dist/types/offchain/errors/OffchainNotConfiguredError.d.ts +16 -0
- package/dist/types/offchain/errors/OffchainNotImplementedError.d.ts +15 -0
- package/dist/types/offchain/errors/OffchainRequestFailedError.d.ts +12 -0
- package/dist/types/offchain/errors/OffchainStatusError.d.ts +16 -0
- package/dist/types/offchain/errors/OffchainTransportError.d.ts +43 -0
- package/dist/types/offchain/errors/OffchainValidationError.d.ts +21 -0
- package/dist/types/offchain/errors/backendMessage.d.ts +9 -0
- package/dist/types/offchain/errors/errorCause.d.ts +22 -0
- package/dist/types/offchain/errors/index.d.ts +11 -0
- package/dist/types/offchain/errors/readResponseBody.d.ts +8 -0
- package/dist/types/offchain/index.d.ts +13 -2
- package/dist/types/offchain/opportunities/OffchainOpportunities.d.ts +9 -8
- package/dist/types/offchain/positions/OffchainPositions.d.ts +9 -15
- package/dist/types/offchain/types.d.ts +7 -88
- package/dist/types/sdk/accounts/index.d.ts +2 -2
- package/dist/types/sdk/accounts/liquidations/MultichainLiquidationsService.d.ts +15 -14
- package/dist/types/sdk/accounts/liquidations/index.d.ts +2 -2
- package/dist/types/sdk/accounts/liquidations/types.d.ts +9 -15
- package/dist/types/sdk/base/MultichainConstruct.d.ts +63 -14
- package/dist/types/sdk/base/index.d.ts +2 -2
- package/dist/types/sdk/chain/chains.d.ts +10 -2
- package/dist/types/sdk/chain/index.d.ts +2 -2
- package/dist/types/sdk/index.d.ts +5 -5
- package/dist/types/sdk/market/MarketSuite.d.ts +1 -1
- package/dist/types/sdk/opportunities/MultichainOpportunitiesService.d.ts +13 -16
- package/dist/types/sdk/positions/MultichainPositionsService.d.ts +2 -12
- package/dist/types/sdk/types/index.d.ts +2 -2
- package/dist/types/sdk/types/multichain.d.ts +8 -43
- package/package.json +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/new-sdk/errors/NoSourceServedError.ts
|
|
3
|
+
/**
|
|
4
|
+
* The failure of a chain no source served. It is reported in that chain's
|
|
5
|
+
* metadata entry rather than thrown: the other chains of the same read may
|
|
6
|
+
* still have been served.
|
|
7
|
+
**/
|
|
8
|
+
var NoSourceServedError = class extends AggregateError {
|
|
9
|
+
/**
|
|
10
|
+
* Chain no source served.
|
|
11
|
+
**/
|
|
12
|
+
chainId;
|
|
13
|
+
constructor(chainId, reasons) {
|
|
14
|
+
super(reasons, `no source could serve chain ${chainId}`);
|
|
15
|
+
this.name = "NoSourceServedError";
|
|
16
|
+
this.chainId = chainId;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
exports.NoSourceServedError = NoSourceServedError;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let viem = require("viem");
|
|
3
|
+
//#region src/new-sdk/errors/SourceChainMismatchError.ts
|
|
4
|
+
/**
|
|
5
|
+
* Thrown at construction when a source covers anything other than exactly the
|
|
6
|
+
* chains a {@link GearboxSDK} was built for, see
|
|
7
|
+
* {@link GearboxSDKOptions.networks}.
|
|
8
|
+
**/
|
|
9
|
+
var SourceChainMismatchError = class extends viem.BaseError {
|
|
10
|
+
name = "SourceChainMismatchError";
|
|
11
|
+
/**
|
|
12
|
+
* Source whose coverage does not match.
|
|
13
|
+
**/
|
|
14
|
+
source;
|
|
15
|
+
/**
|
|
16
|
+
* Chains the SDK covers.
|
|
17
|
+
**/
|
|
18
|
+
expected;
|
|
19
|
+
/**
|
|
20
|
+
* Chains the source covers.
|
|
21
|
+
**/
|
|
22
|
+
covered;
|
|
23
|
+
constructor(source, expected, covered) {
|
|
24
|
+
super(`The ${source} source does not cover the chains this GearboxSDK was built for.`, { metaMessages: [`Expected: ${named(expected)}`, `Covered: ${named(covered)}`] });
|
|
25
|
+
this.source = source;
|
|
26
|
+
this.expected = [...expected];
|
|
27
|
+
this.covered = [...covered];
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
function named(chains) {
|
|
31
|
+
return chains.join(", ") || "no chain";
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.SourceChainMismatchError = SourceChainMismatchError;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let viem = require("viem");
|
|
3
|
+
//#region src/new-sdk/errors/SourceUnavailableError.ts
|
|
4
|
+
/**
|
|
5
|
+
* Thrown when a source branch of a namespace is read in a mode that has no such
|
|
6
|
+
* source, e.g. `sdk.opportunities.onchain` in `offchain` mode.
|
|
7
|
+
**/
|
|
8
|
+
var SourceUnavailableError = class extends viem.BaseError {
|
|
9
|
+
name = "SourceUnavailableError";
|
|
10
|
+
/**
|
|
11
|
+
* Namespace whose branch was read.
|
|
12
|
+
**/
|
|
13
|
+
namespace;
|
|
14
|
+
/**
|
|
15
|
+
* Source this mode does not read.
|
|
16
|
+
**/
|
|
17
|
+
source;
|
|
18
|
+
constructor(namespace, source) {
|
|
19
|
+
super(`The ${namespace} namespace has no ${source} source in this mode.`);
|
|
20
|
+
this.namespace = namespace;
|
|
21
|
+
this.source = source;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
//#endregion
|
|
25
|
+
exports.SourceUnavailableError = SourceUnavailableError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_new_sdk_errors_SourceChainMismatchError = require("./SourceChainMismatchError.js");
|
|
3
|
+
//#region src/new-sdk/errors/assertSameChains.ts
|
|
4
|
+
/**
|
|
5
|
+
* Rejects a source covering anything other than exactly the chains the SDK was
|
|
6
|
+
* built for. A source is not narrowed to them: it has to name them.
|
|
7
|
+
**/
|
|
8
|
+
function assertSameChains(source, expected, covered) {
|
|
9
|
+
if (covered.length !== expected.length || covered.some((chain) => !expected.includes(chain))) throw new require_new_sdk_errors_SourceChainMismatchError.SourceChainMismatchError(source, expected, covered);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
exports.assertSameChains = assertSameChains;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/new-sdk/errors/everyChainFailed.ts
|
|
3
|
+
/**
|
|
4
|
+
* Whether a merged envelope covered chains and lost every one of them, which is
|
|
5
|
+
* what makes a read a failure rather than a partial result.
|
|
6
|
+
**/
|
|
7
|
+
function everyChainFailed(meta) {
|
|
8
|
+
return meta.chains.length > 0 && meta.chains.every((chain) => chain.status === "error");
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
exports.everyChainFailed = everyChainFailed;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_new_sdk_errors_AllSourcesFailedError = require("./AllSourcesFailedError.js");
|
|
3
|
+
const require_new_sdk_errors_SourceChainMismatchError = require("./SourceChainMismatchError.js");
|
|
4
|
+
const require_new_sdk_errors_assertSameChains = require("./assertSameChains.js");
|
|
5
|
+
const require_new_sdk_errors_everyChainFailed = require("./everyChainFailed.js");
|
|
6
|
+
const require_new_sdk_errors_MissingSourceError = require("./MissingSourceError.js");
|
|
7
|
+
const require_new_sdk_errors_NoSourceServedError = require("./NoSourceServedError.js");
|
|
8
|
+
const require_new_sdk_errors_SourceUnavailableError = require("./SourceUnavailableError.js");
|
|
9
|
+
exports.AllSourcesFailedError = require_new_sdk_errors_AllSourcesFailedError.AllSourcesFailedError;
|
|
10
|
+
exports.MissingSourceError = require_new_sdk_errors_MissingSourceError.MissingSourceError;
|
|
11
|
+
exports.NoSourceServedError = require_new_sdk_errors_NoSourceServedError.NoSourceServedError;
|
|
12
|
+
exports.SourceChainMismatchError = require_new_sdk_errors_SourceChainMismatchError.SourceChainMismatchError;
|
|
13
|
+
exports.SourceUnavailableError = require_new_sdk_errors_SourceUnavailableError.SourceUnavailableError;
|
|
14
|
+
exports.assertSameChains = require_new_sdk_errors_assertSameChains.assertSameChains;
|
|
15
|
+
exports.everyChainFailed = require_new_sdk_errors_everyChainFailed.everyChainFailed;
|
|
@@ -1,14 +1,34 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_new_sdk_errors_AllSourcesFailedError = require("./errors/AllSourcesFailedError.js");
|
|
3
|
+
const require_new_sdk_errors_SourceChainMismatchError = require("./errors/SourceChainMismatchError.js");
|
|
4
|
+
const require_new_sdk_errors_assertSameChains = require("./errors/assertSameChains.js");
|
|
5
|
+
const require_new_sdk_errors_everyChainFailed = require("./errors/everyChainFailed.js");
|
|
6
|
+
const require_new_sdk_errors_MissingSourceError = require("./errors/MissingSourceError.js");
|
|
7
|
+
const require_new_sdk_errors_NoSourceServedError = require("./errors/NoSourceServedError.js");
|
|
8
|
+
const require_new_sdk_errors_SourceUnavailableError = require("./errors/SourceUnavailableError.js");
|
|
9
|
+
require("./errors/index.js");
|
|
3
10
|
const require_new_sdk_AbstractNamespace = require("./AbstractNamespace.js");
|
|
11
|
+
const require_new_sdk_utils_filterResponse = require("./utils/filterResponse.js");
|
|
12
|
+
const require_new_sdk_utils_mergeChains = require("./utils/mergeChains.js");
|
|
13
|
+
require("./utils/index.js");
|
|
4
14
|
const require_new_sdk_opportunities_OpportunitiesNamespace = require("./opportunities/OpportunitiesNamespace.js");
|
|
5
15
|
require("./opportunities/index.js");
|
|
6
16
|
const require_new_sdk_positions_PositionsNamespace = require("./positions/PositionsNamespace.js");
|
|
7
17
|
require("./positions/index.js");
|
|
8
18
|
const require_new_sdk_GearboxSDK = require("./GearboxSDK.js");
|
|
9
|
-
require("./
|
|
19
|
+
require("./types.js");
|
|
10
20
|
exports.AbstractNamespace = require_new_sdk_AbstractNamespace.AbstractNamespace;
|
|
11
|
-
exports.AllSourcesFailedError =
|
|
21
|
+
exports.AllSourcesFailedError = require_new_sdk_errors_AllSourcesFailedError.AllSourcesFailedError;
|
|
22
|
+
exports.DEFAULT_MAX_OFFCHAIN_LAG = require_new_sdk_utils_mergeChains.DEFAULT_MAX_OFFCHAIN_LAG;
|
|
12
23
|
exports.GearboxSDK = require_new_sdk_GearboxSDK.GearboxSDK;
|
|
24
|
+
exports.MissingSourceError = require_new_sdk_errors_MissingSourceError.MissingSourceError;
|
|
25
|
+
exports.NoSourceServedError = require_new_sdk_errors_NoSourceServedError.NoSourceServedError;
|
|
13
26
|
exports.OpportunitiesNamespace = require_new_sdk_opportunities_OpportunitiesNamespace.OpportunitiesNamespace;
|
|
14
27
|
exports.PositionsNamespace = require_new_sdk_positions_PositionsNamespace.PositionsNamespace;
|
|
28
|
+
exports.SourceChainMismatchError = require_new_sdk_errors_SourceChainMismatchError.SourceChainMismatchError;
|
|
29
|
+
exports.SourceUnavailableError = require_new_sdk_errors_SourceUnavailableError.SourceUnavailableError;
|
|
30
|
+
exports.assertSameChains = require_new_sdk_errors_assertSameChains.assertSameChains;
|
|
31
|
+
exports.everyChainFailed = require_new_sdk_errors_everyChainFailed.everyChainFailed;
|
|
32
|
+
exports.filterResponse = require_new_sdk_utils_filterResponse.filterResponse;
|
|
33
|
+
exports.mergeChainList = require_new_sdk_utils_mergeChains.mergeChainList;
|
|
34
|
+
exports.mergeChainOne = require_new_sdk_utils_mergeChains.mergeChainOne;
|
|
@@ -2,120 +2,68 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_model_opportunities = require("../../model/opportunities.js");
|
|
3
3
|
require("../../model/index.js");
|
|
4
4
|
const require_new_sdk_AbstractNamespace = require("../AbstractNamespace.js");
|
|
5
|
+
const require_new_sdk_utils_filterResponse = require("../utils/filterResponse.js");
|
|
6
|
+
const require_new_sdk_utils_mergeChains = require("../utils/mergeChains.js");
|
|
7
|
+
require("../utils/index.js");
|
|
5
8
|
//#region src/new-sdk/opportunities/OpportunitiesNamespace.ts
|
|
6
9
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* Empty of on-chain conflicts today — every yield group here is off-chain only,
|
|
10
|
-
* so onchain-first would already yield the backend's value. It is written out
|
|
11
|
-
* anyway so that adding an on-chain estimate of one of them is a deliberate
|
|
12
|
-
* change to this list rather than a silent regression.
|
|
13
|
-
**/
|
|
14
|
-
const OFFCHAIN_OWNED_FIELDS = /* @__PURE__ */ new Set(["supplyApy", "collateralApy"]);
|
|
15
|
-
/**
|
|
16
|
-
* The `opportunities` namespace of the combined SDK.
|
|
17
|
-
*
|
|
18
|
-
* A stateless router over the two sources, see {@link AbstractNamespace} for
|
|
19
|
-
* the routing itself. What is specific to opportunities is the reads below and
|
|
20
|
-
* the merge policy at the bottom of the class.
|
|
21
|
-
*
|
|
22
|
-
* The class implements the methods of every mode; {@link GearboxSDK} exposes it
|
|
23
|
-
* as its mode's slice of {@link OpportunitiesByMode}, so calling a method the
|
|
24
|
-
* mode does not have is a compile error rather than a runtime one.
|
|
10
|
+
* The `opportunities` namespace of a {@link GearboxSDK}, see
|
|
11
|
+
* {@link OpportunitiesByMode} for what each mode offers.
|
|
25
12
|
**/
|
|
26
13
|
var OpportunitiesNamespace = class extends require_new_sdk_AbstractNamespace.AbstractNamespace {
|
|
27
|
-
|
|
28
|
-
|
|
14
|
+
/**
|
|
15
|
+
* {@inheritDoc OpportunitiesBase.merge}
|
|
16
|
+
**/
|
|
17
|
+
merge = {
|
|
18
|
+
list: (onchain, offchain) => require_new_sdk_utils_mergeChains.mergeChainList(onchain, offchain, this.maxOffchainLagSeconds),
|
|
19
|
+
pool: (onchain, offchain) => require_new_sdk_utils_mergeChains.mergeChainOne(onchain, offchain, this.maxOffchainLagSeconds),
|
|
20
|
+
strategy: (onchain, offchain) => require_new_sdk_utils_mergeChains.mergeChainOne(onchain, offchain, this.maxOffchainLagSeconds)
|
|
21
|
+
};
|
|
22
|
+
constructor(onchain, offchain, options) {
|
|
23
|
+
super("Opportunities", onchain?.opportunities, offchain?.opportunities, options);
|
|
29
24
|
}
|
|
30
25
|
/**
|
|
31
26
|
* {@inheritDoc OpportunitiesBase.list}
|
|
32
27
|
**/
|
|
33
28
|
async list(filter) {
|
|
34
|
-
return this.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
}, (api) => api.opportunities.list(filter));
|
|
29
|
+
return this.merged("list opportunities", {
|
|
30
|
+
fromChain: (source) => source.list(filter),
|
|
31
|
+
fromBackend: (source) => source.list(filter),
|
|
32
|
+
merge: this.merge.list
|
|
33
|
+
});
|
|
41
34
|
}
|
|
42
35
|
/**
|
|
43
36
|
* {@inheritDoc OpportunitiesBase.getPool}
|
|
44
37
|
**/
|
|
45
38
|
async getPool(key) {
|
|
46
|
-
return this.
|
|
39
|
+
return this.merged("get pool opportunity", {
|
|
40
|
+
fromChain: (source) => source.getPool(key),
|
|
41
|
+
fromBackend: (source) => source.getPool(key),
|
|
42
|
+
merge: this.merge.pool
|
|
43
|
+
});
|
|
47
44
|
}
|
|
48
45
|
/**
|
|
49
46
|
* {@inheritDoc OpportunitiesBase.getStrategy}
|
|
50
47
|
**/
|
|
51
48
|
async getStrategy(key) {
|
|
52
|
-
return this.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
return this.merged("get strategy opportunity", {
|
|
50
|
+
fromChain: (source) => source.getStrategy(key),
|
|
51
|
+
fromBackend: (source) => source.getStrategy(key),
|
|
52
|
+
merge: this.merge.strategy
|
|
53
|
+
});
|
|
56
54
|
}
|
|
57
55
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* The metric a caller may name is gated by the reader's type, so the kind of
|
|
61
|
-
* the key is not re-checked here.
|
|
56
|
+
* {@inheritDoc OpportunitiesBase.filter}
|
|
62
57
|
**/
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
filter(response, filter) {
|
|
59
|
+
return require_new_sdk_utils_filterResponse.filterResponse(response, filter, require_model_opportunities.matchesOpportunityFilter);
|
|
60
|
+
}
|
|
61
|
+
history(key) {
|
|
62
|
+
return { chart: (metric, range) => this.offchain.getHistory({
|
|
65
63
|
opportunity: key,
|
|
66
64
|
range,
|
|
67
65
|
metric
|
|
68
|
-
})
|
|
69
|
-
metric,
|
|
70
|
-
points: [],
|
|
71
|
-
metadata: {}
|
|
72
|
-
});
|
|
73
|
-
return {
|
|
74
|
-
data: result.points,
|
|
75
|
-
metadata: {
|
|
76
|
-
...result.metadata,
|
|
77
|
-
source: meta
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Merges the two versions of one opportunity.
|
|
83
|
-
*
|
|
84
|
-
* The chain wins every field it fills; the backend fills the rest and owns
|
|
85
|
-
* {@link OFFCHAIN_OWNED_FIELDS}.
|
|
86
|
-
*
|
|
87
|
-
* The rule is onchain-first, field-wise, and never deeper than one level: a
|
|
88
|
-
* group like `totalSupply` or `supplyApy` is taken whole from one source, so
|
|
89
|
-
* a row never mixes an on-chain token amount with a backend dollar value
|
|
90
|
-
* derived from a different block.
|
|
91
|
-
**/
|
|
92
|
-
mergeOne(onchain, offchain) {
|
|
93
|
-
const merged = { ...onchain };
|
|
94
|
-
for (const [field, value] of Object.entries(offchain)) {
|
|
95
|
-
if (value === void 0) continue;
|
|
96
|
-
if (OFFCHAIN_OWNED_FIELDS.has(field) || merged[field] === void 0) merged[field] = value;
|
|
97
|
-
}
|
|
98
|
-
return merged;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Unions the two lists by canonical opportunity id.
|
|
102
|
-
*
|
|
103
|
-
* Rows present in both are merged by {@link OpportunitiesNamespace.mergeOne}.
|
|
104
|
-
* Rows only the backend knows are appended: a chain the SDK does not cover,
|
|
105
|
-
* or a market it has not loaded, is a reason to show more rather than fewer
|
|
106
|
-
* opportunities.
|
|
107
|
-
**/
|
|
108
|
-
mergeList(onchain, offchain) {
|
|
109
|
-
const byId = /* @__PURE__ */ new Map();
|
|
110
|
-
for (const row of onchain) byId.set(require_model_opportunities.opportunityId(row), row);
|
|
111
|
-
const extra = [];
|
|
112
|
-
for (const row of offchain) {
|
|
113
|
-
const id = require_model_opportunities.opportunityId(row);
|
|
114
|
-
const existing = byId.get(id);
|
|
115
|
-
if (existing) byId.set(id, this.mergeOne(existing, row));
|
|
116
|
-
else extra.push(row);
|
|
117
|
-
}
|
|
118
|
-
return [...byId.values(), ...extra];
|
|
66
|
+
}) };
|
|
119
67
|
}
|
|
120
68
|
};
|
|
121
69
|
//#endregion
|
|
@@ -2,117 +2,50 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_model_positions = require("../../model/positions.js");
|
|
3
3
|
require("../../model/index.js");
|
|
4
4
|
const require_new_sdk_AbstractNamespace = require("../AbstractNamespace.js");
|
|
5
|
+
const require_new_sdk_utils_filterResponse = require("../utils/filterResponse.js");
|
|
6
|
+
const require_new_sdk_utils_mergeChains = require("../utils/mergeChains.js");
|
|
7
|
+
require("../utils/index.js");
|
|
5
8
|
//#region src/new-sdk/positions/PositionsNamespace.ts
|
|
6
9
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* The yield and PnL groups are derived from a position's history and from
|
|
10
|
-
* incentive programs, neither of which the chain has. `targetCollateral` is
|
|
11
|
-
* there for a subtler reason: the read model defines it as the collateral the
|
|
12
|
-
* position was opened into, which only the backend can know, while the chain
|
|
13
|
-
* approximates it with the dominant collateral held right now.
|
|
14
|
-
**/
|
|
15
|
-
const OFFCHAIN_OWNED_FIELDS = /* @__PURE__ */ new Set([
|
|
16
|
-
"apy",
|
|
17
|
-
"netApy",
|
|
18
|
-
"pnl",
|
|
19
|
-
"targetCollateral"
|
|
20
|
-
]);
|
|
21
|
-
/**
|
|
22
|
-
* The `positions` namespace of the combined SDK.
|
|
23
|
-
*
|
|
24
|
-
* A stateless router over the two sources, see {@link AbstractNamespace} for the
|
|
25
|
-
* routing itself. What is specific to positions is the reads below and the merge
|
|
26
|
-
* policy at the bottom of the class.
|
|
27
|
-
*
|
|
28
|
-
* The class implements the methods of every mode; {@link GearboxSDK} exposes it
|
|
29
|
-
* as its mode's slice of {@link PositionsByMode}, so calling a method the mode
|
|
30
|
-
* does not have is a compile error rather than a runtime one.
|
|
10
|
+
* The `positions` namespace of a {@link GearboxSDK}, see
|
|
11
|
+
* {@link PositionsByMode} for what each mode offers.
|
|
31
12
|
**/
|
|
32
13
|
var PositionsNamespace = class extends require_new_sdk_AbstractNamespace.AbstractNamespace {
|
|
33
|
-
|
|
34
|
-
|
|
14
|
+
/**
|
|
15
|
+
* {@inheritDoc PositionsBase.merge}
|
|
16
|
+
**/
|
|
17
|
+
merge = { list: (onchain, offchain) => require_new_sdk_utils_mergeChains.mergeChainList(onchain, offchain, this.maxOffchainLagSeconds) };
|
|
18
|
+
constructor(onchain, offchain, options) {
|
|
19
|
+
super("Positions", onchain?.positions, offchain?.positions, options);
|
|
35
20
|
}
|
|
36
21
|
/**
|
|
37
22
|
* {@inheritDoc PositionsBase.list}
|
|
38
23
|
**/
|
|
39
24
|
async list(wallet, filter) {
|
|
40
|
-
return this.
|
|
41
|
-
|
|
25
|
+
return this.merged("list positions", {
|
|
26
|
+
fromChain: (source) => source.list({
|
|
42
27
|
wallet,
|
|
43
28
|
filter
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
history(key) {
|
|
52
|
-
return { chart: (metric, range) => this.#chart(key, metric, range) };
|
|
29
|
+
}),
|
|
30
|
+
fromBackend: (source) => source.list({
|
|
31
|
+
wallet,
|
|
32
|
+
filter
|
|
33
|
+
}),
|
|
34
|
+
merge: this.merge.list
|
|
35
|
+
});
|
|
53
36
|
}
|
|
54
37
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* The metric a caller may name is gated by the reader's type, so the kind of
|
|
58
|
-
* the key is not re-checked here.
|
|
38
|
+
* {@inheritDoc PositionsBase.filter}
|
|
59
39
|
**/
|
|
60
|
-
|
|
61
|
-
|
|
40
|
+
filter(response, filter) {
|
|
41
|
+
return require_new_sdk_utils_filterResponse.filterResponse(response, filter, require_model_positions.matchesPositionFilter);
|
|
42
|
+
}
|
|
43
|
+
history(key) {
|
|
44
|
+
return { chart: (metric, range) => this.offchain.getHistory({
|
|
62
45
|
position: key,
|
|
63
46
|
range,
|
|
64
47
|
metric
|
|
65
|
-
})
|
|
66
|
-
metric,
|
|
67
|
-
points: [],
|
|
68
|
-
metadata: {}
|
|
69
|
-
});
|
|
70
|
-
return {
|
|
71
|
-
data: result.points,
|
|
72
|
-
metadata: {
|
|
73
|
-
...result.metadata,
|
|
74
|
-
source: meta
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Merges the two versions of one position.
|
|
80
|
-
*
|
|
81
|
-
* The chain wins every field it fills; the backend fills the rest and owns
|
|
82
|
-
* {@link OFFCHAIN_OWNED_FIELDS}.
|
|
83
|
-
*
|
|
84
|
-
* The rule is onchain-first, field-wise, and never deeper than one level: a
|
|
85
|
-
* group like `totalValue` or `apy` is taken whole from one source, so a row
|
|
86
|
-
* never mixes an on-chain token amount with a backend dollar value derived
|
|
87
|
-
* from a different block.
|
|
88
|
-
**/
|
|
89
|
-
mergeOne(onchain, offchain) {
|
|
90
|
-
const merged = { ...onchain };
|
|
91
|
-
for (const [field, value] of Object.entries(offchain)) {
|
|
92
|
-
if (value === void 0) continue;
|
|
93
|
-
if (OFFCHAIN_OWNED_FIELDS.has(field) || merged[field] === void 0) merged[field] = value;
|
|
94
|
-
}
|
|
95
|
-
return merged;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Unions the two lists by canonical position id.
|
|
99
|
-
*
|
|
100
|
-
* Rows present in both are merged by {@link PositionsNamespace.mergeOne}.
|
|
101
|
-
* Rows only the backend knows are appended: a chain the SDK does not cover,
|
|
102
|
-
* or a market it has not loaded, must not hide a position the wallet actually
|
|
103
|
-
* holds.
|
|
104
|
-
**/
|
|
105
|
-
mergeList(onchain, offchain) {
|
|
106
|
-
const byId = /* @__PURE__ */ new Map();
|
|
107
|
-
for (const row of onchain) byId.set(require_model_positions.positionId(row), row);
|
|
108
|
-
const extra = [];
|
|
109
|
-
for (const row of offchain) {
|
|
110
|
-
const id = require_model_positions.positionId(row);
|
|
111
|
-
const existing = byId.get(id);
|
|
112
|
-
if (existing) byId.set(id, this.mergeOne(existing, row));
|
|
113
|
-
else extra.push(row);
|
|
114
|
-
}
|
|
115
|
-
return [...byId.values(), ...extra];
|
|
48
|
+
}) };
|
|
116
49
|
}
|
|
117
50
|
};
|
|
118
51
|
//#endregion
|
|
@@ -1,22 +1 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
//#region src/new-sdk/types.ts
|
|
3
|
-
/**
|
|
4
|
-
* Thrown when a read had sources to ask and none of them answered.
|
|
5
|
-
*
|
|
6
|
-
* A read that partially succeeded never throws: it returns what it has and
|
|
7
|
-
* reports the rest in {@link SourceMeta}.
|
|
8
|
-
**/
|
|
9
|
-
var AllSourcesFailedError = class extends Error {
|
|
10
|
-
/**
|
|
11
|
-
* Outcome of every source the failed read asked.
|
|
12
|
-
**/
|
|
13
|
-
meta;
|
|
14
|
-
constructor(action, meta) {
|
|
15
|
-
const reasons = [...meta.chains.filter((c) => c.status === "error").map((c) => `${c.network}: ${c.error}`), ...meta.offchain?.status === "error" ? [`offchain: ${meta.offchain.error}`] : []];
|
|
16
|
-
super(`cannot ${action}, every source failed (${reasons.join("; ")})`);
|
|
17
|
-
this.name = "AllSourcesFailedError";
|
|
18
|
-
this.meta = meta;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
//#endregion
|
|
22
|
-
exports.AllSourcesFailedError = AllSourcesFailedError;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/new-sdk/utils/filterResponse.ts
|
|
3
|
+
/**
|
|
4
|
+
* Narrows an already-read list, rows and metadata alike. `undefined` passes
|
|
5
|
+
* through, so a read still in flight stays that way.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - Row type.
|
|
8
|
+
* @typeParam F - Filter type.
|
|
9
|
+
* @param matches - Whether one row satisfies the filter, e.g.
|
|
10
|
+
* `matchesOpportunityFilter`.
|
|
11
|
+
**/
|
|
12
|
+
function filterResponse(response, filter, matches) {
|
|
13
|
+
if (!response) return;
|
|
14
|
+
const chainIds = filter?.chainIds;
|
|
15
|
+
return {
|
|
16
|
+
data: response.data.filter((row) => matches(row, filter)),
|
|
17
|
+
meta: { chains: chainIds ? response.meta.chains.filter((chain) => chainIds.includes(chain.chainId)) : response.meta.chains }
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
exports.filterResponse = filterResponse;
|
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_new_sdk_utils_filterResponse = require("./filterResponse.js");
|
|
2
3
|
require("./history.js");
|
|
4
|
+
const require_new_sdk_utils_mergeChains = require("./mergeChains.js");
|
|
5
|
+
exports.DEFAULT_MAX_OFFCHAIN_LAG = require_new_sdk_utils_mergeChains.DEFAULT_MAX_OFFCHAIN_LAG;
|
|
6
|
+
exports.filterResponse = require_new_sdk_utils_filterResponse.filterResponse;
|
|
7
|
+
exports.mergeChainList = require_new_sdk_utils_mergeChains.mergeChainList;
|
|
8
|
+
exports.mergeChainOne = require_new_sdk_utils_mergeChains.mergeChainOne;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_new_sdk_errors_NoSourceServedError = require("../errors/NoSourceServedError.js");
|
|
3
|
+
require("../errors/index.js");
|
|
4
|
+
//#region src/new-sdk/utils/mergeChains.ts
|
|
5
|
+
/**
|
|
6
|
+
* How many seconds the backend may lag the chain and still be used.
|
|
7
|
+
**/
|
|
8
|
+
const DEFAULT_MAX_OFFCHAIN_LAG = 120;
|
|
9
|
+
/**
|
|
10
|
+
* Merges two lists chain by chain: a chain is served by the backend when it is
|
|
11
|
+
* within `maxLagSeconds` of the chain, by the chain otherwise, and reported as
|
|
12
|
+
* an error when neither source succeeded.
|
|
13
|
+
**/
|
|
14
|
+
function mergeChainList(onchain, offchain, maxLagSeconds = 120) {
|
|
15
|
+
if (!onchain && !offchain) return;
|
|
16
|
+
const fromOnchain = /* @__PURE__ */ new Map();
|
|
17
|
+
for (const chain of onchain?.meta.chains ?? []) fromOnchain.set(chain.chainId, chain);
|
|
18
|
+
const fromOffchain = /* @__PURE__ */ new Map();
|
|
19
|
+
for (const chain of offchain?.meta.chains ?? []) fromOffchain.set(chain.chainId, chain);
|
|
20
|
+
const data = [];
|
|
21
|
+
const chains = [];
|
|
22
|
+
for (const chainId of /* @__PURE__ */ new Set([...fromOnchain.keys(), ...fromOffchain.keys()])) {
|
|
23
|
+
const onchainMeta = fromOnchain.get(chainId);
|
|
24
|
+
const offchainMeta = fromOffchain.get(chainId);
|
|
25
|
+
const winner = pickSource(onchainMeta, offchainMeta, maxLagSeconds);
|
|
26
|
+
if (winner === "onchain" && onchain && onchainMeta) {
|
|
27
|
+
data.push(...onchain.data.filter((row) => row.chainId === chainId));
|
|
28
|
+
chains.push(onchainMeta);
|
|
29
|
+
} else if (winner === "offchain" && offchain && offchainMeta) {
|
|
30
|
+
data.push(...offchain.data.filter((row) => row.chainId === chainId));
|
|
31
|
+
chains.push(offchainMeta);
|
|
32
|
+
} else chains.push(failedChain(chainId, onchainMeta, offchainMeta));
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
data,
|
|
36
|
+
meta: { chains }
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Merges two versions of one entity under the same freshness rule as
|
|
41
|
+
* {@link mergeChainList}, returning the winning response whole.
|
|
42
|
+
**/
|
|
43
|
+
function mergeChainOne(onchain, offchain, maxLagSeconds = 120) {
|
|
44
|
+
switch (pickSource(onchain?.meta.chains[0], offchain?.meta.chains[0], maxLagSeconds)) {
|
|
45
|
+
case "onchain": return onchain;
|
|
46
|
+
case "offchain": return offchain;
|
|
47
|
+
default: return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Which side serves one chain, or `undefined` when neither can.
|
|
52
|
+
**/
|
|
53
|
+
function pickSource(onchain, offchain, maxLagSeconds) {
|
|
54
|
+
const onchainOk = onchain?.status === "success" ? onchain : void 0;
|
|
55
|
+
const offchainOk = offchain?.status === "success" ? offchain : void 0;
|
|
56
|
+
if (onchainOk && offchainOk) return isFresh(onchainOk, offchainOk, maxLagSeconds) ? "offchain" : "onchain";
|
|
57
|
+
if (onchainOk) return "onchain";
|
|
58
|
+
if (offchainOk) return "offchain";
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Whether the backend is close enough behind the chain to be preferred.
|
|
62
|
+
**/
|
|
63
|
+
function isFresh(onchain, offchain, maxLagSeconds) {
|
|
64
|
+
return offchain.timestamp >= onchain.timestamp - maxLagSeconds;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Metadata for a chain neither source served.
|
|
68
|
+
**/
|
|
69
|
+
function failedChain(chainId, onchain, offchain) {
|
|
70
|
+
if (!onchain || !offchain) return onchain ?? offchain;
|
|
71
|
+
const reasons = [onchain, offchain].map((chain) => chain.status === "error" ? chain.error : void 0).filter((reason) => !!reason);
|
|
72
|
+
return {
|
|
73
|
+
chainId,
|
|
74
|
+
status: "error",
|
|
75
|
+
error: reasons.length === 1 ? reasons[0] : new require_new_sdk_errors_NoSourceServedError.NoSourceServedError(chainId, reasons)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
exports.DEFAULT_MAX_OFFCHAIN_LAG = DEFAULT_MAX_OFFCHAIN_LAG;
|
|
80
|
+
exports.mergeChainList = mergeChainList;
|
|
81
|
+
exports.mergeChainOne = mergeChainOne;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|