@gearbox-protocol/sdk 17.2.0 → 17.3.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/dist/cjs/onchain/accounts/intents/leverage-band.js +1 -1
- package/dist/cjs/onchain/accounts/intents/maxBorrow.js +15 -11
- package/dist/cjs/onchain/accounts/intents/testing/sdk-mock.js +5 -2
- package/dist/cjs/onchain/chain/chains.js +1 -2
- package/dist/cjs/onchain/index.js +2 -0
- package/dist/cjs/onchain/market/MarketSuite.js +11 -4
- package/dist/cjs/onchain/market/credit/CreditSuite.js +25 -138
- package/dist/cjs/onchain/market/credit/CreditSuiteStrategy.js +179 -0
- package/dist/cjs/onchain/market/credit/index.js +2 -0
- package/dist/cjs/onchain/market/index.js +2 -0
- package/dist/cjs/onchain/market/pool/PoolQuotaKeeperV310Contract.js +4 -1
- package/dist/cjs/onchain/opportunities/OpportunitiesService.js +7 -9
- package/dist/cjs/rewards/{multichain.js → RewardsService.js} +18 -30
- package/dist/cjs/rewards/errors.js +4 -4
- package/dist/cjs/rewards/helpers.js +88 -0
- package/dist/cjs/rewards/index.js +2 -2
- package/dist/cjs/rewards/merkl-api.js +14 -25
- package/dist/cjs/rewards/toMerklRewards.js +21 -77
- package/dist/cjs/rewards/toTurtleRewards.js +46 -62
- package/dist/cjs/rewards/turtle-api.js +17 -0
- package/dist/cjs/rewards/types.js +1 -0
- package/dist/cjs/sdk/prepare/PrepareApi.js +1 -1
- package/dist/esm/onchain/accounts/intents/leverage-band.js +1 -1
- package/dist/esm/onchain/accounts/intents/maxBorrow.js +15 -11
- package/dist/esm/onchain/accounts/intents/testing/sdk-mock.js +5 -2
- package/dist/esm/onchain/chain/chains.js +1 -2
- package/dist/esm/onchain/index.js +2 -1
- package/dist/esm/onchain/market/MarketSuite.js +11 -4
- package/dist/esm/onchain/market/credit/CreditSuite.js +27 -140
- package/dist/esm/onchain/market/credit/CreditSuiteStrategy.js +178 -0
- package/dist/esm/onchain/market/credit/index.js +2 -1
- package/dist/esm/onchain/market/index.js +2 -1
- package/dist/esm/onchain/market/pool/PoolQuotaKeeperV310Contract.js +4 -1
- package/dist/esm/onchain/opportunities/OpportunitiesService.js +7 -9
- package/dist/esm/rewards/{multichain.js → RewardsService.js} +19 -31
- package/dist/esm/rewards/errors.js +4 -4
- package/dist/esm/rewards/helpers.js +83 -0
- package/dist/esm/rewards/index.js +2 -2
- package/dist/esm/rewards/merkl-api.js +15 -25
- package/dist/esm/rewards/toMerklRewards.js +23 -78
- package/dist/esm/rewards/toTurtleRewards.js +46 -62
- package/dist/esm/rewards/turtle-api.js +17 -1
- package/dist/esm/rewards/types.js +1 -0
- package/dist/esm/sdk/prepare/PrepareApi.js +1 -1
- package/dist/types/model/errors/operation-errors.d.ts +6 -1
- package/dist/types/onchain/accounts/intents/maxBorrow.d.ts +12 -10
- package/dist/types/onchain/index.d.ts +2 -1
- package/dist/types/onchain/market/MarketSuite.d.ts +6 -0
- package/dist/types/onchain/market/credit/CreditSuite.d.ts +18 -52
- package/dist/types/onchain/market/credit/CreditSuiteStrategy.d.ts +78 -0
- package/dist/types/onchain/market/credit/index.d.ts +2 -1
- package/dist/types/onchain/market/credit/types.d.ts +3 -1
- package/dist/types/onchain/market/index.d.ts +2 -1
- package/dist/types/onchain/market/pool/PoolQuotaKeeperV310Contract.d.ts +2 -0
- package/dist/types/rewards/RewardsService.d.ts +29 -0
- package/dist/types/rewards/errors.d.ts +2 -2
- package/dist/types/rewards/helpers.d.ts +44 -0
- package/dist/types/rewards/index.d.ts +3 -3
- package/dist/types/rewards/merkl-api.d.ts +5 -10
- package/dist/types/rewards/toMerklRewards.d.ts +5 -48
- package/dist/types/rewards/toTurtleRewards.d.ts +3 -3
- package/dist/types/rewards/turtle-api.d.ts +5 -1
- package/dist/types/rewards/types.d.ts +41 -0
- package/package.json +1 -1
- package/dist/types/rewards/multichain.d.ts +0 -28
|
@@ -3,11 +3,23 @@ import "../onchain/index.js";
|
|
|
3
3
|
import { fetchMerklUserRewards } from "./merkl-api.js";
|
|
4
4
|
import { toMerklRewards } from "./toMerklRewards.js";
|
|
5
5
|
import { toTurtleRewards } from "./toTurtleRewards.js";
|
|
6
|
-
import { fetchTurtleWalletRewards,
|
|
6
|
+
import { fetchTurtleWalletRewards, readTurtleClaimed } from "./turtle-api.js";
|
|
7
7
|
import { getAddress } from "viem";
|
|
8
|
-
//#region src/rewards/
|
|
9
|
-
var
|
|
10
|
-
|
|
8
|
+
//#region src/rewards/RewardsService.ts
|
|
9
|
+
var RewardsService = class extends MultichainConstruct {
|
|
10
|
+
#keys;
|
|
11
|
+
constructor(sdk, keys = {}) {
|
|
12
|
+
super(sdk);
|
|
13
|
+
this.#keys = keys;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Every claimable reward a wallet holds — Merkl campaigns and the Gearbox
|
|
17
|
+
* organisation's Turtle streams — across the chains the handle carries.
|
|
18
|
+
* A chain is `status: "error"` only when every source failed on it; a chain
|
|
19
|
+
* with nothing to claim is a `"success"` with no rewards.
|
|
20
|
+
**/
|
|
21
|
+
async list(wallet, chainIds) {
|
|
22
|
+
const { merklApiKey, turtleApiKey } = this.#keys;
|
|
11
23
|
const user = getAddress(wallet);
|
|
12
24
|
const turtle = turtleApiKey ? fetchTurtleWalletRewards(user, turtleApiKey) : void 0;
|
|
13
25
|
turtle?.catch(() => {});
|
|
@@ -15,13 +27,13 @@ var RewardsFanOut = class extends MultichainConstruct {
|
|
|
15
27
|
chainIds,
|
|
16
28
|
label: "list rewards",
|
|
17
29
|
block: "state",
|
|
18
|
-
run: async (sdk
|
|
30
|
+
run: async (sdk) => {
|
|
19
31
|
const sources = [fetchMerklUserRewards({
|
|
20
32
|
chainId: sdk.chainId,
|
|
21
33
|
user,
|
|
22
34
|
apiKey: merklApiKey
|
|
23
35
|
}).then((response) => toMerklRewards(sdk, response))];
|
|
24
|
-
if (turtle) sources.push(turtle.then(async (rewards) => toTurtleRewards(sdk, rewards, await
|
|
36
|
+
if (turtle) sources.push(turtle.then(async (rewards) => toTurtleRewards(sdk, rewards, await readTurtleClaimed(sdk, user, rewards))));
|
|
25
37
|
const settled = await Promise.allSettled(sources);
|
|
26
38
|
const failed = settled.flatMap((r) => r.status === "rejected" ? [r.reason] : []);
|
|
27
39
|
if (failed.length === settled.length) throw failed.length === 1 ? failed[0] : new AggregateError(failed, "no rewards source answered");
|
|
@@ -31,29 +43,5 @@ var RewardsFanOut = class extends MultichainConstruct {
|
|
|
31
43
|
});
|
|
32
44
|
}
|
|
33
45
|
};
|
|
34
|
-
async function readClaimed(sdk, user, { proofs }, blockNumber) {
|
|
35
|
-
const onChain = proofs.filter((p) => p.chainId === sdk.chainId);
|
|
36
|
-
if (onChain.length === 0) return /* @__PURE__ */ new Map();
|
|
37
|
-
const claimed = await sdk.client.multicall({
|
|
38
|
-
contracts: onChain.map((p) => ({
|
|
39
|
-
address: p.contractAddress,
|
|
40
|
-
abi: turtleStreamAbi,
|
|
41
|
-
functionName: "getClaimedRewards",
|
|
42
|
-
args: [user]
|
|
43
|
-
})),
|
|
44
|
-
allowFailure: false,
|
|
45
|
-
blockNumber
|
|
46
|
-
});
|
|
47
|
-
return new Map(onChain.map((p, i) => [p.streamId, claimed[i]]));
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Every claimable reward a wallet holds — Merkl campaigns and the Gearbox
|
|
51
|
-
* organisation's Turtle streams — across the chains the handle carries.
|
|
52
|
-
* A chain is `status: "error"` only when every source failed on it; a chain
|
|
53
|
-
* with nothing to claim is a `"success"` with no rows.
|
|
54
|
-
**/
|
|
55
|
-
async function getRewardsMultichain({ sdk, ...props }) {
|
|
56
|
-
return new RewardsFanOut(sdk).list(props);
|
|
57
|
-
}
|
|
58
46
|
//#endregion
|
|
59
|
-
export {
|
|
47
|
+
export { RewardsService };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseError } from "viem";
|
|
2
2
|
//#region src/rewards/errors.ts
|
|
3
3
|
/**
|
|
4
|
-
* Thrown when
|
|
4
|
+
* Thrown when Merkl did not answer for a chain.
|
|
5
5
|
*
|
|
6
6
|
* Everything a reader needs is in the message: this error travels to consumers
|
|
7
7
|
* inside a chain's {@link ChainFailed} metadata, where it is typed `unknown`
|
|
@@ -10,10 +10,10 @@ import { BaseError } from "viem";
|
|
|
10
10
|
var MerklRequestFailedError = class extends BaseError {
|
|
11
11
|
name = "MerklRequestFailedError";
|
|
12
12
|
chainId;
|
|
13
|
-
constructor(chainId, path,
|
|
13
|
+
constructor(chainId, path, cause) {
|
|
14
14
|
super(`Merkl could not be reached for chain ${chainId}.`, {
|
|
15
|
-
cause:
|
|
16
|
-
metaMessages:
|
|
15
|
+
cause: cause instanceof Error ? cause : void 0,
|
|
16
|
+
metaMessages: [`${path} — ${describe(cause)}`]
|
|
17
17
|
});
|
|
18
18
|
this.chainId = chainId;
|
|
19
19
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { AddressMap } from "../onchain/utils/AddressMap.js";
|
|
2
|
+
import { toBigInt } from "../onchain/utils/formatter.js";
|
|
3
|
+
import "../onchain/index.js";
|
|
4
|
+
import { formatUnits, isAddress } from "viem";
|
|
5
|
+
//#region src/rewards/helpers.ts
|
|
6
|
+
/**
|
|
7
|
+
* The pool a source names, and its share token. A v3.1 pool is its own
|
|
8
|
+
* ERC-4626 share token, so its address is the only token a campaign can name;
|
|
9
|
+
* the `AddressMap` owns the casing a source writes it in.
|
|
10
|
+
*/
|
|
11
|
+
function resolvePool(sdk) {
|
|
12
|
+
const pools = AddressMap.fromMappedArray(sdk.marketRegister.pools.map(({ pool }) => pool.address), (address) => address);
|
|
13
|
+
return (address) => {
|
|
14
|
+
if (!address || !isAddress(address, { strict: false })) return void 0;
|
|
15
|
+
const pool = pools.get(address);
|
|
16
|
+
const poolToken = pool && sdk.tokensMeta.getToken(pool);
|
|
17
|
+
return pool && poolToken ? {
|
|
18
|
+
pool,
|
|
19
|
+
poolToken
|
|
20
|
+
} : void 0;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Sources send amounts as free-form strings, which `toBigInt` throws on. */
|
|
24
|
+
function parseAmount(value) {
|
|
25
|
+
if (value === void 0) return void 0;
|
|
26
|
+
try {
|
|
27
|
+
return toBigInt(value || 0);
|
|
28
|
+
} catch {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** What is left of `total` after `claimed`, when anything is. */
|
|
33
|
+
function remaining(total, claimed) {
|
|
34
|
+
if (total === void 0 || claimed === void 0) return void 0;
|
|
35
|
+
return total > claimed ? total - claimed : void 0;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* An incentive token is rarely protocol collateral, so the registry usually
|
|
39
|
+
* has no entry for it — and both sources always name it.
|
|
40
|
+
*/
|
|
41
|
+
function toRewardToken(sdk, address, named) {
|
|
42
|
+
return sdk.tokensMeta.getToken(address) ?? {
|
|
43
|
+
chainId: sdk.chainId,
|
|
44
|
+
address,
|
|
45
|
+
symbol: named.symbol,
|
|
46
|
+
name: named.name ?? named.symbol,
|
|
47
|
+
decimals: named.decimals || 18
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** A missing or unparsable price is `null`. */
|
|
51
|
+
function toPricedAmount(token, value, price) {
|
|
52
|
+
const usd = Number(price ?? NaN);
|
|
53
|
+
return {
|
|
54
|
+
token,
|
|
55
|
+
value,
|
|
56
|
+
valueUsd: Number.isFinite(usd) ? Number(formatUnits(value, token.decimals)) * usd : null
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* One reward per pool and incentive token, at the last part's price. Amounts
|
|
61
|
+
* are summed raw and priced once at the end — pricing each part and adding the results would round
|
|
62
|
+
* every one of them.
|
|
63
|
+
*/
|
|
64
|
+
function mergeRewards(sdk, source, parts) {
|
|
65
|
+
const merged = /* @__PURE__ */ new Map();
|
|
66
|
+
for (const part of parts) {
|
|
67
|
+
const key = `${part.pool}_${part.token.address}`;
|
|
68
|
+
const seen = merged.get(key);
|
|
69
|
+
merged.set(key, seen ? {
|
|
70
|
+
...part,
|
|
71
|
+
value: seen.value + part.value
|
|
72
|
+
} : part);
|
|
73
|
+
}
|
|
74
|
+
return [...merged.values()].map(({ pool, poolToken, token, value, price }) => ({
|
|
75
|
+
source,
|
|
76
|
+
chainId: sdk.chainId,
|
|
77
|
+
pool,
|
|
78
|
+
poolToken,
|
|
79
|
+
amount: toPricedAmount(token, value, price)
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
export { mergeRewards, parseAmount, remaining, resolvePool, toRewardToken };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { MerklRequestFailedError, TurtleRequestFailedError } from "./errors.js";
|
|
2
|
-
import {
|
|
3
|
-
export { MerklRequestFailedError,
|
|
2
|
+
import { RewardsService } from "./RewardsService.js";
|
|
3
|
+
export { MerklRequestFailedError, RewardsService, TurtleRequestFailedError };
|
|
@@ -1,42 +1,32 @@
|
|
|
1
1
|
import { MerklRequestFailedError } from "./errors.js";
|
|
2
2
|
//#region src/rewards/merkl-api.ts
|
|
3
|
-
|
|
4
|
-
* Merkl's own host and the Angle mirror, tried in this order.
|
|
5
|
-
*/
|
|
6
|
-
const MERKL_DOMAINS = ["https://api.merkl.xyz", "https://api-merkl.angle.money"];
|
|
3
|
+
const MERKL_API_URL = "https://api.merkl.xyz";
|
|
7
4
|
const MERKL_API_KEY_HEADER = "X-API-Key";
|
|
8
5
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* Merkl has no timeout of its own, and a hung connection would otherwise stall
|
|
7
|
+
* its leg of a fan-out for as long as the socket lives.
|
|
11
8
|
*/
|
|
12
|
-
const
|
|
9
|
+
const TIMEOUT = 1e4;
|
|
13
10
|
/**
|
|
14
11
|
* The wallet's raw Merkl rewards on one chain.
|
|
15
12
|
*
|
|
16
|
-
* Rejects with {@link MerklRequestFailedError} when
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* was never established.
|
|
13
|
+
* Rejects with {@link MerklRequestFailedError} when Merkl does not answer, so a
|
|
14
|
+
* caller can tell an unreachable Merkl from a wallet with nothing to claim. A
|
|
15
|
+
* non-2xx counts as no answer: it carries no rewards either way, and treating
|
|
16
|
+
* it as success would report emptiness that was never established.
|
|
21
17
|
*/
|
|
22
18
|
async function fetchMerklUserRewards({ chainId, user, apiKey }) {
|
|
23
19
|
const path = `/v4/users/${user}/rewards?chainId=${chainId}`;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
headers,
|
|
29
|
-
signal: AbortSignal.timeout(ATTEMPT_TIMEOUT)
|
|
20
|
+
try {
|
|
21
|
+
const response = await fetch(`${MERKL_API_URL}${path}`, {
|
|
22
|
+
headers: apiKey ? { [MERKL_API_KEY_HEADER]: apiKey } : void 0,
|
|
23
|
+
signal: AbortSignal.timeout(TIMEOUT)
|
|
30
24
|
});
|
|
31
|
-
if (!response.ok) {
|
|
32
|
-
attempts.push([domain, /* @__PURE__ */ new Error(`answered ${response.status}`)]);
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
25
|
+
if (!response.ok) throw new Error(`answered ${response.status}`);
|
|
35
26
|
return await response.json();
|
|
36
27
|
} catch (error) {
|
|
37
|
-
|
|
28
|
+
throw new MerklRequestFailedError(chainId, path, error);
|
|
38
29
|
}
|
|
39
|
-
throw new MerklRequestFailedError(chainId, path, attempts);
|
|
40
30
|
}
|
|
41
31
|
//#endregion
|
|
42
|
-
export { MERKL_API_KEY_HEADER,
|
|
32
|
+
export { MERKL_API_KEY_HEADER, fetchMerklUserRewards };
|
|
@@ -1,84 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { toBigInt } from "../onchain/utils/formatter.js";
|
|
4
|
-
import "../onchain/index.js";
|
|
5
|
-
import { formatUnits, getAddress, isAddress } from "viem";
|
|
1
|
+
import { mergeRewards, parseAmount, remaining, resolvePool, toRewardToken } from "./helpers.js";
|
|
2
|
+
import { getAddress, isAddress } from "viem";
|
|
6
3
|
//#region src/rewards/toMerklRewards.ts
|
|
7
4
|
/**
|
|
8
|
-
* Merkl's answer for one chain,
|
|
5
|
+
* Merkl's answer for one chain, one reward per pool and incentive token.
|
|
6
|
+
* A breakdown Merkl cannot be read from is skipped rather than thrown on: it
|
|
7
|
+
* must not sink the chain, which the caller would then be told is unreachable.
|
|
9
8
|
*/
|
|
10
9
|
function toMerklRewards(sdk, response) {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
if (!
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
seen.value += amount;
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
const poolToken = sdk.tokensMeta.getToken(pool);
|
|
32
|
-
if (!poolToken) continue;
|
|
33
|
-
claimable.set(key, {
|
|
34
|
-
chainId: sdk.chainId,
|
|
35
|
-
pool,
|
|
36
|
-
poolToken,
|
|
37
|
-
token: toRewardToken(sdk, rewardTokenAddress, reward.token),
|
|
38
|
-
value: amount,
|
|
39
|
-
price: reward.token.price
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return [...claimable.values()].map(toReward);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* `toBigInt` throws on anything `BigInt()` cannot parse, and Merkl's amounts
|
|
47
|
-
* are free-form strings.
|
|
48
|
-
*/
|
|
49
|
-
function toAmounts(reason) {
|
|
50
|
-
try {
|
|
51
|
-
return {
|
|
52
|
-
total: toBigInt(reason.amount || 0),
|
|
53
|
-
claimed: toBigInt(reason.claimed || 0)
|
|
54
|
-
};
|
|
55
|
-
} catch {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
function toReward({ price, token, value, ...rest }) {
|
|
60
|
-
return {
|
|
61
|
-
...rest,
|
|
62
|
-
source: "merkl",
|
|
63
|
-
amount: {
|
|
64
|
-
token,
|
|
65
|
-
value,
|
|
66
|
-
valueUsd: price === void 0 ? null : Number(formatUnits(value, token.decimals)) * price
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* An incentive token is rarely protocol collateral, so the registry usually
|
|
72
|
-
* has no entry for it — and both sources always name it.
|
|
73
|
-
*/
|
|
74
|
-
function toRewardToken(sdk, address, named) {
|
|
75
|
-
return sdk.tokensMeta.getToken(address) ?? {
|
|
76
|
-
chainId: sdk.chainId,
|
|
77
|
-
address,
|
|
78
|
-
symbol: named.symbol,
|
|
79
|
-
name: named.name ?? named.symbol,
|
|
80
|
-
decimals: named.decimals || 18
|
|
81
|
-
};
|
|
10
|
+
const poolOf = resolvePool(sdk);
|
|
11
|
+
const parts = response.flatMap(({ rewards }) => rewards).flatMap(({ token, breakdowns }) => {
|
|
12
|
+
if (!isAddress(token.address, { strict: false })) return [];
|
|
13
|
+
const rewardToken = toRewardToken(sdk, getAddress(token.address), token);
|
|
14
|
+
return breakdowns.flatMap(({ reason, amount, claimed }) => {
|
|
15
|
+
const pool = poolOf(reason?.split("_").find((p) => p.startsWith("0x")));
|
|
16
|
+
const value = remaining(parseAmount(amount), parseAmount(claimed));
|
|
17
|
+
if (!pool || !value) return [];
|
|
18
|
+
return [{
|
|
19
|
+
...pool,
|
|
20
|
+
token: rewardToken,
|
|
21
|
+
value,
|
|
22
|
+
price: token.price
|
|
23
|
+
}];
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
return mergeRewards(sdk, "merkl", parts);
|
|
82
27
|
}
|
|
83
28
|
//#endregion
|
|
84
|
-
export { toMerklRewards
|
|
29
|
+
export { toMerklRewards };
|
|
@@ -1,76 +1,60 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BigIntMath } from "../onchain/utils/bigint-math.js";
|
|
3
|
-
import { toBigInt } from "../onchain/utils/formatter.js";
|
|
4
|
-
import "../onchain/index.js";
|
|
5
|
-
import { toRewardToken } from "./toMerklRewards.js";
|
|
1
|
+
import { mergeRewards, parseAmount, remaining, resolvePool, toRewardToken } from "./helpers.js";
|
|
6
2
|
import { formatUnits, getAddress, isAddress } from "viem";
|
|
7
3
|
//#region src/rewards/toTurtleRewards.ts
|
|
8
4
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
5
|
+
* The streams targeting a pool on the SDK's chain, merged per pool and
|
|
6
|
+
* incentive. A token reward is what is committed and not yet claimed.
|
|
11
7
|
*/
|
|
12
8
|
function toTurtleRewards(sdk, { streams, proofs }, claimed) {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
9
|
+
const poolOf = resolvePool(sdk);
|
|
10
|
+
const committed = new Map(proofs.map((p) => [p.streamId, p.amount]));
|
|
11
|
+
const targeted = streams.flatMap((stream) => {
|
|
12
|
+
const target = stream.stream.customArgs.targetToken;
|
|
13
|
+
if (Number(target?.chain.chainId) !== sdk.chainId) return [];
|
|
14
|
+
const pool = poolOf(target?.address);
|
|
15
|
+
return pool ? [{
|
|
16
|
+
stream,
|
|
17
|
+
...pool
|
|
18
|
+
}] : [];
|
|
19
|
+
});
|
|
20
|
+
const tokens = targeted.flatMap(({ stream, pool, poolToken }) => {
|
|
21
|
+
const { point, rewardToken, lastSnapshot } = stream.stream;
|
|
22
|
+
if (point || !rewardToken || !isAddress(rewardToken.address, { strict: false })) return [];
|
|
23
|
+
const value = remaining(parseAmount(committed.get(stream.streamId)), claimed.get(stream.streamId));
|
|
24
|
+
if (!value) return [];
|
|
25
|
+
return [{
|
|
26
|
+
pool,
|
|
27
|
+
poolToken,
|
|
28
|
+
token: toRewardToken(sdk, getAddress(rewardToken.address), rewardToken),
|
|
29
|
+
value,
|
|
30
|
+
price: lastSnapshot?.rewardTokenPrice
|
|
31
|
+
}];
|
|
32
|
+
});
|
|
33
|
+
return [...mergeRewards(sdk, "turtle", tokens), ...toPoints(sdk, targeted)];
|
|
34
|
+
}
|
|
35
|
+
/** Points accrue per snapshot; the last one holds the running total. */
|
|
36
|
+
function toPoints(sdk, targeted) {
|
|
37
|
+
const merged = /* @__PURE__ */ new Map();
|
|
38
|
+
for (const { stream, pool, poolToken } of targeted) {
|
|
39
|
+
const { point } = stream.stream;
|
|
40
|
+
const raw = parseAmount(stream.snapshots.at(-1)?.rewardsAccumulated);
|
|
41
|
+
if (!point || !raw) continue;
|
|
42
|
+
const key = `${pool}_${point.id}`;
|
|
43
|
+
const value = (merged.get(key)?.points.value ?? 0) + Number(formatUnits(raw, point.decimals));
|
|
44
|
+
merged.set(key, {
|
|
24
45
|
source: "turtle",
|
|
25
46
|
chainId: sdk.chainId,
|
|
26
47
|
pool,
|
|
27
|
-
poolToken
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const key = `${pool}_${point.id}`;
|
|
34
|
-
const seen = rows.get(key);
|
|
35
|
-
rows.set(key, {
|
|
36
|
-
...base,
|
|
37
|
-
points: {
|
|
38
|
-
id: point.id,
|
|
39
|
-
name: point.name,
|
|
40
|
-
multiplier: null,
|
|
41
|
-
value: (seen?.points.value ?? 0) + Number(formatUnits(value, point.decimals))
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
if (!rewardToken || !isAddress(rewardToken.address, { strict: false })) continue;
|
|
47
|
-
const committed = parse(proofByStream.get(streamId)?.amount);
|
|
48
|
-
const done = claimed.get(streamId);
|
|
49
|
-
if (committed === void 0 || done === void 0) continue;
|
|
50
|
-
const value = BigIntMath.max(committed - done, 0n);
|
|
51
|
-
if (value === 0n) continue;
|
|
52
|
-
const token = toRewardToken(sdk, getAddress(rewardToken.address), rewardToken);
|
|
53
|
-
const key = `${pool}_${token.address}`;
|
|
54
|
-
const total = (rows.get(key)?.amount.value ?? 0n) + value;
|
|
55
|
-
const price = Number(stream.lastSnapshot?.rewardTokenPrice ?? NaN);
|
|
56
|
-
rows.set(key, {
|
|
57
|
-
...base,
|
|
58
|
-
amount: {
|
|
59
|
-
token,
|
|
60
|
-
value: total,
|
|
61
|
-
valueUsd: Number.isFinite(price) ? Number(formatUnits(total, token.decimals)) * price : null
|
|
48
|
+
poolToken,
|
|
49
|
+
points: {
|
|
50
|
+
id: point.id,
|
|
51
|
+
name: point.name,
|
|
52
|
+
multiplier: null,
|
|
53
|
+
value
|
|
62
54
|
}
|
|
63
55
|
});
|
|
64
56
|
}
|
|
65
|
-
return [...
|
|
66
|
-
}
|
|
67
|
-
function parse(amount) {
|
|
68
|
-
if (amount === void 0) return void 0;
|
|
69
|
-
try {
|
|
70
|
-
return toBigInt(amount);
|
|
71
|
-
} catch {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
57
|
+
return [...merged.values()];
|
|
74
58
|
}
|
|
75
59
|
//#endregion
|
|
76
60
|
export { toTurtleRewards };
|
|
@@ -44,5 +44,21 @@ async function get(path, apiKey) {
|
|
|
44
44
|
throw new TurtleRequestFailedError(path, error);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
+
/** What each of the wallet's streams on the chain has paid out, at the latest block. */
|
|
48
|
+
async function readTurtleClaimed(sdk, user, { proofs }) {
|
|
49
|
+
const onChain = proofs.filter((p) => p.chainId === sdk.chainId);
|
|
50
|
+
if (onChain.length === 0) return /* @__PURE__ */ new Map();
|
|
51
|
+
const claimed = await sdk.client.multicall({
|
|
52
|
+
contracts: onChain.map((p) => ({
|
|
53
|
+
address: p.contractAddress,
|
|
54
|
+
abi: turtleStreamAbi,
|
|
55
|
+
functionName: "getClaimedRewards",
|
|
56
|
+
args: [user]
|
|
57
|
+
})),
|
|
58
|
+
allowFailure: false,
|
|
59
|
+
blockTag: "latest"
|
|
60
|
+
});
|
|
61
|
+
return new Map(onChain.map((p, i) => [p.streamId, claimed[i]]));
|
|
62
|
+
}
|
|
47
63
|
//#endregion
|
|
48
|
-
export { fetchTurtleWalletRewards, turtleStreamAbi };
|
|
64
|
+
export { fetchTurtleWalletRewards, readTurtleClaimed, turtleStreamAbi };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -230,7 +230,7 @@ var PrepareApi = class extends MultichainConstruct {
|
|
|
230
230
|
const sdk = await this.#chain(strategy.chainId);
|
|
231
231
|
const at = stateBlock(sdk);
|
|
232
232
|
Object.assign(state, marketOf(sdk, strategy.creditManager));
|
|
233
|
-
const targetToken = params.targetToken ?? sdk.marketRegister.findCreditManager(strategy.creditManager).
|
|
233
|
+
const targetToken = params.targetToken ?? sdk.marketRegister.findCreditManager(strategy.creditManager).strategy?.targetCollateral;
|
|
234
234
|
if (!targetToken) return refused(noStrategyTargetCollateral(strategy.creditManager), state);
|
|
235
235
|
const reused = await reusable(sdk, strategy, params.creditAccount);
|
|
236
236
|
if (reused && "error" in reused) return refused(reused.error, state);
|
|
@@ -11,11 +11,16 @@ import { Address } from "viem";
|
|
|
11
11
|
* - `poolAvailableLiquidity` — the pool's available liquidity
|
|
12
12
|
* - `managerDebtAvailable` — this credit manager's remaining debt allowance
|
|
13
13
|
* - `maxDebt` — the facade's per-account `debtLimits.maxDebt`
|
|
14
|
+
* - `quotaAvailable` — remaining quota the market takes for the suite's
|
|
15
|
+
* strategy target collateral (`limit - totalQuoted`, floored at 0); only an
|
|
16
|
+
* account being opened is held to it
|
|
17
|
+
* - `minDebt` — every other limit left less than the facade's `minDebt`, so
|
|
18
|
+
* no account can be opened; the amount is 0
|
|
14
19
|
* - `debtPerBlockLimit` — facade takes no new debt this block; in practice
|
|
15
20
|
* `maxDebtPerBlockMultiplier == 0` after a with-loss liquidation
|
|
16
21
|
* - `poolDebtLimit` — pool-wide debt cap; used on account-opening only
|
|
17
22
|
**/
|
|
18
|
-
type BorrowLimitCause = "poolAvailableLiquidity" | "managerDebtAvailable" | "maxDebt" | "debtPerBlockLimit" | "poolDebtLimit";
|
|
23
|
+
type BorrowLimitCause = "poolAvailableLiquidity" | "managerDebtAvailable" | "maxDebt" | "quotaAvailable" | "minDebt" | "debtPerBlockLimit" | "poolDebtLimit";
|
|
19
24
|
/**
|
|
20
25
|
* The credit manager is paused and takes no multicall at all.
|
|
21
26
|
**/
|
|
@@ -29,17 +29,19 @@ interface MaxBorrowProps {
|
|
|
29
29
|
* Collateral is valued the way the transaction will be judged — at safe
|
|
30
30
|
* prices, under its liquidation threshold, capped by the quota the borrow
|
|
31
31
|
* buys for it, all of which is {@link collateralValuation}'s business. The ceiling
|
|
32
|
-
* is then held to what the market will actually lend
|
|
33
|
-
*
|
|
34
|
-
* binds first.
|
|
32
|
+
* is then held to what the market will actually lend,
|
|
33
|
+
* {@link CreditSuite.maxBorrowAmount}: the pool's free liquidity, the
|
|
34
|
+
* manager's own allowance and the facade's `maxDebt`, whichever binds first.
|
|
35
35
|
*
|
|
36
|
-
* The facade's `minDebt` is
|
|
37
|
-
* ceiling answered as `0n` because the collateral is too
|
|
38
|
-
* would tell a form nothing about what it is holding —
|
|
39
|
-
* to see is the one they are short of. Collateral
|
|
40
|
-
* therefore answers with it, whether or not the market
|
|
41
|
-
* a loan under the floor is refused by `borrow`
|
|
42
|
-
* naming both ends.
|
|
36
|
+
* The facade's `minDebt` is not applied to the collateral's own ceiling. It
|
|
37
|
+
* is a floor, and a ceiling answered as `0n` because the collateral is too
|
|
38
|
+
* small for this market would tell a form nothing about what it is holding —
|
|
39
|
+
* the number a user needs to see is the one they are short of. Collateral
|
|
40
|
+
* that carries something therefore answers with it, whether or not the market
|
|
41
|
+
* would lend that little; a loan under the floor is refused by `borrow`
|
|
42
|
+
* itself, with `debtOutOfRange` naming both ends. A market whose own capacity
|
|
43
|
+
* is under `minDebt` is different: the answer is `0n`, because no loan of any
|
|
44
|
+
* size exists there.
|
|
43
45
|
*
|
|
44
46
|
* Nothing is fetched or simulated — the account does not exist yet and every
|
|
45
47
|
* input is loaded market state, so a form can call this on each keystroke.
|