@toon-protocol/client-mcp 0.20.4 → 0.20.5

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.
@@ -3511,7 +3511,7 @@ function buildIlpPrepare(params) {
3511
3511
  };
3512
3512
  }
3513
3513
 
3514
- // ../client/dist/chunk-OM3MHJHC.js
3514
+ // ../client/dist/chunk-7TDAU43L.js
3515
3515
  var cachedO1js = null;
3516
3516
  var cachedPaymentChannel = null;
3517
3517
  var compiledContract = null;
@@ -3564,6 +3564,72 @@ async function getCompiledPaymentChannel() {
3564
3564
  function getCompiledVerificationKeyHash() {
3565
3565
  return compiledVerificationKeyHash;
3566
3566
  }
3567
+ function abandonLeakedMinaTransaction(o1js) {
3568
+ const ctx = o1js.Mina.currentTransaction;
3569
+ const c = ctx;
3570
+ if (!c || typeof c.has !== "function" || typeof c.leave !== "function")
3571
+ return;
3572
+ for (let i = 0; i < 64 && c.has(); i += 1) {
3573
+ try {
3574
+ const id = typeof c.id === "function" ? c.id() : void 0;
3575
+ c.leave(id);
3576
+ } catch {
3577
+ break;
3578
+ }
3579
+ }
3580
+ }
3581
+ async function buildMinaTransaction(o1js, feePayer, f) {
3582
+ try {
3583
+ return await o1js.Mina.transaction(feePayer, f);
3584
+ } catch (err) {
3585
+ abandonLeakedMinaTransaction(o1js);
3586
+ throw err;
3587
+ }
3588
+ }
3589
+ var MINA_ACCOUNT_CREATION_FEE_NANOMINA = 1000000000n;
3590
+ var MinaFeePayerUnfundedError = class extends Error {
3591
+ constructor(address, requiredNanomina, haveNanomina, graphqlUrl) {
3592
+ const mina = (n) => `${(Number(n) / 1e9).toFixed(3)} MINA`;
3593
+ const state = haveNanomina === void 0 ? `does not exist on-chain (0 MINA)` : `holds only ${mina(haveNanomina)}`;
3594
+ super(
3595
+ `Mina fee-payer wallet ${address} ${state} but needs ~${mina(
3596
+ requiredNanomina
3597
+ )} to open a payment channel (\u22481 MINA account-creation fee + tx fees) on ${graphqlUrl}. Fund ${address} and retry \u2014 no circuit was compiled (this check runs before the ~1-3 min compile).`
3598
+ );
3599
+ this.address = address;
3600
+ this.requiredNanomina = requiredNanomina;
3601
+ this.haveNanomina = haveNanomina;
3602
+ this.graphqlUrl = graphqlUrl;
3603
+ this.name = "MinaFeePayerUnfundedError";
3604
+ }
3605
+ address;
3606
+ requiredNanomina;
3607
+ haveNanomina;
3608
+ graphqlUrl;
3609
+ };
3610
+ async function assertMinaFeePayerFunded(params) {
3611
+ const { o1js, payerPublicKey, requiredNanomina, graphqlUrl } = params;
3612
+ const address = payerPublicKey.toBase58();
3613
+ const res = await o1js.fetchAccount({ publicKey: payerPublicKey });
3614
+ if (res.error || !res.account) {
3615
+ throw new MinaFeePayerUnfundedError(
3616
+ address,
3617
+ requiredNanomina,
3618
+ void 0,
3619
+ graphqlUrl
3620
+ );
3621
+ }
3622
+ const raw = res.account.balance?.toString();
3623
+ const have = raw !== void 0 ? BigInt(raw) : 0n;
3624
+ if (have < requiredNanomina) {
3625
+ throw new MinaFeePayerUnfundedError(
3626
+ address,
3627
+ requiredNanomina,
3628
+ have,
3629
+ graphqlUrl
3630
+ );
3631
+ }
3632
+ }
3567
3633
  var MINA_CHANNEL_STATE_OPEN = 1n;
3568
3634
  var MINA_CHANNEL_STATE_UNINITIALIZED = 0n;
3569
3635
  async function openMinaChannelOnChain(params) {
@@ -3622,7 +3688,8 @@ async function openMinaChannelOnChain(params) {
3622
3688
  const tokenIdField = Field2(params.tokenId ?? "1");
3623
3689
  await fetchAccount({ publicKey: zkAppPublicKey });
3624
3690
  await fetchAccount({ publicKey: payerPublicKey });
3625
- const initTx = await Mina.transaction(
3691
+ const initTx = await buildMinaTransaction(
3692
+ await getO1js(),
3626
3693
  { sender: payerPublicKey, fee: Number(txFee) },
3627
3694
  async () => {
3628
3695
  await channel.initializeChannel(
@@ -3651,7 +3718,8 @@ async function openMinaChannelOnChain(params) {
3651
3718
  const channel = await getZkApp();
3652
3719
  await fetchAccount({ publicKey: zkAppPublicKey });
3653
3720
  const amountField = Field2(params.deposit.amount.toString());
3654
- const depositTx = await Mina.transaction(
3721
+ const depositTx = await buildMinaTransaction(
3722
+ await getO1js(),
3655
3723
  { sender: payerPublicKey, fee: Number(txFee) },
3656
3724
  async () => {
3657
3725
  await channel.deposit(amountField, payerPublicKey);
@@ -3693,31 +3761,48 @@ var APPSTATE_CHANNEL_HASH = 0;
3693
3761
  var APPSTATE_CHANNEL_STATE = 3;
3694
3762
  var STATE_UNINITIALIZED = 0n;
3695
3763
  var STATE_OPEN = 1n;
3764
+ function requiredDeployBalanceNanomina(feeNanomina) {
3765
+ return MINA_ACCOUNT_CREATION_FEE_NANOMINA + feeNanomina * 2n;
3766
+ }
3696
3767
  var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
3697
3768
  async function deployMinaChannelZkApp(params) {
3698
3769
  const { o1js } = await loadMinaRuntime();
3699
3770
  const { Mina, PrivateKey, AccountUpdate, fetchAccount } = o1js;
3700
- const progress = params.onProgress ?? (() => {
3701
- });
3771
+ const progress = params.onProgress ?? (() => void 0);
3702
3772
  const network = Mina.Network(params.graphqlUrl);
3703
3773
  Mina.setActiveInstance(network);
3704
3774
  const payerKeyBase58 = hexToMinaBase58PrivateKey(params.payerPrivateKey);
3705
3775
  const payerPrivateKey = PrivateKey.fromBase58(payerKeyBase58);
3706
3776
  const payerPublicKey = payerPrivateKey.toPublicKey();
3777
+ const feeNanomina = params.feeNanomina ?? 100000000n;
3778
+ await assertMinaFeePayerFunded({
3779
+ o1js,
3780
+ payerPublicKey,
3781
+ requiredNanomina: requiredDeployBalanceNanomina(feeNanomina),
3782
+ graphqlUrl: params.graphqlUrl
3783
+ });
3784
+ const zkAppPrivateKey = params.zkAppPrivateKey ? PrivateKey.fromBase58(params.zkAppPrivateKey) : PrivateKey.random();
3785
+ const zkAppPublicKey = zkAppPrivateKey.toPublicKey();
3786
+ const zkAppAddress = zkAppPublicKey.toBase58();
3787
+ if (!params.zkAppPrivateKey) {
3788
+ await params.onDeploying?.({
3789
+ zkAppAddress,
3790
+ zkAppPrivateKey: zkAppPrivateKey.toBase58(),
3791
+ feePayer: payerPublicKey.toBase58()
3792
+ });
3793
+ }
3707
3794
  progress(
3708
3795
  "compiling the PaymentChannel circuit (one-time, can take 1-3 minutes)\u2026"
3709
3796
  );
3710
3797
  const PaymentChannel = await getCompiledPaymentChannel();
3711
- const zkAppPrivateKey = PrivateKey.random();
3712
- const zkAppPublicKey = zkAppPrivateKey.toPublicKey();
3713
- const zkAppAddress = zkAppPublicKey.toBase58();
3714
3798
  const zkApp = new PaymentChannel(zkAppPublicKey);
3715
3799
  await fetchAccount({ publicKey: payerPublicKey });
3716
3800
  progress(
3717
3801
  `deploying a dedicated PaymentChannel zkApp ${zkAppAddress} (costs the 1 MINA account-creation fee + tx fee)\u2026`
3718
3802
  );
3719
- const fee = Number(params.feeNanomina ?? 100000000n);
3720
- const deployTx = await Mina.transaction(
3803
+ const fee = Number(feeNanomina);
3804
+ const deployTx = await buildMinaTransaction(
3805
+ o1js,
3721
3806
  { sender: payerPublicKey, fee },
3722
3807
  async () => {
3723
3808
  AccountUpdate.fundNewAccount(payerPublicKey);
@@ -3759,8 +3844,7 @@ async function deployMinaChannelZkApp(params) {
3759
3844
  async function ensureOwnedMinaZkApp(params) {
3760
3845
  const { o1js } = await loadMinaRuntime();
3761
3846
  const { Mina, PrivateKey, PublicKey, Field: Field2, Poseidon, fetchAccount } = o1js;
3762
- const progress = params.onProgress ?? (() => {
3763
- });
3847
+ const progress = params.onProgress ?? (() => void 0);
3764
3848
  const network = Mina.Network(params.graphqlUrl);
3765
3849
  Mina.setActiveInstance(network);
3766
3850
  const payerKeyBase58 = hexToMinaBase58PrivateKey(params.payerPrivateKey);
@@ -3786,13 +3870,19 @@ async function ensureOwnedMinaZkApp(params) {
3786
3870
  ownRecord: false
3787
3871
  });
3788
3872
  }
3873
+ let pendingOwnKey;
3789
3874
  for (const candidate of candidates) {
3790
3875
  let appState;
3791
3876
  try {
3792
3877
  const res = await fetchAccount({
3793
3878
  publicKey: PublicKey.fromBase58(candidate.address)
3794
3879
  });
3795
- if (res.error || !res.account) continue;
3880
+ if (res.error || !res.account) {
3881
+ if (candidate.ownRecord && params.deployed?.zkAppPrivateKey) {
3882
+ pendingOwnKey = params.deployed.zkAppPrivateKey;
3883
+ }
3884
+ continue;
3885
+ }
3796
3886
  appState = res.account.zkapp?.appState;
3797
3887
  } catch {
3798
3888
  continue;
@@ -3812,7 +3902,11 @@ async function ensureOwnedMinaZkApp(params) {
3812
3902
  return { zkAppAddress: candidate.address, deployed: false };
3813
3903
  }
3814
3904
  }
3815
- const record = await deployMinaChannelZkApp(params);
3905
+ const record = await deployMinaChannelZkApp({
3906
+ ...params,
3907
+ ...pendingOwnKey ? { zkAppPrivateKey: pendingOwnKey } : {},
3908
+ ...params.onDeploying ? { onDeploying: params.onDeploying } : {}
3909
+ });
3816
3910
  await params.onDeployed?.(record);
3817
3911
  return { zkAppAddress: record.zkAppAddress, deployed: true, record };
3818
3912
  }
@@ -3853,4 +3947,4 @@ export {
3853
3947
  @noble/curves/secp256k1.js:
3854
3948
  (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
3855
3949
  */
3856
- //# sourceMappingURL=chunk-IYPIOSEF.js.map
3950
+ //# sourceMappingURL=chunk-7ZUHCA5C.js.map