Skip to content

Commit cdeb62c

Browse files
committed
feat: generate and store quote creation data
1 parent c997a37 commit cdeb62c

95 files changed

Lines changed: 1413 additions & 375 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.mockery.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
with-expecter: true
22
dir: test/mocks
3+
resolve-type-alias: false
4+
issue-845-fix: true
35
filename: "{{ .InterfaceName | snakecase }}_mock.go"
46
mockname: "{{ .InterfaceName | firstUpper }}Mock"
57
outpkg: mocks
@@ -55,4 +57,4 @@ packages:
5557
UseCaseRegistry:
5658
github.com/gorilla/sessions:
5759
interfaces:
58-
Store:
60+
Store:

OpenApi.yml

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ components:
6060
description: Available liquidity for PegIn operations in wei
6161
example: "5000000000000000000"
6262
pegoutLiquidityAmount:
63-
description: Available liquidity for PegOut operations in satoshi
64-
example: "500000000"
63+
description: Available liquidity for PegOut operations in wei
64+
example: "5000000000000000000"
6565
required:
6666
- peginLiquidityAmount
6767
- pegoutLiquidityAmount
@@ -198,10 +198,12 @@ components:
198198
type: object
199199
PeginConfigurationDTO:
200200
properties:
201-
callFee:
202-
type: string
203201
callTime:
204202
type: integer
203+
feePercentage:
204+
type: number
205+
fixedFee:
206+
type: string
205207
maxValue:
206208
type: string
207209
minValue:
@@ -217,6 +219,22 @@ components:
217219
$ref: '#/components/schemas/PeginConfigurationDTO'
218220
type: object
219221
type: object
222+
PeginCreationDataDTO:
223+
properties:
224+
fixedFee:
225+
description: The fixed fee used to compute the call fee
226+
type: integer
227+
gasPrice:
228+
description: The gas price used to compute the gas fee
229+
type: integer
230+
percentageFee:
231+
description: The percentage fee used to compute the call fee
232+
type: number
233+
required:
234+
- gasPrice
235+
- percentageFee
236+
- fixedFee
237+
type: object
220238
PeginQuoteDTO:
221239
properties:
222240
agreementTimestamp:
@@ -333,6 +351,10 @@ components:
333351
type: object
334352
PeginQuoteStatusDTO:
335353
properties:
354+
creationData:
355+
$ref: '#/components/schemas/PeginCreationDataDTO'
356+
description: Values used to compute some fields of the quote
357+
type: object
336358
detail:
337359
$ref: '#/components/schemas/PeginQuoteDTO'
338360
description: Agreed specification of the quote
@@ -344,17 +366,20 @@ components:
344366
required:
345367
- detail
346368
- status
369+
- creationData
347370
type: object
348371
PegoutConfigurationDTO:
349372
properties:
350373
bridgeTransactionMin:
351374
type: string
352-
callFee:
353-
type: string
354375
expireBlocks:
355376
type: integer
356377
expireTime:
357378
type: integer
379+
feePercentage:
380+
type: number
381+
fixedFee:
382+
type: string
358383
maxValue:
359384
type: string
360385
minValue:
@@ -370,6 +395,26 @@ components:
370395
$ref: '#/components/schemas/PegoutConfigurationDTO'
371396
type: object
372397
type: object
398+
PegoutCreationDataDTO:
399+
properties:
400+
feeRate:
401+
description: The fee rate used to compute the gas fee
402+
type: number
403+
fixedFee:
404+
description: The fixed fee used to compute the call fee
405+
type: integer
406+
gasPrice:
407+
description: The gas price used to compute the gas fee
408+
type: integer
409+
percentageFee:
410+
description: The percentage fee used to compute the call fee
411+
type: number
412+
required:
413+
- gasPrice
414+
- percentageFee
415+
- fixedFee
416+
- feeRate
417+
type: object
373418
PegoutQuoteDTO:
374419
properties:
375420
agreementTimestamp:
@@ -455,6 +500,10 @@ components:
455500
type: object
456501
PegoutQuoteStatusDTO:
457502
properties:
503+
creationData:
504+
$ref: '#/components/schemas/PegoutCreationDataDTO'
505+
description: Values used to compute some fields of the quote
506+
type: object
458507
detail:
459508
$ref: '#/components/schemas/PegoutQuoteDTO'
460509
description: Agreed specification of the quote
@@ -466,10 +515,16 @@ components:
466515
required:
467516
- detail
468517
- status
518+
- creationData
469519
type: object
470520
ProviderDetail:
471521
properties:
472522
fee:
523+
deprecated: true
524+
type: integer
525+
feePercentage:
526+
type: number
527+
fixedFee:
473528
type: integer
474529
maxTransactionValue:
475530
type: integer
@@ -479,6 +534,8 @@ components:
479534
type: integer
480535
required:
481536
- fee
537+
- fixedFee
538+
- feePercentage
482539
- minTransactionValue
483540
- maxTransactionValue
484541
- requiredConfirmations

internal/adapters/dataproviders/bitcoin/derivative_wallet.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ func (wallet *DerivativeWallet) importPublicKey() error {
110110
return errors.New("public key imported, rescan started, please wait for the rescan process to finish before initializing the server again")
111111
}
112112

113-
func (wallet *DerivativeWallet) EstimateTxFees(toAddress string, value *entities.Wei) (*entities.Wei, error) {
113+
func (wallet *DerivativeWallet) EstimateTxFees(toAddress string, value *entities.Wei) (blockchain.BtcFeeEstimation, error) {
114114
const quoteHashLength = 32
115115

116116
if _, err := btcutil.DecodeAddress(toAddress, wallet.conn.NetworkParams); err != nil {
117-
return nil, err
117+
return blockchain.BtcFeeEstimation{}, err
118118
}
119119
if err := EnsureLoadedBtcWallet(wallet.conn); err != nil {
120-
return nil, err
120+
return blockchain.BtcFeeEstimation{}, err
121121
}
122122

123123
amountInSatoshi, _ := value.ToSatoshi().Int64()
@@ -128,11 +128,11 @@ func (wallet *DerivativeWallet) EstimateTxFees(toAddress string, value *entities
128128

129129
feeRate, err := wallet.estimateFeeRate()
130130
if err != nil {
131-
return nil, err
131+
return blockchain.BtcFeeEstimation{}, err
132132
}
133133
changeAddress, err := wallet.rskAccount.BtcAddress()
134134
if err != nil {
135-
return nil, err
135+
return blockchain.BtcFeeEstimation{}, err
136136
}
137137

138138
opts := btcjson.WalletCreateFundedPsbtOpts{
@@ -144,14 +144,17 @@ func (wallet *DerivativeWallet) EstimateTxFees(toAddress string, value *entities
144144

145145
simulatedTx, err := wallet.conn.client.WalletCreateFundedPsbt(nil, output, nil, &opts, nil)
146146
if err != nil {
147-
return nil, err
147+
return blockchain.BtcFeeEstimation{}, err
148148
}
149149
btcFee, err := btcutil.NewAmount(simulatedTx.Fee)
150150
if err != nil {
151-
return nil, err
151+
return blockchain.BtcFeeEstimation{}, err
152152
}
153153
satoshiFee := btcFee.ToUnit(btcutil.AmountSatoshi)
154-
return entities.SatoshiToWei(uint64(satoshiFee)), nil
154+
return blockchain.BtcFeeEstimation{
155+
Value: entities.SatoshiToWei(uint64(satoshiFee)),
156+
FeeRate: utils.NewBigFloat64(*feeRate),
157+
}, nil
155158
}
156159

157160
func (wallet *DerivativeWallet) GetBalance() (*entities.Wei, error) {

internal/adapters/dataproviders/bitcoin/derivative_wallet_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/rsksmart/liquidity-provider-server/internal/adapters/dataproviders/rootstock/account"
1515
"github.com/rsksmart/liquidity-provider-server/internal/entities"
1616
"github.com/rsksmart/liquidity-provider-server/internal/entities/blockchain"
17+
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
1718
"github.com/rsksmart/liquidity-provider-server/test"
1819
"github.com/rsksmart/liquidity-provider-server/test/mocks"
1920
"github.com/stretchr/testify/assert"
@@ -415,7 +416,8 @@ func testEstimateFees(t *testing.T, rskAccount *account.RskAccount, addressInfo
415416
require.NoError(t, err)
416417
fee, err := wallet.EstimateTxFees(testnetAddress, amount)
417418
require.NoError(t, err)
418-
assert.Equal(t, entities.NewWei(600000000000000), fee)
419+
assert.Equal(t, entities.NewWei(600000000000000), fee.Value)
420+
assert.Equal(t, utils.NewBigFloat64(feeRate), fee.FeeRate)
419421
client.AssertExpectations(t)
420422
}
421423

@@ -441,7 +443,8 @@ func testEstimateFeesExtra(t *testing.T, rskAccount *account.RskAccount, address
441443
require.NoError(t, err)
442444
fee, err := wallet.EstimateTxFees(testnetAddress, amount)
443445
require.NoError(t, err)
444-
assert.Equal(t, entities.NewWei(600000000000000), fee)
446+
assert.Equal(t, entities.NewWei(600000000000000), fee.Value)
447+
assert.Equal(t, utils.NewBigFloat64(0.00011), fee.FeeRate)
445448
client.AssertExpectations(t)
446449
}
447450

internal/adapters/dataproviders/bitcoin/watchonly_wallet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (wallet *WatchOnlyWallet) createWallet() (*btcjson.GetWalletInfoResult, err
5353
return wallet.conn.client.GetWalletInfo()
5454
}
5555

56-
func (wallet *WatchOnlyWallet) EstimateTxFees(toAddress string, value *entities.Wei) (*entities.Wei, error) {
57-
return nil, errors.New("cannot estimate from a watch-only wallet")
56+
func (wallet *WatchOnlyWallet) EstimateTxFees(toAddress string, value *entities.Wei) (blockchain.BtcFeeEstimation, error) {
57+
return blockchain.BtcFeeEstimation{}, errors.New("cannot estimate from a watch-only wallet")
5858
}
5959

6060
func (wallet *WatchOnlyWallet) GetBalance() (*entities.Wei, error) {

internal/adapters/dataproviders/bitcoin/watchonly_wallet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestWatchOnlyWallet_EstimateTxFees(t *testing.T) {
166166
require.NoError(t, err)
167167
result, err := wallet.EstimateTxFees("address", nil)
168168
require.ErrorContains(t, err, "cannot estimate from a watch-only wallet")
169-
require.Nil(t, result)
169+
require.Empty(t, result)
170170
}
171171

172172
// TestWatchOnlyWallet_GetTransactions This test are reused from the bitcoind wallet tests suite since they share behavior

internal/adapters/dataproviders/database/mongo/common_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,10 @@ func getClientAndCollectionMocks(collectionName string) (*mocks.DbClientBindingM
106106
db.On("Collection", collectionName).Return(collection)
107107
return client, collection
108108
}
109+
110+
func getClientAndDatabaseMocks() (*mocks.DbClientBindingMock, *mocks.DbBindingMock) {
111+
client := &mocks.DbClientBindingMock{}
112+
db := &mocks.DbBindingMock{}
113+
client.On("Database", mongo.DbName).Return(db)
114+
return client, db
115+
}

internal/adapters/dataproviders/database/mongo/liquidity_provider_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/rsksmart/liquidity-provider-server/internal/adapters/dataproviders/database/mongo"
66
"github.com/rsksmart/liquidity-provider-server/internal/entities"
77
"github.com/rsksmart/liquidity-provider-server/internal/entities/liquidity_provider"
8+
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
89
"github.com/rsksmart/liquidity-provider-server/test"
910
log "github.com/sirupsen/logrus"
1011
"github.com/stretchr/testify/assert"
@@ -23,7 +24,8 @@ var peginTestConfig = &entities.Signed[liquidity_provider.PeginConfiguration]{
2324
TimeForDeposit: 1,
2425
CallTime: 2,
2526
PenaltyFee: entities.NewWei(3),
26-
CallFee: entities.NewWei(4),
27+
FixedFee: entities.NewWei(4),
28+
FeePercentage: utils.NewBigFloat64(4.5),
2729
MaxValue: entities.NewWei(5),
2830
MinValue: entities.NewWei(6),
2931
},
@@ -36,7 +38,8 @@ var pegoutTestConfig = &entities.Signed[liquidity_provider.PegoutConfiguration]{
3638
TimeForDeposit: 1,
3739
ExpireTime: 2,
3840
PenaltyFee: entities.NewWei(3),
39-
CallFee: entities.NewWei(4),
41+
FixedFee: entities.NewWei(4),
42+
FeePercentage: utils.NewBigFloat64(4.5),
4043
MaxValue: entities.NewWei(5),
4144
MinValue: entities.NewWei(6),
4245
ExpireBlocks: 7,
@@ -76,7 +79,7 @@ func TestLpMongoRepository_GetPeginConfiguration(t *testing.T) {
7679
filter := bson.D{primitive.E{Key: "name", Value: mongo.ConfigurationName("pegin")}}
7780
log.SetLevel(log.DebugLevel)
7881
t.Run("pegin configuration read successfully", func(t *testing.T) {
79-
const expectedLog = "READ interaction with db: {Value:{TimeForDeposit:1 CallTime:2 PenaltyFee:3 CallFee:4 MaxValue:5 MinValue:6} Signature:pegin signature Hash:pegin hash}"
82+
const expectedLog = "READ interaction with db: {Value:{TimeForDeposit:1 CallTime:2 PenaltyFee:3 FixedFee:4 FeePercentage:4.5 MaxValue:5 MinValue:6} Signature:pegin signature Hash:pegin hash}"
8083
client, collection := getClientAndCollectionMocks(mongo.LiquidityProviderCollection)
8184
repo := mongo.NewLiquidityProviderRepository(mongo.NewConnection(client, time.Duration(1)))
8285
collection.On("FindOne", mock.Anything, filter).
@@ -111,7 +114,7 @@ func TestLpMongoRepository_GetPegoutConfiguration(t *testing.T) {
111114
filter := bson.D{primitive.E{Key: "name", Value: mongo.ConfigurationName("pegout")}}
112115
log.SetLevel(log.DebugLevel)
113116
t.Run("pegout configuration read successfully", func(t *testing.T) {
114-
const expectedLog = "READ interaction with db: {Value:{TimeForDeposit:1 ExpireTime:2 PenaltyFee:3 CallFee:4 MaxValue:5 MinValue:6 ExpireBlocks:7 BridgeTransactionMin:8} Signature:pegout signature Hash:pegout hash}"
117+
const expectedLog = "READ interaction with db: {Value:{TimeForDeposit:1 ExpireTime:2 PenaltyFee:3 FixedFee:4 FeePercentage:4.5 MaxValue:5 MinValue:6 ExpireBlocks:7 BridgeTransactionMin:8} Signature:pegout signature Hash:pegout hash}"
115118
client, collection := getClientAndCollectionMocks(mongo.LiquidityProviderCollection)
116119
repo := mongo.NewLiquidityProviderRepository(mongo.NewConnection(client, time.Duration(1)))
117120
collection.On("FindOne", mock.Anything, filter).
@@ -216,7 +219,7 @@ func TestLpMongoRepository_UpsertPeginConfiguration(t *testing.T) {
216219
configName := mongo.ConfigurationName("pegin")
217220
filter := bson.D{primitive.E{Key: "name", Value: configName}}
218221
t.Run("pegin configuration upserted successfully", func(t *testing.T) {
219-
const expectedLog = "INSERT interaction with db: {Signed:{Value:{TimeForDeposit:1 CallTime:2 PenaltyFee:3 CallFee:4 MaxValue:5 MinValue:6} Signature:pegin signature Hash:pegin hash} Name:pegin}"
222+
const expectedLog = "INSERT interaction with db: {Signed:{Value:{TimeForDeposit:1 CallTime:2 PenaltyFee:3 FixedFee:4 FeePercentage:4.5 MaxValue:5 MinValue:6} Signature:pegin signature Hash:pegin hash} Name:pegin}"
220223
client, collection := getClientAndCollectionMocks(mongo.LiquidityProviderCollection)
221224
repo := mongo.NewLiquidityProviderRepository(mongo.NewConnection(client, time.Duration(1)))
222225
collection.On("ReplaceOne", mock.Anything, filter, mongo.StoredConfiguration[liquidity_provider.PeginConfiguration]{
@@ -243,7 +246,7 @@ func TestLpMongoRepository_UpsertPegoutConfiguration(t *testing.T) {
243246
configName := mongo.ConfigurationName("pegout")
244247
filter := bson.D{primitive.E{Key: "name", Value: configName}}
245248
t.Run("pegout configuration upserted successfully", func(t *testing.T) {
246-
const expectedLog = "INSERT interaction with db: {Signed:{Value:{TimeForDeposit:1 ExpireTime:2 PenaltyFee:3 CallFee:4 MaxValue:5 MinValue:6 ExpireBlocks:7 BridgeTransactionMin:8} Signature:pegout signature Hash:pegout hash} Name:pegout}"
249+
const expectedLog = "INSERT interaction with db: {Signed:{Value:{TimeForDeposit:1 ExpireTime:2 PenaltyFee:3 FixedFee:4 FeePercentage:4.5 MaxValue:5 MinValue:6 ExpireBlocks:7 BridgeTransactionMin:8} Signature:pegout signature Hash:pegout hash} Name:pegout}"
247250
client, collection := getClientAndCollectionMocks(mongo.LiquidityProviderCollection)
248251
repo := mongo.NewLiquidityProviderRepository(mongo.NewConnection(client, time.Duration(1)))
249252
collection.On("ReplaceOne", mock.Anything, filter, mongo.StoredConfiguration[liquidity_provider.PegoutConfiguration]{

0 commit comments

Comments
 (0)