Skip to content

Commit

Permalink
Adds Anvil Set Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kalverra committed Oct 8, 2024
1 parent c31feba commit fc4b806
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/client/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ func (m *RPCClient) AnvilDropTransaction(params []interface{}) error {
return nil
}

// AnvilSetStorageAt sets storage at address
// API Reference https://book.getfoundry.sh/reference/anvil/
func (m *RPCClient) AnvilSetStorageAt(params []interface{}) error {
rInt, err := rand.Int()
if err != nil {
return err
}
payload := map[string]interface{}{
"jsonrpc": "2.0",
"method": "anvil_setStorageAt",
"params": params,
"id": rInt,
}
if _, err := m.client.R().SetBody(payload).Post(m.URL); err != nil {
return errors.Wrap(err, "anvil_setStorageAt")
}
return nil
}

type CurrentBlockResponse struct {
Result string `json:"result"`
}
Expand Down
18 changes: 18 additions & 0 deletions lib/client/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ func TestRPCAPI(t *testing.T) {
t.Logf("status: %v", status)
})

t.Run("(anvil) test set storage at address", func(t *testing.T) {
ac, err := StartAnvil([]string{"--balance", "1", "--block-time", "5"})
require.NoError(t, err)
client, err := ethclient.Dial(ac.URL)
require.NoError(t, err)

randomAddress := common.HexToAddress("0x0d2026b3EE6eC71FC6746ADb6311F6d3Ba1C000B")

anvilClient := NewRPCClient(ac.URL, nil)
err = anvilClient.AnvilSetStorageAt([]interface{}{randomAddress, "0x0", "0x420"})
require.NoError(t, err)
status, err := client.StorageAt(context.Background(), randomAddress, common.HexToHash("0x0"), nil)
require.NoError(t, err)
require.Equal(t, "0x420", string(status))

t.Logf("status: %v", status)
})

t.Run("(anvil) test we can shrink the block and control transaction inclusion", func(t *testing.T) {
ac, err := StartAnvil([]string{"--balance", "1", "--block-time", "1"})
require.NoError(t, err)
Expand Down

0 comments on commit fc4b806

Please sign in to comment.