anvilutils

package module
v0.0.0-...-0bc4ac8 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 13 Imported by: 0

README

Go Reference Go Report Card Go Coverage Badge

anvil utils

anvil utils is a library that provides cheating functionality executed in anvil foundry for interacting with account, transaction especially erc20 and erc721 token

installation

To use this library, you need to install anvil foundry first. (see this link)

TL;DR:

curl -L https://foundry.paradigm.xyz | bash
foundryup

Import:

import github.com/haupc/anvilutils

cheat code:

  • Write Erc20 balance
  • Write native balance
  • Set Erc20 approval
  • Set code for address
  • Start impersonate an address
  • Stop impersonate an address
  • Impersonate a txn(require impersonate account)
  • Impersonate and make only 1 txn
  • Take Erc721 token from another account
  • Set approve Erc721 token

How to Use:

You can run anvil separately or use forkCmd to run an anvil chain.

    // if you want to fork on test code, you can use fork command
    // otherwise, you can run anvil separately then call setup cheats
    forkCmd, err := anvilutils.NewForkCommand(ForkOpts{})
    if err != nil {
        // do something with err
    }
    forkUrl, err := forkCmd.Start()
    if err != nil {
        // do something with err
    }

    // setup client for anvil node
    client := client.NewClient(forkUrl)
    cheat := anvilutils.NewCheat(client)
    cheat.WriteErc20Balance(helper.DummyContract, helper.DummyAccount, big.NewInt(1234567890123))
    
    // call api, do everything for testing
    // do something with onchain data using client.GlobalClient
    // to sop fork
    forkCmd.Stop()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cheat

type Cheat struct {
	// contains filtered or unexported fields
}

func NewCheat

func NewCheat(client *client.Client) *Cheat

func (*Cheat) ImpersonateAccountAndSendTransaction

func (c *Cheat) ImpersonateAccountAndSendTransaction(from, to common.Address, value *big.Int, data []byte, gas uint64, gasPrice *big.Int) (txHash common.Hash, err error)

func (*Cheat) SendImpersonateTxn

func (c *Cheat) SendImpersonateTxn(from, to common.Address, value *big.Int, data []byte, gas uint64, gasPrice *big.Int) (txHash common.Hash, err error)

make an impersonate txn

func (*Cheat) SetApprovalErc20

func (c *Cheat) SetApprovalErc20(owner, token, spender common.Address, amount *big.Int) error

func (*Cheat) SetCode

func (c *Cheat) SetCode(account common.Address, code []byte) error

set code for an address

func (*Cheat) SetStorageAt

func (c *Cheat) SetStorageAt(contractAddress common.Address, slotIdx, data common.Hash) error

set storge for an contract

func (*Cheat) StartImpersonateAccount

func (c *Cheat) StartImpersonateAccount(account common.Address) error

after this call, all transactions from specified account can be executed without signing

func (*Cheat) StopImpersonateAccount

func (c *Cheat) StopImpersonateAccount(account common.Address) error

stop impersonating account

func (*Cheat) TakeErc721Token

func (c *Cheat) TakeErc721Token(tokenAddress common.Address, tokenId *big.Int, receiver common.Address) error

func (*Cheat) WriteErc20Balance

func (c *Cheat) WriteErc20Balance(contract, user common.Address, amount *big.Int) error

write balance of erc20 token for an account

func (*Cheat) WriteNativeBalance

func (c *Cheat) WriteNativeBalance(user common.Address, amount *big.Int) error

write native balance for an account

type ForkCmd

type ForkCmd struct {
	// contains filtered or unexported fields
}

func NewForkCommand

func NewForkCommand(opt ForkOpts) (*ForkCmd, error)

func (*ForkCmd) Start

func (f *ForkCmd) Start() (string, error)

start fork a network for testing purposes

func (*ForkCmd) Stop

func (f *ForkCmd) Stop() error

stop current fork process

type ForkOpts

type ForkOpts struct {
	ForkUrl     string
	Port        string
	BlockNumber *big.Int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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