UNDER THE FLOW
先看一下达成条件
pragma solidity ^0.7.0;
import {ImprovedERC20} from "../src/ImprovedERC20.sol";
import {console} from "forge-std/console.sol";
import {Script} from "forge-std/Script.sol";
contract IsSolved is Script {
function run() external view {
ImprovedERC20 erc20 = ImprovedERC20(vm.envAddress("ImprovedERC20"));
address user = vm.envAddress("USER");
if (erc20.balanceOf(user) > 0) {
console.log("is-solved:true");
} else {
console.log("is-solved:false");
}
}
}
让用户的erc余额大于0,看样子是要偷一些内容。
看看合约本身
pragma solidity ^0.7.0;
import {IImprovedERC20} from "./interfaces/IImprovedERC20.sol";
contract ImprovedERC20 is IImprovedERC20 {
mapping(address => uint256) public override balanceOf;
mapping(address => mapping(address => uint256)) public override allowance;
address public override owner;
string public override name;
string public override symbol;
uint8 public override decimals;
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals,
uint256 _initialSupply
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
owner = msg.sender;
balanceOf[msg.sender] = _initialSupply;
}
function transfer(
address _to,
uint256 _value
) external override returns (bool) {
require(balanceOf[msg.sender] >= _value, "Insufficient balance");
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
return true;
}
function transferFrom(
address _from,
address _to,
uint256 _value
) external override returns (bool) {
require(balanceOf[_from] >= _value, "Insufficient balance");
require(
allowance[_from][msg.sender] - _value > 0,
"Insufficient allowance"
);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
return true;
}
function approve(
address _spender,
uint256 _value
) external override returns (bool) {
allowance[msg.sender][_spender] = _value;
return true;
}
function mint(uint256 _value) external override {
require(msg.sender == owner, "Only owner can mint");
balanceOf[msg.sender] += _value;
}
function burn(address _who, uint256 _value) external override {
require(balanceOf[_who] >= _value, "Insufficient balance");
balanceOf[_who] -= _value;
}
}
deploy.sol
pragma solidity ^0.7.0;
import {ImprovedERC20} from "../src/ImprovedERC20.sol";
import {Script} from "forge-std/Script.sol";
import {console} from "forge-std/console.sol";
contract Deploy is Script {
function run() external {
vm.startBroadcast();
ImprovedERC20 erc20 = new ImprovedERC20(
"Improved ERC20",
"IMPERC20",
18,
100 ether
);
address otherUser = address(uint160(vm.envAddress("USER")) + 1);
erc20.transfer(otherUser, 100 ether);
console.log("address:ImprovedERC20", address(erc20));
console.log("address:Other_User", address(otherUser));
}
}
注意分析,我们注意到他创建了两个user,在初始化的时候为自己赋予18个token,100eth。然后向otheruser转了100 ether。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容