@msafe/sui-app-store 0.0.15 → 0.0.17

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
@@ -568,8 +568,9 @@ function isSameTarget(target1, target2) {
568
568
  var GAS_OBJECT_SPEC = "txn.gas";
569
569
 
570
570
  // src/apps/mpay/utils/random.ts
571
+ var import_nanoid = require("nanoid");
571
572
  function generateGroupId() {
572
- return "nanoid()";
573
+ return (0, import_nanoid.nanoid)();
573
574
  }
574
575
 
575
576
  // src/apps/mpay/builder/CreateStreamHelper.ts
@@ -2062,6 +2063,76 @@ function normalizeAddressFilter(address) {
2062
2063
  return address.length !== 0 ? address.map((addr) => (0, import_utils8.normalizeSuiAddress)(addr)) : void 0;
2063
2064
  }
2064
2065
 
2066
+ // src/apps/mpay/error/NotEnoughBalanceError.ts
2067
+ var NotEnoughBalanceError = class extends MPayError {
2068
+ constructor(coinType, requestAmount, gotAmount) {
2069
+ super(4 /* NotEnoughBalance */, `Not enough balance: ${coinType}`, {
2070
+ context: {
2071
+ coinType,
2072
+ requestAmount,
2073
+ gotAmount
2074
+ }
2075
+ });
2076
+ this.coinType = coinType;
2077
+ this.requestAmount = requestAmount;
2078
+ this.gotAmount = gotAmount;
2079
+ }
2080
+ };
2081
+
2082
+ // src/apps/mpay/sui/iterator/coin.ts
2083
+ var DEF_REQ_PAGE_SIZE = 25;
2084
+ async function getCoinsWithAmount(suiClient, owner, requestAmount, coinType = "0x2::sui::SUI", pageSize = DEF_REQ_PAGE_SIZE) {
2085
+ const it = new OwnedCoinIterator(suiClient, owner, coinType, pageSize);
2086
+ let totalAmount = BigInt(0);
2087
+ const res = [];
2088
+ while (await it.hasNext() && totalAmount < requestAmount) {
2089
+ const val = await it.next();
2090
+ if (!val) {
2091
+ continue;
2092
+ }
2093
+ res.push(val);
2094
+ totalAmount += BigInt(val.balance);
2095
+ }
2096
+ if (totalAmount < requestAmount) {
2097
+ throw new NotEnoughBalanceError(coinType, requestAmount, totalAmount);
2098
+ }
2099
+ return res;
2100
+ }
2101
+ var OwnedCoinIterator = class extends EntryIterator {
2102
+ constructor(suiClient, owner, coinType, reqPageSize) {
2103
+ super(new OwnedCoinRequester(suiClient, owner, coinType, reqPageSize));
2104
+ this.suiClient = suiClient;
2105
+ this.owner = owner;
2106
+ this.coinType = coinType;
2107
+ this.reqPageSize = reqPageSize;
2108
+ }
2109
+ };
2110
+ var OwnedCoinRequester = class {
2111
+ constructor(suiClient, owner, coinType, reqPageSize) {
2112
+ this.suiClient = suiClient;
2113
+ this.owner = owner;
2114
+ this.coinType = coinType;
2115
+ this.reqPageSize = reqPageSize;
2116
+ if (reqPageSize <= 0) {
2117
+ throw new SanityError("Invalid reqPageSize");
2118
+ }
2119
+ }
2120
+ nextCursor;
2121
+ async doNextRequest() {
2122
+ const res = await this.suiClient.getCoins({
2123
+ owner: this.owner,
2124
+ coinType: this.coinType,
2125
+ cursor: this.nextCursor,
2126
+ limit: this.reqPageSize
2127
+ });
2128
+ this.nextCursor = res.nextCursor;
2129
+ return {
2130
+ data: res.data,
2131
+ hasNext: res.hasNextPage
2132
+ };
2133
+ }
2134
+ };
2135
+
2065
2136
  // src/apps/mpay/stream/client.ts
2066
2137
  var MSafeAccountAdapter = class {
2067
2138
  constructor(msafe) {
@@ -2077,6 +2148,47 @@ var MSafeAccountAdapter = class {
2077
2148
  return this.msafe.requestCoins(reqs);
2078
2149
  }
2079
2150
  };
2151
+ var MSafeSingleWallet = class {
2152
+ constructor(account) {
2153
+ this.account = account;
2154
+ }
2155
+ async address() {
2156
+ return this.account.address;
2157
+ }
2158
+ };
2159
+ var SingleWalletAdapter = class {
2160
+ constructor(singleWallet, suiClient) {
2161
+ this.singleWallet = singleWallet;
2162
+ this.suiClient = suiClient;
2163
+ }
2164
+ get type() {
2165
+ return "single" /* single */;
2166
+ }
2167
+ async address() {
2168
+ return this.singleWallet.address();
2169
+ }
2170
+ async requestCoins(reqs) {
2171
+ return Promise.all(reqs.map((req) => this.requestCoin(req)));
2172
+ }
2173
+ async requestCoin(req) {
2174
+ if (isSameCoinType(req.coinType, import_utils9.SUI_TYPE_ARG)) {
2175
+ return {
2176
+ primaryCoin: GAS_OBJECT_SPEC
2177
+ };
2178
+ }
2179
+ if (req.amount <= 0) {
2180
+ throw new InvalidInputError("Invalid coin request", "coinAmount", req.amount);
2181
+ }
2182
+ const coins = await getCoinsWithAmount(this.suiClient, await this.address(), req.amount, req.coinType);
2183
+ if (coins.length === 0) {
2184
+ throw new SanityError("no coins available");
2185
+ }
2186
+ return {
2187
+ primaryCoin: coins[0].coinObjectId,
2188
+ mergedCoins: coins.slice(1).map((coin) => coin.coinObjectId)
2189
+ };
2190
+ }
2191
+ };
2080
2192
  var MPayClient = class {
2081
2193
  globals;
2082
2194
  helper;
@@ -2084,6 +2196,10 @@ var MPayClient = class {
2084
2196
  this.globals = Globals.new(env, options);
2085
2197
  this.helper = new MPayHelper(this.globals);
2086
2198
  }
2199
+ connectSingleWallet(wallet) {
2200
+ const adapter = new SingleWalletAdapter(wallet, this.globals.suiClient);
2201
+ this.globals.connectWallet(adapter);
2202
+ }
2087
2203
  connectMSafeAccount(msafe) {
2088
2204
  const adapter = new MSafeAccountAdapter(msafe);
2089
2205
  this.globals.connectWallet(adapter);
@@ -2140,8 +2256,9 @@ var CreateStreamIntention = class _CreateStreamIntention extends CoreBaseIntenti
2140
2256
  txType;
2141
2257
  txSubType;
2142
2258
  async build(input) {
2143
- const { network } = input;
2259
+ const { network, account } = input;
2144
2260
  const mpayClient = new MPayClient(network === "sui:mainnet" ? "prod" /* prod */ : "dev" /* dev */);
2261
+ mpayClient.connectSingleWallet(new MSafeSingleWallet(account));
2145
2262
  const txb = await mpayClient.createStream(this.data);
2146
2263
  return txb;
2147
2264
  }
@@ -2151,11 +2268,11 @@ var CreateStreamIntention = class _CreateStreamIntention extends CoreBaseIntenti
2151
2268
  };
2152
2269
 
2153
2270
  // src/apps/mpay/decoder/create.ts
2154
- var import_utils11 = require("@mysten/sui.js/utils");
2271
+ var import_utils12 = require("@mysten/sui.js/utils");
2155
2272
 
2156
2273
  // src/apps/mpay/decoder/moveCall.ts
2157
2274
  var import_bcs2 = require("@mysten/sui.js/bcs");
2158
- var import_utils10 = require("@mysten/sui.js/utils");
2275
+ var import_utils11 = require("@mysten/sui.js/utils");
2159
2276
  var MoveCallHelper = class _MoveCallHelper {
2160
2277
  constructor(moveCall, txb) {
2161
2278
  this.moveCall = moveCall;
@@ -2175,7 +2292,7 @@ var MoveCallHelper = class _MoveCallHelper {
2175
2292
  }
2176
2293
  decodeInputAddress(argIndex) {
2177
2294
  const input = this.decodePureArg(argIndex, "address");
2178
- return (0, import_utils10.normalizeSuiAddress)(input);
2295
+ return (0, import_utils11.normalizeSuiAddress)(input);
2179
2296
  }
2180
2297
  decodeInputString(argIndex) {
2181
2298
  return this.decodePureArg(argIndex, "string");
@@ -2212,21 +2329,21 @@ var MoveCallHelper = class _MoveCallHelper {
2212
2329
  if (!("Object" in input.value) || !("ImmOrOwned" in input.value.Object)) {
2213
2330
  throw new Error("not ImmOrOwned");
2214
2331
  }
2215
- return (0, import_utils10.normalizeSuiAddress)(input.value.Object.ImmOrOwned.objectId);
2332
+ return (0, import_utils11.normalizeSuiAddress)(input.value.Object.ImmOrOwned.objectId);
2216
2333
  }
2217
- return (0, import_utils10.normalizeSuiAddress)(input.value);
2334
+ return (0, import_utils11.normalizeSuiAddress)(input.value);
2218
2335
  }
2219
2336
  static getSharedObjectId(input) {
2220
2337
  if (input.type !== "object") {
2221
2338
  throw new Error(`not object argument: ${JSON.stringify(input)}`);
2222
2339
  }
2223
2340
  if (typeof input.value !== "object") {
2224
- return (0, import_utils10.normalizeSuiAddress)(input.value);
2341
+ return (0, import_utils11.normalizeSuiAddress)(input.value);
2225
2342
  }
2226
2343
  if (!("Object" in input.value) || !("Shared" in input.value.Object)) {
2227
2344
  throw new Error("not Shared");
2228
2345
  }
2229
- return (0, import_utils10.normalizeSuiAddress)(input.value.Object.Shared.objectId);
2346
+ return (0, import_utils11.normalizeSuiAddress)(input.value.Object.Shared.objectId);
2230
2347
  }
2231
2348
  static getPureInput(input, bcsType) {
2232
2349
  if (input.type !== "pure") {
@@ -2242,7 +2359,7 @@ var MoveCallHelper = class _MoveCallHelper {
2242
2359
  return import_bcs2.bcs.de(bcsType, new Uint8Array(bcsVal));
2243
2360
  }
2244
2361
  typeArg(index) {
2245
- return (0, import_utils10.normalizeStructTag)(this.moveCall.typeArguments[index]);
2362
+ return (0, import_utils11.normalizeStructTag)(this.moveCall.typeArguments[index]);
2246
2363
  }
2247
2364
  txArg(index) {
2248
2365
  return this.moveCall.arguments[index];
@@ -2344,11 +2461,11 @@ var CreateStreamDecodeHelper = class {
2344
2461
  const coinType = moveCall.typeArg(0);
2345
2462
  const paymentCoin = moveCall.txArg(2);
2346
2463
  const paymentCoinMerge = this.getCoinMergeFromNestedResult(paymentCoin, coinType, moveCall);
2347
- if (coinType === (0, import_utils11.normalizeStructTag)(import_utils11.SUI_TYPE_ARG)) {
2464
+ if (coinType === (0, import_utils12.normalizeStructTag)(import_utils12.SUI_TYPE_ARG)) {
2348
2465
  return [paymentCoinMerge];
2349
2466
  }
2350
2467
  const flatFeeCoin = moveCall.txArg(3);
2351
- const flatCoinMerge = this.getCoinMergeFromNestedResult(flatFeeCoin, (0, import_utils11.normalizeStructTag)(import_utils11.SUI_TYPE_ARG), moveCall);
2468
+ const flatCoinMerge = this.getCoinMergeFromNestedResult(flatFeeCoin, (0, import_utils12.normalizeStructTag)(import_utils12.SUI_TYPE_ARG), moveCall);
2352
2469
  return [paymentCoinMerge, flatCoinMerge];
2353
2470
  }
2354
2471
  getCoinMergeFromNestedResult(coinArg, coinType, moveCall) {
@@ -2712,7 +2829,7 @@ var MSafeApps = class {
2712
2829
  // src/apps/plain-transaction/helper.ts
2713
2830
  var import_sui3_utils6 = require("@msafe/sui3-utils");
2714
2831
  var import_transactions9 = require("@mysten/sui.js/transactions");
2715
- var import_utils14 = require("@mysten/sui.js/utils");
2832
+ var import_utils15 = require("@mysten/sui.js/utils");
2716
2833
  var import_sort_keys_recursive2 = __toESM(require("sort-keys-recursive"));
2717
2834
  var PlainTransactionApplication = "msafe-plain-tx";
2718
2835
  var PlainTransactionType = import_sui3_utils6.TransactionSubTypes.others.plain;
@@ -2727,11 +2844,11 @@ var PlainTransactionHelper = class {
2727
2844
  return {
2728
2845
  txType: import_sui3_utils6.TransactionType.Other,
2729
2846
  txSubType: PlainTransactionType,
2730
- intentionData: { content: (0, import_utils14.toHEX)(content) }
2847
+ intentionData: { content: (0, import_utils15.toHEX)(content) }
2731
2848
  };
2732
2849
  }
2733
2850
  async build(input) {
2734
- return import_transactions9.TransactionBlock.from((0, import_utils14.fromHEX)(input.intentionData.content));
2851
+ return import_transactions9.TransactionBlock.from((0, import_utils15.fromHEX)(input.intentionData.content));
2735
2852
  }
2736
2853
  };
2737
2854