Skip to content

Commit

Permalink
feat: complete refactor factory target addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
shunkakinoki committed Feb 27, 2025
1 parent 58b21d9 commit 7fa3885
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 76 deletions.
8 changes: 7 additions & 1 deletion deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func (u *UniversalDeployer) Deploy(ctx context.Context, contractABI abi.ABI, con
Data: input,
GasLimit: uint64(gasLimit),
})
if err != nil {
return common.Address{}, fmt.Errorf("deployer: deploy new transaction: %w", err)
}

_, waitTx, err := u.Wallet.SendTransaction(ctx, tx)
if err != nil {
Expand All @@ -153,6 +156,9 @@ func (u *UniversalDeployer) Deploy(ctx context.Context, contractABI abi.ABI, con
if len(code) == 0 {
return common.Address{}, fmt.Errorf("can't deploy contract")
}
if err != nil {
return common.Address{}, fmt.Errorf("deployer: deploy contract codeAt: %w", err)
}

return contractAddress, nil
}
Expand Down Expand Up @@ -251,7 +257,7 @@ func (u *UniversalDeployer) deployUniversalDeployer1(ctx context.Context, txPara
if err != nil {
return fmt.Errorf("deployer: %w", err)
}
if universalDeployerCodeCheck == nil || len(universalDeployerCodeCheck) == 0 {
if universalDeployerCodeCheck == nil {
return fmt.Errorf("deployer (deployUniversalDeployer1): failed to deploy stage 1 deployer")
}

Expand Down
25 changes: 22 additions & 3 deletions deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/go-sequence/contracts"
"github.com/0xsequence/go-sequence/deployer"
"github.com/0xsequence/go-sequence/testutil"
Expand Down Expand Up @@ -33,10 +34,28 @@ func TestDeployer(t *testing.T) {
ud, err := deployer.NewUniversalDeployer(testWallet)
assert.NoError(t, err)

testSequenceContext := testutil.V1SequenceContext()
testV1SequenceContext := testutil.V1SequenceContext()

// Deploy sequence wallet-contract factory and ensure it equals the expected value in testutil
walletFactoryAddress, err := ud.Deploy(context.Background(), contracts.V1.WalletFactory.ABI, contracts.V1.WalletFactory.Bin, 0, nil, 1000000)
V1WalletFactoryAddress, err := ud.Deploy(context.Background(), contracts.V1.WalletFactory.ABI, contracts.V1.WalletFactory.Bin, 0, nil, 1000000)
assert.NoError(t, err)
assert.Equal(t, testSequenceContext.FactoryAddress, walletFactoryAddress)
assert.Equal(t, testV1SequenceContext.FactoryAddress, V1WalletFactoryAddress)

testV2SequenceContext := testutil.V2SequenceContext()

// Deploy sequence wallet-contract factory and ensure it equals the expected value in testutil
V2WalletFactoryAddress, err := ud.Deploy(context.Background(), contracts.V2.WalletFactory.ABI, contracts.V2.WalletFactory.Bin, 0, nil, 1000000)
assert.NoError(t, err)
// v2 is deployed through the eip2470 factory address
assert.NotEqual(t, testV2SequenceContext.FactoryAddress, V2WalletFactoryAddress)
assert.Equal(t, common.HexToAddress("0xAA4A319D9f7b43e197555d1c219F9195B80F0C8c"), V2WalletFactoryAddress)

testV3SequenceContext := testutil.V3SequenceContext()

// Deploy sequence wallet-contract factory and ensure it equals the expected value in testutil
V3WalletFactoryAddress, err := ud.Deploy(context.Background(), contracts.V3.WalletFactory.ABI, contracts.V3.WalletFactory.Bin, 0, nil, 1000000)
assert.NoError(t, err)
// v3 is deployed through the eip2470 factory address
assert.NotEqual(t, testV3SequenceContext.FactoryAddress, V3WalletFactoryAddress)
assert.Equal(t, common.HexToAddress("0x6843d600C5fF98E75DF4e7b236D9513eD54A5344"), V3WalletFactoryAddress)
}
59 changes: 34 additions & 25 deletions deployer/eip2740_deployer.go → deployer/eip2470_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
)

var (
EIP2740_EOA_DEPLOYER_ADDRESS = common.HexToAddress("0xBb6e024b9cFFACB947A71991E386681B1Cd1477D")
EIP2740_DEPLOYER_ADDRESS = common.HexToAddress("0xce0042B868300000d44A59004Da54A005ffdcf9f")
EIP2740_DEPLOYER_TX = "0xf9016c8085174876e8008303c4d88080b90154608060405234801561001057600080fd5b50610134806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634af63f0214602d575b600080fd5b60cf60048036036040811015604157600080fd5b810190602081018135640100000000811115605b57600080fd5b820183602082011115606c57600080fd5b80359060200191846001830284011164010000000083111715608d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550509135925060eb915050565b604080516001600160a01b039092168252519081900360200190f35b6000818351602085016000f5939250505056fea26469706673582212206b44f8a82cb6b156bfcc3dc6aadd6df4eefd204bc928a4397fd15dacf6d5320564736f6c634300060200331b83247000822470"
EIP2740_DEPLOYER_FUNDING = big.NewInt(1).Mul(big.NewInt(300), big.NewInt(100_000_000_000_000))
EIP2470_EOA_DEPLOYER_ADDRESS = common.HexToAddress("0xBb6e024b9cFFACB947A71991E386681B1Cd1477D")
EIP2470_DEPLOYER_ADDRESS = common.HexToAddress("0xce0042B868300000d44A59004Da54A005ffdcf9f")
EIP2470_DEPLOYER_TX = "0xf9016c8085174876e8008303c4d88080b90154608060405234801561001057600080fd5b50610134806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80634af63f0214602d575b600080fd5b60cf60048036036040811015604157600080fd5b810190602081018135640100000000811115605b57600080fd5b820183602082011115606c57600080fd5b80359060200191846001830284011164010000000083111715608d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550509135925060eb915050565b604080516001600160a01b039092168252519081900360200190f35b6000818351602085016000f5939250505056fea26469706673582212206b44f8a82cb6b156bfcc3dc6aadd6df4eefd204bc928a4397fd15dacf6d5320564736f6c634300060200331b83247000822470"
EIP2470_DEPLOYER_FUNDING = big.NewInt(1).Mul(big.NewInt(300), big.NewInt(100_000_000_000_000))

EIP2740_DEPLOYER_ABI = ethcontract.MustParseABI(`[
EIP2470_DEPLOYER_ABI = ethcontract.MustParseABI(`[
{
"constant": false,
"inputs": [
Expand Down Expand Up @@ -52,13 +52,13 @@ var (
]`)
)

type EIP2740Deployer struct {
type EIP2470Deployer struct {
Wallet *ethwallet.Wallet
provider *ethrpc.Provider
}

func NewEIP2740Deployer(wallet *ethwallet.Wallet) (*EIP2740Deployer, error) {
e := &EIP2740Deployer{
func NewEIP2470Deployer(wallet *ethwallet.Wallet) (*EIP2470Deployer, error) {
e := &EIP2470Deployer{
Wallet: wallet,
}

Expand All @@ -70,21 +70,24 @@ func NewEIP2740Deployer(wallet *ethwallet.Wallet) (*EIP2740Deployer, error) {
return e, nil
}

func (e *EIP2740Deployer) Deploy(ctx context.Context, contractABI abi.ABI, contractBin []byte, contractInstance uint, txParams interface{}, gasLimit uint, contractArgs ...interface{}) (common.Address, error) {
func (e *EIP2470Deployer) Deploy(ctx context.Context, contractABI abi.ABI, contractBin []byte, contractInstance uint, txParams interface{}, gasLimit uint, contractArgs ...interface{}) (common.Address, error) {
// Deploy universal deployer 2 if not yet deployed
code, err := e.provider.CodeAt(ctx, EIP2740_DEPLOYER_ADDRESS, nil)
code, err := e.provider.CodeAt(ctx, EIP2470_DEPLOYER_ADDRESS, nil)
if err != nil {
return common.Address{}, fmt.Errorf("deployer: %w", err)
}
if len(code) == 0 {
err = e.deployEIP2740Deployer(ctx, txParams)
err = e.deployEIP2470Deployer(ctx, txParams)
if err != nil {
return common.Address{}, err
}

code, err = e.provider.CodeAt(ctx, UNIVERSAL_DEPLOYER_2_ADDRESS, nil)
if len(code) == 0 {
return common.Address{}, fmt.Errorf("can't deploy eip2740 deployer")
return common.Address{}, fmt.Errorf("can't deploy EIP2470 deployer")
}
if err != nil {
return common.Address{}, fmt.Errorf("deployer: deploy codeAt: %w", err)
}
}

Expand All @@ -105,19 +108,22 @@ func (e *EIP2740Deployer) Deploy(ctx context.Context, contractABI abi.ABI, contr
return contractAddress, nil
}

// Deploy the contract via EIP2740Deployer by calling the deploy contract method
// Deploy the contract via EIP2470Deployer by calling the deploy contract method
var salt [32]byte
big.NewInt(int64(contractInstance)).SetBytes(salt[:])
input, err := EIP2740_DEPLOYER_ABI.Pack("deploy", deployData, salt)
input, err := EIP2470_DEPLOYER_ABI.Pack("deploy", deployData, salt)
if err != nil {
return common.Address{}, fmt.Errorf("deployer: deploy pack: %w", err)
}

tx, err := e.Wallet.NewTransaction(ctx, &ethtxn.TransactionRequest{
To: &EIP2740_DEPLOYER_ADDRESS,
To: &EIP2470_DEPLOYER_ADDRESS,
Data: input,
GasLimit: uint64(gasLimit),
})
if err != nil {
return common.Address{}, fmt.Errorf("deployer: deploy new transaction: %w", err)
}

_, waitTx, err := e.Wallet.SendTransaction(ctx, tx)
if err != nil {
Expand All @@ -136,21 +142,24 @@ func (e *EIP2740Deployer) Deploy(ctx context.Context, contractABI abi.ABI, contr
if len(code) == 0 {
return common.Address{}, fmt.Errorf("can't deploy contract")
}
if err != nil {
return common.Address{}, fmt.Errorf("deployer: deploy contract codeAt: %w", err)
}

return contractAddress, nil
}

func (e *EIP2740Deployer) deployEIP2740Deployer(ctx context.Context, txParams interface{}) error {
deployerBalance, err := e.provider.BalanceAt(ctx, EIP2740_EOA_DEPLOYER_ADDRESS, nil)
func (e *EIP2470Deployer) deployEIP2470Deployer(ctx context.Context, _txParams interface{}) error {
deployerBalance, err := e.provider.BalanceAt(ctx, EIP2470_EOA_DEPLOYER_ADDRESS, nil)
if err != nil {
return fmt.Errorf("deployer (deployUniversalDeployer1): %w", err)
}

// ensure deployer's wallet balance has necessary funding
if deployerBalance.Cmp(EIP2740_DEPLOYER_FUNDING) < 0 {
if deployerBalance.Cmp(EIP2470_DEPLOYER_FUNDING) < 0 {
signedTxn, err := e.Wallet.NewTransaction(ctx, &ethtxn.TransactionRequest{
To: &EIP2740_EOA_DEPLOYER_ADDRESS,
ETHValue: EIP2740_DEPLOYER_FUNDING,
To: &EIP2470_EOA_DEPLOYER_ADDRESS,
ETHValue: EIP2470_DEPLOYER_FUNDING,
})
if err != nil {
return err
Expand All @@ -171,7 +180,7 @@ func (e *EIP2740Deployer) deployEIP2740Deployer(ctx context.Context, txParams in
// deploying universal deployer contract
provider := e.Wallet.GetProvider()

txHash, err := provider.SendRawTransaction(ctx, EIP2740_DEPLOYER_TX)
txHash, err := provider.SendRawTransaction(ctx, EIP2470_DEPLOYER_TX)
if err != nil {
return err
}
Expand All @@ -185,18 +194,18 @@ func (e *EIP2740Deployer) deployEIP2740Deployer(ctx context.Context, txParams in
}

// lets check the universal deployer address is now deployed, after txn above
universalDeployerCodeCheck, err := provider.CodeAt(ctx, EIP2740_DEPLOYER_ADDRESS, nil)
universalDeployerCodeCheck, err := provider.CodeAt(ctx, EIP2470_DEPLOYER_ADDRESS, nil)
if err != nil {
return fmt.Errorf("deployer: %w", err)
}
if universalDeployerCodeCheck == nil || len(universalDeployerCodeCheck) == 0 {
if universalDeployerCodeCheck == nil {
return fmt.Errorf("deployer (deployUniversalDeployer1): failed to deploy stage 1 deployer")
}

return nil
}

func (e *EIP2740Deployer) ComputeCreate2Address(contractABI abi.ABI, contractBin []byte, contractInstance uint, contractArgs ...interface{}) (common.Address, []byte, error) {
func (e *EIP2470Deployer) ComputeCreate2Address(contractABI abi.ABI, contractBin []byte, contractInstance uint, contractArgs ...interface{}) (common.Address, []byte, error) {
var input []byte
var err error

Expand All @@ -219,7 +228,7 @@ func (e *EIP2740Deployer) ComputeCreate2Address(contractABI abi.ABI, contractBin

hashPack, err := ethcoder.SolidityPack(
[]string{"bytes1", "address", "bytes32", "bytes32"},
[]interface{}{[1]byte{255}, EIP2740_DEPLOYER_ADDRESS, salt, codeHash},
[]interface{}{[1]byte{255}, EIP2470_DEPLOYER_ADDRESS, salt, codeHash},
)
if err != nil {
return common.Address{}, nil, fmt.Errorf("ComputeCreate2Address hashPack: %w", err)
Expand Down
42 changes: 42 additions & 0 deletions deployer/eip2470_deployer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package deployer_test

import (
"context"
"testing"

"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/go-sequence/contracts"
"github.com/0xsequence/go-sequence/deployer"
"github.com/0xsequence/go-sequence/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEIP2470Deployer(t *testing.T) {
assert.Equal(t, testChain.ChainID().Uint64(), uint64(1337))

testWallet := testChain.MustWallet(5)

eip2470Deployer, err := deployer.NewEIP2470Deployer(testWallet)
require.NoError(t, err)

// Deploy sequence wallet-contract factory and ensure it equals the expected value in testutil
V1WalletFactoryAddress, err := eip2470Deployer.Deploy(context.Background(), contracts.V1.WalletFactory.ABI, contracts.V1.WalletFactory.Bin, 0, nil, 1000000)
assert.NoError(t, err)
// NOTICE: v1 is deployed w/ the universal deployer, not the EIP2470 deployer
assert.Equal(t, common.HexToAddress("0xf87Cb4C012256149569615642dE44Cf2373efcC9"), V1WalletFactoryAddress)

testV2SequenceContext := testutil.V2SequenceContext()

// Deploy sequence wallet-contract factory and ensure it equals the expected value in testutil
V2WalletFactoryAddress, err := eip2470Deployer.Deploy(context.Background(), contracts.V2.WalletFactory.ABI, contracts.V2.WalletFactory.Bin, 0, nil, 1000000)
assert.NoError(t, err)
assert.Equal(t, testV2SequenceContext.FactoryAddress, V2WalletFactoryAddress)

testV3SequenceContext := testutil.V3SequenceContext()

// Deploy sequence wallet-contract factory and ensure it equals the expected value in testutil
V3WalletFactoryAddress, err := eip2470Deployer.Deploy(context.Background(), contracts.V3.WalletFactory.ABI, contracts.V3.WalletFactory.Bin, 0, nil, 1000000)
assert.NoError(t, err)
assert.Equal(t, testV3SequenceContext.FactoryAddress, V3WalletFactoryAddress)
}
30 changes: 0 additions & 30 deletions deployer/eip2740_deployer_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/goware/cachestore v0.9.0
github.com/goware/logger v0.3.0
github.com/shopspring/decimal v1.4.0
github.com/spf13/cobra v1.9.1
github.com/stretchr/testify v1.9.0
)

Expand Down Expand Up @@ -46,7 +47,6 @@ require (
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
github.com/spf13/cobra v1.9.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/supranational/blst v0.3.13 // indirect
Expand Down
9 changes: 5 additions & 4 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ var sequenceContextV2 = WalletContext{
}

var sequenceContextV3 = WalletContext{
FactoryAddress: common.HexToAddress("0x6843d600C5fF98E75DF4e7b236D9513eD54A5344"),
MainModuleAddress: common.HexToAddress("0x429937a2aCB7789c5D29Cd3325cC77f02E21539d"),
MainModuleUpgradableAddress: common.HexToAddress("0x429937a2aCB7789c5D29Cd3325cC77f02E21539d"),
GuestModuleAddress: common.HexToAddress("0xC79f7f217D753222EFb5c6CA698A1823c3F15EA5"),
FactoryAddress: common.HexToAddress("0x8dE6A16e1E1A548Af7B19eB87C4747e44b775817"),
MainModuleAddress: common.HexToAddress("0x53BF0CB6B6bFFea6b1F92D1BBf9bC15307020318"),
MainModuleUpgradableAddress: common.HexToAddress("0x64732C84cfb25ff44f952A45374021D353eC4551"),
GuestModuleAddress: common.HexToAddress("0x1396A621e5D64991c44216A3FE59ea7A4F39F341"),
UtilsAddress: common.HexToAddress("0x64732C84cfb25ff44f952A45374021D353eC4551"),
CreationCode: hexutil.Encode(contracts.V3.CreationCode),
}

Expand Down
2 changes: 1 addition & 1 deletion testutil/chain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start:geth": "docker run -p 8545:8545 --log-driver none --rm ethereum/client-go:v1.15.2 --dev --dev.period 2 --networkid ${npm_package_config_testchainChainID} --miner.gaslimit ${npm_package_config_testchainGasLimit} --miner.gasprice 1 --http --http.addr 0.0.0.0 --rpc.allow-unprotected-txs --verbosity 1",
"start:geth:verbose": "docker run -p 8545:8545 --rm ethereum/client-go:v1.15.2 --dev --dev.period 2 --networkid ${npm_package_config_testchainChainID} --miner.gaslimit ${npm_package_config_testchainGasLimit} --miner.gasprice 1 --http --http.addr 0.0.0.0 --rpc.allow-unprotected-txs",
"start:anvil": "anvil --mnemonic \"${npm_package_config_mnemonic}\" --block-time 1 --balance ${npm_package_config_etherBalance} --host 0.0.0.0 --chain-id ${npm_package_config_testchainChainID} --gas-limit ${npm_package_config_testchainGasLimit} --gas-price ${npm_package_config_testchainGasPrice}",
"start:anvil:verbose": "anvil --mnemonic \"${npm_package_config_mnemonic}\" --block-time 1 --balance ${npm_package_config_etherBalance} --host 0.0.0.0 --chain-id ${npm_package_config_testchainChainID} --gas-limit ${npm_package_config_testchainGasLimit} --gas-price ${npm_package_config_testchainGasPrice} --verbose",
"start:anvil:verbose": "anvil --mnemonic \"${npm_package_config_mnemonic}\" --block-time 1 --balance ${npm_package_config_etherBalance} --host 0.0.0.0 --chain-id ${npm_package_config_testchainChainID} --gas-limit ${npm_package_config_testchainGasLimit} --gas-price ${npm_package_config_testchainGasPrice} -v",
"wait:server": "wait-on -t 120000 tcp:127.0.0.1:8545"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ var sequenceContextV2 = sequence.WalletContext{
}

var sequenceContextV3 = sequence.WalletContext{
FactoryAddress: common.HexToAddress("0x6843d600C5fF98E75DF4e7b236D9513eD54A5344"),
MainModuleAddress: common.HexToAddress("0x429937a2aCB7789c5D29Cd3325cC77f02E21539d"),
MainModuleUpgradableAddress: common.HexToAddress("0x429937a2aCB7789c5D29Cd3325cC77f02E21539d"),
GuestModuleAddress: common.HexToAddress("0xC79f7f217D753222EFb5c6CA698A1823c3F15EA5"),
UtilsAddress: common.HexToAddress("0x429937a2aCB7789c5D29Cd3325cC77f02E21539d"),
FactoryAddress: common.HexToAddress("0x8dE6A16e1E1A548Af7B19eB87C4747e44b775817"),
MainModuleAddress: common.HexToAddress("0x53BF0CB6B6bFFea6b1F92D1BBf9bC15307020318"),
MainModuleUpgradableAddress: common.HexToAddress("0x64732C84cfb25ff44f952A45374021D353eC4551"),
GuestModuleAddress: common.HexToAddress("0x1396A621e5D64991c44216A3FE59ea7A4F39F341"),
UtilsAddress: common.HexToAddress("0x53BF0CB6B6bFFea6b1F92D1BBf9bC15307020318"),
CreationCode: hexutil.Encode(contracts.V3.CreationCode),
}

Expand Down
Loading

0 comments on commit 7fa3885

Please sign in to comment.