-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merc-1640 fixing Mercury fee calculation (#168)
* Fixing native/link fee calculation * Inclusing cents_in_usd into the price_scaling_factor to simplify the fee calculation * CalculateFee returns 0 when token price or base fee is 0 * Using decimal package for fee calculation * Fixing tests
- Loading branch information
Showing
4 changed files
with
84 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,39 @@ | ||
package mercury | ||
|
||
import "math/big" | ||
import ( | ||
"math/big" | ||
|
||
// PriceScalingFactor indicates the multiplier applied to token prices. | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
// PriceScalingFactor indicates the multiplier applied to token prices that we expect from data source | ||
// e.g. for a 1e8 multiplier, a LINK/USD value of 7.42 will be represented as 742000000 | ||
// This is what we expect from our data source. | ||
var PRICE_SCALING_FACTOR = big.NewInt(1e8) | ||
// The factor is decreased 1e8 -> 1e6 to comnpensate for baseUSDFee being in cents not usd | ||
var PRICE_SCALING_FACTOR = decimal.NewFromInt(1e6) | ||
|
||
// FeeScalingFactor indicates the multiplier applied to fees. | ||
// e.g. for a 1e18 multiplier, a LINK fee of 7.42 will be represented as 7.42e18 | ||
// This is what will be baked into the report for use on-chain. | ||
var FEE_SCALING_FACTOR = big.NewInt(1e18) | ||
var FEE_SCALING_FACTOR = decimal.NewFromInt(1e18) | ||
|
||
// CalculateFee outputs a fee in wei according to the formula: baseUSDFeeCents * scaleFactor / tokenPriceInUSD | ||
func CalculateFee(tokenPriceInUSD *big.Int, baseUSDFeeCents uint32) *big.Int { | ||
if tokenPriceInUSD.Cmp(big.NewInt(0)) == 0 || baseUSDFeeCents == 0 { | ||
// zero fee if token price or base fee is zero | ||
return big.NewInt(0) | ||
} | ||
|
||
// scale baseFee in USD | ||
baseFeeScaled := decimal.NewFromInt32(int32(baseUSDFeeCents)).Mul(PRICE_SCALING_FACTOR) | ||
|
||
tokenPrice := decimal.NewFromBigInt(tokenPriceInUSD, 0) | ||
|
||
// fee denominated in token | ||
fee := baseFeeScaled.Div(tokenPrice) | ||
|
||
var CENTS_PER_DOLLAR = big.NewInt(100) | ||
// scale fee to the expected format | ||
fee = fee.Mul(FEE_SCALING_FACTOR) | ||
|
||
// CalculateFee outputs a fee in wei | ||
func CalculateFee(tokenPriceInUSD *big.Int, baseUSDFeeCents uint32) (fee *big.Int) { | ||
fee = new(big.Int).Mul(big.NewInt(int64(baseUSDFeeCents)), tokenPriceInUSD) | ||
fee = fee.Mul(fee, FEE_SCALING_FACTOR) | ||
fee = fee.Div(fee, PRICE_SCALING_FACTOR) | ||
fee = fee.Div(fee, CENTS_PER_DOLLAR) | ||
return | ||
// convert to big.Int | ||
return fee.BigInt() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters