@msafe/sui3-sdk 0.0.40-pre-c4411fe.0 → 0.0.40-pre-d8a86ee.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
@@ -8586,6 +8586,72 @@ function normalizeAddressFilter(address) {
8586
8586
  }
8587
8587
  return address.length !== 0 ? address.map((addr) => (0, import_utils6.normalizeSuiAddress)(addr)) : void 0;
8588
8588
  }
8589
+ var NotEnoughBalanceError = class extends MPayError {
8590
+ constructor(coinType, requestAmount, gotAmount) {
8591
+ super(4, `Not enough balance: ${coinType}`, {
8592
+ context: {
8593
+ coinType,
8594
+ requestAmount,
8595
+ gotAmount
8596
+ }
8597
+ });
8598
+ this.coinType = coinType;
8599
+ this.requestAmount = requestAmount;
8600
+ this.gotAmount = gotAmount;
8601
+ }
8602
+ };
8603
+ var DEF_REQ_PAGE_SIZE = 25;
8604
+ async function getCoinsWithAmount(suiClient, owner, requestAmount, coinType = "0x2::sui::SUI", pageSize = DEF_REQ_PAGE_SIZE) {
8605
+ const it = new OwnedCoinIterator(suiClient, owner, coinType, pageSize);
8606
+ let totalAmount = BigInt(0);
8607
+ const res = [];
8608
+ while (await it.hasNext() && totalAmount < requestAmount) {
8609
+ const val = await it.next();
8610
+ if (!val) {
8611
+ continue;
8612
+ }
8613
+ res.push(val);
8614
+ totalAmount += BigInt(val.balance);
8615
+ }
8616
+ if (totalAmount < requestAmount) {
8617
+ throw new NotEnoughBalanceError(coinType, requestAmount, totalAmount);
8618
+ }
8619
+ return res;
8620
+ }
8621
+ var OwnedCoinIterator = class extends EntryIterator {
8622
+ constructor(suiClient, owner, coinType, reqPageSize) {
8623
+ super(new OwnedCoinRequester(suiClient, owner, coinType, reqPageSize));
8624
+ this.suiClient = suiClient;
8625
+ this.owner = owner;
8626
+ this.coinType = coinType;
8627
+ this.reqPageSize = reqPageSize;
8628
+ }
8629
+ };
8630
+ var OwnedCoinRequester = class {
8631
+ constructor(suiClient, owner, coinType, reqPageSize) {
8632
+ this.suiClient = suiClient;
8633
+ this.owner = owner;
8634
+ this.coinType = coinType;
8635
+ this.reqPageSize = reqPageSize;
8636
+ if (reqPageSize <= 0) {
8637
+ throw new SanityError("Invalid reqPageSize");
8638
+ }
8639
+ }
8640
+ nextCursor;
8641
+ async doNextRequest() {
8642
+ const res = await this.suiClient.getCoins({
8643
+ owner: this.owner,
8644
+ coinType: this.coinType,
8645
+ cursor: this.nextCursor,
8646
+ limit: this.reqPageSize
8647
+ });
8648
+ this.nextCursor = res.nextCursor;
8649
+ return {
8650
+ data: res.data,
8651
+ hasNext: res.hasNextPage
8652
+ };
8653
+ }
8654
+ };
8589
8655
  var MSafeAccountAdapter = class {
8590
8656
  constructor(msafe) {
8591
8657
  this.msafe = msafe;
@@ -8600,6 +8666,47 @@ var MSafeAccountAdapter = class {
8600
8666
  return this.msafe.requestCoins(reqs);
8601
8667
  }
8602
8668
  };
8669
+ var MSafeSingleWallet = class {
8670
+ constructor(account) {
8671
+ this.account = account;
8672
+ }
8673
+ async address() {
8674
+ return this.account.address;
8675
+ }
8676
+ };
8677
+ var SingleWalletAdapter = class {
8678
+ constructor(singleWallet, suiClient) {
8679
+ this.singleWallet = singleWallet;
8680
+ this.suiClient = suiClient;
8681
+ }
8682
+ get type() {
8683
+ return "single";
8684
+ }
8685
+ async address() {
8686
+ return this.singleWallet.address();
8687
+ }
8688
+ async requestCoins(reqs) {
8689
+ return Promise.all(reqs.map((req) => this.requestCoin(req)));
8690
+ }
8691
+ async requestCoin(req) {
8692
+ if (isSameCoinType(req.coinType, import_utils.SUI_TYPE_ARG)) {
8693
+ return {
8694
+ primaryCoin: GAS_OBJECT_SPEC
8695
+ };
8696
+ }
8697
+ if (req.amount <= 0) {
8698
+ throw new InvalidInputError("Invalid coin request", "coinAmount", req.amount);
8699
+ }
8700
+ const coins = await getCoinsWithAmount(this.suiClient, await this.address(), req.amount, req.coinType);
8701
+ if (coins.length === 0) {
8702
+ throw new SanityError("no coins available");
8703
+ }
8704
+ return {
8705
+ primaryCoin: coins[0].coinObjectId,
8706
+ mergedCoins: coins.slice(1).map((coin) => coin.coinObjectId)
8707
+ };
8708
+ }
8709
+ };
8603
8710
  var MPayClient = class {
8604
8711
  globals;
8605
8712
  helper;
@@ -8607,6 +8714,10 @@ var MPayClient = class {
8607
8714
  this.globals = Globals.new(env, options);
8608
8715
  this.helper = new MPayHelper(this.globals);
8609
8716
  }
8717
+ connectSingleWallet(wallet) {
8718
+ const adapter = new SingleWalletAdapter(wallet, this.globals.suiClient);
8719
+ this.globals.connectWallet(adapter);
8720
+ }
8610
8721
  connectMSafeAccount(msafe) {
8611
8722
  const adapter = new MSafeAccountAdapter(msafe);
8612
8723
  this.globals.connectWallet(adapter);
@@ -8661,11 +8772,12 @@ var CreateStreamIntention = class _CreateStreamIntention extends CoreBaseIntenti
8661
8772
  txType;
8662
8773
  txSubType;
8663
8774
  async build(input) {
8664
- const { network } = input;
8775
+ const { network, account } = input;
8665
8776
  const mpayClient = new MPayClient(
8666
8777
  network === "sui:mainnet" ? "prod" : "dev"
8667
8778
  /* dev */
8668
8779
  );
8780
+ mpayClient.connectSingleWallet(new MSafeSingleWallet(account));
8669
8781
  const txb = await mpayClient.createStream(this.data);
8670
8782
  return txb;
8671
8783
  }