@msafe/sui-app-store 0.0.14 → 0.0.16

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.mjs CHANGED
@@ -355,7 +355,7 @@ var Globals = class _Globals {
355
355
  };
356
356
 
357
357
  // src/apps/mpay/stream/client.ts
358
- import { normalizeStructTag as normalizeStructTag6, normalizeSuiAddress as normalizeSuiAddress3 } from "@mysten/sui.js/utils";
358
+ import { SUI_TYPE_ARG as SUI_TYPE_ARG3, normalizeStructTag as normalizeStructTag6, normalizeSuiAddress as normalizeSuiAddress3 } from "@mysten/sui.js/utils";
359
359
 
360
360
  // src/apps/mpay/stream/helper.ts
361
361
  import { normalizeStructTag as normalizeStructTag3, SUI_TYPE_ARG as SUI_TYPE_ARG2 } from "@mysten/sui.js/utils";
@@ -676,7 +676,7 @@ var CreateStreamHelper = class _CreateStreamHelper {
676
676
  };
677
677
  }
678
678
  amountForRecipient(recipient, numEpoch) {
679
- return recipient.amountPerEpoch * numEpoch + recipient.cliffAmount;
679
+ return BigInt(recipient.amountPerEpoch) * BigInt(numEpoch) + BigInt(recipient.cliffAmount);
680
680
  }
681
681
  get flatSuiFee() {
682
682
  return FLAT_FEE_SUI;
@@ -2032,6 +2032,76 @@ function normalizeAddressFilter(address) {
2032
2032
  return address.length !== 0 ? address.map((addr) => normalizeSuiAddress2(addr)) : void 0;
2033
2033
  }
2034
2034
 
2035
+ // src/apps/mpay/error/NotEnoughBalanceError.ts
2036
+ var NotEnoughBalanceError = class extends MPayError {
2037
+ constructor(coinType, requestAmount, gotAmount) {
2038
+ super(4 /* NotEnoughBalance */, `Not enough balance: ${coinType}`, {
2039
+ context: {
2040
+ coinType,
2041
+ requestAmount,
2042
+ gotAmount
2043
+ }
2044
+ });
2045
+ this.coinType = coinType;
2046
+ this.requestAmount = requestAmount;
2047
+ this.gotAmount = gotAmount;
2048
+ }
2049
+ };
2050
+
2051
+ // src/apps/mpay/sui/iterator/coin.ts
2052
+ var DEF_REQ_PAGE_SIZE = 25;
2053
+ async function getCoinsWithAmount(suiClient, owner, requestAmount, coinType = "0x2::sui::SUI", pageSize = DEF_REQ_PAGE_SIZE) {
2054
+ const it = new OwnedCoinIterator(suiClient, owner, coinType, pageSize);
2055
+ let totalAmount = BigInt(0);
2056
+ const res = [];
2057
+ while (await it.hasNext() && totalAmount < requestAmount) {
2058
+ const val = await it.next();
2059
+ if (!val) {
2060
+ continue;
2061
+ }
2062
+ res.push(val);
2063
+ totalAmount += BigInt(val.balance);
2064
+ }
2065
+ if (totalAmount < requestAmount) {
2066
+ throw new NotEnoughBalanceError(coinType, requestAmount, totalAmount);
2067
+ }
2068
+ return res;
2069
+ }
2070
+ var OwnedCoinIterator = class extends EntryIterator {
2071
+ constructor(suiClient, owner, coinType, reqPageSize) {
2072
+ super(new OwnedCoinRequester(suiClient, owner, coinType, reqPageSize));
2073
+ this.suiClient = suiClient;
2074
+ this.owner = owner;
2075
+ this.coinType = coinType;
2076
+ this.reqPageSize = reqPageSize;
2077
+ }
2078
+ };
2079
+ var OwnedCoinRequester = class {
2080
+ constructor(suiClient, owner, coinType, reqPageSize) {
2081
+ this.suiClient = suiClient;
2082
+ this.owner = owner;
2083
+ this.coinType = coinType;
2084
+ this.reqPageSize = reqPageSize;
2085
+ if (reqPageSize <= 0) {
2086
+ throw new SanityError("Invalid reqPageSize");
2087
+ }
2088
+ }
2089
+ nextCursor;
2090
+ async doNextRequest() {
2091
+ const res = await this.suiClient.getCoins({
2092
+ owner: this.owner,
2093
+ coinType: this.coinType,
2094
+ cursor: this.nextCursor,
2095
+ limit: this.reqPageSize
2096
+ });
2097
+ this.nextCursor = res.nextCursor;
2098
+ return {
2099
+ data: res.data,
2100
+ hasNext: res.hasNextPage
2101
+ };
2102
+ }
2103
+ };
2104
+
2035
2105
  // src/apps/mpay/stream/client.ts
2036
2106
  var MSafeAccountAdapter = class {
2037
2107
  constructor(msafe) {
@@ -2047,6 +2117,47 @@ var MSafeAccountAdapter = class {
2047
2117
  return this.msafe.requestCoins(reqs);
2048
2118
  }
2049
2119
  };
2120
+ var MSafeSingleWallet = class {
2121
+ constructor(account) {
2122
+ this.account = account;
2123
+ }
2124
+ async address() {
2125
+ return this.account.address;
2126
+ }
2127
+ };
2128
+ var SingleWalletAdapter = class {
2129
+ constructor(singleWallet, suiClient) {
2130
+ this.singleWallet = singleWallet;
2131
+ this.suiClient = suiClient;
2132
+ }
2133
+ get type() {
2134
+ return "single" /* single */;
2135
+ }
2136
+ async address() {
2137
+ return this.singleWallet.address();
2138
+ }
2139
+ async requestCoins(reqs) {
2140
+ return Promise.all(reqs.map((req) => this.requestCoin(req)));
2141
+ }
2142
+ async requestCoin(req) {
2143
+ if (isSameCoinType(req.coinType, SUI_TYPE_ARG3)) {
2144
+ return {
2145
+ primaryCoin: GAS_OBJECT_SPEC
2146
+ };
2147
+ }
2148
+ if (req.amount <= 0) {
2149
+ throw new InvalidInputError("Invalid coin request", "coinAmount", req.amount);
2150
+ }
2151
+ const coins = await getCoinsWithAmount(this.suiClient, await this.address(), req.amount, req.coinType);
2152
+ if (coins.length === 0) {
2153
+ throw new SanityError("no coins available");
2154
+ }
2155
+ return {
2156
+ primaryCoin: coins[0].coinObjectId,
2157
+ mergedCoins: coins.slice(1).map((coin) => coin.coinObjectId)
2158
+ };
2159
+ }
2160
+ };
2050
2161
  var MPayClient = class {
2051
2162
  globals;
2052
2163
  helper;
@@ -2054,6 +2165,10 @@ var MPayClient = class {
2054
2165
  this.globals = Globals.new(env, options);
2055
2166
  this.helper = new MPayHelper(this.globals);
2056
2167
  }
2168
+ connectSingleWallet(wallet) {
2169
+ const adapter = new SingleWalletAdapter(wallet, this.globals.suiClient);
2170
+ this.globals.connectWallet(adapter);
2171
+ }
2057
2172
  connectMSafeAccount(msafe) {
2058
2173
  const adapter = new MSafeAccountAdapter(msafe);
2059
2174
  this.globals.connectWallet(adapter);
@@ -2110,8 +2225,9 @@ var CreateStreamIntention = class _CreateStreamIntention extends CoreBaseIntenti
2110
2225
  txType;
2111
2226
  txSubType;
2112
2227
  async build(input) {
2113
- const { network } = input;
2228
+ const { network, account } = input;
2114
2229
  const mpayClient = new MPayClient(network === "sui:mainnet" ? "prod" /* prod */ : "dev" /* dev */);
2230
+ mpayClient.connectSingleWallet(new MSafeSingleWallet(account));
2115
2231
  const txb = await mpayClient.createStream(this.data);
2116
2232
  return txb;
2117
2233
  }
@@ -2121,7 +2237,7 @@ var CreateStreamIntention = class _CreateStreamIntention extends CoreBaseIntenti
2121
2237
  };
2122
2238
 
2123
2239
  // src/apps/mpay/decoder/create.ts
2124
- import { normalizeStructTag as normalizeStructTag8, SUI_TYPE_ARG as SUI_TYPE_ARG3 } from "@mysten/sui.js/utils";
2240
+ import { normalizeStructTag as normalizeStructTag8, SUI_TYPE_ARG as SUI_TYPE_ARG4 } from "@mysten/sui.js/utils";
2125
2241
 
2126
2242
  // src/apps/mpay/decoder/moveCall.ts
2127
2243
  import { bcs as bcs2 } from "@mysten/sui.js/bcs";
@@ -2314,11 +2430,11 @@ var CreateStreamDecodeHelper = class {
2314
2430
  const coinType = moveCall.typeArg(0);
2315
2431
  const paymentCoin = moveCall.txArg(2);
2316
2432
  const paymentCoinMerge = this.getCoinMergeFromNestedResult(paymentCoin, coinType, moveCall);
2317
- if (coinType === normalizeStructTag8(SUI_TYPE_ARG3)) {
2433
+ if (coinType === normalizeStructTag8(SUI_TYPE_ARG4)) {
2318
2434
  return [paymentCoinMerge];
2319
2435
  }
2320
2436
  const flatFeeCoin = moveCall.txArg(3);
2321
- const flatCoinMerge = this.getCoinMergeFromNestedResult(flatFeeCoin, normalizeStructTag8(SUI_TYPE_ARG3), moveCall);
2437
+ const flatCoinMerge = this.getCoinMergeFromNestedResult(flatFeeCoin, normalizeStructTag8(SUI_TYPE_ARG4), moveCall);
2322
2438
  return [paymentCoinMerge, flatCoinMerge];
2323
2439
  }
2324
2440
  getCoinMergeFromNestedResult(coinArg, coinType, moveCall) {