利用ChainlinkAutomation自动化Bank合约:使用Solidity实现动态存款管理和自动转账概述在这篇文章中,我们将实现一个Bank合约,用户可以通过deposit()方法存款。我们将使用ChainlinkAutomation来自动化合约任务,实现当存
在这篇文章中,我们将实现一个 Bank
合约,用户可以通过 deposit()
方法存款。我们将使用 Chainlink Automation 来自动化合约任务,实现当存款超过指定阈值时,自动将一半的存款转移到指定的地址(如合约拥有者)。
Bank
合约代码// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {AutomationCompatibleInterface} from "@chainlink/contracts/src/v0.8/automation/AutomationCompatible.sol";
contract Bank is AutomationCompatibleInterface {
address public owner;
uint256 public threshold;
mapping(address => uint256) public balances;
event Deposited(address indexed user, uint256 amount);
event Withdrawn(address indexed user, uint256 amount);
constructor(uint256 _threshold) {
owner = msg.sender;
threshold = _threshold;
}
function deposit() public payable {
require(msg.value > 0, "Deposit must be greater than 0");
balances[msg.sender] += msg.value;
emit Deposited(msg.sender, msg.value);
}
function getBalance() external view returns (uint256) {
return address(this).balance;
}
function setThreshold(uint256 _newThreshold) external {
require(msg.sender == owner, "Only owner can set the threshold");
threshold = _newThreshold;
}
// checkUpKeep():在链下间隔执行调用该函数, 该方法返回一个布尔值,告诉网络是否需要自动化执行。
function checkUpkeep(
bytes calldata /* checkData */
)
external
view
override
returns (bool shouldTransferFunds, bytes memory /* performData */)
{
shouldTransferFunds = (address(this).balance > threshold);
}
// performUpKeep():这个方法接受从checkUpKeep()方法返回的信息作为参数。Chainlink Automation 会触发对它的调用。函数应该先进行一些检查,再执行链上其他计算。
function performUpkeep(bytes calldata /* performData */) external override {
if (address(this).balance > threshold) {
uint256 halfBalance = address(this).balance / 2;
// payable(owner).transfer(halfBalance);
(bool success, ) = payable(owner).call{value: halfBalance}("");
require(success, "Transfer failed");
}
}
function withdraw() external {
require(msg.sender == owner, "Only owner can withdraw");
payable(msg.sender).transfer(address(this).balance); // 转账给调用者
}
// Receive function to accept direct Ether transfers
receive() external payable {
deposit();
}
// Fallback function to handle any calls to non-existent functions
fallback() external payable {
deposit();
}
}
使用 需要实现两个方法
checkUpKeep():在链下间隔执行调用该函数, 该方法返回一个布尔值,告诉网络是否需要自动化执行。
performUpKeep():这个方法接受从checkUpKeep()方法返回的信息作为参数。Chainlink Automation 会触发对它的调用。函数应该先进行一些检查,再执行链上其他计算。
更多请参考:https://docs.chain.link/chainlink-automation/guides/compatible-contracts
在上面代码中,
We don't use the checkData in this example. The checkData is defined when the Upkeep was registered.
We don't use the performData in this example. The performData is generated by the Automation Node's call to your checkUpkeep function
设置阈值:首先,确保合约部署时设置了适当的阈值(例如 0.008 ETH)。
执行存款:
deposit()
方法,存入 0.008 ETH。deposit()
方法,存入 0.002 ETH。查询余额:
getBalance()
方法,检查合约的余额是否超过设定的阈值。若余额超过阈值,performUpkeep()
方法将自动执行,余额应减少一半,转账到拥有者地址。验证自动化:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Script, console} from "forge-std/Script.sol";
import {Bank} from "../src/Bank.sol";
contract BankScript is Script {
Bank public bank;
uint256 threshold = 0.008 ether;
function setUp() public {}
function run() public {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
bank = new Bank(threshold);
console.log("Bank deployed to:", address(bank));
vm.stopBroadcast();
}
}
hello-chainlink on main [!?] via 🅒 base
➜ source .env
hello-chainlink on main [!?] via 🅒 base
➜ forge script --chain sepolia BankScript --rpc-url $SEPOLIA_RPC_URL --broadcast --verify -vvvv
[⠊] Compiling...
No files changed, compilation skipped
Traces:
[381711] BankScript::run()
├─ [0] VM::envUint("PRIVATE_KEY") [staticcall]
│ └─ ← [Return] <env var value>
├─ [0] VM::startBroadcast(<pk>)
│ └─ ← [Return]
├─ [334903] → new Bank@0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896
│ └─ ← [Return] 1451 bytes of code
├─ [0] console::log("Bank deployed to:", Bank: [0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896]) [staticcall]
│ └─ ← [Stop]
├─ [0] VM::stopBroadcast()
│ └─ ← [Return]
└─ ← [Stop]
Script ran successfully.
== Logs ==
Bank deployed to: 0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896
## Setting up 1 EVM.
==========================
Simulated On-chain Traces:
[334903] → new Bank@0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896
└─ ← [Return] 1451 bytes of code
==========================
Chain 11155111
Estimated gas price: 9.442339362 gwei
Estimated total gas used for script: 535661
Estimated amount required: 0.005057892944988282 ETH
==========================
##### sepolia
✅ [Success]Hash: 0x8e76814070931a6835a077724977a64537ddbae22dec2d916ec8f793b4a48340
Contract Address: 0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896
Block: 6473990
Paid: 0.002084410794718675 ETH (412147 gas * 5.057445025 gwei)
✅ Sequence #1 on sepolia | Total Paid: 0.002084410794718675 ETH (412147 gas * avg 5.057445025 gwei)
==========================
ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
##
Start verification for (1) contracts
Start verifying contract `0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896` deployed on sepolia
Submitting verification for [src/Bank.sol:Bank] 0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896.
Submitting verification for [src/Bank.sol:Bank] 0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896.
Submitting verification for [src/Bank.sol:Bank] 0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896.
Submitting verification for [src/Bank.sol:Bank] 0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896.
Submitting verification for [src/Bank.sol:Bank] 0x647f8FF9aa0AFC1d560a0C1366734B1f188Aa896.
Submitted contract for verification:
Response: `OK`
GUID: `lasmeniskktpdateydsrciwzzyymya3aa4teq6fbri8yudei8t`
URL: https://sepolia.etherscan.io/address/0x647f8ff9aa0afc1d560a0c1366734b1f188aa896
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified
All (1) contracts were verified!
Transactions saved to: /Users/qiaopengjun/Code/solidity-code/hello-chainlink/broadcast/Bank.s.sol/11155111/run-latest.json
Sensitive values saved to: /Users/qiaopengjun/Code/solidity-code/hello-chainlink/cache/Bank.s.sol/11155111/run-latest.json
hello-chainlink on main [!?] via 🅒 base took 49.6s
➜
https://sepolia.etherscan.io/address/0x647f8ff9aa0afc1d560a0c1366734b1f188aa896#code
https://automation.chain.link/sepolia
Register Upkeep
当 Bank 合约的存款超过 0.008 ETH时, 转移一半的存款到指定的地址 Owner
第一步:deposit 0.008 ETH
第二步:deposit 0.002 ETH
第三步:查询余额,超过临界点 一半转入 owner 故查询余额为 0.005
第四步:查询 Chainlink Automation 进行确认
https://sepolia.etherscan.io/tx/0x7140fd39f6ff0199a0b8e9d123bcd18d93ace527de9fe44d36358fd17362d4bb
https://sepolia.etherscan.io/tx/0xbffc1f8eff9f93fb77d350d06813553a354eed0c82b89ec8e945b90386184f80
第一步存入 0.008
第二步存入 0.002
超过临界点 一半转入 owner
故查询余额为 0.005
https://sepolia.etherscan.io/tx/0x93fd1050b19966c2fad97ea9c5389e2596501b9931c1cfb372fdf885cb91b6c2
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!