@gearbox-protocol/sdk 11.8.5 → 11.8.6
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.
|
@@ -384,8 +384,8 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
384
384
|
`target #${i + 1} (${this.labelAddress(t.target)}) needs ${this.sdk.tokensMeta.formatBN(cm.underlying, amount, { symbol: true })} in pool (leverage: ${Number(leverage / import_sdk.PERCENTAGE_FACTOR)}%)`
|
|
385
385
|
);
|
|
386
386
|
}
|
|
387
|
-
let totalUSD = 0n;
|
|
388
387
|
const deposits = [];
|
|
388
|
+
const claims = new import_sdk.AddressMap();
|
|
389
389
|
for (const [p, minAvailable] of Object.entries(minAvailableByPool)) {
|
|
390
390
|
const market = this.sdk.marketRegister.findByPool(p);
|
|
391
391
|
const pool = market.pool.pool;
|
|
@@ -403,17 +403,15 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
403
403
|
);
|
|
404
404
|
if (diff > 0n) {
|
|
405
405
|
deposits.push([pool, diff]);
|
|
406
|
-
|
|
406
|
+
let claim = claims.get(pool.underlying) ?? 0n;
|
|
407
|
+
claim += diff * import_sdk.PERCENTAGE_FACTOR / 8950n;
|
|
408
|
+
claims.upsert(pool.underlying, claim);
|
|
407
409
|
}
|
|
408
410
|
}
|
|
409
|
-
totalUSD = totalUSD * import_sdk.PERCENTAGE_FACTOR / 8950n;
|
|
410
|
-
this.#logger?.debug(
|
|
411
|
-
`total USD to claim from faucet: ${(0, import_sdk.formatBN)(totalUSD, 8)}`
|
|
412
|
-
);
|
|
413
411
|
const depositor = await this.#getDepositor();
|
|
414
412
|
this.#logger?.debug(`depositor: ${depositor.address}`);
|
|
415
413
|
try {
|
|
416
|
-
await this.#claimFromFaucet(depositor, "depositor",
|
|
414
|
+
await this.#claimFromFaucet(depositor, "depositor", claims);
|
|
417
415
|
} catch (e) {
|
|
418
416
|
if (this.#allowMint) {
|
|
419
417
|
this.#logger?.error(`depositor failed to claim from faucet: ${e}`);
|
|
@@ -530,8 +528,8 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
530
528
|
*/
|
|
531
529
|
async #prepareBorrower(targets) {
|
|
532
530
|
const borrower = await this.#getBorrower();
|
|
533
|
-
let claimUSD = 0n;
|
|
534
531
|
const degenNFTS = {};
|
|
532
|
+
const claims = new import_sdk.AddressMap();
|
|
535
533
|
for (const target of targets) {
|
|
536
534
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
537
535
|
target.creditManager
|
|
@@ -541,27 +539,28 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
541
539
|
);
|
|
542
540
|
const { degenNFT } = cm.creditFacade;
|
|
543
541
|
const minDebt = this.#minDebtMultiplier * cm.creditFacade.minDebt / import_sdk.PERCENTAGE_FACTOR;
|
|
544
|
-
|
|
542
|
+
const claim = claims.get(cm.underlying) ?? 0n;
|
|
543
|
+
claims.upsert(cm.underlying, claim + minDebt * 11n / 10n);
|
|
545
544
|
if ((0, import_viem.isAddress)(degenNFT) && degenNFT !== import_sdk.ADDRESS_0X0) {
|
|
546
545
|
degenNFTS[degenNFT] = (degenNFTS[degenNFT] ?? 0) + 1;
|
|
547
546
|
}
|
|
548
547
|
}
|
|
549
|
-
|
|
550
|
-
await this.#claimFromFaucet(borrower, "borrower", claimUSD);
|
|
548
|
+
await this.#claimFromFaucet(borrower, "borrower", claims);
|
|
551
549
|
for (const [degenNFT, amount] of Object.entries(degenNFTS)) {
|
|
552
550
|
await this.#mintDegenNft(degenNFT, borrower.address, amount);
|
|
553
551
|
}
|
|
554
552
|
this.#logger?.debug("prepared borrower");
|
|
555
553
|
return borrower;
|
|
556
554
|
}
|
|
557
|
-
async #claimFromFaucet(claimer, role,
|
|
555
|
+
async #claimFromFaucet(claimer, role, claims) {
|
|
558
556
|
await (0, import_claimFromFaucet.claimFromFaucet)({
|
|
557
|
+
sdk: this.sdk,
|
|
559
558
|
publicClient: this.#anvil,
|
|
560
559
|
wallet: this.#anvil,
|
|
561
560
|
faucet: this.faucet,
|
|
562
561
|
claimer,
|
|
563
562
|
role,
|
|
564
|
-
|
|
563
|
+
amount: claims.entries().map(([k, v]) => ({ token: k, amount: v })),
|
|
565
564
|
logger: this.#logger
|
|
566
565
|
});
|
|
567
566
|
}
|
|
@@ -697,6 +696,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
697
696
|
value: (0, import_viem.parseEther)("100")
|
|
698
697
|
});
|
|
699
698
|
await (0, import_claimFromFaucet.claimFromFaucet)({
|
|
699
|
+
sdk: this.sdk,
|
|
700
700
|
publicClient: this.#anvil,
|
|
701
701
|
wallet: this.#anvil,
|
|
702
702
|
faucet: this.faucet,
|
|
@@ -27,53 +27,66 @@ var import_sdk = require("../sdk/index.js");
|
|
|
27
27
|
const faucetAbi = (0, import_viem.parseAbi)([
|
|
28
28
|
"function minAmountUSD() external view returns (uint256)",
|
|
29
29
|
"function claim() external",
|
|
30
|
-
"function claim(uint256 amountUSD) external"
|
|
30
|
+
"function claim(uint256 amountUSD) external",
|
|
31
|
+
"function claim((address token, uint256 amount)[] claims) external"
|
|
31
32
|
]);
|
|
32
33
|
async function claimFromFaucet(opts) {
|
|
33
34
|
const {
|
|
35
|
+
sdk,
|
|
34
36
|
publicClient,
|
|
35
37
|
wallet,
|
|
36
38
|
faucet,
|
|
37
39
|
claimer,
|
|
38
40
|
role,
|
|
39
|
-
|
|
41
|
+
amount,
|
|
40
42
|
logger,
|
|
41
43
|
gasMultiplier = 10n
|
|
42
44
|
} = opts;
|
|
43
|
-
let
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
let amnt = "default amount";
|
|
46
|
+
let args;
|
|
47
|
+
if (Array.isArray(amount)) {
|
|
48
|
+
args = [amount];
|
|
49
|
+
amnt = amount.map((v) => {
|
|
50
|
+
try {
|
|
51
|
+
return sdk.tokensMeta.formatBN(v.token, v.amount, { symbol: true });
|
|
52
|
+
} catch {
|
|
53
|
+
return `${v.amount} of ${v.token}`;
|
|
54
|
+
}
|
|
55
|
+
}).join(", ");
|
|
56
|
+
} else if (typeof amount === "bigint") {
|
|
57
|
+
args = [amount];
|
|
58
|
+
amnt = `${(0, import_sdk.formatBN)(args[0], 8)} USD`;
|
|
59
|
+
} else if (typeof amount === "function") {
|
|
60
|
+
const minAmountUSD = await (0, import_actions.readContract)(publicClient, {
|
|
48
61
|
address: faucet,
|
|
49
62
|
abi: faucetAbi,
|
|
50
63
|
functionName: "minAmountUSD"
|
|
51
64
|
});
|
|
52
|
-
logger?.debug(`faucet min amount USD: ${
|
|
53
|
-
|
|
65
|
+
logger?.debug(`faucet min amount USD: ${minAmountUSD}`);
|
|
66
|
+
args = [amount(minAmountUSD)];
|
|
67
|
+
amnt = `${(0, import_sdk.formatBN)(args[0], 8)} USD`;
|
|
68
|
+
} else {
|
|
69
|
+
args = [];
|
|
54
70
|
}
|
|
55
|
-
if (
|
|
71
|
+
if (args[0] === 0n) {
|
|
56
72
|
logger?.debug("amount is 0, skipping claim");
|
|
57
73
|
return;
|
|
58
74
|
}
|
|
59
|
-
const [
|
|
60
|
-
|
|
61
|
-
toClaimUSD ? (0, import_sdk.formatBN)(toClaimUSD, 8) : "default amount"
|
|
62
|
-
];
|
|
63
|
-
logger?.debug(`${usr} claiming ${amnt} USD from faucet`);
|
|
75
|
+
const usr = [role, claimer.address].filter(Boolean).join(" ");
|
|
76
|
+
logger?.debug(`${usr} claiming ${amnt} from faucet`);
|
|
64
77
|
const gas = await publicClient.estimateContractGas({
|
|
65
78
|
account: claimer,
|
|
66
79
|
address: faucet,
|
|
67
80
|
abi: faucetAbi,
|
|
68
81
|
functionName: "claim",
|
|
69
|
-
args
|
|
82
|
+
args
|
|
70
83
|
});
|
|
71
84
|
const hash = await wallet.writeContract({
|
|
72
85
|
account: claimer,
|
|
73
86
|
address: faucet,
|
|
74
87
|
abi: faucetAbi,
|
|
75
88
|
functionName: "claim",
|
|
76
|
-
args
|
|
89
|
+
args,
|
|
77
90
|
chain: wallet.chain,
|
|
78
91
|
gas: gas * gasMultiplier
|
|
79
92
|
});
|
|
@@ -82,12 +95,10 @@ async function claimFromFaucet(opts) {
|
|
|
82
95
|
});
|
|
83
96
|
if (receipt.status === "reverted") {
|
|
84
97
|
throw new Error(
|
|
85
|
-
`${usr} failed to claimed
|
|
98
|
+
`${usr} failed to claimed ${amnt} from faucet, tx: ${hash}`
|
|
86
99
|
);
|
|
87
100
|
}
|
|
88
|
-
logger?.debug(
|
|
89
|
-
`${usr} claimed equivalent of ${amnt} USD from faucet, tx: ${hash}`
|
|
90
|
-
);
|
|
101
|
+
logger?.debug(`${usr} claimed ${amnt} from faucet, tx: ${hash}`);
|
|
91
102
|
}
|
|
92
103
|
// Annotate the CommonJS export names for ESM import in node:
|
|
93
104
|
0 && (module.exports = {
|
|
@@ -13,7 +13,6 @@ import {
|
|
|
13
13
|
AddressMap,
|
|
14
14
|
AddressSet,
|
|
15
15
|
childLogger,
|
|
16
|
-
formatBN,
|
|
17
16
|
MAX_UINT256,
|
|
18
17
|
PERCENTAGE_FACTOR,
|
|
19
18
|
SDKConstruct,
|
|
@@ -376,8 +375,8 @@ class AccountOpener extends SDKConstruct {
|
|
|
376
375
|
`target #${i + 1} (${this.labelAddress(t.target)}) needs ${this.sdk.tokensMeta.formatBN(cm.underlying, amount, { symbol: true })} in pool (leverage: ${Number(leverage / PERCENTAGE_FACTOR)}%)`
|
|
377
376
|
);
|
|
378
377
|
}
|
|
379
|
-
let totalUSD = 0n;
|
|
380
378
|
const deposits = [];
|
|
379
|
+
const claims = new AddressMap();
|
|
381
380
|
for (const [p, minAvailable] of Object.entries(minAvailableByPool)) {
|
|
382
381
|
const market = this.sdk.marketRegister.findByPool(p);
|
|
383
382
|
const pool = market.pool.pool;
|
|
@@ -395,17 +394,15 @@ class AccountOpener extends SDKConstruct {
|
|
|
395
394
|
);
|
|
396
395
|
if (diff > 0n) {
|
|
397
396
|
deposits.push([pool, diff]);
|
|
398
|
-
|
|
397
|
+
let claim = claims.get(pool.underlying) ?? 0n;
|
|
398
|
+
claim += diff * PERCENTAGE_FACTOR / 8950n;
|
|
399
|
+
claims.upsert(pool.underlying, claim);
|
|
399
400
|
}
|
|
400
401
|
}
|
|
401
|
-
totalUSD = totalUSD * PERCENTAGE_FACTOR / 8950n;
|
|
402
|
-
this.#logger?.debug(
|
|
403
|
-
`total USD to claim from faucet: ${formatBN(totalUSD, 8)}`
|
|
404
|
-
);
|
|
405
402
|
const depositor = await this.#getDepositor();
|
|
406
403
|
this.#logger?.debug(`depositor: ${depositor.address}`);
|
|
407
404
|
try {
|
|
408
|
-
await this.#claimFromFaucet(depositor, "depositor",
|
|
405
|
+
await this.#claimFromFaucet(depositor, "depositor", claims);
|
|
409
406
|
} catch (e) {
|
|
410
407
|
if (this.#allowMint) {
|
|
411
408
|
this.#logger?.error(`depositor failed to claim from faucet: ${e}`);
|
|
@@ -522,8 +519,8 @@ class AccountOpener extends SDKConstruct {
|
|
|
522
519
|
*/
|
|
523
520
|
async #prepareBorrower(targets) {
|
|
524
521
|
const borrower = await this.#getBorrower();
|
|
525
|
-
let claimUSD = 0n;
|
|
526
522
|
const degenNFTS = {};
|
|
523
|
+
const claims = new AddressMap();
|
|
527
524
|
for (const target of targets) {
|
|
528
525
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
529
526
|
target.creditManager
|
|
@@ -533,27 +530,28 @@ class AccountOpener extends SDKConstruct {
|
|
|
533
530
|
);
|
|
534
531
|
const { degenNFT } = cm.creditFacade;
|
|
535
532
|
const minDebt = this.#minDebtMultiplier * cm.creditFacade.minDebt / PERCENTAGE_FACTOR;
|
|
536
|
-
|
|
533
|
+
const claim = claims.get(cm.underlying) ?? 0n;
|
|
534
|
+
claims.upsert(cm.underlying, claim + minDebt * 11n / 10n);
|
|
537
535
|
if (isAddress(degenNFT) && degenNFT !== ADDRESS_0X0) {
|
|
538
536
|
degenNFTS[degenNFT] = (degenNFTS[degenNFT] ?? 0) + 1;
|
|
539
537
|
}
|
|
540
538
|
}
|
|
541
|
-
|
|
542
|
-
await this.#claimFromFaucet(borrower, "borrower", claimUSD);
|
|
539
|
+
await this.#claimFromFaucet(borrower, "borrower", claims);
|
|
543
540
|
for (const [degenNFT, amount] of Object.entries(degenNFTS)) {
|
|
544
541
|
await this.#mintDegenNft(degenNFT, borrower.address, amount);
|
|
545
542
|
}
|
|
546
543
|
this.#logger?.debug("prepared borrower");
|
|
547
544
|
return borrower;
|
|
548
545
|
}
|
|
549
|
-
async #claimFromFaucet(claimer, role,
|
|
546
|
+
async #claimFromFaucet(claimer, role, claims) {
|
|
550
547
|
await claimFromFaucet({
|
|
548
|
+
sdk: this.sdk,
|
|
551
549
|
publicClient: this.#anvil,
|
|
552
550
|
wallet: this.#anvil,
|
|
553
551
|
faucet: this.faucet,
|
|
554
552
|
claimer,
|
|
555
553
|
role,
|
|
556
|
-
|
|
554
|
+
amount: claims.entries().map(([k, v]) => ({ token: k, amount: v })),
|
|
557
555
|
logger: this.#logger
|
|
558
556
|
});
|
|
559
557
|
}
|
|
@@ -689,6 +687,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
689
687
|
value: parseEther("100")
|
|
690
688
|
});
|
|
691
689
|
await claimFromFaucet({
|
|
690
|
+
sdk: this.sdk,
|
|
692
691
|
publicClient: this.#anvil,
|
|
693
692
|
wallet: this.#anvil,
|
|
694
693
|
faucet: this.faucet,
|
|
@@ -6,53 +6,66 @@ import { formatBN } from "../sdk/index.js";
|
|
|
6
6
|
const faucetAbi = parseAbi([
|
|
7
7
|
"function minAmountUSD() external view returns (uint256)",
|
|
8
8
|
"function claim() external",
|
|
9
|
-
"function claim(uint256 amountUSD) external"
|
|
9
|
+
"function claim(uint256 amountUSD) external",
|
|
10
|
+
"function claim((address token, uint256 amount)[] claims) external"
|
|
10
11
|
]);
|
|
11
12
|
async function claimFromFaucet(opts) {
|
|
12
13
|
const {
|
|
14
|
+
sdk,
|
|
13
15
|
publicClient,
|
|
14
16
|
wallet,
|
|
15
17
|
faucet,
|
|
16
18
|
claimer,
|
|
17
19
|
role,
|
|
18
|
-
|
|
20
|
+
amount,
|
|
19
21
|
logger,
|
|
20
22
|
gasMultiplier = 10n
|
|
21
23
|
} = opts;
|
|
22
|
-
let
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
let amnt = "default amount";
|
|
25
|
+
let args;
|
|
26
|
+
if (Array.isArray(amount)) {
|
|
27
|
+
args = [amount];
|
|
28
|
+
amnt = amount.map((v) => {
|
|
29
|
+
try {
|
|
30
|
+
return sdk.tokensMeta.formatBN(v.token, v.amount, { symbol: true });
|
|
31
|
+
} catch {
|
|
32
|
+
return `${v.amount} of ${v.token}`;
|
|
33
|
+
}
|
|
34
|
+
}).join(", ");
|
|
35
|
+
} else if (typeof amount === "bigint") {
|
|
36
|
+
args = [amount];
|
|
37
|
+
amnt = `${formatBN(args[0], 8)} USD`;
|
|
38
|
+
} else if (typeof amount === "function") {
|
|
39
|
+
const minAmountUSD = await readContract(publicClient, {
|
|
27
40
|
address: faucet,
|
|
28
41
|
abi: faucetAbi,
|
|
29
42
|
functionName: "minAmountUSD"
|
|
30
43
|
});
|
|
31
|
-
logger?.debug(`faucet min amount USD: ${
|
|
32
|
-
|
|
44
|
+
logger?.debug(`faucet min amount USD: ${minAmountUSD}`);
|
|
45
|
+
args = [amount(minAmountUSD)];
|
|
46
|
+
amnt = `${formatBN(args[0], 8)} USD`;
|
|
47
|
+
} else {
|
|
48
|
+
args = [];
|
|
33
49
|
}
|
|
34
|
-
if (
|
|
50
|
+
if (args[0] === 0n) {
|
|
35
51
|
logger?.debug("amount is 0, skipping claim");
|
|
36
52
|
return;
|
|
37
53
|
}
|
|
38
|
-
const [
|
|
39
|
-
|
|
40
|
-
toClaimUSD ? formatBN(toClaimUSD, 8) : "default amount"
|
|
41
|
-
];
|
|
42
|
-
logger?.debug(`${usr} claiming ${amnt} USD from faucet`);
|
|
54
|
+
const usr = [role, claimer.address].filter(Boolean).join(" ");
|
|
55
|
+
logger?.debug(`${usr} claiming ${amnt} from faucet`);
|
|
43
56
|
const gas = await publicClient.estimateContractGas({
|
|
44
57
|
account: claimer,
|
|
45
58
|
address: faucet,
|
|
46
59
|
abi: faucetAbi,
|
|
47
60
|
functionName: "claim",
|
|
48
|
-
args
|
|
61
|
+
args
|
|
49
62
|
});
|
|
50
63
|
const hash = await wallet.writeContract({
|
|
51
64
|
account: claimer,
|
|
52
65
|
address: faucet,
|
|
53
66
|
abi: faucetAbi,
|
|
54
67
|
functionName: "claim",
|
|
55
|
-
args
|
|
68
|
+
args,
|
|
56
69
|
chain: wallet.chain,
|
|
57
70
|
gas: gas * gasMultiplier
|
|
58
71
|
});
|
|
@@ -61,12 +74,10 @@ async function claimFromFaucet(opts) {
|
|
|
61
74
|
});
|
|
62
75
|
if (receipt.status === "reverted") {
|
|
63
76
|
throw new Error(
|
|
64
|
-
`${usr} failed to claimed
|
|
77
|
+
`${usr} failed to claimed ${amnt} from faucet, tx: ${hash}`
|
|
65
78
|
);
|
|
66
79
|
}
|
|
67
|
-
logger?.debug(
|
|
68
|
-
`${usr} claimed equivalent of ${amnt} USD from faucet, tx: ${hash}`
|
|
69
|
-
);
|
|
80
|
+
logger?.debug(`${usr} claimed ${amnt} from faucet, tx: ${hash}`);
|
|
70
81
|
}
|
|
71
82
|
export {
|
|
72
83
|
claimFromFaucet
|
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
import { type Account, type Address, type PublicClient, type WalletClient } from "viem";
|
|
2
|
-
import { type ILogger } from "../sdk/index.js";
|
|
2
|
+
import { type GearboxSDK, type ILogger } from "../sdk/index.js";
|
|
3
|
+
interface TokenClaim {
|
|
4
|
+
token: Address;
|
|
5
|
+
amount: bigint;
|
|
6
|
+
}
|
|
3
7
|
interface ClaimFromFaucetOptions {
|
|
8
|
+
sdk: GearboxSDK;
|
|
4
9
|
publicClient: PublicClient;
|
|
5
10
|
wallet: WalletClient;
|
|
6
11
|
faucet: Address;
|
|
7
12
|
claimer: Account;
|
|
8
13
|
role?: string;
|
|
9
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Either:
|
|
16
|
+
* - List of tokens with exact amounts to claim
|
|
17
|
+
* - Amount in USD
|
|
18
|
+
* - Function that returns amount in USD
|
|
19
|
+
* - Undefined (will claim default amount)
|
|
20
|
+
*/
|
|
21
|
+
amount?: TokenClaim[] | bigint | ((minAmountUSD: bigint) => bigint);
|
|
10
22
|
gasMultiplier?: bigint;
|
|
11
23
|
logger?: ILogger;
|
|
12
24
|
}
|