Skip to content

Commit 31761a4

Browse files
committed
Move fee-based checks to the appropriate intent handler
1 parent 40ad41b commit 31761a4

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

pkg/code/server/transaction/intent_handler.go

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ func (h *SendPublicPaymentIntentHandler) GetBalanceLocks(ctx context.Context, in
455455
return intentBalanceLocks, nil
456456
}
457457

458-
// todo: Not all multi-mint validation checks are implemented
459458
func (h *SendPublicPaymentIntentHandler) AllowCreation(ctx context.Context, intentRecord *intent.Record, untypedMetadata *transactionpb.Metadata, actions []*transactionpb.Action) error {
460459
typedMetadata := untypedMetadata.GetSendPublicPayment()
461460
if typedMetadata == nil {
@@ -522,27 +521,44 @@ func (h *SendPublicPaymentIntentHandler) AllowCreation(ctx context.Context, inte
522521
}
523522

524523
//
525-
// Part 3: Account validation to determine if it's managed by Code
524+
// Part 3: Exchange data validation
526525
//
527526

528-
err = validateAllUserAccountsManagedByCode(ctx, initiatorAccounts)
529-
if err != nil {
527+
if err := validateExchangeDataWithinIntent(ctx, h.data, typedMetadata.Mint, typedMetadata.ExchangeData); err != nil {
530528
return err
531529
}
532530

533531
//
534-
// Part 4: Exchange data validation
532+
// Part 4: Local simulation
535533
//
536534

537-
if err := validateExchangeDataWithinIntent(ctx, h.data, typedMetadata.Mint, typedMetadata.ExchangeData); err != nil {
535+
simResult, err := LocalSimulation(ctx, h.data, actions)
536+
if err != nil {
538537
return err
539538
}
540539

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+
541557
//
542-
// Part 5: Local simulation
558+
// Part 5: Account validation to determine if it's managed by Code
543559
//
544560

545-
simResult, err := LocalSimulation(ctx, h.data, actions)
561+
err = validateAllUserAccountsManagedByCode(ctx, initiatorAccounts)
546562
if err != nil {
547563
return err
548564
}
@@ -939,7 +955,6 @@ func (h *ReceivePaymentsPubliclyIntentHandler) GetBalanceLocks(ctx context.Conte
939955
}, nil
940956
}
941957

942-
// todo: Not all multi-mint validation checks are implemented
943958
func (h *ReceivePaymentsPubliclyIntentHandler) AllowCreation(ctx context.Context, intentRecord *intent.Record, untypedMetadata *transactionpb.Metadata, actions []*transactionpb.Action) error {
944959
typedMetadata := untypedMetadata.GetReceivePaymentsPublicly()
945960
if typedMetadata == nil {
@@ -1013,45 +1028,28 @@ func (h *ReceivePaymentsPubliclyIntentHandler) AllowCreation(ctx context.Context
10131028
}
10141029

10151030
//
1016-
// Part 3: Gift card account validation
1031+
// Part 3: User account validation to determine if it's managed by Code
10171032
//
10181033

1019-
err = validateClaimedGiftCard(ctx, h.data, giftCardVaultAccount, typedMetadata.Quarks)
1034+
err = validateAllUserAccountsManagedByCode(ctx, initiatorAccounts)
10201035
if err != nil {
10211036
return err
10221037
}
10231038

10241039
//
1025-
// Part 4: Local simulation
1040+
// Part 4: Gift card account validation
10261041
//
10271042

1028-
simResult, err := LocalSimulation(ctx, h.data, actions)
1043+
err = validateClaimedGiftCard(ctx, h.data, giftCardVaultAccount, typedMetadata.Quarks)
10291044
if err != nil {
10301045
return err
10311046
}
10321047

1033-
// Fee payments always operate against the core mint, so we need to add relevant
1034-
// core mint initiator records
1035-
if simResult.HasAnyFeePayments() && !common.IsCoreMint(intentMintAccount) {
1036-
initiatorAccountsByType, ok := initiatorAccountsByMintAndType[common.CoreMintAccount.PublicKey().ToBase58()]
1037-
if !ok {
1038-
return errors.New("initiator core mint accounts don't exist")
1039-
}
1040-
primaryAccountRecords, ok := initiatorAccountsByType[commonpb.AccountType_PRIMARY]
1041-
if !ok {
1042-
return errors.New("initiator core mint primary account doesn't exist")
1043-
}
1044-
for _, records := range primaryAccountRecords {
1045-
initiatorAccounts = append(initiatorAccounts, records)
1046-
initiatorAccountsByVault[records.General.TokenAccount] = records
1047-
}
1048-
}
1049-
10501048
//
1051-
// Part 5: User account validation to determine if it's managed by Code
1049+
// Part 5: Local simulation
10521050
//
10531051

1054-
err = validateAllUserAccountsManagedByCode(ctx, initiatorAccounts)
1052+
simResult, err := LocalSimulation(ctx, h.data, actions)
10551053
if err != nil {
10561054
return err
10571055
}

0 commit comments

Comments
 (0)