@gearbox-protocol/sdk 13.3.3 → 13.3.4
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/sdk/GearboxSDK.js +135 -20
- package/dist/cjs/sdk/accounts/AbstractCreditAccountsService.js +40 -141
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +5 -5
- package/dist/cjs/sdk/base/BaseContract.js +26 -6
- package/dist/cjs/sdk/base/ChainContractsRegister.js +39 -2
- package/dist/cjs/sdk/base/Construct.js +15 -3
- package/dist/cjs/sdk/base/TokensMeta.js +23 -0
- package/dist/cjs/sdk/constants/address-provider.js +0 -22
- package/dist/cjs/sdk/core/AbstractAddressProviderContract.js +15 -0
- package/dist/cjs/sdk/market/MarketRegister.js +74 -3
- package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +31 -50
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedRef.js +16 -0
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedsRegister.js +55 -12
- package/dist/cjs/sdk/options.js +30 -24
- package/dist/cjs/sdk/plugins/BasePlugin.js +24 -0
- package/dist/cjs/sdk/pools/AbstractPoolService.js +6 -0
- package/dist/cjs/sdk/router/AbstractRouterContract.js +4 -1
- package/dist/cjs/sdk/router/RouterV310Contract.js +20 -15
- package/dist/cjs/sdk/utils/AddressMap.js +53 -17
- package/dist/cjs/sdk/utils/AddressSet.js +9 -0
- package/dist/cjs/sdk/utils/viem/sendRawTx.js +16 -0
- package/dist/esm/sdk/GearboxSDK.js +135 -20
- package/dist/esm/sdk/accounts/AbstractCreditAccountsService.js +40 -141
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +5 -5
- package/dist/esm/sdk/base/BaseContract.js +26 -6
- package/dist/esm/sdk/base/ChainContractsRegister.js +39 -2
- package/dist/esm/sdk/base/Construct.js +15 -3
- package/dist/esm/sdk/base/TokensMeta.js +23 -0
- package/dist/esm/sdk/constants/address-provider.js +0 -21
- package/dist/esm/sdk/core/AbstractAddressProviderContract.js +15 -0
- package/dist/esm/sdk/market/MarketRegister.js +74 -3
- package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +31 -50
- package/dist/esm/sdk/market/pricefeeds/PriceFeedRef.js +16 -0
- package/dist/esm/sdk/market/pricefeeds/PriceFeedsRegister.js +55 -12
- package/dist/esm/sdk/options.js +30 -24
- package/dist/esm/sdk/plugins/BasePlugin.js +24 -0
- package/dist/esm/sdk/pools/AbstractPoolService.js +6 -0
- package/dist/esm/sdk/router/AbstractRouterContract.js +4 -1
- package/dist/esm/sdk/router/RouterV310Contract.js +20 -15
- package/dist/esm/sdk/utils/AddressMap.js +53 -17
- package/dist/esm/sdk/utils/AddressSet.js +9 -0
- package/dist/esm/sdk/utils/viem/sendRawTx.js +19 -1
- package/dist/types/sdk/GearboxSDK.d.ts +236 -34
- package/dist/types/sdk/accounts/AbstractCreditAccountsService.d.ts +55 -141
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +14 -5
- package/dist/types/sdk/accounts/types.d.ts +219 -17
- package/dist/types/sdk/base/BaseContract.d.ts +67 -6
- package/dist/types/sdk/base/ChainContractsRegister.d.ts +51 -2
- package/dist/types/sdk/base/Construct.d.ts +31 -0
- package/dist/types/sdk/base/PlaceholderContract.d.ts +3 -0
- package/dist/types/sdk/base/SDKConstruct.d.ts +10 -0
- package/dist/types/sdk/base/TokensMeta.d.ts +59 -2
- package/dist/types/sdk/base/types.d.ts +185 -25
- package/dist/types/sdk/chain/chains.d.ts +78 -18
- package/dist/types/sdk/chain/detectNetwork.d.ts +7 -0
- package/dist/types/sdk/constants/address-provider.d.ts +4 -3
- package/dist/types/sdk/core/AbstractAddressProviderContract.d.ts +23 -0
- package/dist/types/sdk/core/types.d.ts +46 -0
- package/dist/types/sdk/market/MarketRegister.d.ts +81 -0
- package/dist/types/sdk/market/adapters/PlaceholderAdapterContracts.d.ts +3 -0
- package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +40 -50
- package/dist/types/sdk/market/oracle/types.d.ts +76 -57
- package/dist/types/sdk/market/pricefeeds/AbstractLPPriceFeed.d.ts +3 -0
- package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +3 -0
- package/dist/types/sdk/market/pricefeeds/PriceFeedRef.d.ts +22 -2
- package/dist/types/sdk/market/pricefeeds/PriceFeedsRegister.d.ts +77 -13
- package/dist/types/sdk/market/pricefeeds/types.d.ts +70 -10
- package/dist/types/sdk/options.d.ts +13 -4
- package/dist/types/sdk/plugins/BasePlugin.d.ts +39 -0
- package/dist/types/sdk/plugins/types.d.ts +73 -43
- package/dist/types/sdk/pools/AbstractPoolService.d.ts +12 -0
- package/dist/types/sdk/pools/types.d.ts +75 -6
- package/dist/types/sdk/router/AbstractRouterContract.d.ts +21 -2
- package/dist/types/sdk/router/RouterV310Contract.d.ts +27 -15
- package/dist/types/sdk/router/types.d.ts +51 -69
- package/dist/types/sdk/types/state.d.ts +32 -3
- package/dist/types/sdk/utils/AddressMap.d.ts +61 -17
- package/dist/types/sdk/utils/AddressSet.d.ts +15 -0
- package/dist/types/sdk/utils/viem/sendRawTx.d.ts +5 -1
- package/package.json +4 -2
|
@@ -9,8 +9,8 @@ class ChainContractsRegister {
|
|
|
9
9
|
labels = new AddressMap([], "labels");
|
|
10
10
|
client;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
|
|
12
|
+
* Shared token metadata (symbol, decimals) for all tokens known on this chain.
|
|
13
|
+
**/
|
|
14
14
|
tokensMeta;
|
|
15
15
|
logger;
|
|
16
16
|
constructor(client, logger) {
|
|
@@ -18,6 +18,9 @@ class ChainContractsRegister {
|
|
|
18
18
|
this.tokensMeta = new TokensMeta(client);
|
|
19
19
|
this.logger = logger;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Clears all registered contracts, address labels, and token metadata.
|
|
23
|
+
**/
|
|
21
24
|
resetContracts() {
|
|
22
25
|
this.logger?.debug(
|
|
23
26
|
`resetting contacts register with ${this.contracts.size} contracts`
|
|
@@ -26,9 +29,19 @@ class ChainContractsRegister {
|
|
|
26
29
|
this.contracts.clear();
|
|
27
30
|
this.tokensMeta.reset();
|
|
28
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Looks up a contract by address.
|
|
34
|
+
* @param address - On-chain address.
|
|
35
|
+
* @returns The contract wrapper, or `undefined` if not registered.
|
|
36
|
+
*/
|
|
29
37
|
getContract(address) {
|
|
30
38
|
return this.contracts.get(address);
|
|
31
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Looks up a contract by address, throwing if not found.
|
|
42
|
+
* @param address - On-chain address.
|
|
43
|
+
* @throws If no contract is registered at this address.
|
|
44
|
+
*/
|
|
32
45
|
mustGetContract(address) {
|
|
33
46
|
const contract = this.contracts.mustGet(address);
|
|
34
47
|
if (!contract) {
|
|
@@ -36,9 +49,21 @@ class ChainContractsRegister {
|
|
|
36
49
|
}
|
|
37
50
|
return contract;
|
|
38
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Registers (or replaces) a contract at the given address.
|
|
54
|
+
* @param address - On-chain address.
|
|
55
|
+
* @param contract - Contract wrapper instance.
|
|
56
|
+
*/
|
|
39
57
|
setContract(address, contract) {
|
|
40
58
|
this.contracts.upsert(address, contract);
|
|
41
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Assigns a human-readable label to an address for use in logging and
|
|
62
|
+
* parsed call output.
|
|
63
|
+
* @param address - On-chain address.
|
|
64
|
+
* @param label - Static label string, or a function that receives the
|
|
65
|
+
* current label and returns a new one.
|
|
66
|
+
*/
|
|
42
67
|
setAddressLabel(address, label) {
|
|
43
68
|
if (address === NOT_DEPLOYED) {
|
|
44
69
|
return;
|
|
@@ -49,13 +74,25 @@ class ChainContractsRegister {
|
|
|
49
74
|
this.labels.upsert(address, label(this.labels.get(address)));
|
|
50
75
|
}
|
|
51
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns a display string for an address, incorporating its label if one exists.
|
|
79
|
+
* @param address - On-chain address.
|
|
80
|
+
* @param omitAddress - When `true`, returns only the label (no address prefix).
|
|
81
|
+
* Falls back to the raw address when no label is set.
|
|
82
|
+
*/
|
|
52
83
|
labelAddress(address, omitAddress) {
|
|
53
84
|
const label = this.labels.get(address);
|
|
54
85
|
return label ? omitAddress ? label : `${address} [${label}]` : address;
|
|
55
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* The viem {@link Chain} object for this register's connected client.
|
|
89
|
+
**/
|
|
56
90
|
get chain() {
|
|
57
91
|
return this.client.chain;
|
|
58
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Numeric chain ID (e.g. `1` for Ethereum mainnet).
|
|
95
|
+
**/
|
|
59
96
|
get chainId() {
|
|
60
97
|
return this.client.chain.id;
|
|
61
98
|
}
|
|
@@ -4,9 +4,6 @@ class Construct {
|
|
|
4
4
|
logger;
|
|
5
5
|
client;
|
|
6
6
|
#register;
|
|
7
|
-
/**
|
|
8
|
-
* Indicates that contract state needs to be updated
|
|
9
|
-
*/
|
|
10
7
|
#dirty = false;
|
|
11
8
|
constructor(options) {
|
|
12
9
|
if (options instanceof ChainContractsRegister) {
|
|
@@ -38,12 +35,22 @@ class Construct {
|
|
|
38
35
|
safeGetRegister() {
|
|
39
36
|
return this.#register;
|
|
40
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* The viem {@link Chain} object associated with the connected client.
|
|
40
|
+
**/
|
|
41
41
|
get chain() {
|
|
42
42
|
return this.client.chain;
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Numeric chain ID (e.g. `1` for Ethereum mainnet).
|
|
46
|
+
**/
|
|
44
47
|
get chainId() {
|
|
45
48
|
return this.client.chain.id;
|
|
46
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Gearbox network type for this chain (e.g. `"Mainnet"`, `"Arbitrum"`).
|
|
52
|
+
* @throws If the chain was not created by the Gearbox SDK.
|
|
53
|
+
*/
|
|
47
54
|
get networkType() {
|
|
48
55
|
if ("network" in this.chain) {
|
|
49
56
|
return this.chain.network;
|
|
@@ -51,6 +58,7 @@ class Construct {
|
|
|
51
58
|
throw new Error(`chain ${this.chain.id} is not a Gearbox SDK chain`);
|
|
52
59
|
}
|
|
53
60
|
/**
|
|
61
|
+
* @internal
|
|
54
62
|
* Indicates that contract state diverged from onchain state and needs to be updated
|
|
55
63
|
*/
|
|
56
64
|
get dirty() {
|
|
@@ -59,6 +67,9 @@ class Construct {
|
|
|
59
67
|
set dirty(value) {
|
|
60
68
|
this.#dirty = value;
|
|
61
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Information about tokens known on this chain
|
|
72
|
+
*/
|
|
62
73
|
get tokensMeta() {
|
|
63
74
|
return this.register.tokensMeta;
|
|
64
75
|
}
|
|
@@ -66,6 +77,7 @@ class Construct {
|
|
|
66
77
|
return this.#register?.labelAddress(address, omitAddress) ?? address;
|
|
67
78
|
}
|
|
68
79
|
/**
|
|
80
|
+
* @internal
|
|
69
81
|
* Returns list of addresses that should be watched for events to sync state
|
|
70
82
|
*/
|
|
71
83
|
get watchAddresses() {
|
|
@@ -12,13 +12,26 @@ class TokensMeta extends AddressMap {
|
|
|
12
12
|
super(void 0, "tokensMeta");
|
|
13
13
|
this.#client = client;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Clears all token metadata
|
|
17
|
+
**/
|
|
15
18
|
reset() {
|
|
16
19
|
this.clear();
|
|
17
20
|
this.#phantomTokensLoaded = void 0;
|
|
18
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the symbol string for a token.
|
|
24
|
+
* @param token - Token address.
|
|
25
|
+
* @throws If the token is not in the registry.
|
|
26
|
+
*/
|
|
19
27
|
symbol(token) {
|
|
20
28
|
return this.mustGet(token).symbol;
|
|
21
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns the decimal count for a token.
|
|
32
|
+
* @param token - Token address.
|
|
33
|
+
* @throws If the token is not in the registry.
|
|
34
|
+
*/
|
|
22
35
|
decimals(token) {
|
|
23
36
|
return this.mustGet(token).decimals;
|
|
24
37
|
}
|
|
@@ -53,9 +66,19 @@ class TokensMeta extends AddressMap {
|
|
|
53
66
|
const asStr = formatBN(amount, this.decimals(token), precision);
|
|
54
67
|
return symbol ? `${asStr} ${this.symbol(token)}` : asStr;
|
|
55
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Finds a token by its symbol (e.g. `"USDC"`).
|
|
71
|
+
* @param symbol - Case-sensitive ticker symbol.
|
|
72
|
+
* @returns The matching metadata, or `undefined` if no token has this symbol.
|
|
73
|
+
*/
|
|
56
74
|
findBySymbol(symbol) {
|
|
57
75
|
return this.values().find((v) => v.symbol === symbol);
|
|
58
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Finds a token by its symbol, throwing if not found.
|
|
79
|
+
* @param symbol - Case-sensitive ticker symbol.
|
|
80
|
+
* @throws If no token matches the symbol.
|
|
81
|
+
*/
|
|
59
82
|
mustFindBySymbol(symbol) {
|
|
60
83
|
const meta = this.findBySymbol(symbol);
|
|
61
84
|
if (!meta) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { NOT_DEPLOYED } from "./addresses.js";
|
|
2
1
|
const NO_VERSION = 0;
|
|
3
2
|
const AP_ACCOUNT_FACTORY = "ACCOUNT_FACTORY";
|
|
4
3
|
const AP_ACL = "ACL";
|
|
@@ -32,28 +31,8 @@ const AP_WETH_GATEWAY = "WETH_GATEWAY";
|
|
|
32
31
|
const AP_WETH_TOKEN = "WETH_TOKEN";
|
|
33
32
|
const AP_ZAPPER_REGISTER = "ZAPPER_REGISTER";
|
|
34
33
|
const AP_ZERO_PRICE_FEED = "ZERO_PRICE_FEED";
|
|
35
|
-
const ADDRESS_PROVIDER = {
|
|
36
|
-
Mainnet: "0x9ea7b04Da02a5373317D745c1571c84aaD03321D",
|
|
37
|
-
Arbitrum: "0x7d04eCdb892Ae074f03B5D0aBA03796F90F3F2af",
|
|
38
|
-
Optimism: "0x3761ca4BFAcFCFFc1B8034e69F19116dD6756726",
|
|
39
|
-
Base: NOT_DEPLOYED,
|
|
40
|
-
Sonic: "0x4b27b296273B72d7c7bfee1ACE93DC081467C41B",
|
|
41
|
-
// New networks
|
|
42
|
-
MegaETH: NOT_DEPLOYED,
|
|
43
|
-
Monad: NOT_DEPLOYED,
|
|
44
|
-
Berachain: NOT_DEPLOYED,
|
|
45
|
-
Avalanche: NOT_DEPLOYED,
|
|
46
|
-
BNB: NOT_DEPLOYED,
|
|
47
|
-
WorldChain: NOT_DEPLOYED,
|
|
48
|
-
Etherlink: NOT_DEPLOYED,
|
|
49
|
-
Hemi: NOT_DEPLOYED,
|
|
50
|
-
Lisk: NOT_DEPLOYED,
|
|
51
|
-
Plasma: NOT_DEPLOYED,
|
|
52
|
-
Somnia: NOT_DEPLOYED
|
|
53
|
-
};
|
|
54
34
|
const ADDRESS_PROVIDER_V310 = "0xF7f0a609BfAb9a0A98786951ef10e5FE26cC1E38";
|
|
55
35
|
export {
|
|
56
|
-
ADDRESS_PROVIDER,
|
|
57
36
|
ADDRESS_PROVIDER_V310,
|
|
58
37
|
AP_ACCOUNT_FACTORY,
|
|
59
38
|
AP_ACL,
|
|
@@ -33,6 +33,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
33
33
|
this.#addresses[k][version] = overriden;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* {@inheritDoc IAddressProviderContract.getAddress}
|
|
38
|
+
**/
|
|
36
39
|
getAddress(contract, version = NO_VERSION) {
|
|
37
40
|
if (!this.#addresses[contract]) {
|
|
38
41
|
throw new Error(`Address ${contract}, version: ${version} not found`);
|
|
@@ -43,6 +46,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
43
46
|
}
|
|
44
47
|
return result;
|
|
45
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* {@inheritDoc IAddressProviderContract.getLatest}
|
|
51
|
+
**/
|
|
46
52
|
getLatest(contract, range) {
|
|
47
53
|
const allVersions = this.#addresses[contract];
|
|
48
54
|
if (!allVersions) {
|
|
@@ -62,6 +68,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
62
68
|
}
|
|
63
69
|
return [address, version];
|
|
64
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* {@inheritDoc IAddressProviderContract.mustGetLatest}
|
|
73
|
+
**/
|
|
65
74
|
mustGetLatest(contract, range) {
|
|
66
75
|
const result = this.getLatest(contract, range);
|
|
67
76
|
if (!result) {
|
|
@@ -69,6 +78,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
69
78
|
}
|
|
70
79
|
return result;
|
|
71
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* {@inheritDoc IAddressProviderContract.state}
|
|
83
|
+
**/
|
|
72
84
|
get state() {
|
|
73
85
|
return {
|
|
74
86
|
baseParams: {
|
|
@@ -80,6 +92,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
80
92
|
addresses: this.#addresses
|
|
81
93
|
};
|
|
82
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* {@inheritDoc IAddressProviderContract.stateHuman}
|
|
97
|
+
**/
|
|
83
98
|
stateHuman(raw = true) {
|
|
84
99
|
return {
|
|
85
100
|
...super.stateHuman(raw),
|
|
@@ -10,9 +10,6 @@ import { simulateWithPriceUpdates } from "../utils/viem/index.js";
|
|
|
10
10
|
import { MarketConfiguratorContract } from "./MarketConfiguratorContract.js";
|
|
11
11
|
import { MarketSuite } from "./MarketSuite.js";
|
|
12
12
|
class MarketRegister extends SDKConstruct {
|
|
13
|
-
/**
|
|
14
|
-
* Mapping pool.address -> MarketSuite
|
|
15
|
-
*/
|
|
16
13
|
#markets = new AddressMap(void 0, "markets");
|
|
17
14
|
#marketFilter;
|
|
18
15
|
#marketConfigurators = new AddressMap(
|
|
@@ -20,12 +17,21 @@ class MarketRegister extends SDKConstruct {
|
|
|
20
17
|
"marketConfigurators"
|
|
21
18
|
);
|
|
22
19
|
#ignoreMarkets;
|
|
20
|
+
/**
|
|
21
|
+
* @param sdk - Top-level SDK instance.
|
|
22
|
+
* @param ignoreMarkets - Pool addresses of markets to exclude from loading.
|
|
23
|
+
**/
|
|
23
24
|
constructor(sdk, ignoreMarkets = []) {
|
|
24
25
|
super(sdk);
|
|
25
26
|
this.#ignoreMarkets = new Set(
|
|
26
27
|
ignoreMarkets.map((m) => m.toLowerCase())
|
|
27
28
|
);
|
|
28
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Restores market state from a previously serialized snapshot,
|
|
32
|
+
* bypassing on-chain reads.
|
|
33
|
+
* @param state - Array of market data snapshots.
|
|
34
|
+
**/
|
|
29
35
|
hydrate(state) {
|
|
30
36
|
this.#markets.clear();
|
|
31
37
|
const configurators = new Set(state.map((m) => m.configurator));
|
|
@@ -44,6 +50,13 @@ class MarketRegister extends SDKConstruct {
|
|
|
44
50
|
);
|
|
45
51
|
}
|
|
46
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Fetches all markets from the on-chain for the given market configurators.
|
|
55
|
+
*
|
|
56
|
+
* @param marketConfigurators - Addresses of market configurator contracts to query.
|
|
57
|
+
* @param ignoreUpdateablePrices - When `true`, skips generating off-chain
|
|
58
|
+
* price updates before loading
|
|
59
|
+
**/
|
|
47
60
|
async loadMarkets(marketConfigurators, ignoreUpdateablePrices) {
|
|
48
61
|
if (!marketConfigurators.length) {
|
|
49
62
|
this.logger?.warn(
|
|
@@ -66,6 +79,10 @@ class MarketRegister extends SDKConstruct {
|
|
|
66
79
|
underlying: ADDRESS_0X0
|
|
67
80
|
};
|
|
68
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* The active filter used to scope market compressor queries.
|
|
84
|
+
* @throws If the register has not been hydrated or attached yet.
|
|
85
|
+
**/
|
|
69
86
|
get marketFilter() {
|
|
70
87
|
if (!this.#marketFilter) {
|
|
71
88
|
throw new Error(
|
|
@@ -74,6 +91,15 @@ class MarketRegister extends SDKConstruct {
|
|
|
74
91
|
}
|
|
75
92
|
return this.#marketFilter;
|
|
76
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Re-synchronizes market state with the chain. If during sdk synchronization
|
|
96
|
+
* we detected that some markets or market configurators were changed,
|
|
97
|
+
* we reload everything.
|
|
98
|
+
*
|
|
99
|
+
* Otherwise only prices are refreshed.
|
|
100
|
+
*
|
|
101
|
+
* @param ignoreUpdateablePrices - When `true`, skips off-chain price updates.
|
|
102
|
+
**/
|
|
77
103
|
async syncState(ignoreUpdateablePrices) {
|
|
78
104
|
const dirty = this.markets.some((m) => m.dirty) || this.marketConfigurators.some((c) => c.dirty);
|
|
79
105
|
if (dirty) {
|
|
@@ -184,26 +210,50 @@ class MarketRegister extends SDKConstruct {
|
|
|
184
210
|
...this.marketFilter.configurators
|
|
185
211
|
]);
|
|
186
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Serializable snapshot of all loaded markets, suitable for hydration.
|
|
215
|
+
**/
|
|
187
216
|
get state() {
|
|
188
217
|
return this.markets.map((market) => market.state);
|
|
189
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Returns a human-readable snapshot of all markets.
|
|
221
|
+
* @param raw - When `true`, includes raw/unformatted values.
|
|
222
|
+
**/
|
|
190
223
|
stateHuman(raw = true) {
|
|
191
224
|
return {
|
|
192
225
|
markets: this.markets.map((market) => market.stateHuman(raw))
|
|
193
226
|
};
|
|
194
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* All pool suites across loaded markets.
|
|
230
|
+
**/
|
|
195
231
|
get pools() {
|
|
196
232
|
return this.markets.map((market) => market.pool);
|
|
197
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* All price oracles across loaded markets.
|
|
236
|
+
**/
|
|
198
237
|
get priceOracles() {
|
|
199
238
|
return this.markets.map((market) => market.priceOracle);
|
|
200
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* All credit manager suites across loaded markets.
|
|
242
|
+
**/
|
|
201
243
|
get creditManagers() {
|
|
202
244
|
return this.markets.flatMap((market) => market.creditManagers);
|
|
203
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* All known market configurator contracts.
|
|
248
|
+
**/
|
|
204
249
|
get marketConfigurators() {
|
|
205
250
|
return this.#marketConfigurators.values();
|
|
206
251
|
}
|
|
252
|
+
/**
|
|
253
|
+
* Finds a credit manager suite by its on-chain address.
|
|
254
|
+
* @param creditManager - Credit manager contract address.
|
|
255
|
+
* @throws If no loaded market contains the given credit manager.
|
|
256
|
+
**/
|
|
207
257
|
findCreditManager(creditManager) {
|
|
208
258
|
const addr = creditManager.toLowerCase();
|
|
209
259
|
for (const market of this.markets) {
|
|
@@ -215,6 +265,11 @@ class MarketRegister extends SDKConstruct {
|
|
|
215
265
|
}
|
|
216
266
|
throw new Error(`cannot find credit manager ${creditManager}`);
|
|
217
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Finds the market that contains the given credit manager.
|
|
270
|
+
* @param creditManager - Credit manager contract address.
|
|
271
|
+
* @throws If no loaded market contains the given credit manager.
|
|
272
|
+
**/
|
|
218
273
|
findByCreditManager(creditManager) {
|
|
219
274
|
const addr = creditManager.toLowerCase();
|
|
220
275
|
const market = this.markets.find(
|
|
@@ -227,6 +282,11 @@ class MarketRegister extends SDKConstruct {
|
|
|
227
282
|
}
|
|
228
283
|
return market;
|
|
229
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* Finds the market that uses the given price oracle.
|
|
287
|
+
* @param address - Price oracle contract address.
|
|
288
|
+
* @throws If no loaded market uses the given oracle.
|
|
289
|
+
**/
|
|
230
290
|
findByPriceOracle(address) {
|
|
231
291
|
const addr = address.toLowerCase();
|
|
232
292
|
for (const market of this.markets) {
|
|
@@ -236,6 +296,11 @@ class MarketRegister extends SDKConstruct {
|
|
|
236
296
|
}
|
|
237
297
|
throw new Error(`cannot find market for price oracle ${address}`);
|
|
238
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Finds the market associated with the given pool.
|
|
301
|
+
* @param address - Pool contract address.
|
|
302
|
+
* @throws If no loaded market uses the given pool.
|
|
303
|
+
**/
|
|
239
304
|
findByPool(address) {
|
|
240
305
|
const addr = address.toLowerCase();
|
|
241
306
|
for (const market of this.markets) {
|
|
@@ -245,9 +310,15 @@ class MarketRegister extends SDKConstruct {
|
|
|
245
310
|
}
|
|
246
311
|
throw new Error(`cannot find market for pool ${address}`);
|
|
247
312
|
}
|
|
313
|
+
/**
|
|
314
|
+
* Underlying address map of pool address to market suite
|
|
315
|
+
**/
|
|
248
316
|
get marketsMap() {
|
|
249
317
|
return this.#markets;
|
|
250
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* All loaded market suites.
|
|
321
|
+
**/
|
|
251
322
|
get markets() {
|
|
252
323
|
return this.#markets.values();
|
|
253
324
|
}
|
|
@@ -13,26 +13,26 @@ const ZERO_PRICE_FEED = stringToHex("PRICE_FEED::ZERO", { size: 32 });
|
|
|
13
13
|
class PriceOracleBaseContract extends BaseContract {
|
|
14
14
|
sdk;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
|
|
16
|
+
* {@inheritDoc IPriceOracleContract.mainPriceFeeds}
|
|
17
|
+
**/
|
|
18
18
|
mainPriceFeeds = new AddressMap(
|
|
19
19
|
void 0,
|
|
20
20
|
"mainPriceFeeds"
|
|
21
21
|
);
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
24
|
-
|
|
23
|
+
* {@inheritDoc IPriceOracleContract.reservePriceFeeds}
|
|
24
|
+
**/
|
|
25
25
|
reservePriceFeeds = new AddressMap(
|
|
26
26
|
void 0,
|
|
27
27
|
"reservePriceFeeds"
|
|
28
28
|
);
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
31
|
-
|
|
30
|
+
* {@inheritDoc IPriceOracleContract.mainPrices}
|
|
31
|
+
**/
|
|
32
32
|
mainPrices = new PriceFeedAnswerMap(void 0, "mainPrices");
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
35
|
-
|
|
34
|
+
* {@inheritDoc IPriceOracleContract.reservePrices}
|
|
35
|
+
**/
|
|
36
36
|
reservePrices = new PriceFeedAnswerMap(
|
|
37
37
|
void 0,
|
|
38
38
|
"reservePrices"
|
|
@@ -48,11 +48,8 @@ class PriceOracleBaseContract extends BaseContract {
|
|
|
48
48
|
this.#loadState(priceFeedMap, priceFeedTree);
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
|
-
*
|
|
52
|
-
|
|
53
|
-
* @param opts Option to include main/reserve feeds only, defaults to both
|
|
54
|
-
* @returns
|
|
55
|
-
*/
|
|
51
|
+
* {@inheritDoc IPriceOracleContract.priceFeedsForTokens}
|
|
52
|
+
**/
|
|
56
53
|
priceFeedsForTokens(tokens, opts) {
|
|
57
54
|
const main = opts?.main ?? true;
|
|
58
55
|
const reserve = opts?.reserve ?? true;
|
|
@@ -62,39 +59,26 @@ class PriceOracleBaseContract extends BaseContract {
|
|
|
62
59
|
]).filter((f) => !!f);
|
|
63
60
|
}
|
|
64
61
|
/**
|
|
65
|
-
*
|
|
66
|
-
|
|
67
|
-
* @param token
|
|
68
|
-
* @returns
|
|
69
|
-
*/
|
|
62
|
+
* {@inheritDoc IPriceOracleContract.mainPrice}
|
|
63
|
+
**/
|
|
70
64
|
mainPrice(token) {
|
|
71
65
|
return this.mainPrices.price(token);
|
|
72
66
|
}
|
|
73
67
|
/**
|
|
74
|
-
*
|
|
75
|
-
|
|
76
|
-
* @param token
|
|
77
|
-
* @returns
|
|
78
|
-
*/
|
|
68
|
+
* {@inheritDoc IPriceOracleContract.reservePrice}
|
|
69
|
+
**/
|
|
79
70
|
reservePrice(token) {
|
|
80
71
|
return this.reservePrices.price(token);
|
|
81
72
|
}
|
|
82
73
|
/**
|
|
83
|
-
*
|
|
84
|
-
|
|
85
|
-
* @param priceFeed
|
|
86
|
-
* @returns
|
|
87
|
-
*/
|
|
74
|
+
* {@inheritDoc IPriceOracleContract.usesPriceFeed}
|
|
75
|
+
**/
|
|
88
76
|
usesPriceFeed(priceFeed) {
|
|
89
77
|
return this.#priceFeedTree.has(priceFeed);
|
|
90
78
|
}
|
|
91
79
|
/**
|
|
92
|
-
*
|
|
93
|
-
|
|
94
|
-
* @param to
|
|
95
|
-
* @param amount
|
|
96
|
-
* @param reserve use reserve price feed instead of main
|
|
97
|
-
*/
|
|
80
|
+
* {@inheritDoc IPriceOracleContract.convert}
|
|
81
|
+
**/
|
|
98
82
|
convert(from, to, amount, reserve = false) {
|
|
99
83
|
if (from === to) {
|
|
100
84
|
return amount;
|
|
@@ -106,11 +90,8 @@ class PriceOracleBaseContract extends BaseContract {
|
|
|
106
90
|
return amount * fromPrice * toScale / (toPrice * fromScale);
|
|
107
91
|
}
|
|
108
92
|
/**
|
|
109
|
-
*
|
|
110
|
-
|
|
111
|
-
* @param amount
|
|
112
|
-
* @param reserve use reserve price feed instead of main
|
|
113
|
-
*/
|
|
93
|
+
* {@inheritDoc IPriceOracleContract.convertToUSD}
|
|
94
|
+
**/
|
|
114
95
|
convertToUSD(from, amount, reserve = false) {
|
|
115
96
|
if (amount === 0n) {
|
|
116
97
|
return 0n;
|
|
@@ -120,11 +101,8 @@ class PriceOracleBaseContract extends BaseContract {
|
|
|
120
101
|
return amount * price / scale;
|
|
121
102
|
}
|
|
122
103
|
/**
|
|
123
|
-
*
|
|
124
|
-
|
|
125
|
-
* @param amount
|
|
126
|
-
* @param reserve use reserve price feed instead of main
|
|
127
|
-
*/
|
|
104
|
+
* {@inheritDoc IPriceOracleContract.convertFromUSD}
|
|
105
|
+
**/
|
|
128
106
|
convertFromUSD(to, amount, reserve = false) {
|
|
129
107
|
if (amount === 0n) {
|
|
130
108
|
return 0n;
|
|
@@ -134,14 +112,14 @@ class PriceOracleBaseContract extends BaseContract {
|
|
|
134
112
|
return amount * scale / price;
|
|
135
113
|
}
|
|
136
114
|
/**
|
|
137
|
-
*
|
|
138
|
-
|
|
115
|
+
* {@inheritDoc IPriceOracleContract.priceFeeds}
|
|
116
|
+
**/
|
|
139
117
|
get priceFeeds() {
|
|
140
118
|
return this.#priceFeedTree.values().map((node) => this.sdk.priceFeeds.mustGet(node.baseParams.addr));
|
|
141
119
|
}
|
|
142
120
|
/**
|
|
143
|
-
*
|
|
144
|
-
|
|
121
|
+
* {@inheritDoc IPriceOracleContract.syncStateMulticall}
|
|
122
|
+
**/
|
|
145
123
|
syncStateMulticall() {
|
|
146
124
|
const args = [this.address];
|
|
147
125
|
const [address] = this.sdk.addressProvider.mustGetLatest(
|
|
@@ -207,11 +185,14 @@ class PriceOracleBaseContract extends BaseContract {
|
|
|
207
185
|
});
|
|
208
186
|
}
|
|
209
187
|
/**
|
|
210
|
-
*
|
|
211
|
-
|
|
188
|
+
* {@inheritDoc IPriceOracleContract.watchAddresses}
|
|
189
|
+
**/
|
|
212
190
|
get watchAddresses() {
|
|
213
191
|
return /* @__PURE__ */ new Set([this.address]);
|
|
214
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* {@inheritDoc IPriceOracleContract.stateHuman}
|
|
195
|
+
**/
|
|
215
196
|
stateHuman(raw = true) {
|
|
216
197
|
return {
|
|
217
198
|
...super.stateHuman(raw),
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { Construct } from "../../base/index.js";
|
|
2
2
|
import { formatDuration } from "../../utils/index.js";
|
|
3
3
|
class PriceFeedRef extends Construct {
|
|
4
|
+
/**
|
|
5
|
+
* On-chain address of the referenced price feed contract.
|
|
6
|
+
**/
|
|
4
7
|
address;
|
|
8
|
+
/**
|
|
9
|
+
* Maximum allowed age (in seconds) of the feed's answer before the
|
|
10
|
+
* oracle considers it stale.
|
|
11
|
+
**/
|
|
5
12
|
stalenessPeriod;
|
|
6
13
|
#priceFeed;
|
|
7
14
|
constructor(options, address, stalenessPeriod) {
|
|
@@ -9,6 +16,10 @@ class PriceFeedRef extends Construct {
|
|
|
9
16
|
this.address = address;
|
|
10
17
|
this.stalenessPeriod = stalenessPeriod;
|
|
11
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Lazily resolved price feed contract instance.
|
|
21
|
+
* @throws If the feed is not registered in the contracts register.
|
|
22
|
+
**/
|
|
12
23
|
get priceFeed() {
|
|
13
24
|
if (!this.#priceFeed) {
|
|
14
25
|
this.#priceFeed = this.register.mustGetContract(
|
|
@@ -17,6 +28,11 @@ class PriceFeedRef extends Construct {
|
|
|
17
28
|
}
|
|
18
29
|
return this.#priceFeed;
|
|
19
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns a human-readable snapshot of the feed state, including the
|
|
33
|
+
* staleness period formatted as a duration string.
|
|
34
|
+
* @param raw - When `true`, includes raw/unformatted values.
|
|
35
|
+
**/
|
|
20
36
|
stateHuman(raw = true) {
|
|
21
37
|
return {
|
|
22
38
|
...this.priceFeed.stateHuman(raw),
|