@fepvenancio/stela-sdk 0.8.1 → 0.9.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/index.cjs CHANGED
@@ -258,11 +258,35 @@ var TOKENS = [
258
258
  addresses: {
259
259
  sepolia: "0x04f2345306bf8ef1c8c1445661354ef08421aa092459445a5d6b46641237e943"
260
260
  }
261
+ },
262
+ // NFT Collections (ERC721)
263
+ {
264
+ symbol: "GENESIS",
265
+ name: "Stela Genesis",
266
+ decimals: 0,
267
+ assetType: "ERC721",
268
+ addresses: {
269
+ sepolia: "0x0265ea52ffbf1b7e1a029b94fe1a2023899dd0bc02eb1f11c9b04ea90e957d28"
270
+ }
271
+ },
272
+ {
273
+ symbol: "MockNFT",
274
+ name: "Mock ERC721",
275
+ decimals: 0,
276
+ assetType: "ERC721",
277
+ addresses: {
278
+ sepolia: "0x032bfd52134ad92beae1bc5018a3748e1d1240efd627220e314c07e4c63433d5"
279
+ }
261
280
  }
262
281
  ];
263
282
  function normalizeHex(addr) {
264
283
  return "0x" + addr.replace(/^0x0*/i, "").toLowerCase();
265
284
  }
285
+ function getNFTCollections(network) {
286
+ return TOKENS.filter(
287
+ (t) => t.assetType === "ERC721" && t.addresses[network] !== void 0
288
+ );
289
+ }
266
290
  function getTokensForNetwork(network) {
267
291
  return TOKENS.filter((t) => t.addresses[network] !== void 0);
268
292
  }
@@ -288,6 +312,74 @@ function sharesToPercentage(shares, totalSupply, currentIssuedPercentage) {
288
312
  function calculateFeeShares(shares, feeBps) {
289
313
  return shares * feeBps / MAX_BPS;
290
314
  }
315
+ function divCeil(a, b) {
316
+ if (a === 0n) return 0n;
317
+ return (a + b - 1n) / b;
318
+ }
319
+ function proRataInterest(amount, elapsed, duration) {
320
+ if (amount === 0n || elapsed === 0n) return 0n;
321
+ if (elapsed >= duration) return amount;
322
+ return divCeil(amount * elapsed, duration);
323
+ }
324
+
325
+ // src/math/position.ts
326
+ var DEFAULT_DUST_BUFFER_SECONDS = 60n;
327
+ function shareProportionBps(shares, totalSupply) {
328
+ if (totalSupply === 0n) return 0n;
329
+ return shares * MAX_BPS / totalSupply;
330
+ }
331
+ function proportionalAssetValue(assetValue, shares, totalSupply) {
332
+ if (totalSupply === 0n || shares === 0n) return 0n;
333
+ return assetValue * shares / totalSupply;
334
+ }
335
+ function computePositionValue(params) {
336
+ const { shares, totalSupply, elapsed, duration } = params;
337
+ const shareBps = shareProportionBps(shares, totalSupply);
338
+ const debt = params.debtAssets.map((asset) => ({
339
+ asset,
340
+ proportionalValue: proportionalAssetValue(asset.value, shares, totalSupply)
341
+ }));
342
+ const interest = params.interestAssets.map((asset) => {
343
+ const fullInterest = proportionalAssetValue(asset.value, shares, totalSupply);
344
+ const accruedInterest = duration === 0n ? fullInterest : proRataInterest(fullInterest, elapsed, duration);
345
+ return { asset, fullInterest, accruedInterest };
346
+ });
347
+ const collateral = params.collateralAssets.map((asset) => ({
348
+ asset,
349
+ proportionalValue: proportionalAssetValue(asset.value, shares, totalSupply)
350
+ }));
351
+ return {
352
+ inscriptionId: params.inscriptionId,
353
+ shares,
354
+ totalSupply,
355
+ shareBps,
356
+ debt,
357
+ interest,
358
+ collateral,
359
+ accrued: interest,
360
+ elapsed,
361
+ duration
362
+ };
363
+ }
364
+ function accruedInterestWithBuffer(amount, elapsed, duration, bufferSeconds = DEFAULT_DUST_BUFFER_SECONDS) {
365
+ if (duration === 0n) return amount;
366
+ const bufferedElapsed = elapsed + bufferSeconds;
367
+ return proRataInterest(amount, bufferedElapsed, duration);
368
+ }
369
+ function computeSafePositionFloor(params) {
370
+ const { shares, totalSupply, elapsed, duration } = params;
371
+ const buffer = params.bufferSeconds ?? DEFAULT_DUST_BUFFER_SECONDS;
372
+ const debtFloor = params.debtAssets.map((asset) => ({
373
+ asset,
374
+ proportionalValue: proportionalAssetValue(asset.value, shares, totalSupply)
375
+ }));
376
+ const interestFloor = params.interestAssets.map((asset) => {
377
+ const fullInterest = proportionalAssetValue(asset.value, shares, totalSupply);
378
+ const safeAccrued = accruedInterestWithBuffer(fullInterest, elapsed, duration, buffer);
379
+ return { asset, proportionalValue: safeAccrued };
380
+ });
381
+ return { debtFloor, interestFloor };
382
+ }
291
383
  var SELECTORS = {
292
384
  InscriptionCreated: starknet.hash.getSelectorFromName("InscriptionCreated"),
293
385
  InscriptionSigned: starknet.hash.getSelectorFromName("InscriptionSigned"),
@@ -3470,9 +3562,14 @@ function extractFieldU256(val) {
3470
3562
  }
3471
3563
  var ShareClient = class {
3472
3564
  contract;
3565
+ address;
3566
+ account;
3473
3567
  constructor(opts) {
3568
+ this.address = opts.stelaAddress;
3474
3569
  this.contract = new starknet.Contract(stela_default, opts.stelaAddress, opts.provider);
3570
+ this.account = opts.account;
3475
3571
  }
3572
+ // ── Read Methods ───────────────────────────────────────────────────
3476
3573
  /** Get share balance for an account on a specific inscription */
3477
3574
  async balanceOf(account, inscriptionId) {
3478
3575
  const result = await this.contract.call("balance_of", [account, ...toU256(inscriptionId)]);
@@ -3496,6 +3593,53 @@ var ShareClient = class {
3496
3593
  const result = await this.contract.call("is_approved_for_all", [owner, operator]);
3497
3594
  return Boolean(result[0]);
3498
3595
  }
3596
+ // ── Call Builders ──────────────────────────────────────────────────
3597
+ /**
3598
+ * Build a call to transfer shares (ERC1155 safeTransferFrom).
3599
+ * This enables secondary market trading of lending positions.
3600
+ *
3601
+ * @param from - Current share holder
3602
+ * @param to - Recipient address
3603
+ * @param inscriptionId - The inscription (token ID)
3604
+ * @param amount - Number of shares to transfer
3605
+ * @param data - Optional calldata (empty array by default)
3606
+ */
3607
+ buildTransferShares(from, to, inscriptionId, amount, data = []) {
3608
+ return {
3609
+ contractAddress: this.address,
3610
+ entrypoint: "safe_transfer_from",
3611
+ calldata: [from, to, ...toU256(inscriptionId), ...toU256(amount), String(data.length), ...data]
3612
+ };
3613
+ }
3614
+ /**
3615
+ * Build a call to approve an operator for all ERC1155 tokens.
3616
+ * Required before a marketplace contract can transfer shares on your behalf.
3617
+ *
3618
+ * @param operator - The address to approve (e.g., marketplace contract)
3619
+ * @param approved - Whether to approve or revoke
3620
+ */
3621
+ buildSetApprovalForAll(operator, approved) {
3622
+ return {
3623
+ contractAddress: this.address,
3624
+ entrypoint: "set_approval_for_all",
3625
+ calldata: [operator, approved ? "1" : "0"]
3626
+ };
3627
+ }
3628
+ // ── Execute Methods ────────────────────────────────────────────────
3629
+ /** Transfer shares to another address */
3630
+ async transferShares(from, to, inscriptionId, amount, data = []) {
3631
+ if (!this.account) throw new Error("Account required for write operations");
3632
+ const result = await this.account.execute([
3633
+ this.buildTransferShares(from, to, inscriptionId, amount, data)
3634
+ ]);
3635
+ return { transaction_hash: result.transaction_hash };
3636
+ }
3637
+ /** Approve or revoke an operator for all ERC1155 tokens */
3638
+ async setApprovalForAll(operator, approved) {
3639
+ if (!this.account) throw new Error("Account required for write operations");
3640
+ const result = await this.account.execute([this.buildSetApprovalForAll(operator, approved)]);
3641
+ return { transaction_hash: result.transaction_hash };
3642
+ }
3499
3643
  };
3500
3644
  function extractBigInt(result) {
3501
3645
  const arr = result;
@@ -3780,7 +3924,8 @@ var StelaSdk = class {
3780
3924
  });
3781
3925
  this.shares = new ShareClient({
3782
3926
  stelaAddress: this.stelaAddress,
3783
- provider: opts.provider
3927
+ provider: opts.provider,
3928
+ account: opts.account
3784
3929
  });
3785
3930
  const stelaContract = new starknet.Contract(stela_default, this.stelaAddress, opts.provider);
3786
3931
  this.locker = new LockerClient(stelaContract, opts.provider, opts.account);
@@ -3796,6 +3941,7 @@ exports.AUCTION_RESERVE_BPS = AUCTION_RESERVE_BPS;
3796
3941
  exports.ApiClient = ApiClient;
3797
3942
  exports.ApiError = ApiError;
3798
3943
  exports.CHAIN_ID = CHAIN_ID;
3944
+ exports.DEFAULT_DUST_BUFFER_SECONDS = DEFAULT_DUST_BUFFER_SECONDS;
3799
3945
  exports.EXPLORER_TX_URL = EXPLORER_TX_URL;
3800
3946
  exports.GRACE_PERIOD = GRACE_PERIOD;
3801
3947
  exports.InscriptionClient = InscriptionClient;
@@ -3809,11 +3955,15 @@ exports.StelaSdk = StelaSdk;
3809
3955
  exports.TOKENS = TOKENS;
3810
3956
  exports.VALID_STATUSES = VALID_STATUSES;
3811
3957
  exports.VIRTUAL_SHARE_OFFSET = VIRTUAL_SHARE_OFFSET;
3958
+ exports.accruedInterestWithBuffer = accruedInterestWithBuffer;
3812
3959
  exports.addressesEqual = addressesEqual;
3813
3960
  exports.calculateFeeShares = calculateFeeShares;
3961
+ exports.computePositionValue = computePositionValue;
3962
+ exports.computeSafePositionFloor = computeSafePositionFloor;
3814
3963
  exports.computeStatus = computeStatus;
3815
3964
  exports.convertToShares = convertToShares;
3816
3965
  exports.deserializeSignature = deserializeSignature;
3966
+ exports.divCeil = divCeil;
3817
3967
  exports.findTokenByAddress = findTokenByAddress;
3818
3968
  exports.formatAddress = formatAddress;
3819
3969
  exports.formatDuration = formatDuration;
@@ -3826,6 +3976,7 @@ exports.getCollectionBorrowAcceptanceTypedData = getCollectionBorrowAcceptanceTy
3826
3976
  exports.getCollectionLendOfferTypedData = getCollectionLendOfferTypedData;
3827
3977
  exports.getInscriptionOrderTypedData = getInscriptionOrderTypedData;
3828
3978
  exports.getLendOfferTypedData = getLendOfferTypedData;
3979
+ exports.getNFTCollections = getNFTCollections;
3829
3980
  exports.getRefinanceApprovalTypedData = getRefinanceApprovalTypedData;
3830
3981
  exports.getRefinanceOfferTypedData = getRefinanceOfferTypedData;
3831
3982
  exports.getRenegotiationProposalTypedData = getRenegotiationProposalTypedData;
@@ -3837,9 +3988,12 @@ exports.normalizeAddress = normalizeAddress;
3837
3988
  exports.parseAmount = parseAmount;
3838
3989
  exports.parseEvent = parseEvent;
3839
3990
  exports.parseEvents = parseEvents;
3991
+ exports.proRataInterest = proRataInterest;
3992
+ exports.proportionalAssetValue = proportionalAssetValue;
3840
3993
  exports.resolveNetwork = resolveNetwork;
3841
3994
  exports.scaleByPercentage = scaleByPercentage;
3842
3995
  exports.serializeSignature = serializeSignature;
3996
+ exports.shareProportionBps = shareProportionBps;
3843
3997
  exports.sharesToPercentage = sharesToPercentage;
3844
3998
  exports.toHex = toHex;
3845
3999
  exports.toU256 = toU256;