@agether/sdk 2.3.4 → 2.4.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/cli.d.ts +27 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +341 -1
- package/dist/clients/AgentIdentityClient.d.ts +188 -0
- package/dist/clients/AgentIdentityClient.d.ts.map +1 -0
- package/dist/clients/AgentIdentityClient.js +337 -0
- package/dist/clients/AgetherClient.d.ts +74 -0
- package/dist/clients/AgetherClient.d.ts.map +1 -0
- package/dist/clients/AgetherClient.js +172 -0
- package/dist/clients/MorphoClient.d.ts +482 -0
- package/dist/clients/MorphoClient.d.ts.map +1 -0
- package/dist/clients/MorphoClient.js +1717 -0
- package/dist/clients/ScoringClient.d.ts +89 -0
- package/dist/clients/ScoringClient.d.ts.map +1 -0
- package/dist/clients/ScoringClient.js +93 -0
- package/dist/clients/X402Client.d.ts +168 -0
- package/dist/clients/X402Client.d.ts.map +1 -0
- package/dist/clients/X402Client.js +378 -0
- package/dist/index.d.mts +87 -1
- package/dist/index.d.ts +87 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +341 -1
- package/dist/index.mjs +341 -1
- package/dist/types/index.d.ts +132 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +46 -0
- package/dist/utils/abis.d.ts +29 -0
- package/dist/utils/abis.d.ts.map +1 -0
- package/dist/utils/abis.js +139 -0
- package/dist/utils/config.d.ts +36 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +168 -0
- package/dist/utils/format.d.ts +44 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/format.js +75 -0
- package/package.json +1 -1
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Agether CLI — Direct Morpho Blue Credit for AI Agents
|
|
4
|
+
*
|
|
5
|
+
* Architecture (v2 — Safe + Safe7579):
|
|
6
|
+
* - All commands sign transactions directly with the agent's private key
|
|
7
|
+
* - Uses MorphoClient for lending (ERC-4337 UserOps through Safe account)
|
|
8
|
+
* - Uses X402Client for paid API calls
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* agether init <private-key> [--agent-id <id>] Initialize
|
|
12
|
+
* agether register [--name <n>] Register ERC-8004 + Safe account
|
|
13
|
+
* agether balance Check balances
|
|
14
|
+
* agether status Show Morpho positions
|
|
15
|
+
* agether score Get credit score (x402-gated)
|
|
16
|
+
* agether markets List Morpho markets
|
|
17
|
+
* agether deposit --amount 0.05 --token WETH Deposit collateral
|
|
18
|
+
* agether borrow --amount 100 Borrow USDC
|
|
19
|
+
* agether deposit-and-borrow --amount 0.05 --token WETH --borrow 100
|
|
20
|
+
* agether repay --amount 50 Repay USDC
|
|
21
|
+
* agether withdraw --amount 0.05 --token WETH Withdraw collateral
|
|
22
|
+
* agether sponsor --amount 0.05 --token WETH --agent-id 123
|
|
23
|
+
* agether fund --amount 50 Fund Safe account with USDC
|
|
24
|
+
* agether x402 <url> [--method GET|POST] [--body <json>]
|
|
25
|
+
*/
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;;;;;;;;;GAuBG"}
|
package/dist/cli.js
CHANGED
|
@@ -870,7 +870,259 @@ var init_MorphoClient = __esm({
|
|
|
870
870
|
};
|
|
871
871
|
}
|
|
872
872
|
// ════════════════════════════════════════════════════════
|
|
873
|
-
// Lending
|
|
873
|
+
// Supply-Side (Lending) — earn yield by supplying USDC
|
|
874
|
+
// ════════════════════════════════════════════════════════
|
|
875
|
+
/**
|
|
876
|
+
* Supply USDC to a Morpho Blue market as a lender (earn yield).
|
|
877
|
+
*
|
|
878
|
+
* Unlike `supplyCollateral` (borrower-side), this is the **lender-side**:
|
|
879
|
+
* you deposit the loanToken (USDC) into the market's supply pool and earn
|
|
880
|
+
* interest paid by borrowers.
|
|
881
|
+
*
|
|
882
|
+
* @param usdcAmount - Amount of USDC to supply (e.g. '500')
|
|
883
|
+
* @param collateralSymbol - Market collateral token to identify which market (e.g. 'WETH')
|
|
884
|
+
* Optional — defaults to highest-APY market
|
|
885
|
+
*/
|
|
886
|
+
async supplyAsset(usdcAmount, collateralSymbol) {
|
|
887
|
+
const acctAddr = await this.getAccountAddress();
|
|
888
|
+
const amount = import_ethers.ethers.parseUnits(usdcAmount, 6);
|
|
889
|
+
const morphoAddr = this.config.contracts.morphoBlue;
|
|
890
|
+
const usdcAddr = this.config.contracts.usdc;
|
|
891
|
+
let params;
|
|
892
|
+
let usedCollateral;
|
|
893
|
+
if (collateralSymbol) {
|
|
894
|
+
params = await this.findMarketForCollateral(collateralSymbol);
|
|
895
|
+
usedCollateral = collateralSymbol;
|
|
896
|
+
} else {
|
|
897
|
+
const rates = await this.getMarketRates();
|
|
898
|
+
if (rates.length === 0) throw new AgetherError("No markets available", "NO_MARKETS");
|
|
899
|
+
const best = rates.reduce((a, b) => a.supplyApy > b.supplyApy ? a : b);
|
|
900
|
+
params = await this.findMarketForCollateral(best.collateralToken);
|
|
901
|
+
usedCollateral = best.collateralToken;
|
|
902
|
+
}
|
|
903
|
+
const marketId = import_ethers.ethers.keccak256(
|
|
904
|
+
import_ethers.ethers.AbiCoder.defaultAbiCoder().encode(
|
|
905
|
+
["address", "address", "address", "address", "uint256"],
|
|
906
|
+
[params.loanToken, params.collateralToken, params.oracle, params.irm, params.lltv]
|
|
907
|
+
)
|
|
908
|
+
);
|
|
909
|
+
const usdcContract = new import_ethers.Contract(usdcAddr, ERC20_ABI, this._signer);
|
|
910
|
+
const acctBalance = await usdcContract.balanceOf(acctAddr);
|
|
911
|
+
if (acctBalance < amount) {
|
|
912
|
+
const shortfall = amount - acctBalance;
|
|
913
|
+
const eoaBalance = await usdcContract.balanceOf(await this.getSignerAddress());
|
|
914
|
+
if (eoaBalance < shortfall) {
|
|
915
|
+
throw new AgetherError(
|
|
916
|
+
`Insufficient USDC. Need ${usdcAmount}, AgentAccount has ${import_ethers.ethers.formatUnits(acctBalance, 6)}, EOA has ${import_ethers.ethers.formatUnits(eoaBalance, 6)}.`,
|
|
917
|
+
"INSUFFICIENT_BALANCE"
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
const transferTx = await usdcContract.transfer(acctAddr, shortfall);
|
|
921
|
+
await transferTx.wait();
|
|
922
|
+
this._refreshSigner();
|
|
923
|
+
}
|
|
924
|
+
const targets = [usdcAddr, morphoAddr];
|
|
925
|
+
const values = [0n, 0n];
|
|
926
|
+
const datas = [
|
|
927
|
+
erc20Iface.encodeFunctionData("approve", [morphoAddr, amount]),
|
|
928
|
+
morphoIface.encodeFunctionData("supply", [
|
|
929
|
+
this._toTuple(params),
|
|
930
|
+
amount,
|
|
931
|
+
0n,
|
|
932
|
+
acctAddr,
|
|
933
|
+
"0x"
|
|
934
|
+
])
|
|
935
|
+
];
|
|
936
|
+
const receipt = await this.batch(targets, values, datas);
|
|
937
|
+
return {
|
|
938
|
+
tx: receipt.hash,
|
|
939
|
+
amount: usdcAmount,
|
|
940
|
+
marketId,
|
|
941
|
+
collateralToken: usedCollateral,
|
|
942
|
+
agentAccount: acctAddr
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* Withdraw supplied USDC (+ earned interest) from a Morpho Blue market.
|
|
947
|
+
*
|
|
948
|
+
* @param usdcAmount - Amount to withdraw (e.g. '100' or 'all' for full position)
|
|
949
|
+
* @param collateralSymbol - Market collateral to identify which market
|
|
950
|
+
* @param receiver - Destination address (defaults to EOA)
|
|
951
|
+
*/
|
|
952
|
+
async withdrawSupply(usdcAmount, collateralSymbol, receiver) {
|
|
953
|
+
const acctAddr = await this.getAccountAddress();
|
|
954
|
+
const morphoAddr = this.config.contracts.morphoBlue;
|
|
955
|
+
const dest = receiver || await this.getSignerAddress();
|
|
956
|
+
let params;
|
|
957
|
+
if (collateralSymbol) {
|
|
958
|
+
params = await this.findMarketForCollateral(collateralSymbol);
|
|
959
|
+
} else {
|
|
960
|
+
const { params: p } = await this._findActiveSupplyMarket();
|
|
961
|
+
params = p;
|
|
962
|
+
}
|
|
963
|
+
const marketId = import_ethers.ethers.keccak256(
|
|
964
|
+
import_ethers.ethers.AbiCoder.defaultAbiCoder().encode(
|
|
965
|
+
["address", "address", "address", "address", "uint256"],
|
|
966
|
+
[params.loanToken, params.collateralToken, params.oracle, params.irm, params.lltv]
|
|
967
|
+
)
|
|
968
|
+
);
|
|
969
|
+
let withdrawAssets;
|
|
970
|
+
let withdrawShares;
|
|
971
|
+
if (usdcAmount === "all") {
|
|
972
|
+
const pos = await this.morphoBlue.position(marketId, acctAddr);
|
|
973
|
+
withdrawShares = BigInt(pos.supplyShares);
|
|
974
|
+
withdrawAssets = 0n;
|
|
975
|
+
if (withdrawShares === 0n) throw new AgetherError("No supply position to withdraw", "NO_SUPPLY");
|
|
976
|
+
} else {
|
|
977
|
+
withdrawAssets = import_ethers.ethers.parseUnits(usdcAmount, 6);
|
|
978
|
+
withdrawShares = 0n;
|
|
979
|
+
}
|
|
980
|
+
const data = morphoIface.encodeFunctionData("withdraw", [
|
|
981
|
+
this._toTuple(params),
|
|
982
|
+
withdrawAssets,
|
|
983
|
+
withdrawShares,
|
|
984
|
+
acctAddr,
|
|
985
|
+
dest
|
|
986
|
+
]);
|
|
987
|
+
const receipt = await this.exec(morphoAddr, data);
|
|
988
|
+
let remainingSupply = "0";
|
|
989
|
+
try {
|
|
990
|
+
const pos = await this.morphoBlue.position(marketId, acctAddr);
|
|
991
|
+
const mkt = await this.morphoBlue.market(marketId);
|
|
992
|
+
const totalSupplyAssets = BigInt(mkt.totalSupplyAssets);
|
|
993
|
+
const totalSupplyShares = BigInt(mkt.totalSupplyShares);
|
|
994
|
+
const currentAssets = totalSupplyShares > 0n ? BigInt(pos.supplyShares) * totalSupplyAssets / totalSupplyShares : 0n;
|
|
995
|
+
remainingSupply = import_ethers.ethers.formatUnits(currentAssets, 6);
|
|
996
|
+
} catch (e) {
|
|
997
|
+
console.warn("[agether] failed to read remaining supply:", e instanceof Error ? e.message : e);
|
|
998
|
+
}
|
|
999
|
+
return {
|
|
1000
|
+
tx: receipt.hash,
|
|
1001
|
+
amount: usdcAmount,
|
|
1002
|
+
remainingSupply,
|
|
1003
|
+
destination: dest
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Get supply (lending) positions with yield tracking.
|
|
1008
|
+
*
|
|
1009
|
+
* Uses Morpho GraphQL API (no eth_getLogs / no DB / no indexer):
|
|
1010
|
+
* 1. `userByAddress` → all market positions with current supplyAssets, supplyApy
|
|
1011
|
+
* 2. `transactions` → all MarketSupply/MarketWithdraw history for net deposited
|
|
1012
|
+
* 3. earnedYield = currentSupplyAssets − netDeposited
|
|
1013
|
+
*
|
|
1014
|
+
* @param collateralSymbol - Market collateral token (optional, returns all if omitted)
|
|
1015
|
+
*/
|
|
1016
|
+
async getSupplyPositions(collateralSymbol) {
|
|
1017
|
+
const acctAddr = (await this.getAccountAddress()).toLowerCase();
|
|
1018
|
+
const chainId = this.config.chainId;
|
|
1019
|
+
const positionsQuery = `{
|
|
1020
|
+
userByAddress(address: "${acctAddr}", chainId: ${chainId}) {
|
|
1021
|
+
marketPositions {
|
|
1022
|
+
market {
|
|
1023
|
+
uniqueKey
|
|
1024
|
+
loanAsset { symbol address decimals }
|
|
1025
|
+
collateralAsset { symbol address }
|
|
1026
|
+
state { supplyApy }
|
|
1027
|
+
}
|
|
1028
|
+
state {
|
|
1029
|
+
supplyShares
|
|
1030
|
+
supplyAssets
|
|
1031
|
+
supplyAssetsUsd
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
}`;
|
|
1036
|
+
const posResp = await import_axios.default.post(MORPHO_API_URL, { query: positionsQuery }, { timeout: 15e3 });
|
|
1037
|
+
const user = posResp.data?.data?.userByAddress;
|
|
1038
|
+
if (!user?.marketPositions) return [];
|
|
1039
|
+
const activePositions = user.marketPositions.filter(
|
|
1040
|
+
(p) => p.state && BigInt(p.state.supplyShares ?? "0") > 0n
|
|
1041
|
+
);
|
|
1042
|
+
if (activePositions.length === 0) return [];
|
|
1043
|
+
const filtered = collateralSymbol ? activePositions.filter((p) => {
|
|
1044
|
+
const sym = p.market.collateralAsset?.symbol;
|
|
1045
|
+
return sym && sym.toUpperCase() === collateralSymbol.toUpperCase();
|
|
1046
|
+
}) : activePositions;
|
|
1047
|
+
if (filtered.length === 0) return [];
|
|
1048
|
+
const netDepositedMap = await this._computeNetDepositedAll(acctAddr, chainId);
|
|
1049
|
+
const results = [];
|
|
1050
|
+
for (const p of filtered) {
|
|
1051
|
+
const currentAssets = BigInt(p.state.supplyAssets ?? "0");
|
|
1052
|
+
const marketKey = p.market.uniqueKey.toLowerCase();
|
|
1053
|
+
const netDeposited = netDepositedMap.get(marketKey) ?? 0n;
|
|
1054
|
+
const earnedYield = currentAssets > netDeposited ? currentAssets - netDeposited : 0n;
|
|
1055
|
+
results.push({
|
|
1056
|
+
marketId: p.market.uniqueKey,
|
|
1057
|
+
loanToken: p.market.loanAsset.symbol,
|
|
1058
|
+
collateralToken: p.market.collateralAsset?.symbol ?? "none",
|
|
1059
|
+
supplyShares: p.state.supplyShares,
|
|
1060
|
+
suppliedAssets: import_ethers.ethers.formatUnits(currentAssets, p.market.loanAsset.decimals),
|
|
1061
|
+
netDeposited: import_ethers.ethers.formatUnits(netDeposited, p.market.loanAsset.decimals),
|
|
1062
|
+
earnedYield: import_ethers.ethers.formatUnits(earnedYield, p.market.loanAsset.decimals),
|
|
1063
|
+
supplyApy: p.market.state?.supplyApy ?? 0
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
return results;
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* Pay a recipient using ONLY earned yield from a supply position.
|
|
1070
|
+
*
|
|
1071
|
+
* Computes available yield, verifies the requested amount doesn't exceed it,
|
|
1072
|
+
* then withdraws from the supply position and sends directly to the recipient.
|
|
1073
|
+
*
|
|
1074
|
+
* @param recipient - Address to receive the USDC
|
|
1075
|
+
* @param usdcAmount - Amount to pay from yield (e.g. '5.50')
|
|
1076
|
+
* @param collateralSymbol - Market collateral to identify which supply position
|
|
1077
|
+
*/
|
|
1078
|
+
async payFromYield(recipient, usdcAmount, collateralSymbol) {
|
|
1079
|
+
const acctAddr = await this.getAccountAddress();
|
|
1080
|
+
const morphoAddr = this.config.contracts.morphoBlue;
|
|
1081
|
+
const amount = import_ethers.ethers.parseUnits(usdcAmount, 6);
|
|
1082
|
+
const positions = await this.getSupplyPositions(collateralSymbol);
|
|
1083
|
+
if (positions.length === 0) {
|
|
1084
|
+
throw new AgetherError("No supply position found", "NO_SUPPLY");
|
|
1085
|
+
}
|
|
1086
|
+
const pos = positions.reduce(
|
|
1087
|
+
(a, b) => parseFloat(a.earnedYield) > parseFloat(b.earnedYield) ? a : b
|
|
1088
|
+
);
|
|
1089
|
+
const availableYield = import_ethers.ethers.parseUnits(pos.earnedYield, 6);
|
|
1090
|
+
if (amount > availableYield) {
|
|
1091
|
+
throw new AgetherError(
|
|
1092
|
+
`Requested ${usdcAmount} USDC exceeds available yield of ${pos.earnedYield} USDC. Use withdrawSupply to withdraw principal.`,
|
|
1093
|
+
"EXCEEDS_YIELD"
|
|
1094
|
+
);
|
|
1095
|
+
}
|
|
1096
|
+
const params = await this.findMarketForCollateral(pos.collateralToken);
|
|
1097
|
+
const data = morphoIface.encodeFunctionData("withdraw", [
|
|
1098
|
+
this._toTuple(params),
|
|
1099
|
+
amount,
|
|
1100
|
+
0n,
|
|
1101
|
+
acctAddr,
|
|
1102
|
+
recipient
|
|
1103
|
+
]);
|
|
1104
|
+
const receipt = await this.exec(morphoAddr, data);
|
|
1105
|
+
let remainingYield = "0";
|
|
1106
|
+
let remainingSupply = "0";
|
|
1107
|
+
try {
|
|
1108
|
+
const updatedPositions = await this.getSupplyPositions(pos.collateralToken);
|
|
1109
|
+
if (updatedPositions.length > 0) {
|
|
1110
|
+
remainingYield = updatedPositions[0].earnedYield;
|
|
1111
|
+
remainingSupply = updatedPositions[0].suppliedAssets;
|
|
1112
|
+
}
|
|
1113
|
+
} catch (e) {
|
|
1114
|
+
console.warn("[agether] failed to read remaining yield:", e instanceof Error ? e.message : e);
|
|
1115
|
+
}
|
|
1116
|
+
return {
|
|
1117
|
+
tx: receipt.hash,
|
|
1118
|
+
yieldWithdrawn: usdcAmount,
|
|
1119
|
+
recipient,
|
|
1120
|
+
remainingYield,
|
|
1121
|
+
remainingSupply
|
|
1122
|
+
};
|
|
1123
|
+
}
|
|
1124
|
+
// ════════════════════════════════════════════════════════
|
|
1125
|
+
// Collateral & Borrowing Operations (all via AgentAccount)
|
|
874
1126
|
// ════════════════════════════════════════════════════════
|
|
875
1127
|
/**
|
|
876
1128
|
* Deposit collateral into Morpho Blue.
|
|
@@ -1501,6 +1753,94 @@ var init_MorphoClient = __esm({
|
|
|
1501
1753
|
const params = await this.findMarketForCollateral("WETH");
|
|
1502
1754
|
return { params, symbol: "WETH" };
|
|
1503
1755
|
}
|
|
1756
|
+
/** Find the first market where the agent has a supply (lending) position. */
|
|
1757
|
+
async _findActiveSupplyMarket() {
|
|
1758
|
+
const acctAddr = await this.getAccountAddress();
|
|
1759
|
+
const markets = await this.getMarkets();
|
|
1760
|
+
for (const m of markets) {
|
|
1761
|
+
if (!m.collateralAsset || m.collateralAsset.address === import_ethers.ethers.ZeroAddress) continue;
|
|
1762
|
+
try {
|
|
1763
|
+
const pos = await this.morphoBlue.position(m.uniqueKey, acctAddr);
|
|
1764
|
+
if (BigInt(pos.supplyShares) > 0n) {
|
|
1765
|
+
return {
|
|
1766
|
+
params: {
|
|
1767
|
+
loanToken: m.loanAsset.address,
|
|
1768
|
+
collateralToken: m.collateralAsset.address,
|
|
1769
|
+
oracle: m.oracle,
|
|
1770
|
+
irm: m.irm,
|
|
1771
|
+
lltv: m.lltv
|
|
1772
|
+
},
|
|
1773
|
+
symbol: m.collateralAsset.symbol
|
|
1774
|
+
};
|
|
1775
|
+
}
|
|
1776
|
+
} catch (e) {
|
|
1777
|
+
console.warn("[agether] _findActiveSupplyMarket position check failed:", e instanceof Error ? e.message : e);
|
|
1778
|
+
continue;
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
throw new AgetherError("No active supply position found", "NO_SUPPLY");
|
|
1782
|
+
}
|
|
1783
|
+
/**
|
|
1784
|
+
* Compute net deposited amounts per market using Morpho GraphQL API.
|
|
1785
|
+
*
|
|
1786
|
+
* Fetches all MarketSupply and MarketWithdraw transactions for the account,
|
|
1787
|
+
* then computes: netDeposited[marketId] = Σ Supply.assets − Σ Withdraw.assets
|
|
1788
|
+
*
|
|
1789
|
+
* Returns a Map<marketId (lowercase), bigint>.
|
|
1790
|
+
*
|
|
1791
|
+
* Uses pagination (100 per page) for completeness, though agent accounts
|
|
1792
|
+
* typically have single-digit transaction counts.
|
|
1793
|
+
*/
|
|
1794
|
+
async _computeNetDepositedAll(accountAddr, chainId) {
|
|
1795
|
+
const result = /* @__PURE__ */ new Map();
|
|
1796
|
+
let skip = 0;
|
|
1797
|
+
const pageSize = 100;
|
|
1798
|
+
let hasMore = true;
|
|
1799
|
+
while (hasMore) {
|
|
1800
|
+
const txQuery = `{
|
|
1801
|
+
transactions(
|
|
1802
|
+
first: ${pageSize}
|
|
1803
|
+
skip: ${skip}
|
|
1804
|
+
where: {
|
|
1805
|
+
userAddress_in: ["${accountAddr}"]
|
|
1806
|
+
type_in: [MarketSupply, MarketWithdraw]
|
|
1807
|
+
chainId_in: [${chainId}]
|
|
1808
|
+
}
|
|
1809
|
+
) {
|
|
1810
|
+
pageInfo { count countTotal }
|
|
1811
|
+
items {
|
|
1812
|
+
type
|
|
1813
|
+
data {
|
|
1814
|
+
... on MarketTransferTransactionData {
|
|
1815
|
+
assets
|
|
1816
|
+
market { uniqueKey }
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
}`;
|
|
1822
|
+
const resp = await import_axios.default.post(MORPHO_API_URL, { query: txQuery }, { timeout: 15e3 });
|
|
1823
|
+
const txData = resp.data?.data?.transactions;
|
|
1824
|
+
if (!txData?.items) break;
|
|
1825
|
+
for (const tx of txData.items) {
|
|
1826
|
+
const marketKey = tx.data?.market?.uniqueKey?.toLowerCase();
|
|
1827
|
+
if (!marketKey || !tx.data?.assets) continue;
|
|
1828
|
+
const assets = BigInt(tx.data.assets);
|
|
1829
|
+
const current = result.get(marketKey) ?? 0n;
|
|
1830
|
+
if (tx.type === "MarketSupply") {
|
|
1831
|
+
result.set(marketKey, current + assets);
|
|
1832
|
+
} else if (tx.type === "MarketWithdraw") {
|
|
1833
|
+
const newVal = current - assets;
|
|
1834
|
+
result.set(marketKey, newVal > 0n ? newVal : 0n);
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
const fetched = skip + txData.items.length;
|
|
1838
|
+
const total = txData.pageInfo?.countTotal ?? 0;
|
|
1839
|
+
hasMore = fetched < total;
|
|
1840
|
+
skip += pageSize;
|
|
1841
|
+
}
|
|
1842
|
+
return result;
|
|
1843
|
+
}
|
|
1504
1844
|
};
|
|
1505
1845
|
}
|
|
1506
1846
|
});
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentIdentityClient - Integration with ag0 (ERC-8004)
|
|
3
|
+
*
|
|
4
|
+
* ERC-8004 Contract Addresses:
|
|
5
|
+
* - Sepolia IdentityRegistry: 0x8004A818BFB912233c491871b3d84c89A494BD9e
|
|
6
|
+
* - Sepolia ReputationRegistry: 0x8004B663056A597Dffe9eCcC1965A193B7388713
|
|
7
|
+
*
|
|
8
|
+
* SDKs:
|
|
9
|
+
* - TypeScript: https://github.com/agent0lab/agent0-ts
|
|
10
|
+
* - Python: https://github.com/agent0lab/agent0-py
|
|
11
|
+
*
|
|
12
|
+
* Docs: https://sdk.ag0.xyz/docs
|
|
13
|
+
*/
|
|
14
|
+
import { Signer } from 'ethers';
|
|
15
|
+
import { AgetherConfig } from '../types';
|
|
16
|
+
export declare const ERC8004_SEPOLIA: {
|
|
17
|
+
identityRegistry: string;
|
|
18
|
+
reputationRegistry: string;
|
|
19
|
+
};
|
|
20
|
+
export interface AgentIdentityClientOptions {
|
|
21
|
+
config: AgetherConfig;
|
|
22
|
+
signer: Signer;
|
|
23
|
+
/** Optional: Use ag0 TypeScript SDK instead of direct contracts */
|
|
24
|
+
useAg0SDK?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface AgentMetadata {
|
|
27
|
+
name: string;
|
|
28
|
+
description: string;
|
|
29
|
+
image?: string;
|
|
30
|
+
endpoints?: {
|
|
31
|
+
name: string;
|
|
32
|
+
endpoint: string;
|
|
33
|
+
version?: string;
|
|
34
|
+
}[];
|
|
35
|
+
x402Support?: boolean;
|
|
36
|
+
active?: boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface FeedbackInput {
|
|
39
|
+
agentId: bigint;
|
|
40
|
+
value: number;
|
|
41
|
+
decimals?: number;
|
|
42
|
+
tag1?: string;
|
|
43
|
+
tag2?: string;
|
|
44
|
+
endpoint?: string;
|
|
45
|
+
feedbackURI?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface ReputationSummary {
|
|
48
|
+
count: number;
|
|
49
|
+
totalValue: number;
|
|
50
|
+
averageValue: number;
|
|
51
|
+
clients: string[];
|
|
52
|
+
}
|
|
53
|
+
export declare class AgentIdentityClient {
|
|
54
|
+
readonly config: AgetherConfig;
|
|
55
|
+
private signer;
|
|
56
|
+
private identityRegistry;
|
|
57
|
+
private reputationRegistry;
|
|
58
|
+
constructor(options: AgentIdentityClientOptions);
|
|
59
|
+
/**
|
|
60
|
+
* Register a new agent (minimal - no metadata)
|
|
61
|
+
*/
|
|
62
|
+
register(): Promise<{
|
|
63
|
+
agentId: bigint;
|
|
64
|
+
txHash: string;
|
|
65
|
+
}>;
|
|
66
|
+
/**
|
|
67
|
+
* Register agent with IPFS/HTTP URI to metadata JSON
|
|
68
|
+
* @param agentURI URI pointing to agent metadata (ipfs:// or https://)
|
|
69
|
+
*/
|
|
70
|
+
registerWithURI(agentURI: string): Promise<{
|
|
71
|
+
agentId: bigint;
|
|
72
|
+
txHash: string;
|
|
73
|
+
}>;
|
|
74
|
+
/**
|
|
75
|
+
* Check if the signer already owns an ERC-8004 identity token.
|
|
76
|
+
* Returns true if balanceOf > 0, false otherwise.
|
|
77
|
+
* Note: Cannot determine the specific agentId without enumeration —
|
|
78
|
+
* use agether init <pk> --agent-id <id> if you know your agentId.
|
|
79
|
+
*/
|
|
80
|
+
hasExistingIdentity(): Promise<boolean>;
|
|
81
|
+
/**
|
|
82
|
+
* Register only if no identity exists; otherwise throw.
|
|
83
|
+
* Prevents accidental double-registration.
|
|
84
|
+
*/
|
|
85
|
+
registerOrGet(): Promise<{
|
|
86
|
+
agentId: bigint;
|
|
87
|
+
txHash: string | null;
|
|
88
|
+
existing: boolean;
|
|
89
|
+
}>;
|
|
90
|
+
/**
|
|
91
|
+
* Register with URI only if no identity exists; otherwise throw.
|
|
92
|
+
* Prevents accidental double-registration.
|
|
93
|
+
*/
|
|
94
|
+
registerOrGetWithURI(agentURI: string): Promise<{
|
|
95
|
+
agentId: bigint;
|
|
96
|
+
txHash: string | null;
|
|
97
|
+
existing: boolean;
|
|
98
|
+
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Register agent with URI and onchain metadata
|
|
101
|
+
*/
|
|
102
|
+
registerWithMetadata(agentURI: string, metadata: {
|
|
103
|
+
key: string;
|
|
104
|
+
value: string;
|
|
105
|
+
}[]): Promise<{
|
|
106
|
+
agentId: bigint;
|
|
107
|
+
txHash: string;
|
|
108
|
+
}>;
|
|
109
|
+
/**
|
|
110
|
+
* Get agent owner address
|
|
111
|
+
*/
|
|
112
|
+
getOwner(agentId: bigint): Promise<string>;
|
|
113
|
+
/**
|
|
114
|
+
* Get agent URI (metadata JSON location)
|
|
115
|
+
*/
|
|
116
|
+
getAgentURI(agentId: bigint): Promise<string>;
|
|
117
|
+
/**
|
|
118
|
+
* Update agent URI
|
|
119
|
+
*/
|
|
120
|
+
setAgentURI(agentId: bigint, newURI: string): Promise<string>;
|
|
121
|
+
/**
|
|
122
|
+
* Set onchain metadata (key-value)
|
|
123
|
+
*/
|
|
124
|
+
setMetadata(agentId: bigint, key: string, value: string): Promise<string>;
|
|
125
|
+
/**
|
|
126
|
+
* Get onchain metadata
|
|
127
|
+
*/
|
|
128
|
+
getMetadata(agentId: bigint, key: string): Promise<string>;
|
|
129
|
+
/**
|
|
130
|
+
* Transfer agent to new owner
|
|
131
|
+
*/
|
|
132
|
+
transfer(agentId: bigint, to: string): Promise<string>;
|
|
133
|
+
/**
|
|
134
|
+
* Fetch and parse agent metadata from URI
|
|
135
|
+
*/
|
|
136
|
+
fetchAgentMetadata(agentId: bigint): Promise<AgentMetadata | null>;
|
|
137
|
+
/**
|
|
138
|
+
* Give feedback to an agent
|
|
139
|
+
*/
|
|
140
|
+
giveFeedback(input: FeedbackInput): Promise<string>;
|
|
141
|
+
/**
|
|
142
|
+
* Give positive feedback (shorthand)
|
|
143
|
+
*/
|
|
144
|
+
givePosisitiveFeedback(agentId: bigint, value?: number, tags?: {
|
|
145
|
+
tag1?: string;
|
|
146
|
+
tag2?: string;
|
|
147
|
+
}): Promise<string>;
|
|
148
|
+
/**
|
|
149
|
+
* Give negative feedback (e.g., for defaults)
|
|
150
|
+
*/
|
|
151
|
+
giveNegativeFeedback(agentId: bigint, value?: number, tags?: {
|
|
152
|
+
tag1?: string;
|
|
153
|
+
tag2?: string;
|
|
154
|
+
}): Promise<string>;
|
|
155
|
+
/**
|
|
156
|
+
* Record credit default in reputation system
|
|
157
|
+
*/
|
|
158
|
+
recordCreditDefault(agentId: bigint, creditLineId: bigint): Promise<string>;
|
|
159
|
+
/**
|
|
160
|
+
* Get reputation summary for an agent
|
|
161
|
+
*/
|
|
162
|
+
getReputation(agentId: bigint, tag1?: string, tag2?: string): Promise<ReputationSummary>;
|
|
163
|
+
/**
|
|
164
|
+
* Check if agent has negative credit reputation
|
|
165
|
+
*/
|
|
166
|
+
hasNegativeCreditReputation(agentId: bigint): Promise<boolean>;
|
|
167
|
+
/**
|
|
168
|
+
* Verify agent is eligible for Agether credit
|
|
169
|
+
* Checks:
|
|
170
|
+
* 1. Agent exists in ERC-8004 registry
|
|
171
|
+
* 2. Agent has no negative credit reputation
|
|
172
|
+
*/
|
|
173
|
+
verifyForCredit(agentId: bigint): Promise<{
|
|
174
|
+
eligible: boolean;
|
|
175
|
+
reason?: string;
|
|
176
|
+
owner?: string;
|
|
177
|
+
reputation?: ReputationSummary;
|
|
178
|
+
}>;
|
|
179
|
+
private parseAgentIdFromReceipt;
|
|
180
|
+
/**
|
|
181
|
+
* Get contract addresses
|
|
182
|
+
*/
|
|
183
|
+
getContractAddresses(): {
|
|
184
|
+
identity: string;
|
|
185
|
+
reputation: string;
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=AgentIdentityClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AgentIdentityClient.d.ts","sourceRoot":"","sources":["../../src/clients/AgentIdentityClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAU,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AA0CzC,eAAO,MAAM,eAAe;;;CAG3B,CAAC;AAEF,MAAM,WAAW,0BAA0B;IACzC,MAAM,EAAE,aAAa,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,mEAAmE;IACnE,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;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