Skip to content

Commit

Permalink
added WETH storage slot utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Jul 29, 2023
1 parent 8dca185 commit 7095aa3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
38 changes: 38 additions & 0 deletions w3vm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package w3vm

import (
"crypto/rand"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3/internal/crypto"
)

// zero values
Expand All @@ -19,6 +21,42 @@ func RandA() (addr common.Address) {
return addr
}

var (
weth9BalancePos = big.NewInt(3)
weth9AllowancePos = big.NewInt(4)
)

// WETHBalanceSlot returns the storage slot that stores the WETH balance of
// the given addr.
func WETHBalanceSlot(addr common.Address) common.Hash {
return slot(weth9BalancePos, addr)
}

// WETHAllowanceSlot returns the storage slot that stores the WETH allowance
// of the given owner and spender.
func WETHAllowanceSlot(owner, spender common.Address) common.Hash {
return slot2(weth9AllowancePos, owner, spender)
}

func slot(pos *big.Int, acc common.Address) common.Hash {
data := make([]byte, 64)
copy(data[12:32], acc[:])
pos.FillBytes(data[32:])

return crypto.Keccak256Hash(data)
}

func slot2(pos *big.Int, acc, acc2 common.Address) common.Hash {
data := make([]byte, 64)
copy(data[12:32], acc[:])
pos.FillBytes(data[32:])

copy(data[32:], crypto.Keccak256(data))
copy(data[12:32], acc2[:])

return crypto.Keccak256Hash(data)
}

// nilToZero converts sets a pointer to the zero value if it is nil.
func nilToZero[T any](ptr *T) *T {
if ptr == nil {
Expand Down
58 changes: 58 additions & 0 deletions w3vm/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package w3vm_test

import (
"strconv"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/w3vm"
)

func TestWETHBalanceSlot(t *testing.T) {
tests := []struct {
Addr common.Address
WantSlot common.Hash
}{
{
Addr: w3.A("0x000000000000000000000000000000000000dEaD"),
WantSlot: w3.H("0x262bb27bbdd95c1cdc8e16957e36e38579ea44f7f6413dd7a9c75939def06b2c"),
},
{
Addr: w3.A("0x000000000000000000000000000000000000c0Fe"),
WantSlot: w3.H("0xf68b260b81af177c0bf1a03b5d62b15aea1b486f8df26c77f33aed7538cfeb2c"),
},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
gotSlot := w3vm.WETHBalanceSlot(test.Addr)
if test.WantSlot != gotSlot {
t.Fatalf("want %s, got %s", test.WantSlot, gotSlot)
}
})
}
}

func TestWETHAllowanceSlot(t *testing.T) {
tests := []struct {
Owner common.Address
Spender common.Address
WantSlot common.Hash
}{
{
Owner: w3.A("0x000000000000000000000000000000000000dEaD"),
Spender: w3.A("0x000000000000000000000000000000000000c0Fe"),
WantSlot: w3.H("0xea3c5e9cf6f5b7aba5d41ca731cf1f8cb1373e841a1cb336cc4bfeddc27c7f8b"),
},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
gotSlot := w3vm.WETHAllowanceSlot(test.Owner, test.Spender)
if test.WantSlot != gotSlot {
t.Fatalf("want %s, got %s", test.WantSlot, gotSlot)
}
})
}
}

0 comments on commit 7095aa3

Please sign in to comment.