@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.js CHANGED
@@ -256,11 +256,35 @@ var TOKENS = [
256
256
  addresses: {
257
257
  sepolia: "0x04f2345306bf8ef1c8c1445661354ef08421aa092459445a5d6b46641237e943"
258
258
  }
259
+ },
260
+ // NFT Collections (ERC721)
261
+ {
262
+ symbol: "GENESIS",
263
+ name: "Stela Genesis",
264
+ decimals: 0,
265
+ assetType: "ERC721",
266
+ addresses: {
267
+ sepolia: "0x0265ea52ffbf1b7e1a029b94fe1a2023899dd0bc02eb1f11c9b04ea90e957d28"
268
+ }
269
+ },
270
+ {
271
+ symbol: "MockNFT",
272
+ name: "Mock ERC721",
273
+ decimals: 0,
274
+ assetType: "ERC721",
275
+ addresses: {
276
+ sepolia: "0x032bfd52134ad92beae1bc5018a3748e1d1240efd627220e314c07e4c63433d5"
277
+ }
259
278
  }
260
279
  ];
261
280
  function normalizeHex(addr) {
262
281
  return "0x" + addr.replace(/^0x0*/i, "").toLowerCase();
263
282
  }
283
+ function getNFTCollections(network) {
284
+ return TOKENS.filter(
285
+ (t) => t.assetType === "ERC721" && t.addresses[network] !== void 0
286
+ );
287
+ }
264
288
  function getTokensForNetwork(network) {
265
289
  return TOKENS.filter((t) => t.addresses[network] !== void 0);
266
290
  }
@@ -286,6 +310,74 @@ function sharesToPercentage(shares, totalSupply, currentIssuedPercentage) {
286
310
  function calculateFeeShares(shares, feeBps) {
287
311
  return shares * feeBps / MAX_BPS;
288
312
  }
313
+ function divCeil(a, b) {
314
+ if (a === 0n) return 0n;
315
+ return (a + b - 1n) / b;
316
+ }
317
+ function proRataInterest(amount, elapsed, duration) {
318
+ if (amount === 0n || elapsed === 0n) return 0n;
319
+ if (elapsed >= duration) return amount;
320
+ return divCeil(amount * elapsed, duration);
321
+ }
322
+
323
+ // src/math/position.ts
324
+ var DEFAULT_DUST_BUFFER_SECONDS = 60n;
325
+ function shareProportionBps(shares, totalSupply) {
326
+ if (totalSupply === 0n) return 0n;
327
+ return shares * MAX_BPS / totalSupply;
328
+ }
329
+ function proportionalAssetValue(assetValue, shares, totalSupply) {
330
+ if (totalSupply === 0n || shares === 0n) return 0n;
331
+ return assetValue * shares / totalSupply;
332
+ }
333
+ function computePositionValue(params) {
334
+ const { shares, totalSupply, elapsed, duration } = params;
335
+ const shareBps = shareProportionBps(shares, totalSupply);
336
+ const debt = params.debtAssets.map((asset) => ({
337
+ asset,
338
+ proportionalValue: proportionalAssetValue(asset.value, shares, totalSupply)
339
+ }));
340
+ const interest = params.interestAssets.map((asset) => {
341
+ const fullInterest = proportionalAssetValue(asset.value, shares, totalSupply);
342
+ const accruedInterest = duration === 0n ? fullInterest : proRataInterest(fullInterest, elapsed, duration);
343
+ return { asset, fullInterest, accruedInterest };
344
+ });
345
+ const collateral = params.collateralAssets.map((asset) => ({
346
+ asset,
347
+ proportionalValue: proportionalAssetValue(asset.value, shares, totalSupply)
348
+ }));
349
+ return {
350
+ inscriptionId: params.inscriptionId,
351
+ shares,
352
+ totalSupply,
353
+ shareBps,
354
+ debt,
355
+ interest,
356
+ collateral,
357
+ accrued: interest,
358
+ elapsed,
359
+ duration
360
+ };
361
+ }
362
+ function accruedInterestWithBuffer(amount, elapsed, duration, bufferSeconds = DEFAULT_DUST_BUFFER_SECONDS) {
363
+ if (duration === 0n) return amount;
364
+ const bufferedElapsed = elapsed + bufferSeconds;
365
+ return proRataInterest(amount, bufferedElapsed, duration);
366
+ }
367
+ function computeSafePositionFloor(params) {
368
+ const { shares, totalSupply, elapsed, duration } = params;
369
+ const buffer = params.bufferSeconds ?? DEFAULT_DUST_BUFFER_SECONDS;
370
+ const debtFloor = params.debtAssets.map((asset) => ({
371
+ asset,
372
+ proportionalValue: proportionalAssetValue(asset.value, shares, totalSupply)
373
+ }));
374
+ const interestFloor = params.interestAssets.map((asset) => {
375
+ const fullInterest = proportionalAssetValue(asset.value, shares, totalSupply);
376
+ const safeAccrued = accruedInterestWithBuffer(fullInterest, elapsed, duration, buffer);
377
+ return { asset, proportionalValue: safeAccrued };
378
+ });
379
+ return { debtFloor, interestFloor };
380
+ }
289
381
  var SELECTORS = {
290
382
  InscriptionCreated: hash.getSelectorFromName("InscriptionCreated"),
291
383
  InscriptionSigned: hash.getSelectorFromName("InscriptionSigned"),
@@ -1050,6 +1142,210 @@ var stela_default = [
1050
1142
  {
1051
1143
  name: "collateral_asset_count",
1052
1144
  type: "core::integer::u32"
1145
+ },
1146
+ {
1147
+ name: "auction_started",
1148
+ type: "core::bool"
1149
+ },
1150
+ {
1151
+ name: "auction_start_time",
1152
+ type: "core::integer::u64"
1153
+ }
1154
+ ]
1155
+ },
1156
+ {
1157
+ type: "struct",
1158
+ name: "stela::snip12::CollectionLendOffer",
1159
+ members: [
1160
+ {
1161
+ name: "lender",
1162
+ type: "core::starknet::contract_address::ContractAddress"
1163
+ },
1164
+ {
1165
+ name: "debt_hash",
1166
+ type: "core::felt252"
1167
+ },
1168
+ {
1169
+ name: "interest_hash",
1170
+ type: "core::felt252"
1171
+ },
1172
+ {
1173
+ name: "debt_count",
1174
+ type: "core::integer::u32"
1175
+ },
1176
+ {
1177
+ name: "interest_count",
1178
+ type: "core::integer::u32"
1179
+ },
1180
+ {
1181
+ name: "collection_address",
1182
+ type: "core::starknet::contract_address::ContractAddress"
1183
+ },
1184
+ {
1185
+ name: "duration",
1186
+ type: "core::integer::u64"
1187
+ },
1188
+ {
1189
+ name: "deadline",
1190
+ type: "core::integer::u64"
1191
+ },
1192
+ {
1193
+ name: "nonce",
1194
+ type: "core::felt252"
1195
+ }
1196
+ ]
1197
+ },
1198
+ {
1199
+ type: "struct",
1200
+ name: "stela::snip12::CollectionBorrowAcceptance",
1201
+ members: [
1202
+ {
1203
+ name: "offer_hash",
1204
+ type: "core::felt252"
1205
+ },
1206
+ {
1207
+ name: "borrower",
1208
+ type: "core::starknet::contract_address::ContractAddress"
1209
+ },
1210
+ {
1211
+ name: "token_id",
1212
+ type: "core::integer::u256"
1213
+ },
1214
+ {
1215
+ name: "nonce",
1216
+ type: "core::felt252"
1217
+ }
1218
+ ]
1219
+ },
1220
+ {
1221
+ type: "struct",
1222
+ name: "stela::snip12::RenegotiationProposal",
1223
+ members: [
1224
+ {
1225
+ name: "inscription_id",
1226
+ type: "core::integer::u256"
1227
+ },
1228
+ {
1229
+ name: "proposer",
1230
+ type: "core::starknet::contract_address::ContractAddress"
1231
+ },
1232
+ {
1233
+ name: "new_duration",
1234
+ type: "core::integer::u64"
1235
+ },
1236
+ {
1237
+ name: "new_interest_hash",
1238
+ type: "core::felt252"
1239
+ },
1240
+ {
1241
+ name: "new_interest_count",
1242
+ type: "core::integer::u32"
1243
+ },
1244
+ {
1245
+ name: "proposal_deadline",
1246
+ type: "core::integer::u64"
1247
+ },
1248
+ {
1249
+ name: "nonce",
1250
+ type: "core::felt252"
1251
+ }
1252
+ ]
1253
+ },
1254
+ {
1255
+ type: "struct",
1256
+ name: "stela::snip12::CollateralSaleOffer",
1257
+ members: [
1258
+ {
1259
+ name: "inscription_id",
1260
+ type: "core::integer::u256"
1261
+ },
1262
+ {
1263
+ name: "borrower",
1264
+ type: "core::starknet::contract_address::ContractAddress"
1265
+ },
1266
+ {
1267
+ name: "min_price",
1268
+ type: "core::integer::u256"
1269
+ },
1270
+ {
1271
+ name: "payment_token",
1272
+ type: "core::starknet::contract_address::ContractAddress"
1273
+ },
1274
+ {
1275
+ name: "allowed_buyer",
1276
+ type: "core::starknet::contract_address::ContractAddress"
1277
+ },
1278
+ {
1279
+ name: "deadline",
1280
+ type: "core::integer::u64"
1281
+ },
1282
+ {
1283
+ name: "nonce",
1284
+ type: "core::felt252"
1285
+ }
1286
+ ]
1287
+ },
1288
+ {
1289
+ type: "struct",
1290
+ name: "stela::snip12::RefinanceOffer",
1291
+ members: [
1292
+ {
1293
+ name: "inscription_id",
1294
+ type: "core::integer::u256"
1295
+ },
1296
+ {
1297
+ name: "new_lender",
1298
+ type: "core::starknet::contract_address::ContractAddress"
1299
+ },
1300
+ {
1301
+ name: "new_debt_hash",
1302
+ type: "core::felt252"
1303
+ },
1304
+ {
1305
+ name: "new_interest_hash",
1306
+ type: "core::felt252"
1307
+ },
1308
+ {
1309
+ name: "new_debt_count",
1310
+ type: "core::integer::u32"
1311
+ },
1312
+ {
1313
+ name: "new_interest_count",
1314
+ type: "core::integer::u32"
1315
+ },
1316
+ {
1317
+ name: "new_duration",
1318
+ type: "core::integer::u64"
1319
+ },
1320
+ {
1321
+ name: "deadline",
1322
+ type: "core::integer::u64"
1323
+ },
1324
+ {
1325
+ name: "nonce",
1326
+ type: "core::felt252"
1327
+ }
1328
+ ]
1329
+ },
1330
+ {
1331
+ type: "struct",
1332
+ name: "stela::snip12::RefinanceApproval",
1333
+ members: [
1334
+ {
1335
+ name: "inscription_id",
1336
+ type: "core::integer::u256"
1337
+ },
1338
+ {
1339
+ name: "offer_hash",
1340
+ type: "core::felt252"
1341
+ },
1342
+ {
1343
+ name: "borrower",
1344
+ type: "core::starknet::contract_address::ContractAddress"
1345
+ },
1346
+ {
1347
+ name: "nonce",
1348
+ type: "core::felt252"
1053
1349
  }
1054
1350
  ]
1055
1351
  },
@@ -1602,6 +1898,206 @@ var stela_default = [
1602
1898
  ],
1603
1899
  outputs: [],
1604
1900
  state_mutability: "external"
1901
+ },
1902
+ {
1903
+ type: "function",
1904
+ name: "settle_collection",
1905
+ inputs: [
1906
+ {
1907
+ name: "offer",
1908
+ type: "stela::snip12::CollectionLendOffer"
1909
+ },
1910
+ {
1911
+ name: "acceptance",
1912
+ type: "stela::snip12::CollectionBorrowAcceptance"
1913
+ },
1914
+ {
1915
+ name: "debt_assets",
1916
+ type: "core::array::Array::<stela::types::asset::Asset>"
1917
+ },
1918
+ {
1919
+ name: "interest_assets",
1920
+ type: "core::array::Array::<stela::types::asset::Asset>"
1921
+ },
1922
+ {
1923
+ name: "lender_sig",
1924
+ type: "core::array::Array::<core::felt252>"
1925
+ },
1926
+ {
1927
+ name: "borrower_sig",
1928
+ type: "core::array::Array::<core::felt252>"
1929
+ }
1930
+ ],
1931
+ outputs: [],
1932
+ state_mutability: "external"
1933
+ },
1934
+ {
1935
+ type: "function",
1936
+ name: "commit_renegotiation",
1937
+ inputs: [
1938
+ {
1939
+ name: "inscription_id",
1940
+ type: "core::integer::u256"
1941
+ },
1942
+ {
1943
+ name: "proposal_hash",
1944
+ type: "core::felt252"
1945
+ }
1946
+ ],
1947
+ outputs: [],
1948
+ state_mutability: "external"
1949
+ },
1950
+ {
1951
+ type: "function",
1952
+ name: "execute_renegotiation",
1953
+ inputs: [
1954
+ {
1955
+ name: "inscription_id",
1956
+ type: "core::integer::u256"
1957
+ },
1958
+ {
1959
+ name: "proposal",
1960
+ type: "stela::snip12::RenegotiationProposal"
1961
+ },
1962
+ {
1963
+ name: "proposer_sig",
1964
+ type: "core::array::Array::<core::felt252>"
1965
+ },
1966
+ {
1967
+ name: "new_interest_assets",
1968
+ type: "core::array::Array::<stela::types::asset::Asset>"
1969
+ }
1970
+ ],
1971
+ outputs: [],
1972
+ state_mutability: "external"
1973
+ },
1974
+ {
1975
+ type: "function",
1976
+ name: "buy_collateral",
1977
+ inputs: [
1978
+ {
1979
+ name: "inscription_id",
1980
+ type: "core::integer::u256"
1981
+ },
1982
+ {
1983
+ name: "offer",
1984
+ type: "stela::snip12::CollateralSaleOffer"
1985
+ },
1986
+ {
1987
+ name: "borrower_sig",
1988
+ type: "core::array::Array::<core::felt252>"
1989
+ },
1990
+ {
1991
+ name: "sale_price",
1992
+ type: "core::integer::u256"
1993
+ }
1994
+ ],
1995
+ outputs: [],
1996
+ state_mutability: "external"
1997
+ },
1998
+ {
1999
+ type: "function",
2000
+ name: "refinance",
2001
+ inputs: [
2002
+ {
2003
+ name: "offer",
2004
+ type: "stela::snip12::RefinanceOffer"
2005
+ },
2006
+ {
2007
+ name: "new_debt_assets",
2008
+ type: "core::array::Array::<stela::types::asset::Asset>"
2009
+ },
2010
+ {
2011
+ name: "new_interest_assets",
2012
+ type: "core::array::Array::<stela::types::asset::Asset>"
2013
+ },
2014
+ {
2015
+ name: "new_lender_sig",
2016
+ type: "core::array::Array::<core::felt252>"
2017
+ },
2018
+ {
2019
+ name: "approval",
2020
+ type: "stela::snip12::RefinanceApproval"
2021
+ },
2022
+ {
2023
+ name: "borrower_sig",
2024
+ type: "core::array::Array::<core::felt252>"
2025
+ }
2026
+ ],
2027
+ outputs: [],
2028
+ state_mutability: "external"
2029
+ },
2030
+ {
2031
+ type: "function",
2032
+ name: "start_auction",
2033
+ inputs: [
2034
+ {
2035
+ name: "inscription_id",
2036
+ type: "core::integer::u256"
2037
+ }
2038
+ ],
2039
+ outputs: [],
2040
+ state_mutability: "external"
2041
+ },
2042
+ {
2043
+ type: "function",
2044
+ name: "bid",
2045
+ inputs: [
2046
+ {
2047
+ name: "inscription_id",
2048
+ type: "core::integer::u256"
2049
+ }
2050
+ ],
2051
+ outputs: [],
2052
+ state_mutability: "external"
2053
+ },
2054
+ {
2055
+ type: "function",
2056
+ name: "claim_collateral",
2057
+ inputs: [
2058
+ {
2059
+ name: "inscription_id",
2060
+ type: "core::integer::u256"
2061
+ }
2062
+ ],
2063
+ outputs: [],
2064
+ state_mutability: "external"
2065
+ },
2066
+ {
2067
+ type: "function",
2068
+ name: "get_auction_price",
2069
+ inputs: [
2070
+ {
2071
+ name: "inscription_id",
2072
+ type: "core::integer::u256"
2073
+ },
2074
+ {
2075
+ name: "debt_index",
2076
+ type: "core::integer::u32"
2077
+ }
2078
+ ],
2079
+ outputs: [
2080
+ {
2081
+ type: "core::integer::u256"
2082
+ }
2083
+ ],
2084
+ state_mutability: "view"
2085
+ },
2086
+ {
2087
+ type: "function",
2088
+ name: "get_auction_end_time",
2089
+ inputs: [
2090
+ {
2091
+ name: "inscription_id",
2092
+ type: "core::integer::u256"
2093
+ }
2094
+ ],
2095
+ outputs: [
2096
+ {
2097
+ type: "core::integer::u64"
2098
+ }
2099
+ ],
2100
+ state_mutability: "view"
1605
2101
  }
1606
2102
  ]
1607
2103
  },
@@ -3064,9 +3560,14 @@ function extractFieldU256(val) {
3064
3560
  }
3065
3561
  var ShareClient = class {
3066
3562
  contract;
3563
+ address;
3564
+ account;
3067
3565
  constructor(opts) {
3566
+ this.address = opts.stelaAddress;
3068
3567
  this.contract = new Contract(stela_default, opts.stelaAddress, opts.provider);
3568
+ this.account = opts.account;
3069
3569
  }
3570
+ // ── Read Methods ───────────────────────────────────────────────────
3070
3571
  /** Get share balance for an account on a specific inscription */
3071
3572
  async balanceOf(account, inscriptionId) {
3072
3573
  const result = await this.contract.call("balance_of", [account, ...toU256(inscriptionId)]);
@@ -3090,6 +3591,53 @@ var ShareClient = class {
3090
3591
  const result = await this.contract.call("is_approved_for_all", [owner, operator]);
3091
3592
  return Boolean(result[0]);
3092
3593
  }
3594
+ // ── Call Builders ──────────────────────────────────────────────────
3595
+ /**
3596
+ * Build a call to transfer shares (ERC1155 safeTransferFrom).
3597
+ * This enables secondary market trading of lending positions.
3598
+ *
3599
+ * @param from - Current share holder
3600
+ * @param to - Recipient address
3601
+ * @param inscriptionId - The inscription (token ID)
3602
+ * @param amount - Number of shares to transfer
3603
+ * @param data - Optional calldata (empty array by default)
3604
+ */
3605
+ buildTransferShares(from, to, inscriptionId, amount, data = []) {
3606
+ return {
3607
+ contractAddress: this.address,
3608
+ entrypoint: "safe_transfer_from",
3609
+ calldata: [from, to, ...toU256(inscriptionId), ...toU256(amount), String(data.length), ...data]
3610
+ };
3611
+ }
3612
+ /**
3613
+ * Build a call to approve an operator for all ERC1155 tokens.
3614
+ * Required before a marketplace contract can transfer shares on your behalf.
3615
+ *
3616
+ * @param operator - The address to approve (e.g., marketplace contract)
3617
+ * @param approved - Whether to approve or revoke
3618
+ */
3619
+ buildSetApprovalForAll(operator, approved) {
3620
+ return {
3621
+ contractAddress: this.address,
3622
+ entrypoint: "set_approval_for_all",
3623
+ calldata: [operator, approved ? "1" : "0"]
3624
+ };
3625
+ }
3626
+ // ── Execute Methods ────────────────────────────────────────────────
3627
+ /** Transfer shares to another address */
3628
+ async transferShares(from, to, inscriptionId, amount, data = []) {
3629
+ if (!this.account) throw new Error("Account required for write operations");
3630
+ const result = await this.account.execute([
3631
+ this.buildTransferShares(from, to, inscriptionId, amount, data)
3632
+ ]);
3633
+ return { transaction_hash: result.transaction_hash };
3634
+ }
3635
+ /** Approve or revoke an operator for all ERC1155 tokens */
3636
+ async setApprovalForAll(operator, approved) {
3637
+ if (!this.account) throw new Error("Account required for write operations");
3638
+ const result = await this.account.execute([this.buildSetApprovalForAll(operator, approved)]);
3639
+ return { transaction_hash: result.transaction_hash };
3640
+ }
3093
3641
  };
3094
3642
  function extractBigInt(result) {
3095
3643
  const arr = result;
@@ -3374,7 +3922,8 @@ var StelaSdk = class {
3374
3922
  });
3375
3923
  this.shares = new ShareClient({
3376
3924
  stelaAddress: this.stelaAddress,
3377
- provider: opts.provider
3925
+ provider: opts.provider,
3926
+ account: opts.account
3378
3927
  });
3379
3928
  const stelaContract = new Contract(stela_default, this.stelaAddress, opts.provider);
3380
3929
  this.locker = new LockerClient(stelaContract, opts.provider, opts.account);
@@ -3382,6 +3931,6 @@ var StelaSdk = class {
3382
3931
  }
3383
3932
  };
3384
3933
 
3385
- export { ASSET_TYPE_ENUM, ASSET_TYPE_NAMES, AUCTION_DURATION, AUCTION_PENALTY_BPS, AUCTION_RESERVE_BPS, ApiClient, ApiError, CHAIN_ID, EXPLORER_TX_URL, GRACE_PERIOD, InscriptionClient, LockerClient, MAX_BPS, SELECTORS, STATUS_LABELS, STELA_ADDRESS, ShareClient, StelaSdk, TOKENS, VALID_STATUSES, VIRTUAL_SHARE_OFFSET, addressesEqual, calculateFeeShares, computeStatus, convertToShares, deserializeSignature, findTokenByAddress, formatAddress, formatDuration, formatTimestamp, formatTokenValue, fromU256, getBatchLendOfferTypedData, getCollateralSaleOfferTypedData, getCollectionBorrowAcceptanceTypedData, getCollectionLendOfferTypedData, getInscriptionOrderTypedData, getLendOfferTypedData, getRefinanceApprovalTypedData, getRefinanceOfferTypedData, getRenegotiationProposalTypedData, getTokensForNetwork, hashAssets, hashBatchEntries, inscriptionIdToHex, normalizeAddress, parseAmount, parseEvent, parseEvents, resolveNetwork, scaleByPercentage, serializeSignature, sharesToPercentage, toHex, toU256 };
3934
+ export { ASSET_TYPE_ENUM, ASSET_TYPE_NAMES, AUCTION_DURATION, AUCTION_PENALTY_BPS, AUCTION_RESERVE_BPS, ApiClient, ApiError, CHAIN_ID, DEFAULT_DUST_BUFFER_SECONDS, EXPLORER_TX_URL, GRACE_PERIOD, InscriptionClient, LockerClient, MAX_BPS, SELECTORS, STATUS_LABELS, STELA_ADDRESS, ShareClient, StelaSdk, TOKENS, VALID_STATUSES, VIRTUAL_SHARE_OFFSET, accruedInterestWithBuffer, addressesEqual, calculateFeeShares, computePositionValue, computeSafePositionFloor, computeStatus, convertToShares, deserializeSignature, divCeil, findTokenByAddress, formatAddress, formatDuration, formatTimestamp, formatTokenValue, fromU256, getBatchLendOfferTypedData, getCollateralSaleOfferTypedData, getCollectionBorrowAcceptanceTypedData, getCollectionLendOfferTypedData, getInscriptionOrderTypedData, getLendOfferTypedData, getNFTCollections, getRefinanceApprovalTypedData, getRefinanceOfferTypedData, getRenegotiationProposalTypedData, getTokensForNetwork, hashAssets, hashBatchEntries, inscriptionIdToHex, normalizeAddress, parseAmount, parseEvent, parseEvents, proRataInterest, proportionalAssetValue, resolveNetwork, scaleByPercentage, serializeSignature, shareProportionBps, sharesToPercentage, toHex, toU256 };
3386
3935
  //# sourceMappingURL=index.js.map
3387
3936
  //# sourceMappingURL=index.js.map