@fepvenancio/stela-sdk 0.8.0 → 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"),
@@ -1052,6 +1144,210 @@ var stela_default = [
1052
1144
  {
1053
1145
  name: "collateral_asset_count",
1054
1146
  type: "core::integer::u32"
1147
+ },
1148
+ {
1149
+ name: "auction_started",
1150
+ type: "core::bool"
1151
+ },
1152
+ {
1153
+ name: "auction_start_time",
1154
+ type: "core::integer::u64"
1155
+ }
1156
+ ]
1157
+ },
1158
+ {
1159
+ type: "struct",
1160
+ name: "stela::snip12::CollectionLendOffer",
1161
+ members: [
1162
+ {
1163
+ name: "lender",
1164
+ type: "core::starknet::contract_address::ContractAddress"
1165
+ },
1166
+ {
1167
+ name: "debt_hash",
1168
+ type: "core::felt252"
1169
+ },
1170
+ {
1171
+ name: "interest_hash",
1172
+ type: "core::felt252"
1173
+ },
1174
+ {
1175
+ name: "debt_count",
1176
+ type: "core::integer::u32"
1177
+ },
1178
+ {
1179
+ name: "interest_count",
1180
+ type: "core::integer::u32"
1181
+ },
1182
+ {
1183
+ name: "collection_address",
1184
+ type: "core::starknet::contract_address::ContractAddress"
1185
+ },
1186
+ {
1187
+ name: "duration",
1188
+ type: "core::integer::u64"
1189
+ },
1190
+ {
1191
+ name: "deadline",
1192
+ type: "core::integer::u64"
1193
+ },
1194
+ {
1195
+ name: "nonce",
1196
+ type: "core::felt252"
1197
+ }
1198
+ ]
1199
+ },
1200
+ {
1201
+ type: "struct",
1202
+ name: "stela::snip12::CollectionBorrowAcceptance",
1203
+ members: [
1204
+ {
1205
+ name: "offer_hash",
1206
+ type: "core::felt252"
1207
+ },
1208
+ {
1209
+ name: "borrower",
1210
+ type: "core::starknet::contract_address::ContractAddress"
1211
+ },
1212
+ {
1213
+ name: "token_id",
1214
+ type: "core::integer::u256"
1215
+ },
1216
+ {
1217
+ name: "nonce",
1218
+ type: "core::felt252"
1219
+ }
1220
+ ]
1221
+ },
1222
+ {
1223
+ type: "struct",
1224
+ name: "stela::snip12::RenegotiationProposal",
1225
+ members: [
1226
+ {
1227
+ name: "inscription_id",
1228
+ type: "core::integer::u256"
1229
+ },
1230
+ {
1231
+ name: "proposer",
1232
+ type: "core::starknet::contract_address::ContractAddress"
1233
+ },
1234
+ {
1235
+ name: "new_duration",
1236
+ type: "core::integer::u64"
1237
+ },
1238
+ {
1239
+ name: "new_interest_hash",
1240
+ type: "core::felt252"
1241
+ },
1242
+ {
1243
+ name: "new_interest_count",
1244
+ type: "core::integer::u32"
1245
+ },
1246
+ {
1247
+ name: "proposal_deadline",
1248
+ type: "core::integer::u64"
1249
+ },
1250
+ {
1251
+ name: "nonce",
1252
+ type: "core::felt252"
1253
+ }
1254
+ ]
1255
+ },
1256
+ {
1257
+ type: "struct",
1258
+ name: "stela::snip12::CollateralSaleOffer",
1259
+ members: [
1260
+ {
1261
+ name: "inscription_id",
1262
+ type: "core::integer::u256"
1263
+ },
1264
+ {
1265
+ name: "borrower",
1266
+ type: "core::starknet::contract_address::ContractAddress"
1267
+ },
1268
+ {
1269
+ name: "min_price",
1270
+ type: "core::integer::u256"
1271
+ },
1272
+ {
1273
+ name: "payment_token",
1274
+ type: "core::starknet::contract_address::ContractAddress"
1275
+ },
1276
+ {
1277
+ name: "allowed_buyer",
1278
+ type: "core::starknet::contract_address::ContractAddress"
1279
+ },
1280
+ {
1281
+ name: "deadline",
1282
+ type: "core::integer::u64"
1283
+ },
1284
+ {
1285
+ name: "nonce",
1286
+ type: "core::felt252"
1287
+ }
1288
+ ]
1289
+ },
1290
+ {
1291
+ type: "struct",
1292
+ name: "stela::snip12::RefinanceOffer",
1293
+ members: [
1294
+ {
1295
+ name: "inscription_id",
1296
+ type: "core::integer::u256"
1297
+ },
1298
+ {
1299
+ name: "new_lender",
1300
+ type: "core::starknet::contract_address::ContractAddress"
1301
+ },
1302
+ {
1303
+ name: "new_debt_hash",
1304
+ type: "core::felt252"
1305
+ },
1306
+ {
1307
+ name: "new_interest_hash",
1308
+ type: "core::felt252"
1309
+ },
1310
+ {
1311
+ name: "new_debt_count",
1312
+ type: "core::integer::u32"
1313
+ },
1314
+ {
1315
+ name: "new_interest_count",
1316
+ type: "core::integer::u32"
1317
+ },
1318
+ {
1319
+ name: "new_duration",
1320
+ type: "core::integer::u64"
1321
+ },
1322
+ {
1323
+ name: "deadline",
1324
+ type: "core::integer::u64"
1325
+ },
1326
+ {
1327
+ name: "nonce",
1328
+ type: "core::felt252"
1329
+ }
1330
+ ]
1331
+ },
1332
+ {
1333
+ type: "struct",
1334
+ name: "stela::snip12::RefinanceApproval",
1335
+ members: [
1336
+ {
1337
+ name: "inscription_id",
1338
+ type: "core::integer::u256"
1339
+ },
1340
+ {
1341
+ name: "offer_hash",
1342
+ type: "core::felt252"
1343
+ },
1344
+ {
1345
+ name: "borrower",
1346
+ type: "core::starknet::contract_address::ContractAddress"
1347
+ },
1348
+ {
1349
+ name: "nonce",
1350
+ type: "core::felt252"
1055
1351
  }
1056
1352
  ]
1057
1353
  },
@@ -1604,6 +1900,206 @@ var stela_default = [
1604
1900
  ],
1605
1901
  outputs: [],
1606
1902
  state_mutability: "external"
1903
+ },
1904
+ {
1905
+ type: "function",
1906
+ name: "settle_collection",
1907
+ inputs: [
1908
+ {
1909
+ name: "offer",
1910
+ type: "stela::snip12::CollectionLendOffer"
1911
+ },
1912
+ {
1913
+ name: "acceptance",
1914
+ type: "stela::snip12::CollectionBorrowAcceptance"
1915
+ },
1916
+ {
1917
+ name: "debt_assets",
1918
+ type: "core::array::Array::<stela::types::asset::Asset>"
1919
+ },
1920
+ {
1921
+ name: "interest_assets",
1922
+ type: "core::array::Array::<stela::types::asset::Asset>"
1923
+ },
1924
+ {
1925
+ name: "lender_sig",
1926
+ type: "core::array::Array::<core::felt252>"
1927
+ },
1928
+ {
1929
+ name: "borrower_sig",
1930
+ type: "core::array::Array::<core::felt252>"
1931
+ }
1932
+ ],
1933
+ outputs: [],
1934
+ state_mutability: "external"
1935
+ },
1936
+ {
1937
+ type: "function",
1938
+ name: "commit_renegotiation",
1939
+ inputs: [
1940
+ {
1941
+ name: "inscription_id",
1942
+ type: "core::integer::u256"
1943
+ },
1944
+ {
1945
+ name: "proposal_hash",
1946
+ type: "core::felt252"
1947
+ }
1948
+ ],
1949
+ outputs: [],
1950
+ state_mutability: "external"
1951
+ },
1952
+ {
1953
+ type: "function",
1954
+ name: "execute_renegotiation",
1955
+ inputs: [
1956
+ {
1957
+ name: "inscription_id",
1958
+ type: "core::integer::u256"
1959
+ },
1960
+ {
1961
+ name: "proposal",
1962
+ type: "stela::snip12::RenegotiationProposal"
1963
+ },
1964
+ {
1965
+ name: "proposer_sig",
1966
+ type: "core::array::Array::<core::felt252>"
1967
+ },
1968
+ {
1969
+ name: "new_interest_assets",
1970
+ type: "core::array::Array::<stela::types::asset::Asset>"
1971
+ }
1972
+ ],
1973
+ outputs: [],
1974
+ state_mutability: "external"
1975
+ },
1976
+ {
1977
+ type: "function",
1978
+ name: "buy_collateral",
1979
+ inputs: [
1980
+ {
1981
+ name: "inscription_id",
1982
+ type: "core::integer::u256"
1983
+ },
1984
+ {
1985
+ name: "offer",
1986
+ type: "stela::snip12::CollateralSaleOffer"
1987
+ },
1988
+ {
1989
+ name: "borrower_sig",
1990
+ type: "core::array::Array::<core::felt252>"
1991
+ },
1992
+ {
1993
+ name: "sale_price",
1994
+ type: "core::integer::u256"
1995
+ }
1996
+ ],
1997
+ outputs: [],
1998
+ state_mutability: "external"
1999
+ },
2000
+ {
2001
+ type: "function",
2002
+ name: "refinance",
2003
+ inputs: [
2004
+ {
2005
+ name: "offer",
2006
+ type: "stela::snip12::RefinanceOffer"
2007
+ },
2008
+ {
2009
+ name: "new_debt_assets",
2010
+ type: "core::array::Array::<stela::types::asset::Asset>"
2011
+ },
2012
+ {
2013
+ name: "new_interest_assets",
2014
+ type: "core::array::Array::<stela::types::asset::Asset>"
2015
+ },
2016
+ {
2017
+ name: "new_lender_sig",
2018
+ type: "core::array::Array::<core::felt252>"
2019
+ },
2020
+ {
2021
+ name: "approval",
2022
+ type: "stela::snip12::RefinanceApproval"
2023
+ },
2024
+ {
2025
+ name: "borrower_sig",
2026
+ type: "core::array::Array::<core::felt252>"
2027
+ }
2028
+ ],
2029
+ outputs: [],
2030
+ state_mutability: "external"
2031
+ },
2032
+ {
2033
+ type: "function",
2034
+ name: "start_auction",
2035
+ inputs: [
2036
+ {
2037
+ name: "inscription_id",
2038
+ type: "core::integer::u256"
2039
+ }
2040
+ ],
2041
+ outputs: [],
2042
+ state_mutability: "external"
2043
+ },
2044
+ {
2045
+ type: "function",
2046
+ name: "bid",
2047
+ inputs: [
2048
+ {
2049
+ name: "inscription_id",
2050
+ type: "core::integer::u256"
2051
+ }
2052
+ ],
2053
+ outputs: [],
2054
+ state_mutability: "external"
2055
+ },
2056
+ {
2057
+ type: "function",
2058
+ name: "claim_collateral",
2059
+ inputs: [
2060
+ {
2061
+ name: "inscription_id",
2062
+ type: "core::integer::u256"
2063
+ }
2064
+ ],
2065
+ outputs: [],
2066
+ state_mutability: "external"
2067
+ },
2068
+ {
2069
+ type: "function",
2070
+ name: "get_auction_price",
2071
+ inputs: [
2072
+ {
2073
+ name: "inscription_id",
2074
+ type: "core::integer::u256"
2075
+ },
2076
+ {
2077
+ name: "debt_index",
2078
+ type: "core::integer::u32"
2079
+ }
2080
+ ],
2081
+ outputs: [
2082
+ {
2083
+ type: "core::integer::u256"
2084
+ }
2085
+ ],
2086
+ state_mutability: "view"
2087
+ },
2088
+ {
2089
+ type: "function",
2090
+ name: "get_auction_end_time",
2091
+ inputs: [
2092
+ {
2093
+ name: "inscription_id",
2094
+ type: "core::integer::u256"
2095
+ }
2096
+ ],
2097
+ outputs: [
2098
+ {
2099
+ type: "core::integer::u64"
2100
+ }
2101
+ ],
2102
+ state_mutability: "view"
1607
2103
  }
1608
2104
  ]
1609
2105
  },
@@ -3066,9 +3562,14 @@ function extractFieldU256(val) {
3066
3562
  }
3067
3563
  var ShareClient = class {
3068
3564
  contract;
3565
+ address;
3566
+ account;
3069
3567
  constructor(opts) {
3568
+ this.address = opts.stelaAddress;
3070
3569
  this.contract = new starknet.Contract(stela_default, opts.stelaAddress, opts.provider);
3570
+ this.account = opts.account;
3071
3571
  }
3572
+ // ── Read Methods ───────────────────────────────────────────────────
3072
3573
  /** Get share balance for an account on a specific inscription */
3073
3574
  async balanceOf(account, inscriptionId) {
3074
3575
  const result = await this.contract.call("balance_of", [account, ...toU256(inscriptionId)]);
@@ -3092,6 +3593,53 @@ var ShareClient = class {
3092
3593
  const result = await this.contract.call("is_approved_for_all", [owner, operator]);
3093
3594
  return Boolean(result[0]);
3094
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
+ }
3095
3643
  };
3096
3644
  function extractBigInt(result) {
3097
3645
  const arr = result;
@@ -3376,7 +3924,8 @@ var StelaSdk = class {
3376
3924
  });
3377
3925
  this.shares = new ShareClient({
3378
3926
  stelaAddress: this.stelaAddress,
3379
- provider: opts.provider
3927
+ provider: opts.provider,
3928
+ account: opts.account
3380
3929
  });
3381
3930
  const stelaContract = new starknet.Contract(stela_default, this.stelaAddress, opts.provider);
3382
3931
  this.locker = new LockerClient(stelaContract, opts.provider, opts.account);
@@ -3392,6 +3941,7 @@ exports.AUCTION_RESERVE_BPS = AUCTION_RESERVE_BPS;
3392
3941
  exports.ApiClient = ApiClient;
3393
3942
  exports.ApiError = ApiError;
3394
3943
  exports.CHAIN_ID = CHAIN_ID;
3944
+ exports.DEFAULT_DUST_BUFFER_SECONDS = DEFAULT_DUST_BUFFER_SECONDS;
3395
3945
  exports.EXPLORER_TX_URL = EXPLORER_TX_URL;
3396
3946
  exports.GRACE_PERIOD = GRACE_PERIOD;
3397
3947
  exports.InscriptionClient = InscriptionClient;
@@ -3405,11 +3955,15 @@ exports.StelaSdk = StelaSdk;
3405
3955
  exports.TOKENS = TOKENS;
3406
3956
  exports.VALID_STATUSES = VALID_STATUSES;
3407
3957
  exports.VIRTUAL_SHARE_OFFSET = VIRTUAL_SHARE_OFFSET;
3958
+ exports.accruedInterestWithBuffer = accruedInterestWithBuffer;
3408
3959
  exports.addressesEqual = addressesEqual;
3409
3960
  exports.calculateFeeShares = calculateFeeShares;
3961
+ exports.computePositionValue = computePositionValue;
3962
+ exports.computeSafePositionFloor = computeSafePositionFloor;
3410
3963
  exports.computeStatus = computeStatus;
3411
3964
  exports.convertToShares = convertToShares;
3412
3965
  exports.deserializeSignature = deserializeSignature;
3966
+ exports.divCeil = divCeil;
3413
3967
  exports.findTokenByAddress = findTokenByAddress;
3414
3968
  exports.formatAddress = formatAddress;
3415
3969
  exports.formatDuration = formatDuration;
@@ -3422,6 +3976,7 @@ exports.getCollectionBorrowAcceptanceTypedData = getCollectionBorrowAcceptanceTy
3422
3976
  exports.getCollectionLendOfferTypedData = getCollectionLendOfferTypedData;
3423
3977
  exports.getInscriptionOrderTypedData = getInscriptionOrderTypedData;
3424
3978
  exports.getLendOfferTypedData = getLendOfferTypedData;
3979
+ exports.getNFTCollections = getNFTCollections;
3425
3980
  exports.getRefinanceApprovalTypedData = getRefinanceApprovalTypedData;
3426
3981
  exports.getRefinanceOfferTypedData = getRefinanceOfferTypedData;
3427
3982
  exports.getRenegotiationProposalTypedData = getRenegotiationProposalTypedData;
@@ -3433,9 +3988,12 @@ exports.normalizeAddress = normalizeAddress;
3433
3988
  exports.parseAmount = parseAmount;
3434
3989
  exports.parseEvent = parseEvent;
3435
3990
  exports.parseEvents = parseEvents;
3991
+ exports.proRataInterest = proRataInterest;
3992
+ exports.proportionalAssetValue = proportionalAssetValue;
3436
3993
  exports.resolveNetwork = resolveNetwork;
3437
3994
  exports.scaleByPercentage = scaleByPercentage;
3438
3995
  exports.serializeSignature = serializeSignature;
3996
+ exports.shareProportionBps = shareProportionBps;
3439
3997
  exports.sharesToPercentage = sharesToPercentage;
3440
3998
  exports.toHex = toHex;
3441
3999
  exports.toU256 = toU256;