Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: dualVMToken uniswap test #1415

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions solidity_contracts/src/CairoPrecompiles/DualVmToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ contract DualVmToken {
starknetToken = _starknetToken;
}

// /*//////////////////////////////////////////////////////////////
// ERC20 LOGIC
// //////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////
ERC20 LOGIC
//////////////////////////////////////////////////////////////*/

function approve(address spender, uint256 amount) external returns (bool) {
uint256[] memory spenderAddressCalldata = new uint256[](1);
Expand Down Expand Up @@ -139,6 +139,8 @@ contract DualVmToken {
transferCallData[2] = uint256(amountHigh);

starknetToken.delegatecallCairo("transfer", transferCallData);

emit Transfer(msg.sender, to, amount);
return true;
}

Expand All @@ -165,7 +167,6 @@ contract DualVmToken {
starknetToken.delegatecallCairo("transfer_from", transferFromCallData);

emit Transfer(from, to, amount);

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ contract SubContextPrecompile {

function exploitLowLevelCall() public {
(bool success,) = address(revertingSubContext).call(abi.encodeWithSignature("reverting()"));
require(success == false);
}

function exploitChildContext() public {
Expand Down
96 changes: 91 additions & 5 deletions tests/end_to_end/CairoPrecompiles/test_dual_vm_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ async def test_should_transfer(
amount = 1
balance_owner_before = await dual_vm_token.balanceOf(owner.address)
balance_other_before = await dual_vm_token.balanceOf(other.address)
await dual_vm_token.transfer(other.address, amount)
receipt = (await dual_vm_token.transfer(other.address, amount))["receipt"]
events = dual_vm_token.events.parse_events(receipt)
assert events["Transfer"] == [
{
"from": str(owner.address),
"to": str(other.address),
"amount": amount,
}
]
balance_owner_after = await dual_vm_token.balanceOf(owner.address)
balance_other_after = await dual_vm_token.balanceOf(other.address)

Expand All @@ -95,7 +103,15 @@ async def test_should_approve(
allowance_before = await dual_vm_token.allowance(
owner.address, other.address
)
await dual_vm_token.approve(other.address, amount)
receipt = (await dual_vm_token.approve(other.address, amount))["receipt"]
events = dual_vm_token.events.parse_events(receipt)
assert events["Approval"] == [
{
"owner": str(owner.address),
"spender": str(other.address),
"amount": amount,
}
]
allowance_after = await dual_vm_token.allowance(
owner.address, other.address
)
Expand All @@ -108,9 +124,22 @@ async def test_should_transfer_from(
balance_owner_before = await dual_vm_token.balanceOf(owner.address)
balance_other_before = await dual_vm_token.balanceOf(other.address)
await dual_vm_token.approve(other.address, amount)
await dual_vm_token.transferFrom(
owner.address, other.address, amount, caller_eoa=other.starknet_contract
)
receipt = (
await dual_vm_token.transferFrom(
owner.address,
other.address,
amount,
caller_eoa=other.starknet_contract,
)
)["receipt"]
events = dual_vm_token.events.parse_events(receipt)
assert events["Transfer"] == [
{
"from": str(owner.address),
"to": str(other.address),
"amount": amount,
}
]
balance_owner_after = await dual_vm_token.balanceOf(owner.address)
balance_other_after = await dual_vm_token.balanceOf(other.address)

Expand All @@ -126,3 +155,60 @@ async def test_should_revert_tx_cairo_precompiles(
await dual_vm_token.transfer(
other.address, 1, gas_limit=45_000
) # fails with out of gas

class TestIntegrationUniswap:
async def test_should_add_liquidity_and_swap(
starknet_token, dual_vm_token, token_a, router, owner, other
):
amount_a_desired = 1000 * await token_a.decimals()
amount_dual_vm_token_desired = 500 * await dual_vm_token.decimals()

await token_a.mint(owner.address, amount_a_desired)
await token_a.approve(
router.address, amount_a_desired * 2, caller_eoa=owner.starknet_contract
)
await dual_vm_token.approve(
router.address,
amount_dual_vm_token_desired * 2,
caller_eoa=owner.starknet_contract,
)

deadline = 99999999999
to_address = owner.address
success = (
await router.addLiquidity(
token_a.address,
dual_vm_token.address,
amount_a_desired,
amount_dual_vm_token_desired,
0,
0,
to_address,
deadline,
caller_eoa=owner.starknet_contract,
)
)["success"]

assert success == 1

amount_dual_vm_token_desired = 5 * await dual_vm_token.decimals()

balance_other_before = await dual_vm_token.balanceOf(other.address)
amount_in_max = 2**128
success = (
await router.swapTokensForExactTokens(
amount_dual_vm_token_desired,
amount_in_max,
[token_a.address, dual_vm_token.address],
other.address,
deadline,
caller_eoa=owner.starknet_contract,
)
)["success"]
assert success == 1

balance_other_after = await dual_vm_token.balanceOf(other.address)
assert (
balance_other_before + amount_dual_vm_token_desired
== balance_other_after
)
45 changes: 0 additions & 45 deletions tests/end_to_end/UniswapV2/conftest.py

This file was deleted.

33 changes: 33 additions & 0 deletions tests/end_to_end/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from starknet_py.net.account.account import Account

from kakarot_scripts.constants import NETWORK, RPC_CLIENT, NetworkType
from kakarot_scripts.utils.kakarot import deploy as deploy_kakarot
from kakarot_scripts.utils.kakarot import eth_balance_of
from kakarot_scripts.utils.kakarot import get_contract as get_solidity_contract
from kakarot_scripts.utils.kakarot import get_eoa
Expand Down Expand Up @@ -195,3 +196,35 @@ def relayers(worker_id):
except ValueError:
logger.info(f"Error while setting relayer index to {worker_id}")
return


# Uniswap fixtures
obatirou marked this conversation as resolved.
Show resolved Hide resolved

TOTAL_SUPPLY = 10000 * 10**18


@pytest_asyncio.fixture(scope="function")
obatirou marked this conversation as resolved.
Show resolved Hide resolved
async def token_a(owner):
return await deploy_kakarot(
"UniswapV2",
"ERC20",
TOTAL_SUPPLY,
caller_eoa=owner.starknet_contract,
)


@pytest_asyncio.fixture(scope="module")
async def weth(owner):
return await deploy_kakarot("WETH", "WETH9")


@pytest_asyncio.fixture(scope="module")
async def factory(owner):
return await deploy_kakarot("UniswapV2", "UniswapV2Factory", owner.address)


@pytest_asyncio.fixture(scope="module")
async def router(owner, factory, weth):
return await deploy_kakarot(
"UniswapV2Router", "UniswapV2Router02", factory.address, weth.address
)
Loading