33 * Pure functions for contract-related operations
44 */
55
6- import type { Wallet } from '@aztec/aztec.js/wallet' ;
6+ import type { BatchedMethod , Wallet , TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet' ;
77import type { AztecNode } from '@aztec/aztec.js/node' ;
88import { AztecAddress } from '@aztec/aztec.js/addresses' ;
99import { AztecAddress as AztecAddressClass } from '@aztec/aztec.js/addresses' ;
1010import { Fr } from '@aztec/aztec.js/fields' ;
1111import { FunctionSelector } from '@aztec/aztec.js/abi' ;
1212import { BatchCall , getContractInstanceFromInstantiationParams } from '@aztec/aztec.js/contracts' ;
1313import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon' ;
14+ import { type FunctionCall , decodeFromAbi } from '@aztec/stdlib/abi' ;
15+ import { ExecutionPayload } from '@aztec/stdlib/tx' ;
16+ import { UtilityExecutionResult } from '@aztec/stdlib/tx' ;
1417import type { TxReceipt } from '@aztec/stdlib/tx' ;
1518import type { TokenContract } from '@aztec/noir-contracts.js/Token' ;
1619import type { AMMContract } from '../../contracts/target/AMM' ;
1720import type { ProofOfPasswordContract } from '../../contracts/target/ProofOfPassword' ;
21+ import { SubscriptionFPC } from '@gregojuice/contracts/subscription-fpc' ;
1822import { BigDecimal } from '../utils/bigDecimal' ;
1923import type { NetworkConfig } from '../config/networks' ;
2024import type { OnboardingResult } from '../contexts/onboarding/reducer' ;
@@ -26,13 +30,15 @@ export interface SwapContracts {
2630 gregoCoin : TokenContract ;
2731 gregoCoinPremium : TokenContract ;
2832 amm : AMMContract ;
33+ fpc : SubscriptionFPC | null ;
2934}
3035
3136/**
3237 * Contracts returned after drip registration
3338 */
3439export interface DripContracts {
3540 pop : ProofOfPasswordContract ;
41+ fpc : SubscriptionFPC | null ;
3642}
3743
3844/**
@@ -138,7 +144,10 @@ export async function registerSwapContracts(
138144 const gregoCoinPremium = TokenContract . at ( gregoCoinPremiumAddress , wallet ) ;
139145 const amm = AMMContract . at ( ammAddress , wallet ) ;
140146
141- return { gregoCoin, gregoCoinPremium, amm } ;
147+ // Instantiate FPC wrapper if configured
148+ const fpc = subFPC && fpcAddress ? SubscriptionFPC . at ( fpcAddress , wallet ) : null ;
149+
150+ return { gregoCoin, gregoCoinPremium, amm, fpc } ;
142151}
143152
144153/**
@@ -206,7 +215,11 @@ export async function registerDripContracts(
206215 // Instantiate the ProofOfPassword contract
207216 const pop = ProofOfPasswordContract . at ( popAddress , wallet ) ;
208217
209- return { pop } ;
218+ // Instantiate FPC wrapper if configured
219+ const fpcAddr = subFPC ? AztecAddressClass . fromString ( subFPC . address ) : undefined ;
220+ const fpc = fpcAddr ? SubscriptionFPC . at ( fpcAddr , wallet ) : null ;
221+
222+ return { pop, fpc } ;
210223}
211224
212225/**
@@ -339,11 +352,11 @@ function markSubscribed(fpcAddress: string, configIndex: number, userAddress: st
339352 * Uses subscribe on first call, sponsor on subsequent calls.
340353 */
341354export async function executeSponsoredSwap (
342- wallet : Wallet ,
343355 network : NetworkConfig ,
344356 amm : SwapContracts [ 'amm' ] ,
345357 gregoCoin : SwapContracts [ 'gregoCoin' ] ,
346358 gregoCoinPremium : SwapContracts [ 'gregoCoinPremium' ] ,
359+ fpc : SubscriptionFPC ,
347360 userAddress : AztecAddress ,
348361 amountOut : number ,
349362 amountInMax : number ,
@@ -372,12 +385,6 @@ export async function executeSponsoredSwap(
372385 ) ;
373386 }
374387
375- const fpcAddress = AztecAddressClass . fromString ( subFPC . address ) ;
376- const { SubscriptionFPCContract } = await import ( '@gregojuice/contracts/artifacts/SubscriptionFPC' ) ;
377- const { SubscriptionFPC } = await import ( '@gregojuice/contracts/subscription-fpc' ) ;
378- const rawFPC = SubscriptionFPCContract . at ( fpcAddress , wallet ) ;
379- const fpc = new SubscriptionFPC ( rawFPC ) ;
380-
381388 const subscribed = hasSubscription ( subFPC . address , configIndex , userAddress . toString ( ) ) ;
382389
383390 if ( subscribed ) {
@@ -422,12 +429,12 @@ export async function executeUnsponsoredSwap(
422429}
423430
424431export type SubscriptionStatusKind =
425- | 'loading' // query in flight
426- | 'no_fpc' // no FPC configured for this network — hide everything
427- | 'sponsored' // user not yet subscribed, slots available — first swap will be free
428- | 'active' // user has subscription with uses remaining — swap is free
429- | 'full' // no slots left, user never subscribed — must bridge
430- | 'depleted' ; // user's uses exhausted — must bridge
432+ | 'loading' // query in flight
433+ | 'no_fpc' // no FPC configured for this network — hide everything
434+ | 'sponsored' // user not yet subscribed, slots available — first swap will be free
435+ | 'active' // user has subscription with uses remaining — swap is free
436+ | 'full' // no slots left, user never subscribed — must bridge
437+ | 'depleted' ; // user's uses exhausted — must bridge
431438
432439export interface SubscriptionStatus {
433440 kind : SubscriptionStatusKind ;
@@ -440,13 +447,13 @@ export interface SubscriptionStatus {
440447 * Returns the status kind based on available slots and user subscription state.
441448 */
442449export async function querySubscriptionStatus (
443- wallet : Wallet ,
444450 network : NetworkConfig ,
445451 amm : SwapContracts [ 'amm' ] ,
446452 userAddress : AztecAddress ,
453+ fpc : SubscriptionFPC | null ,
447454) : Promise < SubscriptionStatus > {
448455 const subFPC = network . subscriptionFPC ;
449- if ( ! subFPC ) return { kind : 'no_fpc' } ;
456+ if ( ! subFPC || ! fpc ) return { kind : 'no_fpc' } ;
450457
451458 // Derive configIndex + selector from the AMM's function map — take the first entry
452459 const ammFunctions = subFPC . functions [ amm . address . toString ( ) ] ;
@@ -458,15 +465,11 @@ export async function querySubscriptionStatus(
458465 const selector = FunctionSelector . fromString ( selectorHex ) ;
459466 const configId = await poseidon2Hash ( [ amm . address . toField ( ) , selector . toField ( ) , new Fr ( configIndex ) ] ) ;
460467
461- const fpcAddress = AztecAddressClass . fromString ( subFPC . address ) ;
462- const { SubscriptionFPCContract } = await import ( '@gregojuice/contracts/artifacts/SubscriptionFPC' ) ;
463- const rawFPC = SubscriptionFPCContract . at ( fpcAddress , wallet ) ;
464-
465468 // SlotNote is owned by the FPC — must simulate from fpc.address
466469 // SubscriptionNote is owned by the user — must simulate from userAddress
467470 const [ { result : slotsResult } , { result : subInfoResult } ] = await Promise . all ( [
468- rawFPC . methods . count_available_slots ( configId ) . simulate ( { from : fpcAddress } ) ,
469- rawFPC . methods . get_subscription_info ( userAddress , configId ) . simulate ( { from : userAddress } ) ,
471+ fpc . methods . count_available_slots ( configId ) . simulate ( { from : fpc . address } ) ,
472+ fpc . methods . get_subscription_info ( userAddress , configId ) . simulate ( { from : userAddress } ) ,
470473 ] ) ;
471474
472475 const availableSlots = Number ( slotsResult ) ;
@@ -510,6 +513,7 @@ export async function executeDrip(
510513 wallet : Wallet ,
511514 network : NetworkConfig ,
512515 pop : ProofOfPasswordContract ,
516+ fpc : SubscriptionFPC ,
513517 password : string ,
514518 recipient : AztecAddress ,
515519) : Promise < TxReceipt > {
@@ -524,12 +528,6 @@ export async function executeDrip(
524528 throw new Error ( `No subscription config found for ${ pop . address . toString ( ) } selector ${ call . selector . toString ( ) } ` ) ;
525529 }
526530
527- const fpcAddress = AztecAddressClass . fromString ( subFPC . address ) ;
528- const { SubscriptionFPCContract } = await import ( '@gregojuice/contracts/artifacts/SubscriptionFPC' ) ;
529- const { SubscriptionFPC } = await import ( '@gregojuice/contracts/subscription-fpc' ) ;
530- const rawFPC = SubscriptionFPCContract . at ( fpcAddress , wallet ) ;
531- const fpc = new SubscriptionFPC ( rawFPC ) ;
532-
533531 const accounts = await wallet . getAccounts ( ) ;
534532 const userAddress = accounts [ 0 ] ?. item ?? recipient ;
535533
0 commit comments