@zyfai/sdk 0.12.0 → 0.15.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/README.md +38 -22
- package/dist/index.d.mts +99 -17
- package/dist/index.d.ts +99 -17
- package/dist/index.js +386 -52
- package/dist/index.mjs +364 -24
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -54,6 +54,9 @@ var ENDPOINTS = {
|
|
|
54
54
|
USER_WITHDRAW: "/users/withdraw/v2",
|
|
55
55
|
PARTIAL_WITHDRAW: "/users/partial-withdraw/v2",
|
|
56
56
|
LOG_DEPOSIT: "/users/log_deposit/v2",
|
|
57
|
+
FIRST_PERMIT_DEPOSIT: "/users/me/permit-deposit",
|
|
58
|
+
FIRST_PERMIT_DEPOSIT_PREPARE: "/users/me/permit-deposit/prepare",
|
|
59
|
+
FIRST_PERMIT_DEPOSIT_SUBMIT: (id) => `/users/me/permit-deposit/${encodeURIComponent(id)}/submit`,
|
|
57
60
|
FUNDING_ORDERS: "/users/me/funding-orders",
|
|
58
61
|
FUNDING_ORDER: (orderId) => `/users/me/funding-orders/${encodeURIComponent(orderId)}`,
|
|
59
62
|
FUNDING_ORDER_QUOTE: (orderId) => `/users/me/funding-orders/${encodeURIComponent(orderId)}/quote`,
|
|
@@ -261,8 +264,9 @@ var HttpClient = class {
|
|
|
261
264
|
case 403:
|
|
262
265
|
throw new Error("Forbidden: Access denied");
|
|
263
266
|
case 404:
|
|
264
|
-
throw
|
|
265
|
-
`Not found: ${data.message || "Resource not found"}`
|
|
267
|
+
throw Object.assign(
|
|
268
|
+
new Error(`Not found: ${data.message || "Resource not found"}`),
|
|
269
|
+
{ status }
|
|
266
270
|
);
|
|
267
271
|
case 429:
|
|
268
272
|
throw new Error("Rate limit exceeded. Please try again later.");
|
|
@@ -385,6 +389,20 @@ var HttpClient = class {
|
|
|
385
389
|
};
|
|
386
390
|
|
|
387
391
|
// src/utils/poll.ts
|
|
392
|
+
async function pollUntil(fn, predicate, options) {
|
|
393
|
+
const intervalMs = options?.intervalMs ?? 5e3;
|
|
394
|
+
const timeoutMs = options?.timeoutMs ?? 12e4;
|
|
395
|
+
const label = options?.label ?? "condition";
|
|
396
|
+
const started = Date.now();
|
|
397
|
+
while (Date.now() - started < timeoutMs) {
|
|
398
|
+
const value = await fn();
|
|
399
|
+
if (predicate(value)) {
|
|
400
|
+
return { value, elapsedMs: Date.now() - started };
|
|
401
|
+
}
|
|
402
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
403
|
+
}
|
|
404
|
+
throw new Error(`Timed out waiting for ${label} after ${timeoutMs}ms`);
|
|
405
|
+
}
|
|
388
406
|
function sleep(ms) {
|
|
389
407
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
390
408
|
}
|
|
@@ -617,6 +635,68 @@ var VAULT_ABI = [
|
|
|
617
635
|
}
|
|
618
636
|
];
|
|
619
637
|
var VAULT_ADDRESS = "0xD580071c47d4a667858B5FafAb85BC9C609beC5D";
|
|
638
|
+
var MERKL_DISTRIBUTOR_ADDRESS = "0x3Ef3D8bA38EBe18DB133cEc108f4D14CE00Dd9Ae";
|
|
639
|
+
var RZFI_TOKEN_ADDRESS = "0x1c08C7df416062b472F553DCC1ed3045489520C5";
|
|
640
|
+
var MERKL_DISTRIBUTOR_ABI = parseAbi([
|
|
641
|
+
"function claim(address[] users, address[] tokens, uint256[] amounts, bytes32[][] proofs)",
|
|
642
|
+
"function claimed(address user, address token) view returns (uint208 amount, uint48 timestamp, bytes32 merkleRoot)",
|
|
643
|
+
"function getMerkleRoot() view returns (bytes32)"
|
|
644
|
+
]);
|
|
645
|
+
var SAFE_ABI = parseAbi([
|
|
646
|
+
"function execTransaction(address to, uint256 value, bytes data, uint8 operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address refundReceiver, bytes signatures) payable returns (bool success)",
|
|
647
|
+
"function getOwners() view returns (address[])",
|
|
648
|
+
"function getThreshold() view returns (uint256)"
|
|
649
|
+
]);
|
|
650
|
+
|
|
651
|
+
// src/utils/rzfi-claim.ts
|
|
652
|
+
import {
|
|
653
|
+
encodeFunctionData,
|
|
654
|
+
encodePacked,
|
|
655
|
+
pad,
|
|
656
|
+
zeroAddress
|
|
657
|
+
} from "viem";
|
|
658
|
+
var MERKL_API_URL = "https://api.merkl.xyz/v4";
|
|
659
|
+
async function fetchMerklRzfiReward(safe) {
|
|
660
|
+
const response = await fetch(
|
|
661
|
+
`${MERKL_API_URL}/users/${safe}/rewards?chainId=8453`
|
|
662
|
+
);
|
|
663
|
+
if (!response.ok) {
|
|
664
|
+
throw new Error(`Merkl API error: ${response.status}`);
|
|
665
|
+
}
|
|
666
|
+
const body = await response.json();
|
|
667
|
+
const reward = body[0]?.rewards?.find(
|
|
668
|
+
(r) => r.token.address.toLowerCase() === RZFI_TOKEN_ADDRESS.toLowerCase()
|
|
669
|
+
);
|
|
670
|
+
return reward ? { amount: BigInt(reward.amount), root: reward.root, proofs: reward.proofs } : null;
|
|
671
|
+
}
|
|
672
|
+
function encodeRzfiClaimExecTransaction(params) {
|
|
673
|
+
const { safe, owner, amount, proofs } = params;
|
|
674
|
+
const claimData = encodeFunctionData({
|
|
675
|
+
abi: MERKL_DISTRIBUTOR_ABI,
|
|
676
|
+
functionName: "claim",
|
|
677
|
+
args: [[safe], [RZFI_TOKEN_ADDRESS], [amount], [proofs]]
|
|
678
|
+
});
|
|
679
|
+
const signature = encodePacked(
|
|
680
|
+
["bytes32", "bytes32", "uint8"],
|
|
681
|
+
[pad(owner), pad("0x00"), 1]
|
|
682
|
+
);
|
|
683
|
+
return encodeFunctionData({
|
|
684
|
+
abi: SAFE_ABI,
|
|
685
|
+
functionName: "execTransaction",
|
|
686
|
+
args: [
|
|
687
|
+
MERKL_DISTRIBUTOR_ADDRESS,
|
|
688
|
+
0n,
|
|
689
|
+
claimData,
|
|
690
|
+
0,
|
|
691
|
+
0n,
|
|
692
|
+
0n,
|
|
693
|
+
0n,
|
|
694
|
+
zeroAddress,
|
|
695
|
+
zeroAddress,
|
|
696
|
+
signature
|
|
697
|
+
]
|
|
698
|
+
});
|
|
699
|
+
}
|
|
620
700
|
|
|
621
701
|
// src/config/constants.ts
|
|
622
702
|
var ZYFI_FEE_RATE = 0.1;
|
|
@@ -756,7 +836,8 @@ import {
|
|
|
756
836
|
custom,
|
|
757
837
|
http as http2,
|
|
758
838
|
getAddress as getAddress2,
|
|
759
|
-
|
|
839
|
+
isAddressEqual as isAddressEqual2,
|
|
840
|
+
encodeFunctionData as encodeFunctionData2,
|
|
760
841
|
defineChain
|
|
761
842
|
} from "viem";
|
|
762
843
|
import { execute } from "@relayprotocol/relay-sdk";
|
|
@@ -1074,6 +1155,29 @@ var positiveUnits = (value) => {
|
|
|
1074
1155
|
};
|
|
1075
1156
|
var resolveCollateralMinimum = (storedMinimum, quote) => positiveUnits(storedMinimum) ?? positiveUnits(quote.details?.currencyOut?.minimumAmount) ?? positiveUnits(quote.details?.currencyOut?.amount) ?? "0";
|
|
1076
1157
|
|
|
1158
|
+
// src/deposits/first-permit-deposit.ts
|
|
1159
|
+
import { isAddressEqual } from "viem";
|
|
1160
|
+
var firstPermitTypes = {
|
|
1161
|
+
Permit: [
|
|
1162
|
+
{ name: "owner", type: "address" },
|
|
1163
|
+
{ name: "spender", type: "address" },
|
|
1164
|
+
{ name: "value", type: "uint256" },
|
|
1165
|
+
{ name: "nonce", type: "uint256" },
|
|
1166
|
+
{ name: "deadline", type: "uint256" }
|
|
1167
|
+
]
|
|
1168
|
+
};
|
|
1169
|
+
function matchesFirstPermitDeposit(intent, request) {
|
|
1170
|
+
return intent.chainId === request.chainId && isAddressEqual(intent.token, request.token) && intent.amount === request.amount.toString();
|
|
1171
|
+
}
|
|
1172
|
+
function validateFirstPermit({ domain, message }, request) {
|
|
1173
|
+
const value = BigInt(message.value);
|
|
1174
|
+
const nonce = BigInt(message.nonce);
|
|
1175
|
+
if (domain.chainId !== request.chainId || !isAddressEqual(domain.verifyingContract, request.token) || !isAddressEqual(message.owner, request.owner) || !isAddressEqual(message.spender, request.safe) || value !== request.amount || nonce < 0n || !Number.isSafeInteger(message.deadline) || message.deadline <= Math.floor(Date.now() / 1e3)) {
|
|
1176
|
+
throw new Error("Prepared permit does not match this deposit");
|
|
1177
|
+
}
|
|
1178
|
+
return { ...message, value, nonce, deadline: BigInt(message.deadline) };
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1077
1181
|
// src/deposits/relay-slippage.ts
|
|
1078
1182
|
var DEFAULT_RELAY_SLIPPAGE_BPS = 200;
|
|
1079
1183
|
var MAX_RELAY_SLIPPAGE_BPS = 500;
|
|
@@ -3483,14 +3587,16 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3483
3587
|
return this.httpClient.post(ENDPOINTS.DEPLOY_CHAINS, { chainIds });
|
|
3484
3588
|
}
|
|
3485
3589
|
/**
|
|
3486
|
-
*
|
|
3590
|
+
* Deposit a collateral token the wallet already holds into the Safe.
|
|
3487
3591
|
*
|
|
3488
3592
|
* @remarks
|
|
3489
3593
|
* Use this when the connected wallet holds the strategy collateral on the
|
|
3490
3594
|
* deposit chain. {@link ZyfaiSDK.sendDeposit} calls this automatically in
|
|
3491
3595
|
* that case. A different token uses {@link ZyfaiSDK.sendSwapDeposit}.
|
|
3492
3596
|
*
|
|
3493
|
-
*
|
|
3597
|
+
* Eligible first deposits sign an ERC-2612 permit and the backend sponsors and
|
|
3598
|
+
* registers the pull. Other direct deposits confirm an ERC-20 transfer and
|
|
3599
|
+
* call {@link ZyfaiSDK.logDeposit}.
|
|
3494
3600
|
* Requires {@link ZyfaiSDK.connectAccount}. Pass the **EOA** as `userAddress`.
|
|
3495
3601
|
*
|
|
3496
3602
|
* @group Deposits and withdrawals
|
|
@@ -3514,6 +3620,9 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3514
3620
|
* ```
|
|
3515
3621
|
*/
|
|
3516
3622
|
async sendDirectDeposit(userAddress, chainId, amount, asset, strategy) {
|
|
3623
|
+
return this.sendCollateralDeposit(userAddress, chainId, amount, asset, strategy, true);
|
|
3624
|
+
}
|
|
3625
|
+
async sendCollateralDeposit(userAddress, chainId, amount, asset, strategy, allowPermit) {
|
|
3517
3626
|
try {
|
|
3518
3627
|
if (!userAddress) {
|
|
3519
3628
|
throw new Error("User address is required");
|
|
@@ -3605,6 +3714,16 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3605
3714
|
);
|
|
3606
3715
|
}
|
|
3607
3716
|
}
|
|
3717
|
+
if (allowPermit && this.isPredeployed && this.isConnectedUser(userAddress)) {
|
|
3718
|
+
const sponsored = await this.sendFirstPermitDeposit({
|
|
3719
|
+
owner: getAddress2(userAddress),
|
|
3720
|
+
safe: safeAddress,
|
|
3721
|
+
chainId,
|
|
3722
|
+
token: getAddress2(token),
|
|
3723
|
+
amount: amountBigInt
|
|
3724
|
+
});
|
|
3725
|
+
if (sponsored) return sponsored;
|
|
3726
|
+
}
|
|
3608
3727
|
const txHash = await walletClient.writeContract({
|
|
3609
3728
|
address: token,
|
|
3610
3729
|
abi: erc20TransferWriteAbi(assetSymbol),
|
|
@@ -3637,6 +3756,115 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3637
3756
|
throw new Error(`Deposit failed: ${error.message}`);
|
|
3638
3757
|
}
|
|
3639
3758
|
}
|
|
3759
|
+
/**
|
|
3760
|
+
* Read the current first-deposit permit intent after a pending or interrupted deposit.
|
|
3761
|
+
*
|
|
3762
|
+
* @remarks
|
|
3763
|
+
* Uses the execution API and the SIWE session from {@link ZyfaiSDK.connectAccount}.
|
|
3764
|
+
* A registered intent provides the deposit id for {@link ZyfaiSDK.getDepositStatus}.
|
|
3765
|
+
*
|
|
3766
|
+
* @group Deposits and withdrawals
|
|
3767
|
+
*
|
|
3768
|
+
* @param userAddress - Authenticated EOA
|
|
3769
|
+
* @returns The latest permit intent, or null when no intent exists
|
|
3770
|
+
* @throws If the address differs from the authenticated EOA or the request fails
|
|
3771
|
+
*
|
|
3772
|
+
* @example
|
|
3773
|
+
* ```typescript
|
|
3774
|
+
* const intent = await sdk.getFirstPermitDepositStatus(eoa);
|
|
3775
|
+
* if (intent?.depositId) await sdk.waitForDepositCredit(intent.depositId, intent.chainId);
|
|
3776
|
+
* ```
|
|
3777
|
+
*/
|
|
3778
|
+
async getFirstPermitDepositStatus(userAddress) {
|
|
3779
|
+
if (!this.sessionEoa || !isAddressEqual2(getAddress2(userAddress), this.sessionEoa)) {
|
|
3780
|
+
throw new Error("userAddress does not match authenticated session EOA");
|
|
3781
|
+
}
|
|
3782
|
+
return (await this.getFirstPermitDepositAvailability()).intent;
|
|
3783
|
+
}
|
|
3784
|
+
async getFirstPermitDepositAvailability(chainId) {
|
|
3785
|
+
try {
|
|
3786
|
+
return await this.httpClient.get(
|
|
3787
|
+
ENDPOINTS.FIRST_PERMIT_DEPOSIT,
|
|
3788
|
+
chainId != null ? { params: { chainId } } : void 0
|
|
3789
|
+
);
|
|
3790
|
+
} catch (error) {
|
|
3791
|
+
if (error instanceof Error && "status" in error && error.status === 404) {
|
|
3792
|
+
return { enabledTokens: [], intent: null };
|
|
3793
|
+
}
|
|
3794
|
+
throw error;
|
|
3795
|
+
}
|
|
3796
|
+
}
|
|
3797
|
+
async sendFirstPermitDeposit(request) {
|
|
3798
|
+
const { enabledTokens, intent, permitPreferred } = await this.getFirstPermitDepositAvailability(request.chainId);
|
|
3799
|
+
let id;
|
|
3800
|
+
let accepted;
|
|
3801
|
+
if (intent?.status === "queued" || intent?.status === "submitted") {
|
|
3802
|
+
if (!matchesFirstPermitDeposit(intent, request)) {
|
|
3803
|
+
throw new Error(`First permit deposit ${intent.id} is already pending for a different deposit`);
|
|
3804
|
+
}
|
|
3805
|
+
id = intent.id;
|
|
3806
|
+
accepted = intent;
|
|
3807
|
+
} else {
|
|
3808
|
+
const { owner, chainId, token, amount } = request;
|
|
3809
|
+
if (permitPreferred === false) return null;
|
|
3810
|
+
if (!enabledTokens.includes(`${chainId}:${token.toLowerCase()}`)) return null;
|
|
3811
|
+
const prepared = await this.httpClient.post(
|
|
3812
|
+
ENDPOINTS.FIRST_PERMIT_DEPOSIT_PREPARE,
|
|
3813
|
+
{ owner, chainId, token, amount: amount.toString() }
|
|
3814
|
+
);
|
|
3815
|
+
if (!prepared) return null;
|
|
3816
|
+
const message = validateFirstPermit(prepared, request);
|
|
3817
|
+
const currentWallet = this.getWalletClient();
|
|
3818
|
+
const wallet = currentWallet.chain?.id === chainId ? currentWallet : await this.walletClientForOrigin(chainId);
|
|
3819
|
+
const signature = await wallet.signTypedData({
|
|
3820
|
+
account: wallet.account,
|
|
3821
|
+
domain: prepared.domain,
|
|
3822
|
+
types: firstPermitTypes,
|
|
3823
|
+
primaryType: "Permit",
|
|
3824
|
+
message
|
|
3825
|
+
});
|
|
3826
|
+
id = prepared.id;
|
|
3827
|
+
try {
|
|
3828
|
+
accepted = await this.httpClient.post(
|
|
3829
|
+
ENDPOINTS.FIRST_PERMIT_DEPOSIT_SUBMIT(id),
|
|
3830
|
+
{ signature }
|
|
3831
|
+
);
|
|
3832
|
+
} catch (error) {
|
|
3833
|
+
accepted = await this.getFirstPermitDepositStatus(owner).catch(() => null);
|
|
3834
|
+
if (accepted?.id !== id || accepted.status === "prepared") {
|
|
3835
|
+
throw new Error(
|
|
3836
|
+
`Could not confirm first permit deposit ${id}. Check getFirstPermitDepositStatus before retrying: ${thrownMessage(error)}`
|
|
3837
|
+
);
|
|
3838
|
+
}
|
|
3839
|
+
}
|
|
3840
|
+
}
|
|
3841
|
+
let status = accepted;
|
|
3842
|
+
const { value: registered } = await pollUntil(async () => {
|
|
3843
|
+
const current = status ?? await this.getFirstPermitDepositStatus(request.owner);
|
|
3844
|
+
status = null;
|
|
3845
|
+
if (current?.id !== id || !matchesFirstPermitDeposit(current, request)) {
|
|
3846
|
+
throw new Error(`Unable to resolve first permit deposit ${id}. Check getFirstPermitDepositStatus before retrying.`);
|
|
3847
|
+
}
|
|
3848
|
+
if (current.status === "failed") {
|
|
3849
|
+
throw new Error(`First permit deposit ${id} failed`);
|
|
3850
|
+
}
|
|
3851
|
+
return current;
|
|
3852
|
+
}, (current) => current.status === "registered", {
|
|
3853
|
+
intervalMs: 3e3,
|
|
3854
|
+
label: `first permit deposit ${id} registration. Check getFirstPermitDepositStatus before retrying`
|
|
3855
|
+
});
|
|
3856
|
+
if (!registered.txHash || !registered.depositId) {
|
|
3857
|
+
throw new Error(`First permit deposit ${id} is missing its registration. Check getFirstPermitDepositStatus before retrying.`);
|
|
3858
|
+
}
|
|
3859
|
+
return {
|
|
3860
|
+
success: true,
|
|
3861
|
+
txHash: registered.txHash,
|
|
3862
|
+
smartWallet: request.safe,
|
|
3863
|
+
amount: registered.amount,
|
|
3864
|
+
route: "direct",
|
|
3865
|
+
registration: await this.getDepositStatus(registered.depositId)
|
|
3866
|
+
};
|
|
3867
|
+
}
|
|
3640
3868
|
async sendDeposit(userAddressOrParams, chainId, amount, asset, strategy) {
|
|
3641
3869
|
if (typeof userAddressOrParams !== "string") {
|
|
3642
3870
|
return this.sendRoutedDeposit(userAddressOrParams);
|
|
@@ -3925,12 +4153,13 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
3925
4153
|
BigInt(params.amount)
|
|
3926
4154
|
);
|
|
3927
4155
|
await this.wrapNativeEth(params.chainId, params.amount);
|
|
3928
|
-
const sent = await this.
|
|
4156
|
+
const sent = await this.sendCollateralDeposit(
|
|
3929
4157
|
params.userAddress,
|
|
3930
4158
|
params.chainId,
|
|
3931
4159
|
params.amount,
|
|
3932
4160
|
"WETH",
|
|
3933
|
-
params.strategy
|
|
4161
|
+
params.strategy,
|
|
4162
|
+
false
|
|
3934
4163
|
);
|
|
3935
4164
|
return { ...sent, route: "wrap" };
|
|
3936
4165
|
}
|
|
@@ -4203,7 +4432,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4203
4432
|
safeAddress,
|
|
4204
4433
|
tokenAddress,
|
|
4205
4434
|
to: tokenAddress,
|
|
4206
|
-
data:
|
|
4435
|
+
data: encodeFunctionData2({
|
|
4207
4436
|
abi: ERC20_ABI,
|
|
4208
4437
|
functionName: "transfer",
|
|
4209
4438
|
args: [safeAddress, amountBigInt]
|
|
@@ -4211,6 +4440,91 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
4211
4440
|
value: "0"
|
|
4212
4441
|
};
|
|
4213
4442
|
}
|
|
4443
|
+
/**
|
|
4444
|
+
* Build the rZFI claim as an unsigned transaction for your own signer.
|
|
4445
|
+
*
|
|
4446
|
+
* @remarks
|
|
4447
|
+
* Same pattern as {@link ZyfaiSDK.buildDepositTransfer}: sign and send
|
|
4448
|
+
* `{ to, data, value }` from `userAddress` on Base (chain 8453). It must be
|
|
4449
|
+
* sent by `userAddress` itself; any other sender reverts. The sender pays the
|
|
4450
|
+
* gas. Claims all unclaimed rZFI into the user's Safe.
|
|
4451
|
+
*
|
|
4452
|
+
* @group Rewards
|
|
4453
|
+
*
|
|
4454
|
+
* @param userAddress - Owner EOA, which must send the transaction
|
|
4455
|
+
* @returns Safe address, claimable amount, and the transaction to send
|
|
4456
|
+
*
|
|
4457
|
+
* @throws If there is no rZFI to claim, the reward tree is updating (retry
|
|
4458
|
+
* later), or `userAddress` cannot execute the Safe alone (not an owner, or threshold above 1)
|
|
4459
|
+
*/
|
|
4460
|
+
async buildRzfiClaimTransaction(userAddress) {
|
|
4461
|
+
if (!userAddress) {
|
|
4462
|
+
throw new Error("A user address is required");
|
|
4463
|
+
}
|
|
4464
|
+
const owner = getAddress2(userAddress);
|
|
4465
|
+
const chainId = 8453;
|
|
4466
|
+
await this.hydrateSessionWallet();
|
|
4467
|
+
const safe = await this.getSafeAddressFor(owner, chainId);
|
|
4468
|
+
if (!safe) {
|
|
4469
|
+
throw new Error("Smart wallet address is not available");
|
|
4470
|
+
}
|
|
4471
|
+
const { publicClient } = getChainConfig(chainId, this.rpcUrls);
|
|
4472
|
+
const [owners, threshold] = await Promise.all([
|
|
4473
|
+
publicClient.readContract({
|
|
4474
|
+
address: safe,
|
|
4475
|
+
abi: SAFE_ABI,
|
|
4476
|
+
functionName: "getOwners"
|
|
4477
|
+
}),
|
|
4478
|
+
publicClient.readContract({
|
|
4479
|
+
address: safe,
|
|
4480
|
+
abi: SAFE_ABI,
|
|
4481
|
+
functionName: "getThreshold"
|
|
4482
|
+
})
|
|
4483
|
+
]).catch(() => {
|
|
4484
|
+
throw new Error(`Safe ${safe} is not deployed on Base`);
|
|
4485
|
+
});
|
|
4486
|
+
if (threshold !== 1n || !owners.some((o) => getAddress2(o) === owner)) {
|
|
4487
|
+
throw new Error(
|
|
4488
|
+
`${owner} cannot execute Safe ${safe} alone (not an owner, or threshold above 1)`
|
|
4489
|
+
);
|
|
4490
|
+
}
|
|
4491
|
+
const [reward, [claimed], liveRoot] = await Promise.all([
|
|
4492
|
+
fetchMerklRzfiReward(safe),
|
|
4493
|
+
publicClient.readContract({
|
|
4494
|
+
address: MERKL_DISTRIBUTOR_ADDRESS,
|
|
4495
|
+
abi: MERKL_DISTRIBUTOR_ABI,
|
|
4496
|
+
functionName: "claimed",
|
|
4497
|
+
args: [safe, RZFI_TOKEN_ADDRESS]
|
|
4498
|
+
}),
|
|
4499
|
+
publicClient.readContract({
|
|
4500
|
+
address: MERKL_DISTRIBUTOR_ADDRESS,
|
|
4501
|
+
abi: MERKL_DISTRIBUTOR_ABI,
|
|
4502
|
+
functionName: "getMerkleRoot"
|
|
4503
|
+
})
|
|
4504
|
+
]);
|
|
4505
|
+
const claimable = reward ? reward.amount - claimed : 0n;
|
|
4506
|
+
if (!reward || claimable <= 0n) {
|
|
4507
|
+
throw new Error("No rZFI available to claim");
|
|
4508
|
+
}
|
|
4509
|
+
if (reward.root.toLowerCase() !== liveRoot.toLowerCase()) {
|
|
4510
|
+
throw new Error(
|
|
4511
|
+
"rZFI rewards are being updated on-chain. Retry in a few minutes."
|
|
4512
|
+
);
|
|
4513
|
+
}
|
|
4514
|
+
return {
|
|
4515
|
+
safeAddress: safe,
|
|
4516
|
+
chainId: 8453,
|
|
4517
|
+
claimableAmount: claimable.toString(),
|
|
4518
|
+
to: safe,
|
|
4519
|
+
data: encodeRzfiClaimExecTransaction({
|
|
4520
|
+
safe,
|
|
4521
|
+
owner,
|
|
4522
|
+
amount: reward.amount,
|
|
4523
|
+
proofs: reward.proofs
|
|
4524
|
+
}),
|
|
4525
|
+
value: "0"
|
|
4526
|
+
};
|
|
4527
|
+
}
|
|
4214
4528
|
/**
|
|
4215
4529
|
* Prepare a gas-sponsored rZFI claim into the user's Safe on Base.
|
|
4216
4530
|
*
|
|
@@ -5665,19 +5979,19 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5665
5979
|
}
|
|
5666
5980
|
}
|
|
5667
5981
|
/**
|
|
5668
|
-
* Get
|
|
5982
|
+
* Get balanced (high-risk, high-reward) yield opportunities.
|
|
5669
5983
|
*
|
|
5670
5984
|
* @param chainId - Optional chain ID filter
|
|
5671
5985
|
* @param asset - Optional asset filter (e.g. "USDC", "WETH", "WBTC")
|
|
5672
|
-
* @returns List of
|
|
5986
|
+
* @returns List of balanced opportunities
|
|
5673
5987
|
*
|
|
5674
5988
|
* @example
|
|
5675
5989
|
* ```typescript
|
|
5676
|
-
* const opportunities = await sdk.
|
|
5990
|
+
* const opportunities = await sdk.getBalancedOpportunities(8453, "WETH");
|
|
5677
5991
|
* opportunities.data.forEach(o => console.log(o.protocolName, o.apy));
|
|
5678
5992
|
* ```
|
|
5679
5993
|
*/
|
|
5680
|
-
async
|
|
5994
|
+
async getBalancedOpportunities(chainId, asset, status) {
|
|
5681
5995
|
try {
|
|
5682
5996
|
const response = await this.httpClient.dataGet(
|
|
5683
5997
|
DATA_ENDPOINTS.OPPORTUNITIES_DEGEN(chainId, asset, status)
|
|
@@ -5686,7 +6000,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5686
6000
|
return {
|
|
5687
6001
|
success: true,
|
|
5688
6002
|
chainId,
|
|
5689
|
-
strategyType: "
|
|
6003
|
+
strategyType: "balanced",
|
|
5690
6004
|
data: Array.isArray(data) ? data.map((o) => ({
|
|
5691
6005
|
id: o.id,
|
|
5692
6006
|
protocolId: o.protocol_id || o.protocolId,
|
|
@@ -5697,34 +6011,47 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5697
6011
|
tvl: o.tvl || o.zyfiTvl,
|
|
5698
6012
|
asset: o.asset || o.underlying_token,
|
|
5699
6013
|
risk: o.risk,
|
|
5700
|
-
strategyType: "
|
|
6014
|
+
strategyType: "balanced",
|
|
5701
6015
|
status: o.status
|
|
5702
6016
|
})) : []
|
|
5703
6017
|
};
|
|
5704
6018
|
} catch (error) {
|
|
5705
6019
|
throw new Error(
|
|
5706
|
-
`Failed to get
|
|
6020
|
+
`Failed to get balanced opportunities: ${error.message}`
|
|
5707
6021
|
);
|
|
5708
6022
|
}
|
|
5709
6023
|
}
|
|
5710
6024
|
/**
|
|
5711
|
-
*
|
|
5712
|
-
* `
|
|
6025
|
+
* @deprecated Use {@link getBalancedOpportunities} instead. Retains the
|
|
6026
|
+
* pre-rename `getAggressiveOpportunities` entry point; same HTTP request as
|
|
6027
|
+
* balanced, with legacy `strategyType: "aggressive"` on the response and rows.
|
|
6028
|
+
*/
|
|
6029
|
+
async getAggressiveOpportunities(chainId, asset, status) {
|
|
6030
|
+
const result = await this.getBalancedOpportunities(chainId, asset, status);
|
|
6031
|
+
return {
|
|
6032
|
+
...result,
|
|
6033
|
+
strategyType: "aggressive",
|
|
6034
|
+
data: result.data.map((o) => ({ ...o, strategyType: "aggressive" }))
|
|
6035
|
+
};
|
|
6036
|
+
}
|
|
6037
|
+
/**
|
|
6038
|
+
* Get advanced yield opportunities — pools reachable only with the
|
|
6039
|
+
* `"advanced"` strategy, including protocols whose withdrawals are
|
|
5713
6040
|
* asynchronous (Ipor, Superform). Exiting these pools takes about a day
|
|
5714
6041
|
* on Ipor and three on Superform; track the redemption through
|
|
5715
6042
|
* `getPortfolio().portfolio.pendingAsyncWithdrawals`.
|
|
5716
6043
|
*
|
|
5717
6044
|
* @param chainId - Optional chain ID filter
|
|
5718
6045
|
* @param asset - Optional asset filter (e.g. "USDC", "WETH")
|
|
5719
|
-
* @returns List of
|
|
6046
|
+
* @returns List of advanced opportunities
|
|
5720
6047
|
*
|
|
5721
6048
|
* @example
|
|
5722
6049
|
* ```typescript
|
|
5723
|
-
* const opportunities = await sdk.
|
|
6050
|
+
* const opportunities = await sdk.getAdvancedOpportunities(8453);
|
|
5724
6051
|
* opportunities.data.forEach(o => console.log(o.protocolName, o.apy));
|
|
5725
6052
|
* ```
|
|
5726
6053
|
*/
|
|
5727
|
-
async
|
|
6054
|
+
async getAdvancedOpportunities(chainId, asset, status) {
|
|
5728
6055
|
try {
|
|
5729
6056
|
const response = await this.httpClient.dataGet(
|
|
5730
6057
|
DATA_ENDPOINTS.OPPORTUNITIES_ASYNC(chainId, asset, status)
|
|
@@ -5733,7 +6060,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5733
6060
|
return {
|
|
5734
6061
|
success: true,
|
|
5735
6062
|
chainId,
|
|
5736
|
-
strategyType: "
|
|
6063
|
+
strategyType: "advanced",
|
|
5737
6064
|
data: Array.isArray(data) ? data.map((o) => ({
|
|
5738
6065
|
id: o.id,
|
|
5739
6066
|
protocolId: o.protocol_id || o.protocolId,
|
|
@@ -5744,16 +6071,29 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
5744
6071
|
tvl: o.tvl || o.zyfiTvl,
|
|
5745
6072
|
asset: o.asset || o.underlying_token,
|
|
5746
6073
|
risk: o.risk,
|
|
5747
|
-
strategyType: "
|
|
6074
|
+
strategyType: "advanced",
|
|
5748
6075
|
status: o.status
|
|
5749
6076
|
})) : []
|
|
5750
6077
|
};
|
|
5751
6078
|
} catch (error) {
|
|
5752
6079
|
throw new Error(
|
|
5753
|
-
`Failed to get
|
|
6080
|
+
`Failed to get advanced opportunities: ${error.message}`
|
|
5754
6081
|
);
|
|
5755
6082
|
}
|
|
5756
6083
|
}
|
|
6084
|
+
/**
|
|
6085
|
+
* @deprecated Use {@link getAdvancedOpportunities} instead. Retains the
|
|
6086
|
+
* pre-rename `getAsyncOpportunities` entry point; same HTTP request as
|
|
6087
|
+
* advanced, with legacy `strategyType: "yieldmaxxing"` on the response and rows.
|
|
6088
|
+
*/
|
|
6089
|
+
async getAsyncOpportunities(chainId, asset, status) {
|
|
6090
|
+
const result = await this.getAdvancedOpportunities(chainId, asset, status);
|
|
6091
|
+
return {
|
|
6092
|
+
...result,
|
|
6093
|
+
strategyType: "yieldmaxxing",
|
|
6094
|
+
data: result.data.map((o) => ({ ...o, strategyType: "yieldmaxxing" }))
|
|
6095
|
+
};
|
|
6096
|
+
}
|
|
5757
6097
|
/**
|
|
5758
6098
|
* Get active conservative opportunities (status = "live") with risk and utilization data
|
|
5759
6099
|
* Returns pool info, liquidity depth (true if > 1M), utilization rate, stability metrics, avg APY, and collateral
|
|
@@ -6424,7 +6764,7 @@ var _ZyfaiSDK = class _ZyfaiSDK {
|
|
|
6424
6764
|
if (!tokenUriResponse.tokenUri) {
|
|
6425
6765
|
throw new Error("API did not return a tokenUri");
|
|
6426
6766
|
}
|
|
6427
|
-
const callData =
|
|
6767
|
+
const callData = encodeFunctionData2({
|
|
6428
6768
|
abi: IDENTITY_REGISTRY_ABI,
|
|
6429
6769
|
functionName: "register",
|
|
6430
6770
|
args: [tokenUriResponse.tokenUri]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zyfai/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "TypeScript SDK for self-custodial Zyfai yield agents: discover DeFi opportunities and manage deposits, withdrawals, positions, and strategies across Ethereum, Base, and Arbitrum",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|