-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
116 additions
and
14 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package relayer | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// ErrInsufficientFee is the error of insufficient fee | ||
var ErrInsufficientFee = errors.New("insufficient fee") | ||
|
||
// FeeChecker checks the fee of a transfer | ||
type FeeChecker struct { | ||
tokenFees map[string]map[string]*big.Int | ||
} | ||
|
||
// NewFeeChecker creates a new fee checker | ||
func NewFeeChecker() *FeeChecker { | ||
return &FeeChecker{ | ||
tokenFees: make(map[string]map[string]*big.Int), | ||
} | ||
} | ||
|
||
// SetFee sets the fee of a transfer | ||
func (fc *FeeChecker) SetFee(token, recipient string, fee *big.Int) { | ||
if _, exists := fc.tokenFees[token]; !exists { | ||
fc.tokenFees[token] = make(map[string]*big.Int) | ||
} | ||
fc.tokenFees[token][recipient] = fee | ||
} | ||
|
||
// Check checks the fee of a transfer | ||
func (fc *FeeChecker) Check(transfer *Transfer) error { | ||
requiredFees, exists := fc.tokenFees[transfer.token.String()] | ||
if !exists { | ||
return nil | ||
} | ||
requiredFee, exists := requiredFees[transfer.recipient.String()] | ||
if !exists { | ||
return nil | ||
} | ||
if transfer.fee.Cmp(requiredFee) < 0 { | ||
return errors.Wrapf(ErrInsufficientFee, "fee %d is lower than required %d", transfer.fee, requiredFee) | ||
} | ||
return nil | ||
} |
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
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