Skip to content

Commit

Permalink
[Hebao_1.1] Optimize wallet factory to create uninitialized wallets w…
Browse files Browse the repository at this point in the history
…hen gas price is low (#1633)

* create wallet and adobe

* create wallet and adobe

* create wallet and adobe

* create wallet and adobe

* create wallet and adobe

* create wallet and adobe

* more

* more

* more

* more

* more

* more

* more

* more

* more

* more

* more

* update

* Optimize wallet factory idea2 (#1639)

* [Hebao1.1] check reimburse GasFee (#1638)

* [Hebao1.1] check reimburse GasFee

* Update ForwarderModule.sol

* Update ForwarderModule.sol

Co-authored-by: Daniel Wang <[email protected]>

* [Protocol3.6] Add tokenId in DepositRequested event (#1636)

* improvement

* improvement

* improvement

Co-authored-by: kongliangzhong <[email protected]>
Co-authored-by: wangdong <[email protected]>

* improvement

* improvement

* improvement

* compatible (#1640)

* compatible

* compatible

Co-authored-by: wangdong <[email protected]>

* compatible

* compatible

* compatible

* compatible

* compatible

* more

* bug fixes

* more

* clean

* more

* more

* more

* more

* more

* more

* more

* more

* more

* more

* add testcases for WalletFactory.createWallet2

* update

* sign owner in ENS approval (#1651)

* sign owner in ENS approval

* fix test

* fix test

Co-authored-by: kongliangzhong <[email protected]>

Co-authored-by: wangdong <[email protected]>
Co-authored-by: kongliangzhong <[email protected]>
  • Loading branch information
3 people authored Aug 24, 2020
1 parent daba154 commit ba41c2c
Show file tree
Hide file tree
Showing 10 changed files with 603 additions and 242 deletions.
43 changes: 36 additions & 7 deletions packages/hebao_v1/contracts/base/BaseWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,68 @@ abstract contract BaseWallet is ReentrancyGuard, Wallet
_;
}

modifier onlyFromFactory
{
require(
msg.sender == controller.walletFactory(),
"UNAUTHORIZED"
);
_;
}

/// @dev We need to make sure the Factory address cannot be changed without wallet owner's
/// explicit authorization.
modifier onlyFromFactoryOrModule
{
require(
msg.sender == controller.walletFactory() || modules[msg.sender],
modules[msg.sender] || msg.sender == controller.walletFactory(),
"UNAUTHORIZED"
);
_;
}

/// @dev Set up this wallet by assigning an original owner and controller.
/// @dev Set up this wallet by assigning an original owner
///
/// Note that calling this method more than once will throw.
///
/// @param _controller The Controller instance.
/// @param _initialOwner The owner of this wallet, must not be address(0).
function setup(
address _controller,
function initOwner(
address _initialOwner
)
external
onlyFromFactory
nonReentrant
{
require(controller != Controller(0), "NO_CONTROLLER");
require(_owner == address(0), "INITIALIZED_ALREADY");
require(_initialOwner != address(0), "ZERO_ADDRESS");

controller = Controller(_controller);
_owner = _initialOwner;

emit WalletSetup(_initialOwner);
}

/// @dev Set up this wallet by assigning an controller.
///
/// Note that calling this method more than once will throw.
/// And this method must be invoked before owner is initialized
///
/// @param _controller The Controller instance.
function initController(
Controller _controller
)
external
nonReentrant
{
require(
_owner == address(0) &&
controller == Controller(0) &&
_controller != Controller(0),
"CONTROLLER_INIT_FAILED"
);

controller = _controller;
}

function owner()
override
external
Expand Down
41 changes: 31 additions & 10 deletions packages/hebao_v1/contracts/modules/core/ForwarderModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,7 @@ abstract contract ForwarderModule is BaseModule
)
{
uint gasLeft = gasleft();
uint gasLimit = (metaTx.gasLimit.mul(64) / 63).add(GAS_OVERHEAD);
require(gasLeft >= gasLimit, "OPERATOR_INSUFFICIENT_GAS");

uint gasCost = gasLimit.mul(metaTx.gasPrice);
require(
ERC20(metaTx.gasToken).balanceOf(metaTx.from) >= gasCost,
"WALLET_INSUFFICIENT_GAS"
);
checkSufficientGas(metaTx);

// The trick is to append the really logical message sender and the
// transaction-aware hash to the end of the call data.
Expand Down Expand Up @@ -172,8 +165,9 @@ abstract contract ForwarderModule is BaseModule
// private key is leaked, the hacker won't be able to deplete ether/tokens
// as high meta-tx fees.
bool skipQuota = success && (
metaTx.txAwareHash != 0 ||
metaTx.data.toBytes4(0) == WalletFactory.createWallet.selector &&
metaTx.txAwareHash != 0 || (
metaTx.data.toBytes4(0) == WalletFactory.createWallet.selector ||
metaTx.data.toBytes4(0) == WalletFactory.createWallet2.selector) &&
metaTx.to == controller().walletFactory()
);

Expand All @@ -186,6 +180,33 @@ abstract contract ForwarderModule is BaseModule
skipQuota
);
}
}

function checkSufficientGas(
MetaTx calldata metaTx
)
private
view
{
// Check the relayer has enough Ether gas
uint gasLimit = (metaTx.gasLimit.mul(64) / 63).add(GAS_OVERHEAD);
require(gasleft() >= gasLimit, "OPERATOR_INSUFFICIENT_GAS");

// Check the wallet has enough meta tx gas
if (metaTx.gasPrice == 0) return;

uint gasCost = gasLimit.mul(metaTx.gasPrice);

if (metaTx.gasToken == address(0)) {
require(
metaTx.from.balance >= gasCost,
"WALLET_INSUFFICIENT_ETH_GAS"
);
} else {
require(
ERC20(metaTx.gasToken).balanceOf(metaTx.from) >= gasCost,
"WALLET_INSUFFICIENT_TOKEN_GAS"
);
}
}
}
Loading

0 comments on commit ba41c2c

Please sign in to comment.