Ta có thể viết script (bằng Solidity) để deploy smart contract. Theo convention, tên script sẽ là SimpleStorage.s.sol và nó được đặt trong thư mục scripts.

Để viết script, ta sẽ cần import Script từ "forge-std/Script.sol" và cho contract trong script kế thừa Script.

Info

forge-std được gọi là Forge Standard Library, là một tập các contract được viết sẵn của Foundry nhằm đơn giản hóa quá trình scripting và testing.

// SPDX-License-Identifier: MIT
 
pragma solidity 0.8.19;
 
import {Script} from "forge-std/Script.sol";
import {SimpleStorage} from "../src/SimpleStorage.sol";
 
contract DeploySimpleStorage is Script {
    function run() external returns (SimpleStorage) {
        vm.startBroadcast();
 
        SimpleStorage simpleStorage = new SimpleStorage();
 
        vm.stopBroadcast();
        return simpleStorage;
    }
 
}

Có thể thấy, ta import contract trong thư mục src vào để thực hiện deploy.

Từ khóa vm được gọi là cheat code của Foundry và nó chỉ được dùng trong môi trường của Foundry.

  • vm.startBroadcast: điểm khởi tạo danh sách các transaction sẽ được gửi đến RPC URL.
  • vm.stopBroadcast: điểm kết thúc danh sách các transaction sẽ được gửi đến RPC URL.

Hàm run trong từng script contract sẽ là entry point của script đó.

Deploy

Chạy script để deploy:

forge script script/DeploySimpleStorage.s.sol

Output:

[⠆] Compiling...
[⠔] Compiling 2 files with 0.8.19
[⠒] Solc 0.8.19 finished in 1.08s
Compiler run successful!
Script ran successfully.
Gas used: 338569
 
== Return ==
0: contract SimpleStorage 0x90193C961A926261B756D1E5bb255e67ff9498A1
 
If you wish to simulate on-chain transactions pass a RPC URL.

Nếu không pass RPC URL, Foundry sẽ chạy một Anvil instance, deploy rồi terminate instance này.

RPC URL

Nếu ta truyền vào RPC URL, Foundry sẽ giả lập việc deploy:

forge script script/DeploySimpleStorage.s.sol --rpc-url http://127.0.0.1:8545

Output:

[⠃] Compiling...
No files changed, compilation skipped
Script ran successfully.
 
== Return ==
0: contract SimpleStorage 0x5b73C5498c1E3b4dbA84de0F1833c4a029d90519
 
## Setting up 1 EVM.
 
==========================
 
Chain 31337
 
Estimated gas price: 2.000000001 gwei
 
Estimated total gas used for script: 731498
 
Estimated amount required: 0.001462996000731498 ETH
 
==========================
 
SIMULATION COMPLETE. To broadcast these transactions, add --broadcast and wallet configuration(s) to the previous command. See forge script --help for more.
 
Transactions saved to: D:/updraft/foundry-101\broadcast\DeploySimpleStorage.s.sol\31337\dry-run\run-latest.json
 
Sensitive values saved to: D:/updraft/foundry-101/cache\DeploySimpleStorage.s.sol\31337\dry-run\run-latest.json

Câu lệnh trên sẽ tạo ra thư mục broadcast chứa các transaction đã chạy. Trong thư mục này còn có thư mục dry-run chứa các transaction được chạy mà không được chỉ định RPC URL.

Broadcast

Thêm vào hai flag sau để deploy lên local chain:

--broadcast --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

Resources