Skip to content

Commit

Permalink
First module swap with Gnosis Safe succeed
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Dec 31, 2024
1 parent 56943ef commit 6f15450
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
10 changes: 8 additions & 2 deletions contracts/safe-integration/src/TradingStrategyModuleV0.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import "@guard/GuardV0Base.sol";
*
* This is initial, MVP, version.
*
* Notes
* - See VelvetSafeModule as an example https://github.com/Velvet-Capital/velvet-core/blob/9d487937d0569c12e85b436a1c6f3e68a1dc8c44/contracts/vault/VelvetSafeModule.sol#L16
*
*/
contract TradingStrategyModuleV0 is Module, GuardV0Base {

Expand Down Expand Up @@ -66,17 +69,20 @@ contract TradingStrategyModuleV0 is Module, GuardV0Base {
*/
function performCall(address target, bytes calldata callData) external {

bool success;
bytes memory response;

// Check that the asset manager can perform this function.
// Will revert() on error
_validateCallInternal(msg.sender, target, callData);

// Inherit from Module contract,
// execute a tx on behalf of Gnosis
bool success = exec(
(success, response) = execAndReturnData(
target,
0,
callData,
Enum.Operation.DelegateCall
Enum.Operation.Call
);

if (!success) {
Expand Down
2 changes: 1 addition & 1 deletion eth_defi/abi/safe-integration/TradingStrategyModuleV0.json

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions tests/safe-integration/test_guard_safe_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def web3(anvil_base_fork) -> Web3:
- By default use Anvil forked Base
- Eanble Tenderly testnet with `JSON_RPC_TENDERLY` to debug
- Optionally enable Tenderly testnet with `JSON_RPC_TENDERLY` to debug
otherwise impossible to debug Gnosis Safe transactions
"""

Expand Down Expand Up @@ -177,20 +177,12 @@ def uniswap_v2_whitelisted_trading_strategy_module(

owner = deployer

guard = deploy_contract(
web3,
"safe-integration/TradingStrategyModuleV0.json",
deployer,
deployer,
safe.address,
)

# Deploy guard module
module = deploy_contract(
web3,
"safe-integration/TradingStrategyModuleV0.json",
deployer,
safe.address,
owner,
safe.address,
)

Expand All @@ -207,26 +199,26 @@ def uniswap_v2_whitelisted_trading_strategy_module(
assert_transaction_success_with_explanation(web3, tx_hash)

# Enable asset_manager as the whitelisted trade-executor
tx_hash = guard.functions.allowSender(asset_manager, "Whitelist trade-executor").transact({"from": owner})
tx_hash = module.functions.allowSender(asset_manager, "Whitelist trade-executor").transact({"from": owner})
assert_transaction_success_with_explanation(web3, tx_hash)

# Enable safe as the receiver of tokens
tx_hash = guard.functions.allowReceiver(safe.address, "Whitelist Safe as trade receiver").transact({"from": owner})
tx_hash = module.functions.allowReceiver(safe.address, "Whitelist Safe as trade receiver").transact({"from": owner})
assert_transaction_success_with_explanation(web3, tx_hash)

# Whitelist tokens
guard.functions.whitelistToken(base_usdc.address, "Allow USDC").transact({"from": owner})
guard.functions.whitelistToken(base_weth.address, "Allow WETH").transact({"from": owner})
module.functions.whitelistToken(base_usdc.address, "Allow USDC").transact({"from": owner})
module.functions.whitelistToken(base_weth.address, "Allow WETH").transact({"from": owner})

# Whitelist Uniswap v2
tx_hash = guard.functions.whitelistUniswapV2Router(uniswap_v2.router.address, "Allow Uniswap v2").transact({"from": owner})
tx_hash = module.functions.whitelistUniswapV2Router(uniswap_v2.router.address, "Allow Uniswap v2").transact({"from": owner})
assert_transaction_success_with_explanation(web3, tx_hash)

# Relinquish ownership
tx_hash = guard.functions.transferOwnership(safe.address).transact({"from": owner})
tx_hash = module.functions.transferOwnership(safe.address).transact({"from": owner})
assert_transaction_success_with_explanation(web3, tx_hash)

return guard
return module


def test_enable_safe_module(
Expand Down Expand Up @@ -278,6 +270,8 @@ def test_swap_through_module(
"""Perform Uniswap v2 swap using TradingStrategyModuleV0."""

ts_module = uniswap_v2_whitelisted_trading_strategy_module
assert safe.retrieve_modules() == [ts_module.address]

usdc = base_usdc.contract
weth = base_weth.contract
usdc_amount = 10_000 * 10**6
Expand All @@ -294,6 +288,8 @@ def test_swap_through_module(
tx_hash = ts_module.functions.performCall(target, call_data).transact({"from": asset_manager})
assert_transaction_success_with_explanation(web3, tx_hash)

assert weth.functions.balanceOf(safe.address).call() == 0

trade_call = uniswap_v2.router.functions.swapExactTokensForTokens(
usdc_amount,
0,
Expand All @@ -305,5 +301,5 @@ def test_swap_through_module(
tx_hash = ts_module.functions.performCall(target, call_data).transact({"from": asset_manager})
assert_transaction_success_with_explanation(web3, tx_hash)

assert weth.functions.balanceOf(safe.address).call() == 3696700037078235076
assert weth.functions.balanceOf(safe.address).call() > 0

0 comments on commit 6f15450

Please sign in to comment.