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