Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added product fee to the quote #348

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
353 changes: 265 additions & 88 deletions connectors/bindings/lbc.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions connectors/rsk.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type RSKConnector interface {
GetPegoutCollateral(addr string) (*big.Int, *big.Int, error)
AddPegoutCollateral(opts *bind.TransactOpts) error
GetTransactionReceipt(txHash string) (*gethTypes.Receipt, error)
GetDaoFeePercentage() (uint64, error)
}

type RSKClient interface {
Expand Down Expand Up @@ -935,6 +936,7 @@ func (rsk *RSK) ParseQuote(q *pegin.Quote) (bindings.QuotesPeginQuote, error) {
pq.CallTime = q.LpCallTime
pq.DepositConfirmations = q.Confirmations
pq.TimeForDeposit = q.TimeForDeposit
pq.ProductFeeAmount = big.NewInt(int64(q.ProductFeeAmount))
return pq, nil
}

Expand Down Expand Up @@ -980,6 +982,7 @@ func (rsk *RSK) ParsePegOutQuote(q *pegout.Quote) (bindings.QuotesPegOutQuote, e
pq.TransferTime = q.TransferTime
pq.ExpireDate = q.ExpireDate
pq.ExpireBlock = q.ExpireBlock
pq.ProductFeeAmount = types.NewWei(int64(q.ProductFeeAmount)).AsBigInt()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you parse using wei here and above you parse using big int directly?


return pq, nil
}
Expand Down Expand Up @@ -1279,3 +1282,14 @@ func (rsk *RSK) awaitTx(function func() (*gethTypes.Transaction, error)) (*gethT
}
return nil, err
}

func (rsk *RSK) GetDaoFeePercentage() (uint64, error) {
opts := bind.CallOpts{}
amount, err := rsk.lbc.ProductFeePercentage(&opts)

if err != nil {
return 0, err
}

return amount.Uint64(), nil
}
18 changes: 11 additions & 7 deletions docker-compose/local/.env.regtest
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@ KEY_DIR=geth_keystore
ACCOUNT_NUM=0
PWD_FILE=pwd.txt
CHAIN_ID=33

KEY_SECRET=FlyoverTestEnv/LPS-LOCAL-KEY
PASSWORD_SECRET=FlyoverTestEnv/LPS-LOCAL-PASSWORD
BTC_WALLET_PASSWORD=FlyoverTestEnv/LPS-LOCAL-BTC-WALLET-PASSWORD
BTC_ENCRYPTED_WALLET=true

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
[email protected]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont use mailgun anymore

MAILGUN_DOMAIN=b30c8b203ade9013f16fbd24232f7580-102c75d8-2e73f63d

AWS_ACCESS_KEY_ID=AKIARRR3WWCRU722UAHK
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please dont push this to the repo !!

AWS_SECRET_ACCESS_KEY=DvQLL+krYY3AWY5U+7RsYQ05fBr9/Qpk+1wuM+Ce
AWS_REGION=us-east-1

ENCRYPT_APP_KEY=FlyoverTestEnv/ENCRYPT-KEY
# From previous .env
LIQUIDITY_PROVIDER_RSK_ADDR=0x9D93929A9099be4355fC2389FbF253982F9dF47c
PEGIN_LIQUIDITY_PROVIDER_RSK_ADDR=
PEGOUT_LIQUIDITY_PROVIDER_RSK_ADDR=
LIQUIDITY_PROVIDER_RSK_ADDR=0x9d93929a9099be4355fc2389fbf253982f9df47c
PEGIN_LIQUIDITY_PROVIDER_RSK_ADDR=0x9d93929a9099be4355fc2389fbf253982f9df47c
PEGOUT_LIQUIDITY_PROVIDER_RSK_ADDR=0x9d93929a9099be4355fc2389fbf253982f9df47c

BTCD_RPC_USER=test
BTCD_RPC_PASS=test
Expand Down Expand Up @@ -85,5 +89,5 @@ QUOTE_CACHE_START_BLOCK=0
CAPTCHA_SECRET_KEY=
CAPTCHA_SITE_KEY=
CAPTCHA_THRESHOLD=0.8
DISABLE_CAPTCHA=false
DISABLE_CAPTCHA=true
PROVIDER_TYPE=both
8 changes: 8 additions & 0 deletions docker-compose/local/OpenAPI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ components:
description: The penalty fee that the LP pays if it fails to deliver the
service
type: integer
productFeeAmount:
description: The DAO Fee amount
type: integer
rskRefundAddr:
description: A User RSK refund address
type: string
Expand Down Expand Up @@ -178,6 +181,7 @@ components:
- confirmations
- callOnRegister
- callCost
- productFeeAmount
type: object
PegoutQuoteDTO:
properties:
Expand Down Expand Up @@ -211,6 +215,9 @@ components:
type: integer
penaltyFee:
type: integer
productFeeAmount:
description: The DAO fee amount
type: integer
rskRefundAddress:
type: string
transferConfirmations:
Expand Down Expand Up @@ -238,6 +245,7 @@ components:
- expireDate
- expireBlocks
- callCost
- productFeeAmount
type: object
ProviderDTO:
properties:
Expand Down
4 changes: 4 additions & 0 deletions http/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type PeginQuoteDTO struct {
Confirmations uint16 `json:"confirmations" required:"" description:"The number of confirmations that the LP requires before making the call"`
CallOnRegister bool `json:"callOnRegister" required:"" description:"A boolean value indicating whether the callForUser can be called on registerPegIn"`
CallCost uint64 `json:"callCost" required:"" description:"The estimated cost for the LP to do the call on behalf of the user. Is calculated with gasPrice * gasLimit"`
ProductFeeAmount uint64 `json:"productFeeAmount" required:"" description:"The DAO Fee amount"`
}

func toPeginQuote(quote *pegin.Quote) *PeginQuoteDTO {
Expand All @@ -80,6 +81,7 @@ func toPeginQuote(quote *pegin.Quote) *PeginQuoteDTO {
Confirmations: quote.Confirmations,
CallOnRegister: quote.CallOnRegister,
CallCost: quote.CallCost.Uint64(),
ProductFeeAmount: quote.ProductFeeAmount,
}
}

Expand All @@ -102,6 +104,7 @@ type PegoutQuoteDTO struct {
ExpireDate uint32 `json:"expireDate" required:"" validate:"required"`
ExpireBlock uint32 `json:"expireBlocks" required:"" validate:"required"`
CallCost uint64 `json:"callCost" required:"" description:"The estimated cost for the LP to do the transaction on behalf of the user in Bitcoin network"`
ProductFeeAmount uint64 `json:"productFeeAmount" required:"" description:"The DAO fee amount"`
}

func toPegoutQuote(quote *pegout.Quote) *PegoutQuoteDTO {
Expand All @@ -124,5 +127,6 @@ func toPegoutQuote(quote *pegout.Quote) *PegoutQuoteDTO {
ExpireDate: quote.ExpireDate,
ExpireBlock: quote.ExpireBlock,
CallCost: quote.CallCost.Uint64(),
ProductFeeAmount: quote.ProductFeeAmount,
}
}
64 changes: 44 additions & 20 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const GetCollateralError = "Unable to get collateral"
const ErrorAddingProvider = "Error Adding New provider: %v"
const ErrorRetrivingProviderAddress = "Error Retrieving Provider Address from MongoDB"
const ErrorNotLiquidity = "Not enough liquidity"
const ErrorRetrievingDaoFeePercentage = "Error retrieving dao fee percentage"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to stop doing this pattern of error msg and switch to sentinel errors (not to you or this PR, just adding to my tech debt list)


type LiquidityProviderList struct {
Endpoint string `env:"RSK_ENDPOINT"`
Expand Down Expand Up @@ -830,14 +831,12 @@ func (s *Server) getQuoteHandler(w http.ResponseWriter, r *http.Request) {

var gas uint64
gas, err = s.rsk.EstimateGas(qr.CallEoaOrContractAddress, big.NewInt(int64(qr.ValueToTransfer)), []byte(qr.CallContractArguments))

if err != nil {
log.Error(ErrorEstimatingGas, err.Error())
customError := NewServerError(ErrorEstimatingGas, make(Details), true)
ResponseError(w, customError, http.StatusInternalServerError)
return
}

price, err := s.rsk.GasPrice()
if err != nil {
log.Error(ErrorEstimatingGas, err.Error())
Expand All @@ -864,9 +863,20 @@ func (s *Server) getQuoteHandler(w http.ResponseWriter, r *http.Request) {
}
minLockTxValueInWei := types.SatoshiToWei(minLockTxValueInSatoshi.Uint64())

daoFeePercentage, err := s.rsk.GetDaoFeePercentage()

if err != nil {
log.Error(ErrorRetrievingDaoFeePercentage, err.Error())
customError := NewServerError(ErrorRetrievingDaoFeePercentage, make(map[string]interface{}), true)
ResponseError(w, customError, http.StatusInternalServerError)
return
}

daoFeeAmount := qr.ValueToTransfer * daoFeePercentage / 100
Luisfc68 marked this conversation as resolved.
Show resolved Hide resolved

getQuoteFailed := false
amountBelowMinLockTxValue := false
q := parseReqToQuote(qr, s.rsk.GetLBCAddress(), fedAddress, gas)
q := parseReqToQuote(qr, s.rsk.GetLBCAddress(), fedAddress, gas, daoFeeAmount)
pq, err := s.provider.GetQuote(q, gas, types.NewBigWei(price))
if err != nil {
log.Error("error getting quote: ", err)
Expand All @@ -879,7 +889,6 @@ func (s *Server) getQuoteHandler(w http.ResponseWriter, r *http.Request) {
}

hash, err := s.storeQuote(pq)

if err != nil {
log.Error(err)
errmsg := ErrorStoringProviderQuote + ": " + err.Error()
Expand Down Expand Up @@ -989,7 +998,19 @@ func (s *Server) getPegoutQuoteHandler(w http.ResponseWriter, r *http.Request) {

getQuoteFailed := false
amountBelowMinLockTxValue := false
q := parseReqToPegOutQuote(qr, s.rsk.GetLBCAddress())

daoFeePercentage, err := s.rsk.GetDaoFeePercentage()

if err != nil {
log.Error(ErrorRetrievingDaoFeePercentage, err.Error())
customError := NewServerError(ErrorRetrievingDaoFeePercentage, make(map[string]interface{}), true)
ResponseError(w, customError, http.StatusInternalServerError)
return
}

daoFeeAmount := qr.ValueToTransfer * daoFeePercentage / 100
Luisfc68 marked this conversation as resolved.
Show resolved Hide resolved

q := parseReqToPegOutQuote(qr, s.rsk.GetLBCAddress(), daoFeeAmount)
rskBlockNumber, err := s.rsk.GetRskHeight()
if err != nil {
log.Error("Error getting last block", err.Error())
Expand Down Expand Up @@ -1195,26 +1216,28 @@ func (s *Server) acceptQuoteHandler(w http.ResponseWriter, r *http.Request) {
returnQuoteSignFunc(w, signature, depositAddress)
}

func parseReqToQuote(qr QuoteRequest, lbcAddr string, fedAddr string, limitGas uint64) *pegin.Quote {
func parseReqToQuote(qr QuoteRequest, lbcAddr string, fedAddr string, limitGas uint64, daoFeeAmount uint64) *pegin.Quote {
return &pegin.Quote{
LBCAddr: lbcAddr,
FedBTCAddr: fedAddr,
BTCRefundAddr: qr.BitcoinRefundAddress,
RSKRefundAddr: qr.RskRefundAddress,
ContractAddr: qr.CallEoaOrContractAddress,
Data: qr.CallContractArguments,
Value: types.NewWei(int64(qr.ValueToTransfer)),
GasLimit: uint32(limitGas),
LBCAddr: lbcAddr,
FedBTCAddr: fedAddr,
BTCRefundAddr: qr.BitcoinRefundAddress,
RSKRefundAddr: qr.RskRefundAddress,
ContractAddr: qr.CallEoaOrContractAddress,
Data: qr.CallContractArguments,
Value: types.NewWei(int64(qr.ValueToTransfer)),
GasLimit: uint32(limitGas),
ProductFeeAmount: daoFeeAmount,
}
}

func parseReqToPegOutQuote(qr QuotePegOutRequest, lbcAddr string) *pegout.Quote {
func parseReqToPegOutQuote(qr QuotePegOutRequest, lbcAddr string, productFeeAmount uint64) *pegout.Quote {
return &pegout.Quote{
LBCAddr: lbcAddr,
BtcRefundAddr: qr.BitcoinRefundAddress,
RSKRefundAddr: qr.RskRefundAddress,
DepositAddr: qr.To,
Value: types.NewWei(int64(qr.ValueToTransfer)),
LBCAddr: lbcAddr,
BtcRefundAddr: qr.BitcoinRefundAddress,
RSKRefundAddr: qr.RskRefundAddress,
DepositAddr: qr.To,
Value: types.NewWei(int64(qr.ValueToTransfer)),
ProductFeeAmount: productFeeAmount,
}
}

Expand Down Expand Up @@ -1260,6 +1283,7 @@ func getPegOutProviderByAddress(liquidityProviders []pegout.LiquidityProvider, a
}

func (s *Server) storeQuote(q *pegin.Quote) (string, error) {
log.Debug(q.ProductFeeAmount)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls delete this or add more context to the message

h, err := s.rsk.HashQuote(q)
if err != nil {
return "", err
Expand Down
1 change: 1 addition & 0 deletions pegin/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Quote struct {
Confirmations uint16 `json:"confirmations" db:"confirmations"`
CallOnRegister bool `json:"callOnRegister" db:"call_on_register"`
CallCost *types.Wei `json:"callCost" db:"call_cost"`
ProductFeeAmount uint64 `json:"productFeeAmount" db:"product_fee_amount"`
}

type PunishmentEvent struct {
Expand Down
1 change: 1 addition & 0 deletions pegout/pegout_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Quote struct {
ExpireDate uint32 `json:"expireDate" db:"expire_date" validate:"required"`
ExpireBlock uint32 `json:"expireBlocks" db:"expire_blocks" validate:"required"`
CallCost *types.Wei `json:"callCost" db:"callCost" validate:"required"`
ProductFeeAmount uint64 `json:"productFeeAmount" db:"product_fee_amount" validate:"required"`
}

func (q *Quote) GetExpirationTime() time.Time {
Expand Down
Loading