-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
payment_checker.go
50 lines (42 loc) · 1.73 KB
/
payment_checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package fluxmonitorv2
import (
"math/big"
"github.com/smartcontractkit/chainlink/v2/core/assets"
)
// MinFundedRounds defines the minimum number of rounds that needs to be paid
// to oracles on a contract
const MinFundedRounds int64 = 3
// PaymentChecker provides helper functions to check whether payments are valid
type PaymentChecker struct {
// The minimum amount for a payment set in the ENV Var
MinContractPayment *assets.Link
// The minimum amount for a payment set in the job
MinJobPayment *assets.Link
}
// NewPaymentChecker constructs a new payment checker
func NewPaymentChecker(minContractPayment, minJobPayment *assets.Link) *PaymentChecker {
return &PaymentChecker{
MinContractPayment: minContractPayment,
MinJobPayment: minJobPayment,
}
}
// SufficientFunds checks if the contract has sufficient funding to pay all the
// oracles on a contract for a minimum number of rounds, based on the payment
// amount in the contract
func (c *PaymentChecker) SufficientFunds(availableFunds *big.Int, paymentAmount *big.Int, oracleCount uint8) bool {
min := big.NewInt(int64(oracleCount))
min = min.Mul(min, big.NewInt(MinFundedRounds))
min = min.Mul(min, paymentAmount)
return availableFunds.Cmp(min) >= 0
}
// SufficientPayment checks if the available payment is enough to submit an
// answer. It compares the payment amount on chain with the min payment amount
// listed in the job / ENV var.
func (c *PaymentChecker) SufficientPayment(payment *big.Int) bool {
aboveOrEqMinGlobalPayment := payment.Cmp(c.MinContractPayment.ToInt()) >= 0
aboveOrEqMinJobPayment := true
if c.MinJobPayment != nil {
aboveOrEqMinJobPayment = payment.Cmp(c.MinJobPayment.ToInt()) >= 0
}
return aboveOrEqMinGlobalPayment && aboveOrEqMinJobPayment
}