@owney/sdk 0.3.1 → 0.3.2

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
@@ -948,18 +948,27 @@ var OwneySDK = class {
948
948
  }
949
949
  const agent = this.getAgent(agentId);
950
950
  this.validateAssetSupport(agent, chainId, asset);
951
- this.validateMinDepositAmount(agent, chainId, asset, amount);
951
+ await this.validateMinDepositAmount(agent, state, chainId, asset, amount);
952
952
  return agent.deposit(state, chainId, amount, depositCallback);
953
953
  }
954
954
  const totalAmount = BigInt(amount);
955
955
  const eligibleAgents = this.getEligibleAgents(chainId, asset, {
956
956
  excludeDisabled: true
957
957
  });
958
+ const exemptFlags = await Promise.all(
959
+ eligibleAgents.map(
960
+ (a) => this.hasExistingBalance(a, state, chainId, asset)
961
+ )
962
+ );
963
+ const exempt = new Set(
964
+ eligibleAgents.filter((_, i) => exemptFlags[i]).map((a) => a.id)
965
+ );
958
966
  const agentAmounts = this.splitDepositAmount(
959
967
  totalAmount,
960
968
  eligibleAgents,
961
969
  chainId,
962
- asset
970
+ asset,
971
+ exempt
963
972
  );
964
973
  if (agentAmounts.length === 0) {
965
974
  throw new OwneyError(
@@ -986,7 +995,7 @@ var OwneySDK = class {
986
995
  const assetEntry = chainAssets?.assets.find((a) => a.symbol === asset);
987
996
  return BigInt(assetEntry?.minDepositAmount ?? "0");
988
997
  }
989
- splitDepositAmount(totalAmount, agents, chainId, asset) {
998
+ splitDepositAmount(totalAmount, agents, chainId, asset, exempt = /* @__PURE__ */ new Set()) {
990
999
  if (agents.length === 0)
991
1000
  return [];
992
1001
  const perAgent = totalAmount / BigInt(agents.length);
@@ -996,7 +1005,7 @@ var OwneySDK = class {
996
1005
  amount: i === agents.length - 1 ? perAgent + remainder : perAgent
997
1006
  }));
998
1007
  const valid = splits.filter(
999
- (s) => s.amount >= this.getMinDepositAmount(s.agent, chainId, asset)
1008
+ (s) => exempt.has(s.agent.id) || s.amount >= this.getMinDepositAmount(s.agent, chainId, asset)
1000
1009
  );
1001
1010
  if (valid.length === agents.length) {
1002
1011
  return splits.map((s) => ({ agent: s.agent, amount: String(s.amount) }));
@@ -1005,24 +1014,45 @@ var OwneySDK = class {
1005
1014
  totalAmount,
1006
1015
  valid.map((s) => s.agent),
1007
1016
  chainId,
1008
- asset
1017
+ asset,
1018
+ exempt
1009
1019
  );
1010
1020
  }
1011
- validateMinDepositAmount(agent, chainId, asset, amount) {
1021
+ async validateMinDepositAmount(agent, state, chainId, asset, amount) {
1012
1022
  const minDepositAmount = this.getMinDepositAmount(agent, chainId, asset);
1013
1023
  const parsedAmount = BigInt(amount);
1014
- if (parsedAmount < minDepositAmount) {
1015
- throw new OwneyError(
1016
- "DEPOSIT_AMOUNT_BELOW_MINIMUM",
1017
- `Amount "${amount}" is below the required minimum deposit "${minDepositAmount.toString()}" for agent "${agent.id}" on chain ${chainId} for asset "${asset}".`,
1018
- {
1019
- agentId: agent.id,
1020
- chainId,
1021
- asset,
1022
- amount,
1023
- minDepositAmount: minDepositAmount.toString()
1024
- }
1024
+ if (parsedAmount >= minDepositAmount)
1025
+ return;
1026
+ if (await this.hasExistingBalance(agent, state, chainId, asset))
1027
+ return;
1028
+ throw new OwneyError(
1029
+ "DEPOSIT_AMOUNT_BELOW_MINIMUM",
1030
+ `Amount "${amount}" is below the required minimum deposit "${minDepositAmount.toString()}" for agent "${agent.id}" on chain ${chainId} for asset "${asset}".`,
1031
+ {
1032
+ agentId: agent.id,
1033
+ chainId,
1034
+ asset,
1035
+ amount,
1036
+ minDepositAmount: minDepositAmount.toString()
1037
+ }
1038
+ );
1039
+ }
1040
+ /**
1041
+ * Whether the user already holds a non-zero balance with `agent` for the
1042
+ * given `(chainId, asset)`. Used to exempt top-up deposits from the
1043
+ * per-agent minimum. Fails closed: any error reading balances returns
1044
+ * false, so the minimum is enforced as today.
1045
+ */
1046
+ async hasExistingBalance(agent, state, chainId, asset) {
1047
+ try {
1048
+ const balance = await agent.getBalances(state, chainId);
1049
+ const target = asset.toLowerCase();
1050
+ const token = balance.tokens.find(
1051
+ (t) => t.chainId === chainId && t.asset.toLowerCase() === target
1025
1052
  );
1053
+ return !!token && BigInt(token.amount) > 0n;
1054
+ } catch {
1055
+ return false;
1026
1056
  }
1027
1057
  }
1028
1058
  validateAssetSupport(agent, chainId, asset) {
package/dist/index.d.cts CHANGED
@@ -278,6 +278,13 @@ declare class OwneySDK {
278
278
  private getMinDepositAmount;
279
279
  private splitDepositAmount;
280
280
  private validateMinDepositAmount;
281
+ /**
282
+ * Whether the user already holds a non-zero balance with `agent` for the
283
+ * given `(chainId, asset)`. Used to exempt top-up deposits from the
284
+ * per-agent minimum. Fails closed: any error reading balances returns
285
+ * false, so the minimum is enforced as today.
286
+ */
287
+ private hasExistingBalance;
281
288
  private validateAssetSupport;
282
289
  private getEligibleAgents;
283
290
  /**
package/dist/index.d.ts CHANGED
@@ -278,6 +278,13 @@ declare class OwneySDK {
278
278
  private getMinDepositAmount;
279
279
  private splitDepositAmount;
280
280
  private validateMinDepositAmount;
281
+ /**
282
+ * Whether the user already holds a non-zero balance with `agent` for the
283
+ * given `(chainId, asset)`. Used to exempt top-up deposits from the
284
+ * per-agent minimum. Fails closed: any error reading balances returns
285
+ * false, so the minimum is enforced as today.
286
+ */
287
+ private hasExistingBalance;
281
288
  private validateAssetSupport;
282
289
  private getEligibleAgents;
283
290
  /**
package/dist/index.js CHANGED
@@ -918,18 +918,27 @@ var OwneySDK = class {
918
918
  }
919
919
  const agent = this.getAgent(agentId);
920
920
  this.validateAssetSupport(agent, chainId, asset);
921
- this.validateMinDepositAmount(agent, chainId, asset, amount);
921
+ await this.validateMinDepositAmount(agent, state, chainId, asset, amount);
922
922
  return agent.deposit(state, chainId, amount, depositCallback);
923
923
  }
924
924
  const totalAmount = BigInt(amount);
925
925
  const eligibleAgents = this.getEligibleAgents(chainId, asset, {
926
926
  excludeDisabled: true
927
927
  });
928
+ const exemptFlags = await Promise.all(
929
+ eligibleAgents.map(
930
+ (a) => this.hasExistingBalance(a, state, chainId, asset)
931
+ )
932
+ );
933
+ const exempt = new Set(
934
+ eligibleAgents.filter((_, i) => exemptFlags[i]).map((a) => a.id)
935
+ );
928
936
  const agentAmounts = this.splitDepositAmount(
929
937
  totalAmount,
930
938
  eligibleAgents,
931
939
  chainId,
932
- asset
940
+ asset,
941
+ exempt
933
942
  );
934
943
  if (agentAmounts.length === 0) {
935
944
  throw new OwneyError(
@@ -956,7 +965,7 @@ var OwneySDK = class {
956
965
  const assetEntry = chainAssets?.assets.find((a) => a.symbol === asset);
957
966
  return BigInt(assetEntry?.minDepositAmount ?? "0");
958
967
  }
959
- splitDepositAmount(totalAmount, agents, chainId, asset) {
968
+ splitDepositAmount(totalAmount, agents, chainId, asset, exempt = /* @__PURE__ */ new Set()) {
960
969
  if (agents.length === 0)
961
970
  return [];
962
971
  const perAgent = totalAmount / BigInt(agents.length);
@@ -966,7 +975,7 @@ var OwneySDK = class {
966
975
  amount: i === agents.length - 1 ? perAgent + remainder : perAgent
967
976
  }));
968
977
  const valid = splits.filter(
969
- (s) => s.amount >= this.getMinDepositAmount(s.agent, chainId, asset)
978
+ (s) => exempt.has(s.agent.id) || s.amount >= this.getMinDepositAmount(s.agent, chainId, asset)
970
979
  );
971
980
  if (valid.length === agents.length) {
972
981
  return splits.map((s) => ({ agent: s.agent, amount: String(s.amount) }));
@@ -975,24 +984,45 @@ var OwneySDK = class {
975
984
  totalAmount,
976
985
  valid.map((s) => s.agent),
977
986
  chainId,
978
- asset
987
+ asset,
988
+ exempt
979
989
  );
980
990
  }
981
- validateMinDepositAmount(agent, chainId, asset, amount) {
991
+ async validateMinDepositAmount(agent, state, chainId, asset, amount) {
982
992
  const minDepositAmount = this.getMinDepositAmount(agent, chainId, asset);
983
993
  const parsedAmount = BigInt(amount);
984
- if (parsedAmount < minDepositAmount) {
985
- throw new OwneyError(
986
- "DEPOSIT_AMOUNT_BELOW_MINIMUM",
987
- `Amount "${amount}" is below the required minimum deposit "${minDepositAmount.toString()}" for agent "${agent.id}" on chain ${chainId} for asset "${asset}".`,
988
- {
989
- agentId: agent.id,
990
- chainId,
991
- asset,
992
- amount,
993
- minDepositAmount: minDepositAmount.toString()
994
- }
994
+ if (parsedAmount >= minDepositAmount)
995
+ return;
996
+ if (await this.hasExistingBalance(agent, state, chainId, asset))
997
+ return;
998
+ throw new OwneyError(
999
+ "DEPOSIT_AMOUNT_BELOW_MINIMUM",
1000
+ `Amount "${amount}" is below the required minimum deposit "${minDepositAmount.toString()}" for agent "${agent.id}" on chain ${chainId} for asset "${asset}".`,
1001
+ {
1002
+ agentId: agent.id,
1003
+ chainId,
1004
+ asset,
1005
+ amount,
1006
+ minDepositAmount: minDepositAmount.toString()
1007
+ }
1008
+ );
1009
+ }
1010
+ /**
1011
+ * Whether the user already holds a non-zero balance with `agent` for the
1012
+ * given `(chainId, asset)`. Used to exempt top-up deposits from the
1013
+ * per-agent minimum. Fails closed: any error reading balances returns
1014
+ * false, so the minimum is enforced as today.
1015
+ */
1016
+ async hasExistingBalance(agent, state, chainId, asset) {
1017
+ try {
1018
+ const balance = await agent.getBalances(state, chainId);
1019
+ const target = asset.toLowerCase();
1020
+ const token = balance.tokens.find(
1021
+ (t) => t.chainId === chainId && t.asset.toLowerCase() === target
995
1022
  );
1023
+ return !!token && BigInt(token.amount) > 0n;
1024
+ } catch {
1025
+ return false;
996
1026
  }
997
1027
  }
998
1028
  validateAssetSupport(agent, chainId, asset) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",