model package - github.com/blndgs/model - Go Packages

model

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: MIT Imports: 12 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 data from the CallData field.

The Calldata field in a userOperation is expected to contain the intent json value and or the Intent execution EVM instructions value for solved userOps or conventional userOps respectively. The separator token is required when both intent json and EVM instructions values are present. The separator token is not required when either the Intent or EVM instructions are present.

The separator token is defined as "<intent-end>".

<intent json><intent-end><Intent Execution:EVM instructions>

1. <Intent json>: The Intent JSON definition.

2. <intent-end>: A separator token to separate the Intent JSON from the EVM instructions value.

3. Execution EVM instructions: a hexadecimal 0x prefixed value. Execution EVM instructions are the EVM instructions that will be executed on chain.

Index

Constants

View Source
const (
	Swap = "swap"
	Buy  = "buy"
	Sell = "sell"
)
View Source
const (
	ErrNoIntentFound     userOperationError = "no Intent found"
	ErrIntentInvalidJSON userOperationError = "invalid Intent JSON"
	ErrNoSeparator       userOperationError = "separator token not found"
	ErrNoCalldata        userOperationError = "no CallData found"
)

Define error constants

View Source
const IntentEndToken = "<intent-end>"

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 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 body of an HTTP request to 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 extracts and returns the Ethereum EVM instructions from the CallData field. It returns an error if the EVM instructions value does not exist or does not start with "0x".

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 JSON from the CallData 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 field, 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) HasEVMInstructions added in v0.6.0

func (op *UserOperation) HasEVMInstructions() bool

HasEVMInstructions returns true if the Intent Execution EVM instructions field starts with "0x" either directly or after the intent JSON and separator token.

func (*UserOperation) HasIntent added in v0.4.0

func (op *UserOperation) HasIntent() bool

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

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)

SetEVMInstructions sets the Intent Execution Ethereum EVM instructions of the CallData field.

func (*UserOperation) SetIntent added in v0.4.0

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

SetIntent sets the Intent JSON part of the CallData field.

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 marshaller 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 represents additional extended information about a UserOperation that will be communicated from the Bundler to the Solver. The Solver may change the sequence of UserOperations in the UserOperationExt slice to match the sequence of UserOperations in the UserOps slice. The `OriginalHashValue` field is the hash value of the UserOperation as it was calculated for the userOp submitted by the wallet before the UserOperation is solved and it is a Read-Only field.

Jump to

Keyboard shortcuts

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