From c5d555892bf94c2da09e07d56a7a949c0abf60c4 Mon Sep 17 00:00:00 2001 From: Lucas Magnus Date: Mon, 19 Feb 2024 15:16:06 -0300 Subject: [PATCH] feat: fee charged log --- backend/internal/controller/http/v1/assets.go | 30 +++++++++++++------ backend/internal/controller/http/v1/vault.go | 10 +++---- backend/internal/entity/message.go | 11 ++++--- backend/internal/entity/transaction_log.go | 1 + .../usecase/repo/log_transaction_postgres.go | 6 ++-- ....down.sql => 000044_insert_roles.down.sql} | 0 ...oles.up.sql => 000044_insert_roles.up.sql} | 0 ...000045_add_column_fee_transaction.down.sql | 2 ++ .../000045_add_column_fee_transaction.up.sql | 2 ++ 9 files changed, 40 insertions(+), 22 deletions(-) rename backend/migrations/{0000044_insert_roles.down.sql => 000044_insert_roles.down.sql} (100%) rename backend/migrations/{0000044_insert_roles.up.sql => 000044_insert_roles.up.sql} (100%) create mode 100644 backend/migrations/000045_add_column_fee_transaction.down.sql create mode 100644 backend/migrations/000045_add_column_fee_transaction.up.sql diff --git a/backend/internal/controller/http/v1/assets.go b/backend/internal/controller/http/v1/assets.go index 16f022f1..901692d5 100644 --- a/backend/internal/controller/http/v1/assets.go +++ b/backend/internal/controller/http/v1/assets.go @@ -13,8 +13,6 @@ import ( "github.com/gin-gonic/gin" ) - - type assetsRoutes struct { w usecase.WalletUseCase as usecase.AssetUseCase @@ -22,7 +20,7 @@ type assetsRoutes struct { a usecase.AuthUseCase l usecase.LogTransactionUseCase logger *logger.Logger - pf profanity.ProfanityFilter + pf profanity.ProfanityFilter } func newAssetTomlRoutes(handler *gin.RouterGroup, w usecase.WalletUseCase, as usecase.AssetUseCase, m HTTPControllerMessenger, a usecase.AuthUseCase, l usecase.LogTransactionUseCase, logger *logger.Logger, pf profanity.ProfanityFilter) { @@ -256,7 +254,7 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { } Id := generateID() - _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + response, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ Id: Id, MainSource: sponsor.Key.PublicKey, PublicKeys: []string{sponsor.Key.PublicKey, distPk, issuerPk}, @@ -317,6 +315,7 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { return } + feeCharged := response.Message.(entity.EnvelopeResponse).FeeCharged amount, err := strconv.ParseFloat(request.Amount, 64) if err != nil { amount = 0 @@ -327,6 +326,7 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { TransactionTypeID: entity.CreateAsset, UserID: userID, Description: createLogDescription(entity.CreateAsset, asset.Code, nil, nil), + FeeCharged: &feeCharged, }) if err != nil { r.logger.Error(err, "http - v1 - create asset - create log transaction") @@ -429,7 +429,7 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) { }, } Id := generateID() - _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + response, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ Id: Id, MainSource: asset.Issuer.Key.PublicKey, PublicKeys: []string{asset.Issuer.Key.PublicKey}, @@ -442,6 +442,7 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) { return } + feeCharged := response.Message.(entity.EnvelopeResponse).FeeCharged amount, err := strconv.ParseFloat(request.Amount, 64) if err != nil { amount = 0 @@ -455,6 +456,7 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) { Description: createLogDescription(entity.MintAsset, asset.Code, nil, nil), CurrentSupply: &request.CurrentSupply, CurrentMainVault: &request.CurrentMainVault, + FeeCharged: &feeCharged, }) if err != nil { r.logger.Error(err, "http - v1 - mint asset - create log transaction") @@ -532,7 +534,7 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) { } Id := generateID() - _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + response, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ Id: Id, MainSource: asset.Distributor.Key.PublicKey, PublicKeys: []string{asset.Distributor.Key.PublicKey}, @@ -545,6 +547,7 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) { return } + feeCharged := response.Message.(entity.EnvelopeResponse).FeeCharged amount, err := strconv.ParseFloat(request.Amount, 64) if err != nil { amount = 0 @@ -557,6 +560,7 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) { Description: createLogDescription(entity.BurnAsset, asset.Code, nil, nil), CurrentSupply: &request.CurrentSupply, CurrentMainVault: &request.CurrentMainVault, + FeeCharged: &feeCharged, }) if err != nil { r.logger.Error(err, "http - v1 - burn asset - create log transaction") @@ -643,7 +647,7 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) { } Id := generateID() - _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + response, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ Id: Id, MainSource: sourceWallet.Key.PublicKey, PublicKeys: []string{sourceWallet.Key.PublicKey}, @@ -656,6 +660,7 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) { return } + feeCharged := response.Message.(entity.EnvelopeResponse).FeeCharged amount, err := strconv.ParseFloat(request.Amount, 64) if err != nil { amount = 0 @@ -670,6 +675,7 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) { DestinationPK: &request.DestinationWalletPK, CurrentSupply: &request.CurrentSupply, CurrentMainVault: &request.CurrentMainVault, + FeeCharged: &feeCharged, }) if err != nil { r.logger.Error(err, "http - v1 - transfer asset - create log transaction") @@ -750,7 +756,7 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) { } Id := generateID() - _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + response, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ Id: Id, MainSource: sponsor.Key.PublicKey, PublicKeys: []string{asset.Issuer.Key.PublicKey, sponsor.Key.PublicKey}, @@ -763,6 +769,7 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) { return } + feeCharged := response.Message.(entity.EnvelopeResponse).FeeCharged amount, err := strconv.ParseFloat(request.Amount, 64) if err != nil { amount = 0 @@ -775,6 +782,8 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) { Description: createLogDescription(entity.ClawbackAsset, asset.Code, nil, nil), CurrentSupply: &request.CurrentSupply, CurrentMainVault: &request.CurrentMainVault, + DestinationPK: &request.From, + FeeCharged: &feeCharged, }) if err != nil { r.logger.Error(err, "http - v1 - clawback asset - create log transaction") @@ -859,7 +868,7 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { } Id := generateID() - _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + response, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ Id: Id, MainSource: sponsor.Key.PublicKey, PublicKeys: []string{asset.Issuer.Key.PublicKey, sponsor.Key.PublicKey}, @@ -873,11 +882,14 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { } + feeCharged := response.Message.(entity.EnvelopeResponse).FeeCharged err = r.l.CreateLogTransaction(entity.LogTransaction{ Asset: asset, TransactionTypeID: entity.UpdateAuthFlags, UserID: userID, Description: createLogDescription(entity.UpdateAuthFlags, asset.Code, request.SetFlags, request.ClearFlags), + DestinationPK: &request.TrustorPK, + FeeCharged: &feeCharged, }) if err != nil { r.logger.Error(err, "http - v1 - update auth flags - create log transaction") diff --git a/backend/internal/controller/http/v1/vault.go b/backend/internal/controller/http/v1/vault.go index c382f76a..95adf270 100644 --- a/backend/internal/controller/http/v1/vault.go +++ b/backend/internal/controller/http/v1/vault.go @@ -12,7 +12,6 @@ import ( "github.com/gin-gonic/gin" ) - type vaultRoutes struct { m HTTPControllerMessenger a usecase.AuthUseCase @@ -133,12 +132,12 @@ func (r *vaultRoutes) createVault(c *gin.Context) { }, }) - ops := []entity.Operation{ { - Type: entity.CreateAccountOp, - Target: walletPk, - Amount: _startingBalance, + Type: entity.CreateAccountOp, + Target: walletPk, + //Amount: _startingBalance, + Amount: "10", Sponsor: sponsor.Key.PublicKey, Origin: sponsor.Key.PublicKey, }, @@ -163,7 +162,6 @@ func (r *vaultRoutes) createVault(c *gin.Context) { }) } - Id := generateID() res, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ Id: Id, diff --git a/backend/internal/entity/message.go b/backend/internal/entity/message.go index d872ea8f..be005649 100644 --- a/backend/internal/entity/message.go +++ b/backend/internal/entity/message.go @@ -43,6 +43,7 @@ type ( Hash string `json:"hash"` StatusCode int `json:"statusCode"` Error interface{} `json:"error"` + FeeCharged int64 `json:"feeCharged"` } Operation struct { @@ -77,10 +78,12 @@ type ( } SignTransactionRequest struct { - Id int `json:"id"` - Envelope string `json:"envelope"` - PublicKeys []string `json:"publicKeys"` - Hash string `json:"hash"` + Id int `json:"id"` + Envelope string `json:"envelope"` + PublicKeys []string `json:"publicKeys"` + Hash string `json:"hash"` + FeeBump bool `json:"feeBump"` + FeeBumpProcessingState string `json:"feeBumpProcessingState"` } SorobanTransactionResponse struct { diff --git a/backend/internal/entity/transaction_log.go b/backend/internal/entity/transaction_log.go index 2567ef2d..2dffcc23 100644 --- a/backend/internal/entity/transaction_log.go +++ b/backend/internal/entity/transaction_log.go @@ -12,6 +12,7 @@ type LogTransaction struct { DestinationPK *string `json:"destination_pk" example:"GSDC..."` CurrentSupply *float64 `json:"current_supply" example:"1000"` CurrentMainVault *float64 `json:"current_main_vault" example:"1000"` + FeeCharged *int64 `json:"fee_charged" example:"1000"` } type SumLogTransaction struct { diff --git a/backend/internal/usecase/repo/log_transaction_postgres.go b/backend/internal/usecase/repo/log_transaction_postgres.go index a3e33fb3..abb4d6c2 100644 --- a/backend/internal/usecase/repo/log_transaction_postgres.go +++ b/backend/internal/usecase/repo/log_transaction_postgres.go @@ -18,10 +18,10 @@ func NewLogTransactionRepo(postgres *postgres.Postgres) *LogTransactionRepo { } func (repo *LogTransactionRepo) StoreLogTransaction(log entity.LogTransaction) error { - stmp := `INSERT INTO logtransactions (user_id, transaction_type_id, asset_id, amount, description, origin_pk, destination_pk, current_supply, current_main_vault) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)` + stmp := `INSERT INTO logtransactions (user_id, transaction_type_id, asset_id, amount, description, origin_pk, destination_pk, current_supply, current_main_vault, fee_charged) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)` _, err := repo.Db.Exec(stmp, log.UserID, log.TransactionTypeID, log.Asset.Id, log.Amount, log.Description, log.OriginPK, log.DestinationPK, - log.CurrentSupply, log.CurrentMainVault) + log.CurrentSupply, log.CurrentMainVault, log.FeeCharged) if err != nil { fmt.Println(err) return err diff --git a/backend/migrations/0000044_insert_roles.down.sql b/backend/migrations/000044_insert_roles.down.sql similarity index 100% rename from backend/migrations/0000044_insert_roles.down.sql rename to backend/migrations/000044_insert_roles.down.sql diff --git a/backend/migrations/0000044_insert_roles.up.sql b/backend/migrations/000044_insert_roles.up.sql similarity index 100% rename from backend/migrations/0000044_insert_roles.up.sql rename to backend/migrations/000044_insert_roles.up.sql diff --git a/backend/migrations/000045_add_column_fee_transaction.down.sql b/backend/migrations/000045_add_column_fee_transaction.down.sql new file mode 100644 index 00000000..58118bd5 --- /dev/null +++ b/backend/migrations/000045_add_column_fee_transaction.down.sql @@ -0,0 +1,2 @@ +-- Migration Down Script +ALTER TABLE LogTransactions DROP COLUMN fee_charged; diff --git a/backend/migrations/000045_add_column_fee_transaction.up.sql b/backend/migrations/000045_add_column_fee_transaction.up.sql new file mode 100644 index 00000000..680b3a4e --- /dev/null +++ b/backend/migrations/000045_add_column_fee_transaction.up.sql @@ -0,0 +1,2 @@ +-- Migration Up Script +ALTER TABLE LogTransactions ADD COLUMN fee_charged FLOAT;