@@ -521,44 +521,27 @@ func (h *SendPublicPaymentIntentHandler) AllowCreation(ctx context.Context, inte
521521 }
522522
523523 //
524- // Part 3: Exchange data validation
524+ // Part 3: Account validation to determine if it's managed by Code
525525 //
526526
527- if err := validateExchangeDataWithinIntent (ctx , h .data , typedMetadata .Mint , typedMetadata .ExchangeData ); err != nil {
527+ err = validateAllUserAccountsManagedByCode (ctx , initiatorAccounts )
528+ if err != nil {
528529 return err
529530 }
530531
531532 //
532- // Part 4: Local simulation
533+ // Part 4: Exchange data validation
533534 //
534535
535- simResult , err := LocalSimulation (ctx , h .data , actions )
536- if err != nil {
536+ if err := validateExchangeDataWithinIntent (ctx , h .data , typedMetadata .Mint , typedMetadata .ExchangeData ); err != nil {
537537 return err
538538 }
539539
540- // Fee payments always operate against the core mint, so we need to add relevant
541- // core mint initiator records
542- if simResult .HasAnyFeePayments () && ! common .IsCoreMint (intentMintAccount ) {
543- initiatorAccountsByType , ok := initiatorAccountsByMintAndType [common .CoreMintAccount .PublicKey ().ToBase58 ()]
544- if ! ok {
545- return errors .New ("initiator core mint accounts don't exist" )
546- }
547- primaryAccountRecords , ok := initiatorAccountsByType [commonpb .AccountType_PRIMARY ]
548- if ! ok {
549- return errors .New ("initiator core mint primary account doesn't exist" )
550- }
551- for _ , records := range primaryAccountRecords {
552- initiatorAccounts = append (initiatorAccounts , records )
553- initiatorAccountsByVault [records .General .TokenAccount ] = records
554- }
555- }
556-
557540 //
558- // Part 5: Account validation to determine if it's managed by Code
541+ // Part 5: Local simulation
559542 //
560543
561- err = validateAllUserAccountsManagedByCode (ctx , initiatorAccounts )
544+ simResult , err := LocalSimulation (ctx , h . data , actions )
562545 if err != nil {
563546 return err
564547 }
@@ -724,7 +707,7 @@ func (h *SendPublicPaymentIntentHandler) validateActions(
724707 return nil
725708 }
726709
727- // Check whether the destination account is a core mint token account that's
710+ // Check whether the destination account is a token account of the same mint that's
728711 // been created on the blockchain. If not, a fee is required
729712 err = validateExternalTokenAccountWithinIntent (ctx , h .data , destination , intentMint )
730713 switch err {
@@ -756,7 +739,7 @@ func (h *SendPublicPaymentIntentHandler) validateActions(
756739
757740 //
758741 // Part 4.1: Check destination account is paid exact quark amount from the deposit account
759- // minus any fees if we're operating against the core mint
742+ // minus any fees
760743 //
761744
762745 expectedDestinationPayment := int64 (metadata .ExchangeData .Quarks )
@@ -768,9 +751,7 @@ func (h *SendPublicPaymentIntentHandler) validateActions(
768751 return NewIntentValidationError ("expected at most 1 fee payment" )
769752 }
770753 for _ , feePayment := range feePayments {
771- if common .IsCoreMint (intentMint ) {
772- expectedDestinationPayment += feePayment .DeltaQuarks
773- }
754+ expectedDestinationPayment += feePayment .DeltaQuarks
774755 }
775756
776757 destinationSimulation , ok := simResult .SimulationsByAccount [destination .PublicKey ().ToBase58 ()]
@@ -1584,8 +1565,6 @@ func validateMoneyMovementActionUserAccounts(
15841565 }
15851566 }
15861567 case * transactionpb.Action_FeePayment :
1587- // Fee payments always come from the core mint primary account
1588-
15891568 mint , err = common .GetBackwardsCompatMint (typedAction .FeePayment .Mint )
15901569 if err != nil {
15911570 return err
@@ -1602,8 +1581,8 @@ func validateMoneyMovementActionUserAccounts(
16021581 }
16031582
16041583 sourceAccountInfo , ok := initiatorAccountsByVault [source .PublicKey ().ToBase58 ()]
1605- if ! ok || sourceAccountInfo .General .AccountType != commonpb .AccountType_PRIMARY || sourceAccountInfo . General . MintAccount != common . CoreMintAccount . PublicKey (). ToBase58 () {
1606- return NewActionValidationError (action , "source account must be the core mint primary account" )
1584+ if ! ok || sourceAccountInfo .General .AccountType != commonpb .AccountType_PRIMARY {
1585+ return NewActionValidationError (action , "source account must be the PRMARY account" )
16071586 }
16081587 default :
16091588 continue
@@ -1760,11 +1739,6 @@ func validateFeePayments(
17601739 return err
17611740 }
17621741
1763- // todo: Probably not always going to be the case, but add a strict validation to start
1764- if ! common .IsCoreMint (mintAccount ) {
1765- return NewActionValidationError (feePayment .Action , "fee payment must be made in core mint" )
1766- }
1767-
17681742 if feePaymentAction .Type != expectedFeeType {
17691743 return NewActionValidationErrorf (feePayment .Action , "expected a %s fee payment" , expectedFeeType .String ())
17701744 }
@@ -1783,23 +1757,20 @@ func validateFeePayments(
17831757 }
17841758 feeAmount = - feeAmount // Because it's coming out of a user account in this simulation
17851759
1786- var foundUsdExchangeRecord bool
1787- usdExchangeRecords , err := currency_util .GetPotentialClientCoreMintExchangeRates (ctx , data , currency_lib .USD )
1760+ mintQuarksPerUnit := common .GetMintQuarksPerUnit (mintAccount )
1761+ unitsOfMint := float64 (feeAmount ) / float64 (mintQuarksPerUnit )
1762+
1763+ isValid , _ , err := currency_util .ValidateClientExchangeData (ctx , data , & transactionpb.ExchangeData {
1764+ Currency : string (currency_lib .USD ),
1765+ NativeAmount : expectedUsdValue ,
1766+ ExchangeRate : expectedUsdValue / unitsOfMint ,
1767+ Quarks : uint64 (feeAmount ),
1768+ Mint : mintAccount .ToProto (),
1769+ })
17881770 if err != nil {
17891771 return err
1790- }
1791- for _ , exchangeRecord := range usdExchangeRecords {
1792- usdValue := exchangeRecord .Rate * float64 (feeAmount ) / float64 (common .CoreMintQuarksPerUnit )
1793-
1794- // Allow for some small margin of error
1795- if usdValue > expectedUsdValue - 0.0001 && usdValue < expectedUsdValue + 0.0001 {
1796- foundUsdExchangeRecord = true
1797- break
1798- }
1799- }
1800-
1801- if ! foundUsdExchangeRecord {
1802- return NewActionValidationErrorf (feePayment .Action , "%s fee payment amount must be $%.2f USD" , expectedFeeType .String (), expectedUsdValue )
1772+ } else if ! isValid {
1773+ return NewActionValidationErrorf (feePayment .Action , "%s fee payment amount must be %.2f USD" , expectedFeeType .String (), expectedUsdValue )
18031774 }
18041775
18051776 return nil
@@ -1963,7 +1934,6 @@ func validateTimelockUnlockStateDoesntExist(ctx context.Context, data code_data.
19631934
19641935func validateIntentAndActionMintsMatch (intentMint * common.Account , actions []* transactionpb.Action ) error {
19651936 for _ , action := range actions {
1966- var forceCoreMint bool
19671937 var actionMint * common.Account
19681938 var err error
19691939 switch typed := action .Type .(type ) {
@@ -1974,23 +1944,16 @@ func validateIntentAndActionMintsMatch(intentMint *common.Account, actions []*tr
19741944 case * transactionpb.Action_NoPrivacyWithdraw :
19751945 actionMint , err = common .GetBackwardsCompatMint (typed .NoPrivacyWithdraw .Mint )
19761946 case * transactionpb.Action_FeePayment :
1977- forceCoreMint = true // Fee payments are an exception, since they always operate against the core mint
19781947 actionMint , err = common .GetBackwardsCompatMint (typed .FeePayment .Mint )
19791948 default :
19801949 return errors .New ("unsupported action for mint extraction" )
19811950 }
1982-
19831951 if err != nil {
19841952 return err
19851953 }
1986- if forceCoreMint {
1987- if ! common .IsCoreMint (actionMint ) {
1988- return NewActionValidationErrorf (action , "mint must be %s" , common .CoreMintAccount .PublicKey ().ToBase58 ())
1989- }
1990- } else {
1991- if ! bytes .Equal (intentMint .PublicKey ().ToBytes (), actionMint .PublicKey ().ToBytes ()) {
1992- return NewActionValidationErrorf (action , "mint must be %s" , intentMint .PublicKey ().ToBase58 ())
1993- }
1954+
1955+ if ! bytes .Equal (intentMint .PublicKey ().ToBytes (), actionMint .PublicKey ().ToBytes ()) {
1956+ return NewActionValidationErrorf (action , "mint must be %s" , intentMint .PublicKey ().ToBase58 ())
19941957 }
19951958 }
19961959 return nil
0 commit comments