kkrt-labs / kakarot

Kakarot is a zkEVM written in Cairo, leveraging the STARK proof system.
https://kakarot.org
MIT License
960 stars 276 forks source link

doc: beware of the usage of msg.value and delegatecallCairo #1363

Open ClementWalter opened 2 weeks ago

ClementWalter commented 2 weeks ago

Feature Request

Describe the Feature Request

The following snippet surprised me first

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import {CairoLib} from "kakarot-lib/CairoLib.sol";

using CairoLib for uint256;

contract EthStarknetBridge {

    /// @dev The cairo contract to call
    uint256 constant starknetEth = 0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7;
    uint256 constant TRANSFER_SELECTOR = uint256(keccak256("transfer")) % 2 ** 250;

    /// @notice Sends ETH to a Starknet address
    /// @param toStarknetAddress The Starknet address to send ETH to
    function transfer(uint256 toStarknetAddress) external payable {
        _transfer(toStarknetAddress, msg.value);
    }

    /// @notice Calls the Eth Cairo contract
    /// @param toStarknetAddress The Starknet address to send ETH to
    /// @param amount The amount of ETH to send
    function _transfer(uint256 toStarknetAddress, uint256 amount) private {
        // Split amount in [low, high]
        uint128 amountLow = uint128(amount);
        uint128 amountHigh = uint128(amount >> 128);

        uint256[] memory transferCallData = new uint256[](3);
        transferCallData[0] = toStarknetAddress;
        transferCallData[1] = uint256(amountLow);
        transferCallData[2] = uint256(amountHigh);

        starknetEth.delegatecallCairo(TRANSFER_SELECTOR, transferCallData);
    }
}

as the msg.value was sent 2 times. It's actually the intended behavior as:

This is however worth noticing in the doc

ShantelPeters commented 2 weeks ago

Please I will like to work on this

onlydustapp[bot] commented 2 weeks ago

Hey @ShantelPeters! Thanks for showing interest. We've created an application for you to contribute to Kakarot zkEVM. Go check it out on OnlyDust!