model

package module
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 10 Imported by: 8

README

Model

Reusing data types, domain model or Shared Kernel in DDD parlance.

Test

For the test run the following command:

go test ./...

Documentation

Overview

Package model provides common data structures shared among blndgs projects. This file has been copied from the Bundler project. It is included in the model package to avoid introducing a cyclical dependency between the Model and Bundler projects and accommodate Bundler<->Solver communication. Any modifications made to the Bundler file should be reflected here as well to maintain consistency.

Note: In the future, this file may move here as the single source of truth.

Package model provides structures and methods for the communication between the Bundler and Solver. This file defines extensions to the UserOperation struct and methods for extracting and inserting Intent JSON from/to the CallData and Signature fields.

The CallData field in a userOperation is expected to contain either the Intent JSON or the EVM instructions but not both. The Intent JSON is expected to be appended to the signature value within the Signature field when the Calldata field contains the EVM instructions. The Signature field is expected to contain only the signature when the userOperation is unsolved.

Index

Constants

View Source
const (
	Swap = "swap"
	Buy  = "buy"
	Sell = "sell"
)
View Source
const (
	ErrNoIntentFound     userOperationError = "no Intent found"
	ErrIntentInvalidJSON userOperationError = "invalid Intent JSON"
	ErrNoSignatureValue  userOperationError = "signature value is not found"
	ErrNoCallData        userOperationError = "no CallData found"
	ErrInvalidCallData   userOperationError = "invalid hex-encoded CallData"
	ErrInvalidSignature  userOperationError = "invalid hex-encoded signature"
	ErrInvalidUserOp     userOperationError = "ambiguous UserOperation solved state"
	ErrDoubleIntentDef   userOperationError = "intent JSON is set in both calldata and signature fields"
)

Define error constants

View Source
const SignatureLength = 132

Variables

View Source
var (

	// UserOpPrimitives is the primitive ABI types for each UserOperation field.
	UserOpPrimitives = []abi.ArgumentMarshaling{
		{Name: "sender", InternalType: "Sender", Type: "address"},
		{Name: "nonce", InternalType: "Nonce", Type: "uint256"},
		{Name: "initCode", InternalType: "InitCode", Type: "bytes"},
		{Name: "callData", InternalType: "CallData", Type: "bytes"},
		{Name: "callGasLimit", InternalType: "CallGasLimit", Type: "uint256"},
		{Name: "verificationGasLimit", InternalType: "VerificationGasLimit", Type: "uint256"},
		{Name: "preVerificationGas", InternalType: "PreVerificationGas", Type: "uint256"},
		{Name: "maxFeePerGas", InternalType: "MaxFeePerGas", Type: "uint256"},
		{Name: "maxPriorityFeePerGas", InternalType: "MaxPriorityFeePerGas", Type: "uint256"},
		{Name: "paymasterAndData", InternalType: "PaymasterAndData", Type: "bytes"},
		{Name: "signature", InternalType: "Signature", Type: "bytes"},
	}

	// UserOpType is the ABI type of a UserOperation.
	UserOpType, _ = abi.NewType("tuple", "op", UserOpPrimitives)

	// UserOpArr is the ABI type for an array of UserOperations.
	UserOpArr, _ = abi.NewType("tuple[]", "ops", UserOpPrimitives)
)

Functions

func ExtractJSONFromField added in v0.14.0

func ExtractJSONFromField(fieldData string) (string, bool)

ExtractJSONFromField tries to unmarshal the provided field data into an Intent struct. If successful, it assumes the field data is a valid JSON string of the Intent.

Returns:

  • string: The JSON data if unmarshalling is successful.
  • bool: A boolean indicating if the unmarshalling was successful.

func NewValidator

func NewValidator() error

Types

type Body

type Body struct {
	Intents []*Intent `json:"intents" binding:"required,dive"`
}

type BodyOfUserOps added in v0.4.0

type BodyOfUserOps struct {
	UserOps    []*UserOperation   `json:"user_ops" binding:"required,dive"`
	UserOpsExt []UserOperationExt `json:"user_ops_ext" binding:"required,dive"`
}

BodyOfUserOps represents the request body for HTTP requests sent to the Solver. It contains slices of UserOperation and UserOperationExt, representing the primary data and its extended information required for processing by the Solver.

type Intent

type Intent struct {
	Sender            string           `json:"sender" binding:"required,eth_addr"` // filled by ui
	Kind              string           `json:"kind" binding:"required"`            // ui
	Hash              string           `json:"hash"`                               // ui or bundler
	SellToken         string           `json:"sellToken"`                          // optional for limit orders, ui
	BuyToken          string           `json:"buyToken"`                           // ui
	SellAmount        float64          `json:"sellAmount"`                         // optional for limit orders, ui
	BuyAmount         float64          `json:"buyAmount"`                          // ui
	PartiallyFillable bool             `json:"partiallyFillable"`                  // ui
	CallData          string           `json:"callData"`                           // UI, Bundler, Solver
	Status            ProcessingStatus `json:"status" binding:"status"`            // ui or bundler
	CreatedAt         int64            `json:"createdAt" binding:"opt_int"`        // ui or bundler
	ExpirationAt      int64            `json:"expirationAt" binding:"opt_int"`     // ui or bundler for default expiration (TTL: 100 seconds)
	ChainID           *big.Int         `json:"chainId" binding:"required,chain_id"`
}

func (*Intent) ToJSON added in v0.4.0

func (i *Intent) ToJSON() (string, error)

ToJSON serializes the Intent into a JSON string

func (*Intent) ToString added in v0.4.0

func (i *Intent) ToString() string

ToString provides a string representation of the Intent

func (*Intent) ValidateKind

func (i *Intent) ValidateKind() bool

ValidateKind this function is manually, and it's not bound to Gin's validation. Should be called before posting or responding to a request. Investigate if it can be bound to Gin's validation.

type ProcessingStatus

type ProcessingStatus string
const (
	Received     ProcessingStatus = "Received"
	SentToSolver ProcessingStatus = "SentToSolver"
	Solved       ProcessingStatus = "Solved"
	Unsolved     ProcessingStatus = "Unsolved"
	Expired      ProcessingStatus = "Expired"
	OnChain      ProcessingStatus = "OnChain"
	Invalid      ProcessingStatus = "Invalid"
)

type UserOperation added in v0.4.0

type UserOperation struct {
	Sender               common.Address `json:"sender"               mapstructure:"sender"               validate:"required"`
	Nonce                *big.Int       `json:"nonce"                mapstructure:"nonce"                validate:"required"`
	InitCode             []byte         `json:"initCode"             mapstructure:"initCode"             validate:"required"`
	CallData             []byte         `json:"callData"             mapstructure:"callData"             validate:"required"`
	CallGasLimit         *big.Int       `json:"callGasLimit"         mapstructure:"callGasLimit"         validate:"required"`
	VerificationGasLimit *big.Int       `json:"verificationGasLimit" mapstructure:"verificationGasLimit" validate:"required"`
	PreVerificationGas   *big.Int       `json:"preVerificationGas"   mapstructure:"preVerificationGas"   validate:"required"`
	MaxFeePerGas         *big.Int       `json:"maxFeePerGas"         mapstructure:"maxFeePerGas"         validate:"required"`
	MaxPriorityFeePerGas *big.Int       `json:"maxPriorityFeePerGas" mapstructure:"maxPriorityFeePerGas" validate:"required"`
	PaymasterAndData     []byte         `json:"paymasterAndData"     mapstructure:"paymasterAndData"     validate:"required"`
	Signature            []byte         `json:"signature"            mapstructure:"signature"            validate:"required"`
}

UserOperation represents an EIP-4337 style transaction for a smart contract account.

func (*UserOperation) GetDynamicGasPrice added in v0.4.0

func (op *UserOperation) GetDynamicGasPrice(basefee *big.Int) *big.Int

GetDynamicGasPrice returns the effective gas price paid by the UserOperation given a basefee. If basefee is nil, it will assume a value of 0.

func (*UserOperation) GetEVMInstructions added in v0.6.0

func (op *UserOperation) GetEVMInstructions() ([]byte, error)

GetEVMInstructions returns the Ethereum EVM instructions from the CallData field. It returns an error if the EVM instructions value does not exist or is not a valid hex encoded string.

func (*UserOperation) GetFactory added in v0.4.0

func (op *UserOperation) GetFactory() common.Address

GetFactory returns the address portion of InitCode if applicable. Otherwise it returns the zero address.

func (*UserOperation) GetIntent added in v0.4.0

func (op *UserOperation) GetIntent() (*Intent, error)

GetIntent takes the Intent Type from the CallData or Signature field, decodes it into an Intent struct, and returns the struct.

func (*UserOperation) GetIntentJSON added in v0.4.0

func (op *UserOperation) GetIntentJSON() (string, error)

GetIntentJSON returns the Intent JSON from the CallData or signature fields, if present.

func (*UserOperation) GetMaxGasAvailable added in v0.4.0

func (op *UserOperation) GetMaxGasAvailable() *big.Int

GetMaxGasAvailable returns the max amount of gas that can be consumed by this UserOperation.

func (*UserOperation) GetMaxPrefund added in v0.4.0

func (op *UserOperation) GetMaxPrefund() *big.Int

GetMaxPrefund returns the max amount of wei required to pay for gas fees by either the sender or paymaster.

func (*UserOperation) GetPaymaster added in v0.4.0

func (op *UserOperation) GetPaymaster() common.Address

GetPaymaster returns the address portion of PaymasterAndData if applicable. Otherwise it returns the zero address.

func (*UserOperation) GetUserOpHash added in v0.4.0

func (op *UserOperation) GetUserOpHash(entryPoint common.Address, chainID *big.Int) common.Hash

GetUserOpHash returns the hash of the userOp + entryPoint address + chainID.

func (*UserOperation) HasIntent added in v0.4.0

func (op *UserOperation) HasIntent() bool

HasIntent checks if the CallData or signature field contains a valid Intent JSON that decodes successfully into an Intent struct.

func (*UserOperation) HasSignature added in v0.13.0

func (op *UserOperation) HasSignature() bool

HasSignature checks if the signature field contains a fixed length hex-encoded signature value that is hex-encoded.

func (*UserOperation) MarshalJSON added in v0.4.0

func (op *UserOperation) MarshalJSON() ([]byte, error)

MarshalJSON returns a JSON encoding of the UserOperation.

func (*UserOperation) Pack added in v0.4.0

func (op *UserOperation) Pack() []byte

Pack returns a standard message of the userOp. This cannot be used to generate a userOpHash.

func (*UserOperation) PackForSignature added in v0.4.0

func (op *UserOperation) PackForSignature() []byte

PackForSignature returns a minimal message of the userOp. This can be used to generate a userOpHash.

func (*UserOperation) SetEVMInstructions added in v0.6.0

func (op *UserOperation) SetEVMInstructions(callDataValue []byte) error

SetEVMInstructions sets the EVM instructions in the CallData field of the UserOperation. It appropriately handles the Intent JSON based on the operation's solution state. The function first checks the solved status of the operation. For solved operations, it ensures that the signature has the required length. For unsolved operations, it moves the Intent JSON to the Signature field if present and valid, and then sets the provided EVM instructions in the CallData field.

Parameters:

  • callDataValue: A byte slice containing the EVM instructions to be set in the CallData field.

Returns:

  • error: An error is returned if the operation's status is invalid, if there is no signature in the UserOperation when required, or if any other issue arises during the process. Otherwise, nil is returned, indicating successful setting of the EVM instructions.

func (*UserOperation) SetIntent added in v0.4.0

func (op *UserOperation) SetIntent(intentJSON string) error

SetIntent sets the Intent JSON in the appropriate field of the UserOperation based on the operation's solution state. The function first validates the Intent JSON. If the userOp CallData field does not contain EVM instructions (indicating an unsolved userOp), the intentJSON is set directly in the CallData field. If the CallData field contains EVM instructions (indicating a solved userOp), the function then checks the length of the Signature field. If the length of the Signature is less than the required signature length an error is returned. If the Signature is of the appropriate length, the intentJSON is appended to the Signature field starting at the SignatureLength index.

Returns: - error: An error is returned if the intentJSON is invalid, if there is no signature in the UserOperation when required, or if any other issue arises during the process. Otherwise, nil is returned indicating successful setting of the intent JSON.

func (*UserOperation) String added in v0.7.0

func (op *UserOperation) String() string

func (*UserOperation) ToMap added in v0.4.0

func (op *UserOperation) ToMap() (map[string]any, error)

ToMap returns the current UserOp struct as a map type.

func (*UserOperation) UnmarshalJSON added in v0.6.0

func (op *UserOperation) UnmarshalJSON(data []byte) error

UnmarshalJSON does the reverse of the provided bundler custom JSON marshaler for a UserOperation.

type UserOperationExt added in v0.4.0

type UserOperationExt struct {
	OriginalHashValue string           `json:"original_hash_value" mapstructure:"original_hash_value" validate:"required"`
	ProcessingStatus  ProcessingStatus `json:"processing_status" mapstructure:"processing_status" validate:"required"`
}

UserOperationExt extends the UserOperation with additional information necessary for processing by the Solver.This includes the original hash value of the UserOperation and its processing status.The sequence of UserOperationExt instances must correspond to the sequence in the UserOps slice.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL