@sage-protocol/sdk 0.1.2 → 0.1.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.
@@ -20,7 +20,7 @@ var require_package = __commonJS({
20
20
  "package.json"(exports, module) {
21
21
  module.exports = {
22
22
  name: "@sage-protocol/sdk",
23
- version: "0.1.2",
23
+ version: "0.1.6",
24
24
  description: "Backend-agnostic SDK for interacting with the Sage Protocol (governance, SubDAOs, tokens).",
25
25
  main: "dist/index.cjs",
26
26
  module: "dist/index.mjs",
@@ -250,20 +250,19 @@ var require_abi = __commonJS({
250
250
  var PersonalLicenseReceipt = [
251
251
  "function balanceOf(address account, uint256 id) view returns (uint256)"
252
252
  ];
253
- var SageTreasury = [
254
- "function totalReserves() view returns (uint256)",
255
- "function totalPOL() view returns (uint256)",
256
- "function totalDebt() view returns (uint256)",
257
- "function canonicalPool() view returns (address)",
258
- "function routerOrVault() view returns (address)",
259
- "function maxWithdrawalRate() view returns (uint256)",
260
- "function emergencyWithdrawalLimit() view returns (uint256)",
261
- "function getReserveTokens() view returns (address[])",
262
- "function getReserve(address token) view returns (address tokenAddress,uint256 amount,uint256 value,bool isLP,bool isActive)",
263
- "function pendingWithdrawals(uint256) view returns (address token,address recipient,uint256 amount,uint256 value,address requester,uint256 balanceBefore,uint256 recipientBalanceBefore,uint256 depositSnapshot,bool isLP,bool isEmergency,bool exists)",
264
- "function nextWithdrawalId() view returns (uint256)",
265
- "function manualPrices(address token) view returns (uint256 price,uint256 expiresAt,bool active)",
266
- "function lpContributions(address,address) view returns (uint256)"
253
+ var TreasuryWrapper = [
254
+ "function execute(address,uint256,bytes,bytes32) returns (bool)",
255
+ "function allowedTargets(address) view returns (bool)",
256
+ "function allowedSelectors(bytes4) view returns (bool)",
257
+ "function owners(address) view returns (bool)",
258
+ "function ownerCount() view returns (uint256)",
259
+ "function registry() view returns (address)",
260
+ "event TreasuryAction(address indexed caller, address indexed target, uint256 value, bytes data, bytes32 refId)",
261
+ "event AllowedTargetUpdated(address indexed target, bool allowed)",
262
+ "event AllowedSelectorUpdated(bytes4 indexed selector, bool allowed)",
263
+ "event OwnerAdded(address indexed owner)",
264
+ "event OwnerRemoved(address indexed owner)",
265
+ "event TokensSwept(address indexed token, address indexed to, uint256 amount)"
267
266
  ];
268
267
  var GovernanceBoostMerkle = [
269
268
  "function getProposalConfig(uint256) view returns (tuple(uint256 proposalId,address token,uint256 totalAmount,uint64 startTime,uint64 endTime,uint256 merkleRoot))",
@@ -275,29 +274,6 @@ var require_abi = __commonJS({
275
274
  "function create(uint256 proposalId, address token, uint256 perVoter, uint256 maxVoters)",
276
275
  "function fund(uint256 proposalId, uint256 amount)"
277
276
  ];
278
- var BondDepository = [
279
- // Core getters
280
- "function payoutToken() view returns (address)",
281
- "function principalToken() view returns (address)",
282
- "function treasury() view returns (address)",
283
- // Terms
284
- "function terms() view returns (tuple(uint256 controlVariable,uint256 minimumPrice,uint256 maxPayout,uint256 maxDebt,uint256 vestingTerm,uint256 fee))",
285
- "function totalDebt(address) view returns (uint256)",
286
- // Pricing
287
- "function bondPrice() view returns (uint256)",
288
- "function bondPrice(address) view returns (uint256)",
289
- "function bondPriceInUSD() view returns (uint256)",
290
- "function currentDebt() view returns (uint256)",
291
- "function debtRatio() view returns (uint256)",
292
- "function standardizedDebtRatio() view returns (uint256)",
293
- // User views
294
- "function bondInfo(address) view returns (tuple(uint256 payout,uint256 vesting,uint256 lastBlock,uint256 pricePaid))",
295
- "function pendingPayout(address) view returns (uint256)",
296
- "function percentVestedFor(address) view returns (uint256)",
297
- // Actions
298
- "function deposit(uint256 _amount, uint256 _maxPrice) returns (uint256 payout_)",
299
- "function redeem(address _recipient, bool _stake) returns (uint256)"
300
- ];
301
277
  var Events = {
302
278
  ProposalCreated: "event ProposalCreated(uint256 id, address proposer, address[] targets, uint256[] values, string[] signatures, bytes[] calldatas, uint256 startBlock, uint256 endBlock, string description)"
303
279
  };
@@ -316,10 +292,10 @@ var require_abi = __commonJS({
316
292
  PersonalLibraryFacet,
317
293
  PersonalMarketplace,
318
294
  PersonalLicenseReceipt,
319
- SageTreasury,
295
+ TreasuryWrapper,
296
+ // Protocol treasury (replaces SageTreasury)
320
297
  GovernanceBoostMerkle,
321
298
  GovernanceBoostDirect,
322
- BondDepository,
323
299
  Events
324
300
  };
325
301
  }
@@ -1803,7 +1779,7 @@ var require_validation = __commonJS({
1803
1779
  // src/library/index.js
1804
1780
  var require_library = __commonJS({
1805
1781
  "src/library/index.js"(exports, module) {
1806
- var { Contract, getAddress, keccak256, toUtf8Bytes } = __require("ethers");
1782
+ var { Contract, getAddress, keccak256, toUtf8Bytes, AbiCoder } = __require("ethers");
1807
1783
  var { Interface } = __require("ethers");
1808
1784
  var ABI = require_abi();
1809
1785
  var { SageSDKError, CODES } = require_errors();
@@ -1842,17 +1818,31 @@ var require_library = __commonJS({
1842
1818
  promptCount: Number(promptCount)
1843
1819
  };
1844
1820
  }
1821
+ function _computeLibraryKey(subdao, libraryId) {
1822
+ const coder = AbiCoder.defaultAbiCoder ? AbiCoder.defaultAbiCoder() : new AbiCoder();
1823
+ const encoded = coder.encode(["address", "string"], [getAddress(subdao), String(libraryId)]);
1824
+ return keccak256(encoded);
1825
+ }
1845
1826
  async function getLatestLibrary({ provider, registry, subdao, libraryId = "main" }) {
1846
1827
  if (!provider) throw new SageSDKError(CODES.INVALID_ARGS, "provider required");
1847
1828
  const addr = normalise(registry, "registry");
1848
1829
  const sub = normalise(subdao, "subdao");
1849
1830
  const contract = new Contract(addr, ABI.LibraryRegistry, provider);
1850
- const key = keccak256(toUtf8Bytes(`${sub.toLowerCase()}::${libraryId}`));
1831
+ const key = _computeLibraryKey(sub, libraryId);
1851
1832
  const latestCID = await contract.subdaoLibraryLatest(key).catch(() => "");
1852
1833
  if (!latestCID || latestCID.length === 0) return null;
1853
1834
  const info = await getManifestInfo({ provider, registry: addr, manifestCID: latestCID });
1854
1835
  return info;
1855
1836
  }
1837
+ async function getBeforeAfterForUpdate({ provider, registry, subdao, libraryId = "main", newCid }) {
1838
+ if (!provider) throw new SageSDKError(CODES.INVALID_ARGS, "provider required");
1839
+ const addr = normalise(registry, "registry");
1840
+ const sub = normalise(subdao, "subdao");
1841
+ const contract = new Contract(addr, ABI.LibraryRegistry, provider);
1842
+ const key = _computeLibraryKey(sub, libraryId);
1843
+ const prev = await contract.subdaoLibraryLatest(key).catch(() => "");
1844
+ return { previousCID: prev || null, newCID: String(newCid), libraryId: String(libraryId) };
1845
+ }
1856
1846
  async function hasScopedOwnership({ provider, registry, subdao, manifestCID }) {
1857
1847
  if (!provider) throw new SageSDKError(CODES.INVALID_ARGS, "provider required");
1858
1848
  const addr = normalise(registry, "registry");
@@ -1878,6 +1868,8 @@ var require_library = __commonJS({
1878
1868
  getLatestLibrary,
1879
1869
  hasScopedOwnership,
1880
1870
  buildUpdateLibraryForSubDAOTx,
1871
+ _computeLibraryKey,
1872
+ getBeforeAfterForUpdate,
1881
1873
  /** Build an authorizeTimelock(timelock, subdao) call for a LibraryRegistry. */
1882
1874
  buildAuthorizeTimelockTx: function buildAuthorizeTimelockTx({ registry, timelock, subdao }) {
1883
1875
  const to = normalise(registry, "registry");
@@ -2123,6 +2115,62 @@ var require_governance = __commonJS({
2123
2115
  const data = iface.encodeFunctionData("execute(uint256)", [id]);
2124
2116
  return { to: addr, data, value: BigIntZero };
2125
2117
  }
2118
+ async function decodeProposalEffects({ provider, governor, proposalId, fromBlock = 0, toBlock = "latest" }) {
2119
+ if (!provider) throw new SageSDKError(CODES.INVALID_ARGS, "provider required");
2120
+ const govAddr = normaliseGovernor(governor);
2121
+ const iface = new Interface([ABI.Events.ProposalCreated]);
2122
+ const topic = iface.getEvent("ProposalCreated").topicHash;
2123
+ const id = typeof proposalId === "bigint" ? proposalId : String(proposalId).startsWith("0x") ? BigInt(proposalId) : BigInt(String(proposalId));
2124
+ let parsed = null;
2125
+ let logTxHash = null;
2126
+ const logs = await provider.getLogs({ address: govAddr, fromBlock, toBlock, topics: [topic] });
2127
+ for (const log of logs) {
2128
+ try {
2129
+ const p = iface.parseLog(log);
2130
+ if (BigInt(p.args.id.toString()) === id) {
2131
+ parsed = p;
2132
+ logTxHash = log.transactionHash;
2133
+ break;
2134
+ }
2135
+ } catch (_) {
2136
+ }
2137
+ }
2138
+ if (!parsed) throw new Error("ProposalCreated event not found");
2139
+ const targets = parsed.args.targets.map(getAddress);
2140
+ const calldatas = parsed.args.calldatas.map((d) => typeof d === "string" ? d : "0x" + Buffer.from(d).toString("hex"));
2141
+ const LibraryIface = new Interface(["function updateLibraryForSubDAO(address,string,string,uint256)", "function subdaoLibraryLatest(bytes32) view returns (string)"]);
2142
+ const PromptIface = new Interface(["function updatePromptByGovernance(string,string)"]);
2143
+ const coder = AbiCoder.defaultAbiCoder ? AbiCoder.defaultAbiCoder() : new AbiCoder();
2144
+ const effects = [];
2145
+ for (let i = 0; i < calldatas.length; i++) {
2146
+ const data = calldatas[i];
2147
+ const sel = data.slice(0, 10);
2148
+ try {
2149
+ const decoded = LibraryIface.decodeFunctionData("updateLibraryForSubDAO", data);
2150
+ const [subdao, libraryId, newCid, promptCount] = decoded;
2151
+ let previousCID = null;
2152
+ try {
2153
+ const reg = new Contract(targets[i], ["function subdaoLibraryLatest(bytes32) view returns (string)"], provider);
2154
+ const key = keccak256(coder.encode(["address", "string"], [getAddress(subdao), String(libraryId)]));
2155
+ const prev = await reg.subdaoLibraryLatest(key).catch(() => "");
2156
+ previousCID = prev && prev.length ? String(prev) : null;
2157
+ } catch (_) {
2158
+ }
2159
+ effects.push({ type: "libraryUpdate", index: i, target: targets[i], subdao: getAddress(subdao), libraryId: String(libraryId), previousCid: previousCID, newCid: String(newCid), promptCount: Number(promptCount) });
2160
+ continue;
2161
+ } catch (_) {
2162
+ }
2163
+ try {
2164
+ const decodedP = PromptIface.decodeFunctionData("updatePromptByGovernance", data);
2165
+ const [key, newCid] = decodedP;
2166
+ effects.push({ type: "promptUpdate", index: i, target: targets[i], key: String(key), newCid: String(newCid) });
2167
+ continue;
2168
+ } catch (_) {
2169
+ }
2170
+ effects.push({ type: "unknown", index: i, target: targets[i], selector: sel });
2171
+ }
2172
+ return { governor: govAddr, proposalId: id, tx: logTxHash, actions: { count: calldatas.length }, effects };
2173
+ }
2126
2174
  async function getQuorumAt({ provider, governor, blockTag }) {
2127
2175
  if (!provider) throw new SageSDKError(CODES.INVALID_ARGS, "provider required");
2128
2176
  const addr = normaliseGovernor(governor);
@@ -2252,6 +2300,7 @@ var require_governance = __commonJS({
2252
2300
  buildExecuteTx,
2253
2301
  buildQueueByIdTx,
2254
2302
  buildExecuteByIdTx,
2303
+ decodeProposalEffects,
2255
2304
  makeProposalDescription,
2256
2305
  stripProposalSalt,
2257
2306
  getQuorumAt,
@@ -4091,7 +4140,7 @@ var require_factory = __commonJS({
4091
4140
  // src/browser/library.js
4092
4141
  var require_library2 = __commonJS({
4093
4142
  "src/browser/library.js"(exports, module) {
4094
- var { Contract, getAddress, keccak256, toUtf8Bytes, Interface } = __require("ethers");
4143
+ var { Contract, getAddress, keccak256, AbiCoder, Interface } = __require("ethers");
4095
4144
  var ABI = require_abi();
4096
4145
  var { SageSDKError, CODES } = require_errors();
4097
4146
  var { searchRegistry } = require_search();
@@ -4128,12 +4177,17 @@ var require_library2 = __commonJS({
4128
4177
  promptCount: Number(promptCount)
4129
4178
  };
4130
4179
  }
4180
+ function _computeLibraryKey(subdao, libraryId) {
4181
+ const coder = AbiCoder.defaultAbiCoder ? AbiCoder.defaultAbiCoder() : new AbiCoder();
4182
+ const encoded = coder.encode(["address", "string"], [getAddress(subdao), String(libraryId)]);
4183
+ return keccak256(encoded);
4184
+ }
4131
4185
  async function getLatestLibrary({ provider, registry, subdao, libraryId = "main" }) {
4132
4186
  if (!provider) throw new SageSDKError(CODES.INVALID_ARGS, "provider required");
4133
4187
  const addr = normalise(registry, "registry");
4134
4188
  const sub = normalise(subdao, "subdao");
4135
4189
  const contract = new Contract(addr, ABI.LibraryRegistry, provider);
4136
- const key = keccak256(toUtf8Bytes(`${sub.toLowerCase()}::${libraryId}`));
4190
+ const key = _computeLibraryKey(sub, libraryId);
4137
4191
  const latestCID = await contract.subdaoLibraryLatest(key).catch(() => "");
4138
4192
  if (!latestCID || latestCID.length === 0) return null;
4139
4193
  const info = await getManifestInfo({ provider, registry: addr, manifestCID: latestCID });
@@ -4158,6 +4212,15 @@ var require_library2 = __commonJS({
4158
4212
  ]);
4159
4213
  return { to, data, value: 0n };
4160
4214
  }
4215
+ async function getBeforeAfterForUpdate({ provider, registry, subdao, libraryId = "main", newCid }) {
4216
+ if (!provider) throw new SageSDKError(CODES.INVALID_ARGS, "provider required");
4217
+ const addr = normalise(registry, "registry");
4218
+ const sub = normalise(subdao, "subdao");
4219
+ const contract = new Contract(addr, ABI.LibraryRegistry, provider);
4220
+ const key = _computeLibraryKey(sub, libraryId);
4221
+ const prev = await contract.subdaoLibraryLatest(key).catch(() => "");
4222
+ return { previousCID: prev || null, newCID: String(newCid), libraryId: String(libraryId) };
4223
+ }
4161
4224
  function buildAuthorizeTimelockTx({ registry, timelock, subdao }) {
4162
4225
  const to = normalise(registry, "registry");
4163
4226
  const iface = new Interface(["function authorizeTimelock(address,address)"]);
@@ -4170,6 +4233,7 @@ var require_library2 = __commonJS({
4170
4233
  getLatestLibrary,
4171
4234
  hasScopedOwnership,
4172
4235
  buildUpdateLibraryForSubDAOTx,
4236
+ getBeforeAfterForUpdate,
4173
4237
  buildAuthorizeTimelockTx,
4174
4238
  searchRegistry
4175
4239
  };
@@ -5779,7 +5843,23 @@ var require_treasury = __commonJS({
5779
5843
  confirmWithdrawal,
5780
5844
  cancelWithdrawal,
5781
5845
  setPriceOverride,
5782
- clearPriceOverride
5846
+ clearPriceOverride,
5847
+ // TX builders for app usage
5848
+ buildApproveTx: ({ token, spender, amount, decimals = 18 }) => {
5849
+ if (!token || !spender) throw new SageSDKError(CODES.INVALID_ARGS, "token and spender required");
5850
+ const iface = new Interface(["function approve(address,uint256)"]);
5851
+ return { to: getAddress(token), data: iface.encodeFunctionData("approve", [getAddress(spender), BigInt(amount.toString ? amount.toString() : amount)]), value: 0n };
5852
+ },
5853
+ buildTransferTx: ({ token, to, amount, decimals = 18 }) => {
5854
+ if (!token || !to) throw new SageSDKError(CODES.INVALID_ARGS, "token and to required");
5855
+ const iface = new Interface(["function transfer(address,uint256)"]);
5856
+ return { to: getAddress(token), data: iface.encodeFunctionData("transfer", [getAddress(to), BigInt(amount.toString ? amount.toString() : amount)]), value: 0n };
5857
+ },
5858
+ buildWrapEthTx: ({ weth, amountWei }) => {
5859
+ if (!weth) throw new SageSDKError(CODES.INVALID_ARGS, "weth required");
5860
+ const iface = new Interface(["function deposit() payable"]);
5861
+ return { to: getAddress(weth), data: iface.encodeFunctionData("deposit", []), value: BigInt(amountWei.toString ? amountWei.toString() : amountWei) };
5862
+ }
5783
5863
  };
5784
5864
  }
5785
5865
  });
@@ -6120,12 +6200,40 @@ var require_doppler = __commonJS({
6120
6200
  }
6121
6201
  return { auctionAddress, note: "Lens API not available in this doppler-sdk version" };
6122
6202
  }
6203
+ async function listAuctions(sdk, { limit = 10, cursor } = {}) {
6204
+ const lens = sdk.lens;
6205
+ if (lens) {
6206
+ if (typeof lens.listAuctions === "function") {
6207
+ try {
6208
+ return await lens.listAuctions({ limit, cursor });
6209
+ } catch (e) {
6210
+ }
6211
+ }
6212
+ if (typeof lens.getAuctions === "function") {
6213
+ try {
6214
+ return await lens.getAuctions({ limit, cursor });
6215
+ } catch (e) {
6216
+ }
6217
+ }
6218
+ }
6219
+ const fac = sdk.factory;
6220
+ if (fac && typeof fac.listAuctions === "function") {
6221
+ try {
6222
+ return await fac.listAuctions({ limit, cursor });
6223
+ } catch (e) {
6224
+ }
6225
+ }
6226
+ return { ok: false, error: "LIST_UNSUPPORTED", message: "Listing auctions not supported by this doppler-sdk version" };
6227
+ }
6123
6228
  async function buyTokens(sdk, { auctionAddress, numeraireAmount }) {
6124
6229
  const trade = sdk.trade || sdk.swap || sdk.auction;
6125
6230
  if (trade && typeof trade.buy === "function") {
6126
- return await trade.buy({ auctionAddress, numeraireAmount });
6231
+ try {
6232
+ return await trade.buy({ auctionAddress, numeraireAmount });
6233
+ } catch (e) {
6234
+ }
6127
6235
  }
6128
- throw new Error("Buy is not supported by the current doppler-sdk version");
6236
+ return { ok: false, error: "BUY_UNSUPPORTED", message: "Buy not supported by this doppler-sdk version. Use executeV3BuyExactIn/urExecute helpers or upgrade doppler-sdk." };
6129
6237
  }
6130
6238
  async function migrate(sdk, asset) {
6131
6239
  if (sdk.airlock && typeof sdk.airlock.migrate === "function") {
@@ -6137,6 +6245,7 @@ var require_doppler = __commonJS({
6137
6245
  initDoppler,
6138
6246
  deployDynamicAuction,
6139
6247
  getAuctionStatus,
6248
+ listAuctions,
6140
6249
  buyTokens,
6141
6250
  migrate
6142
6251
  };