Skip to content

The difference between msg.sender in the constructor and in a function. #1364

@Subway2023

Description

@Subway2023

zksolc version: v1.5.10
description: solc + EVM will not cause the transaction to revert, but zksolc +zkevm will. The key point is that zksolc considers t not equal to msg.sender.

contract C {
    address t;
    constructor() {
        t = msg.sender;
    }
    function inv() public view {
        assert(t == msg.sender);
    }
}

Here is the program where I execute the contract.

import { Bytes, Contract, ContractAbi, Web3 } from "web3";
import {
  ContractFactory,
  types,
  Web3ZKsyncL2,
  ZKsyncPlugin,
  ZKsyncWallet,
} from "web3-plugin-zksync";


async function callEth(targetUrl: string, toAddress: string, callData) {
  const url = targetUrl;
  const data = {
    jsonrpc: "2.0",
    id: 1,
    method: "eth_call",
    params: [
      {
        to: toAddress,
        data: callData
      },
      "latest"
    ]
  };

  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  });

  const responseBody = await response.json();
  return responseBody.result;
}

async function main() {
  const web3: Web3 = new Web3(/* optional L1 provider */);
  const targetUrl="HTTP://127.0.0.1:8011"
  web3.registerPlugin(new ZKsyncPlugin(targetUrl));
  const zksync: ZKsyncPlugin = web3.ZKsync;

  const PRIVATE_KEY: string = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
  const wallet: ZKsyncWallet = new zksync.Wallet(PRIVATE_KEY);

  const contractByteCodeString: string = process.argv[2];
  const contractAbiString: string = process.argv[3];
  let callData: string = process.argv[4];
  if (!callData.startsWith('0x')) {
    callData = '0x' + callData;
  }

  const contractAbi: ContractAbi = JSON.parse(contractAbiString);
  const contractByteCode: Bytes = web3.utils.hexToBytes(contractByteCodeString);

  const contractFactory: ContractFactory<ContractAbi> = new ContractFactory(
    contractAbi,
    contractByteCode,
    wallet,
  );

  const contract: Contract<ContractAbi> = await contractFactory.deploy();

  const rawReturnValue = await callEth(targetUrl,contract.options.address,callData);
  console.log('returnValue:', rawReturnValue);
}

main()
  .then(() => console.log("✅ Script executed successfully"))
  .catch((error) => console.error(`❌ Error executing script: ${error}`));

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions