-
Notifications
You must be signed in to change notification settings - Fork 36
/
paymaster.go
68 lines (62 loc) · 2.37 KB
/
paymaster.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package utils
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/zksync-sdk/zksync2-go/contracts/paymasterflow"
"github.com/zksync-sdk/zksync2-go/types"
"log"
"strings"
)
var paymasterFlowAbi abi.ABI
func init() {
var err error
paymasterFlowAbi, err = abi.JSON(strings.NewReader(paymasterflow.IPaymasterFlowMetaData.ABI))
if err != nil {
log.Fatal("failed to load paymasterFlowAbi: %w", err)
}
}
// GetApprovalBasedPaymasterInput returns encoded input for an approval-based paymaster.
func GetApprovalBasedPaymasterInput(paymasterInput types.ApprovalBasedPaymasterInput) ([]byte, error) {
return paymasterFlowAbi.Pack("approvalBased",
paymasterInput.Token,
paymasterInput.MinimalAllowance,
paymasterInput.InnerInput)
}
// GetGeneralPaymasterInput returns encoded input for a general-based paymaster.
func GetGeneralPaymasterInput(paymasterInput types.GeneralPaymasterInput) ([]byte, error) {
return paymasterFlowAbi.Pack("general", paymasterInput.GetInput())
}
// GetPaymasterParams returns a correctly-formed paymaster parameters for common paymaster flows.
func GetPaymasterParams(paymasterAddress common.Address, paymasterInput types.PaymasterInput) (*types.PaymasterParams, error) {
if paymasterInput.GetType() == "General" {
generalPaymasterInput, ok := paymasterInput.(*types.GeneralPaymasterInput)
if !ok {
return &types.PaymasterParams{}, errors.New("cannot convert PaymasterInput to GeneralPaymasterInput type")
}
input, err := GetGeneralPaymasterInput(*generalPaymasterInput)
if err != nil {
return &types.PaymasterParams{}, err
}
return &types.PaymasterParams{
Paymaster: paymasterAddress,
PaymasterInput: input,
}, nil
} else if paymasterInput.GetType() == "ApprovalBased" {
approvalBasedPaymasterInput, ok := paymasterInput.(*types.ApprovalBasedPaymasterInput)
if !ok {
return &types.PaymasterParams{}, errors.New("cannot convert PaymasterInput to ApprovalBasedPaymasterInput type")
}
input, err := GetApprovalBasedPaymasterInput(*approvalBasedPaymasterInput)
if err != nil {
return &types.PaymasterParams{}, err
}
return &types.PaymasterParams{
Paymaster: paymasterAddress,
PaymasterInput: input,
}, nil
} else {
return &types.PaymasterParams{}, fmt.Errorf("cannot recognize given paymaster input type: %s", paymasterInput.GetType())
}
}