Skip to content

Commit

Permalink
fix: DualVMToken totalSupply (#1569)
Browse files Browse the repository at this point in the history
Fixes an issue where `totalSupply` was skipping the high-part of the
u256 reported in
code-423n4/2024-09-kakarot-findings#15

- Correctly deserialize return value
- Update test to launch a token with populated (low, high) parts of the
u256 struct

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1569)
<!-- Reviewable:end -->

Co-authored-by: Clément Walter <clement0walter@gmail.com>
  • Loading branch information
enitrat and ClementWalter authored Nov 4, 2024
1 parent f14aa8e commit dc11293
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion solidity_contracts/src/CairoPrecompiles/DualVmToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ contract DualVmToken {

function totalSupply() external view returns (uint256) {
bytes memory returnData = starknetToken.staticcallCairo("total_supply");
return abi.decode(returnData, (uint256));
(uint128 valueLow, uint128 valueHigh) = abi.decode(returnData, (uint128, uint128));
return uint256(valueLow) + (uint256(valueHigh) << 128);
}

/// @dev This function is used to get the balance of an evm account
Expand Down
2 changes: 1 addition & 1 deletion tests/end_to_end/CairoPrecompiles/test_dual_vm_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@pytest_asyncio.fixture(scope="function")
async def starknet_token(owner):
address = await deploy_starknet(
"StarknetToken", int(1e18), owner.starknet_contract.address
"StarknetToken", int(2**256 - 1), owner.starknet_contract.address
)
return get_contract_starknet("StarknetToken", address=address)

Expand Down

0 comments on commit dc11293

Please sign in to comment.