本文最后更新于 2024-04-22,本文发布时间距今超过 90 天, 文章内容可能已经过时。最新内容请以官方内容为准

Foundry Base Usage

  1. Create a .env (only for Test Account)
  2. Input the private key of test account and RPC_URL
    PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
    RPC_URL=http://127.0.0.1:8545
    
  3. Set the env variables by cmd source ./.env
  4. Use the pre-setting variables by cmd $RPC_URL
  5. Deploy the contracts and get the deployed address by cmd
    1. Deploy the contract without --broadcast will lead to dry-run
    forge script [scriptPath] --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast
    
  6. Call the funtions of contract
    # Call the store function of contract and set the input param as 77
     cast send [contractAddress] "store(uint256)" 77 --rpc-url $RPC_URL --private-key $PRIVATE_KEY
    
    # Call the retrieve function of contract and get the output value
    # Then pass it though pipe to cast to convert it to decimal
    cast call 0x5FbDB2315678afecb367f032d93F642f64180aa3 "retrieve()" | cast --to-dec
    
  7. Use the --interactive command to interact with the contract
    1. If you don’t want to use the explicit --rpc-url and --private-key command, you can use the --interactive command to interact with the contract