forked from slingshot-finance/uniswapv3-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payments.go
50 lines (38 loc) · 1.44 KB
/
payments.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 periphery
import (
_ "embed"
"math/big"
"github.com/daoleno/uniswap-sdk-core/entities"
"github.com/ethereum/go-ethereum/common"
)
//go:embed contracts/interfaces/IPeripheryPaymentsWithFee.sol/IPeripheryPaymentsWithFee.json
var paymentsABI []byte
type FeeOptions struct {
Fee *entities.Percent // The percent of the output that will be taken as a fee.
Recipient common.Address // The recipient of the fee.
}
func encodeFeeBips(fee *entities.Percent) *big.Int {
return fee.Multiply(entities.NewPercent(big.NewInt(10000), big.NewInt(1))).Quotient()
}
func EncodeUnwrapWETH9(amountMinimum *big.Int, recipient common.Address, feeOptions *FeeOptions) ([]byte, error) {
abi := GetABI(paymentsABI)
if feeOptions != nil {
return abi.Pack("unwrapWETH9WithFee", amountMinimum, &recipient, encodeFeeBips(feeOptions.Fee), feeOptions.Recipient)
}
return abi.Pack("unwrapWETH9", amountMinimum, recipient)
}
func EncodeSweepToken(token *entities.Token, amountMinimum *big.Int, recipient common.Address, feeOptions *FeeOptions) ([]byte, error) {
abi := GetABI(paymentsABI)
if feeOptions != nil {
return abi.Pack("sweepTokenWithFee", token.Address, amountMinimum, recipient, encodeFeeBips(feeOptions.Fee), feeOptions.Recipient)
}
return abi.Pack("sweepToken", token.Address, amountMinimum, recipient)
}
func EncodeRefundETH() []byte {
abi := GetABI(paymentsABI)
data, err := abi.Pack("refundETH")
if err != nil {
panic(err)
}
return data
}