與外部合約交互
智能合約可以與以太坊區塊鏈上的其他合約進行交互,允許它們調用函數、讀取變量以及髮送以太幣或其他代幣。促進這種交互的一種方法是使用Web3j(一個用於處理以太坊智能合約的輕量級Java庫)。Web3j可以自動生成智能合約包裝器代碼,從而實現從JVM無縫部署智能合約併與之交互。
要使用Web3j與外部合約交互,首先需要編譯智能合約併生成包裝器代碼。然後,您可以創建和部署智能合約或使用現有合約,從而輕鬆進行交易併直接調用智能合約。
事件和日誌
事件對於跟蹤和監控區塊鏈上的智能合約活動至關重要。它們提供了一種髮出日誌的方法,這些日誌可以存儲起來,併在鏈下繫統中檢索。事件可以更輕鬆地跟蹤特定的合約事件或狀態變量的變化,這對於需要實時更新的dApp(去中心化應用)很有幫助。
日誌是事件髮出的記録,存儲在區塊鏈上,可以實現智能合約和鏈下繫統之間的高效通信,是以太坊生態繫統的重要組成部分。此外,針對日誌建立索引,使應用程序可以輕鬆過濾和搜索特定的事件或數據點。
示例:使用Remix和MetaMask部署智能合約
第1步:打開Remix IDE
首先,在Web瀏覽器中打開Remix IDE(https://remix.ethereum.org/)。
第2步:創建新文件
單擊IDE左上角的“+”按鈕,創建一個新的空白工作區,然後單擊“New File”創建一個新文件。
將文件命名爲“Auction. sol”。
第3步:定義合約
將以下代碼覆製併粘貼到新的“Auction. sol”文件中:
TypeScript
// SPDX-License-Identifier: MIT
// Specify the Solidity version
pragma solidity ^0.8.0;
// Define the Auction contract
contract Auction {
// Declare state variables
address payable public owner; // The owner of the contract (can cancel the auction)
uint public startBlock; // The block number at which the auction starts
uint public endBlock; // The block number at which the auction ends
string public ipfsHash; // IPFS hash for the item being auctioned
bool public canceled; // Whether the auction has been canceled
bool public ended; // Whether the auction has ended
uint public highestBid; // The highest bid so far
address payable public highestBidder; // The address of the highest bidder
// Declare events
event AuctionCanceled(); // Event emitted when the auction is canceled
event HighestBidIncreased(address bidder, uint amount); // Event emitted when a new highest bid is set
event AuctionEnded(address winner, uint amount); // Event emitted when the auction ends
// Declare mapping
mapping(address => uint256) public balances;
// Constructor function
constructor() {
owner = payable(msg.sender); // Set the owner to the address that deploys the contract
startBlock = block.number; // Set the start block to the current block number
endBlock = startBlock + 40320; // Set the end block to 1 week (40320 blocks) after the start block
ipfsHash = ""; // Initialize the IPFS hash to an empty string
}
// Function to place a bid
function placeBid() public payable {
require(block.number >= startBlock && block.number <= endBlock, "Auction is not active."); // Check that the auction is active
require(msg.value > highestBid, "There is already a higher bid."); // Check that the new bid is higher than the current highest bid
require(!canceled, "Auction is canceled."); // Check that the auction has not been canceled
// If there was a previous highest bidder, add their bid amount to their balance
if (highestBidder != address(0)) {
balances[highestBidder] += highestBid;
}
// Set the new highest bid and bidder
highestBid = msg.value;
highestBidder = payable(msg.sender);
// Emit an event to signal a new highest bid has been set
emit HighestBidIncreased(msg.sender, msg.value);
}
// Function to cancel the auction
function cancelAuction() public {
require(msg.sender == owner, "Only the owner can cancel the auction."); // Check that the sender is the owner
require(!ended, "Auction has already ended."); // Check that the auction has not already ended
// Set the canceled flag and emit an event to signal the auction has been canceled
canceled = true;
emit AuctionCanceled();
}
// Function to end the auction
function endAuction() public {
require(block.number > endBlock, "Auction is not over yet."); // Check that the auction is over
require(!canceled, "Auction is canceled."); // Check that the auction has not been canceled
require(!ended, "Auction has already ended."); // Check that the auction has not already ended
// Set the ended flag and emit an event to signal the auction has ended
ended = true;
emit AuctionEnded(highestBidder, highestBid);
// Transfer the highest bid amount to the owner
owner.transfer(highestBid);
// Ifthere was a previous highest bidder, add their bid amount to their balance
if (highestBidder != address(0)) {
balances[highestBidder] += highestBid;
}
}
// Function to set the IPFS hash for the item being auctioned
function setIpfsHash(string memory hash) public {
require(msg.sender == owner, "Only the owner can set the IPFS hash."); // Check that the sender is the owner
ipfsHash = hash; // Set the IPFS hash to the provided value
}
}
本段代碼是一個拍賣合約,允許用戶對物品進行競標,併在指定期限後結束拍賣。該合約還有一個取消拍賣的函數和爲正在拍賣的物品設置IPFS哈希值的函數。
第4步:編譯合約
單擊左側菜單中的“Solidity Compiler”選項卡。在“Compile Auction.sol”下,單擊“Compile”按鈕。合約編譯成功後,文件資源管理器(FILE EXPLORER)中可以看到“Auction.sol”旁會顯示一個緑色的勾號。
第5步:部署合約
點擊左側菜單中的“Deploy & run transactions”選項卡。在“Environment”下,選擇“Injected Web3”。在“Contract”下,選擇“Auction”作爲要部署的合約。單擊“Deploy”按鈕。
第6步:與合約進行交互
合約部署完成後,您可以使用合約中定義的各種函數與之交互。例如,您可以調用place eBid()
函數對物品進行競標。
通過使用Remix和MetaMask,您可以輕鬆地在以太坊網絡上部署智能合約併與之交互,從而在用戶友好的環境中實現去中心化應用的開髮和測試。
要點
與外部合約交互
智能合約可以與以太坊區塊鏈上的其他合約進行交互,允許它們調用函數、讀取變量以及髮送以太幣或其他代幣。促進這種交互的一種方法是使用Web3j(一個用於處理以太坊智能合約的輕量級Java庫)。Web3j可以自動生成智能合約包裝器代碼,從而實現從JVM無縫部署智能合約併與之交互。
要使用Web3j與外部合約交互,首先需要編譯智能合約併生成包裝器代碼。然後,您可以創建和部署智能合約或使用現有合約,從而輕鬆進行交易併直接調用智能合約。
事件和日誌
事件對於跟蹤和監控區塊鏈上的智能合約活動至關重要。它們提供了一種髮出日誌的方法,這些日誌可以存儲起來,併在鏈下繫統中檢索。事件可以更輕鬆地跟蹤特定的合約事件或狀態變量的變化,這對於需要實時更新的dApp(去中心化應用)很有幫助。
日誌是事件髮出的記録,存儲在區塊鏈上,可以實現智能合約和鏈下繫統之間的高效通信,是以太坊生態繫統的重要組成部分。此外,針對日誌建立索引,使應用程序可以輕鬆過濾和搜索特定的事件或數據點。
示例:使用Remix和MetaMask部署智能合約
第1步:打開Remix IDE
首先,在Web瀏覽器中打開Remix IDE(https://remix.ethereum.org/)。
第2步:創建新文件
單擊IDE左上角的“+”按鈕,創建一個新的空白工作區,然後單擊“New File”創建一個新文件。
將文件命名爲“Auction. sol”。
第3步:定義合約
將以下代碼覆製併粘貼到新的“Auction. sol”文件中:
TypeScript
// SPDX-License-Identifier: MIT
// Specify the Solidity version
pragma solidity ^0.8.0;
// Define the Auction contract
contract Auction {
// Declare state variables
address payable public owner; // The owner of the contract (can cancel the auction)
uint public startBlock; // The block number at which the auction starts
uint public endBlock; // The block number at which the auction ends
string public ipfsHash; // IPFS hash for the item being auctioned
bool public canceled; // Whether the auction has been canceled
bool public ended; // Whether the auction has ended
uint public highestBid; // The highest bid so far
address payable public highestBidder; // The address of the highest bidder
// Declare events
event AuctionCanceled(); // Event emitted when the auction is canceled
event HighestBidIncreased(address bidder, uint amount); // Event emitted when a new highest bid is set
event AuctionEnded(address winner, uint amount); // Event emitted when the auction ends
// Declare mapping
mapping(address => uint256) public balances;
// Constructor function
constructor() {
owner = payable(msg.sender); // Set the owner to the address that deploys the contract
startBlock = block.number; // Set the start block to the current block number
endBlock = startBlock + 40320; // Set the end block to 1 week (40320 blocks) after the start block
ipfsHash = ""; // Initialize the IPFS hash to an empty string
}
// Function to place a bid
function placeBid() public payable {
require(block.number >= startBlock && block.number <= endBlock, "Auction is not active."); // Check that the auction is active
require(msg.value > highestBid, "There is already a higher bid."); // Check that the new bid is higher than the current highest bid
require(!canceled, "Auction is canceled."); // Check that the auction has not been canceled
// If there was a previous highest bidder, add their bid amount to their balance
if (highestBidder != address(0)) {
balances[highestBidder] += highestBid;
}
// Set the new highest bid and bidder
highestBid = msg.value;
highestBidder = payable(msg.sender);
// Emit an event to signal a new highest bid has been set
emit HighestBidIncreased(msg.sender, msg.value);
}
// Function to cancel the auction
function cancelAuction() public {
require(msg.sender == owner, "Only the owner can cancel the auction."); // Check that the sender is the owner
require(!ended, "Auction has already ended."); // Check that the auction has not already ended
// Set the canceled flag and emit an event to signal the auction has been canceled
canceled = true;
emit AuctionCanceled();
}
// Function to end the auction
function endAuction() public {
require(block.number > endBlock, "Auction is not over yet."); // Check that the auction is over
require(!canceled, "Auction is canceled."); // Check that the auction has not been canceled
require(!ended, "Auction has already ended."); // Check that the auction has not already ended
// Set the ended flag and emit an event to signal the auction has ended
ended = true;
emit AuctionEnded(highestBidder, highestBid);
// Transfer the highest bid amount to the owner
owner.transfer(highestBid);
// Ifthere was a previous highest bidder, add their bid amount to their balance
if (highestBidder != address(0)) {
balances[highestBidder] += highestBid;
}
}
// Function to set the IPFS hash for the item being auctioned
function setIpfsHash(string memory hash) public {
require(msg.sender == owner, "Only the owner can set the IPFS hash."); // Check that the sender is the owner
ipfsHash = hash; // Set the IPFS hash to the provided value
}
}
本段代碼是一個拍賣合約,允許用戶對物品進行競標,併在指定期限後結束拍賣。該合約還有一個取消拍賣的函數和爲正在拍賣的物品設置IPFS哈希值的函數。
第4步:編譯合約
單擊左側菜單中的“Solidity Compiler”選項卡。在“Compile Auction.sol”下,單擊“Compile”按鈕。合約編譯成功後,文件資源管理器(FILE EXPLORER)中可以看到“Auction.sol”旁會顯示一個緑色的勾號。
第5步:部署合約
點擊左側菜單中的“Deploy & run transactions”選項卡。在“Environment”下,選擇“Injected Web3”。在“Contract”下,選擇“Auction”作爲要部署的合約。單擊“Deploy”按鈕。
第6步:與合約進行交互
合約部署完成後,您可以使用合約中定義的各種函數與之交互。例如,您可以調用place eBid()
函數對物品進行競標。
通過使用Remix和MetaMask,您可以輕鬆地在以太坊網絡上部署智能合約併與之交互,從而在用戶友好的環境中實現去中心化應用的開髮和測試。
要點