Skip to content

Instantly share code, notes, and snippets.

@Zeegaths
Last active February 3, 2024 13:26
Show Gist options
  • Save Zeegaths/236b8b8ce892d53b561b8895f5ca27d0 to your computer and use it in GitHub Desktop.
Save Zeegaths/236b8b8ce892d53b561b8895f5ca27d0 to your computer and use it in GitHub Desktop.
A smart contract that triggers and emits 3 events
let contract_abi = [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": false,
"internalType": "string",
"name": "message",
"type": "string"
}
],
"name": "AddressLog",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "_from",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "_value",
"type": "uint256"
},
{
"indexed": false,
"internalType": "string",
"name": "message",
"type": "string"
}
],
"name": "DepositAmount",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "oldValue",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "newValue",
"type": "uint256"
},
{
"indexed": false,
"internalType": "string",
"name": "message",
"type": "string"
}
],
"name": "ValueIncrease",
"type": "event"
},
{
"inputs": [],
"name": "addAmount",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "checkEvents",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"name": "x",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]
let EventTest = web3.eth.contract(contract_abi);
let EventTestContract = ClientReceipt.at("0xA46AE501D40a136A4412bfeA75078157E6F8cDC0");
EventTestContract.transfer(function(err, data) {
if (!err)
console.log(data);
});
@Zeegaths
ForEvent.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
// Write a smart contract that defines and triggers 3-4 events
// Index the events so that they can be easily searched
// Capture these events in your JavaScript code
contract Events {
uint public x = 0;
event AddressLog(address indexed sender, string message);
event DepositAmount(address indexed _from, uint _value, string message);
event ValueIncrease(address indexed owner, uint oldValue, uint newValue, string message);
function checkEvents() public payable {
emit AddressLog(msg.sender, "Log message emitted");
emit DepositAmount(msg.sender, msg.value, "Deposit successful");
}
function addAmount() public {
uint oldValue = x;
x++;
emit ValueIncrease(msg.sender, oldValue, x, "Value increased");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment