# 【emit】

我們可以寫一個函數裡面加上 `emit` 這個動詞來觸發事件。

舉例來說：如果我們先建立事件並且於函數裡觸發它。

```solidity
pragma solidity ^0.8.11;

contract Test {
   event Deposit(address indexed _from, bytes32 indexed _id, uint _value);
   function deposit(bytes32 _id) public payable {      
      emit Deposit(msg.sender, _id, msg.value);
   }
}
```

後在 Javascript 使用 web3.js 的套件呼叫合約事件，web3.js 在之後的章節會特別介紹！

```javascript
var abi = /* abi as generated using compiler */;
// 要使用合約內容必須要得到合約在 compiler 之後輸出的 abi
var ClientReceipt = web3.eth.contract(abi);
var clientReceiptContract = ClientReceipt.at("0x1234...ab67" /* address */);

// 呼叫事件 Deposit：
var event = clientReceiptContract.Deposit(function(error, result) {
   if (!error)console.log(result);
});
```

在前端（dapp）回傳的結果如下：

```javascript
{
   "returnValues": {
      "_from": "0x1111...FFFFCCCC",
      "_id": "0x50...sd5adb20",
      "_value": "0x420042"
   },
   "raw": {
      "data": "0x7f...91385",
      "topics": ["0xfd4...b4ead7", "0x7f...1a91385"]
   }
}
```

如果我們今天輸入 `0x05416460deb76d57af601be17e777b93592d8d4d4a4096c57876a91c84f4a712` 也可以直接在 Remix 的下方互動環境看見一些資訊，包含 `logs` 等：

```javascript
logs = 
[
	{
		"from": "0x9DD41ECd6e1701CE34523ed98423c1eFb0805aBD",
		"topic": "0x19dacbf83c5de6658e14cbf7bcae5c15eca2eedecf1c66fbca928e4d351bea0f",
		"event": "Deposit",
		"args": {
			"0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
			"1": "0x05416460deb76d57af601be17e777b93592d8d4d4a4096c57876a91c84f4a712",
			"2": "0",
			"_from": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
			"_id": "0x05416460deb76d57af601be17e777b93592d8d4d4a4096c57876a91c84f4a712",
			"_value": "0"
		}
	}
]
```

接下來我們會一一介紹 `indexed`, `data`, `topics` 等專有名詞。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chihaolu.gitbook.io/all-in-one-solidity/part-ii-medium/chapter-10-shi-jian-event/emit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
