Skip to content

Commit

Permalink
feat: add service fee rate and update related calculations (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang authored Feb 27, 2025
1 parent 60ef239 commit 86a95b7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 2 additions & 0 deletions internal/handler/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,7 @@ func (h *handler) Info(c *gin.Context) {
"icy_usd_rate": icyusdWeb3BigInt.Value,
"satoshi_usd_rate": satusdWeb3BigInt.Value,
"min_icy_to_swap": minIcySwap.Value,
"service_fee_rate": h.appConfig.Bitcoin.ServiceFeeRate,
"min_satoshi_fee": h.appConfig.Bitcoin.MinSatshiFee,
}, nil, nil, "swap info retrieved successfully"))
}
14 changes: 7 additions & 7 deletions internal/telemetry/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,25 @@ func (t *Telemetry) IndexIcySwapTransaction() error {
subtotalBig.SetString(swapTx.BtcAmount, 10)

// Calculate percentage-based fee
feePercentage := t.appConfig.Bitcoin.ServiceFeePercentage
percentageFee := new(big.Float).Mul(
svcfeeRate := t.appConfig.Bitcoin.ServiceFeeRate
svcFee := new(big.Float).Mul(
new(big.Float).SetInt(subtotalBig),
new(big.Float).SetFloat64(feePercentage),
new(big.Float).SetFloat64(svcfeeRate),
)

// Convert to int for comparison with min fee
var percentageFeeInt big.Int
percentageFee.Int(&percentageFeeInt)
var svcFeeInt big.Int
svcFee.Int(&svcFeeInt)

// Get minimum fee from config
minFeeBig := big.NewInt(t.appConfig.Bitcoin.MinSatshiFee)

// Use the larger of percentage fee or minimum fee
var serviceFee big.Int
if percentageFeeInt.Cmp(minFeeBig) < 0 {
if svcFeeInt.Cmp(minFeeBig) < 0 {
serviceFee = *minFeeBig
} else {
serviceFee = percentageFeeInt
serviceFee = svcFeeInt
}

// Calculate total (subtotal - service fee)
Expand Down
20 changes: 10 additions & 10 deletions internal/utils/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ type DBConnection struct {
}

type BitcoinConfig struct {
WalletWIF string
BlockstreamAPIURL string
MaxTxFeeUSD float64
ServiceFeePercentage float64
MinSatshiFee int64
WalletWIF string
BlockstreamAPIURL string
MaxTxFeeUSD float64
ServiceFeeRate float64
MinSatshiFee int64
}

func New() *AppConfig {
Expand All @@ -75,11 +75,11 @@ func New() *AppConfig {
SSLMode: os.Getenv("DB_SSL_MODE"),
},
Bitcoin: BitcoinConfig{
WalletWIF: os.Getenv("BTC_WALLET_WIF"),
BlockstreamAPIURL: os.Getenv("BTC_BLOCKSTREAM_API_URL"),
MaxTxFeeUSD: envVarAsFloat("BTC_MAX_TX_FEE_USD", 1.0),
ServiceFeePercentage: envVarAsFloat("BTC_SERVICE_FEE_PERCENTAGE", 0.01),
MinSatshiFee: envVarAsInt64("BTC_MIN_SATOSHI_FEE", 3000),
WalletWIF: os.Getenv("BTC_WALLET_WIF"),
BlockstreamAPIURL: os.Getenv("BTC_BLOCKSTREAM_API_URL"),
MaxTxFeeUSD: envVarAsFloat("BTC_MAX_TX_FEE_USD", 1.0),
ServiceFeeRate: envVarAsFloat("BTC_SERVICE_FEE_PERCENTAGE", 0.01),
MinSatshiFee: envVarAsInt64("BTC_MIN_SATOSHI_FEE", 3000),
},
Blockchain: BlockchainConfig{
BaseRPCEndpoint: os.Getenv("BLOCKCHAIN_BASE_RPC_ENDPOINT"),
Expand Down

0 comments on commit 86a95b7

Please sign in to comment.