https://github.com/falconandrea/au-week4-emit-contract-event
Alchemy University - Task for week4
https://github.com/falconandrea/au-week4-emit-contract-event
alchemy-university exercises hardhat smart-contracts solidity tests web3
Last synced: 2 months ago
JSON representation
Alchemy University - Task for week4
- Host: GitHub
- URL: https://github.com/falconandrea/au-week4-emit-contract-event
- Owner: falconandrea
- Created: 2023-01-17T13:13:11.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-20T17:03:41.000Z (about 2 years ago)
- Last Synced: 2025-01-24T18:36:58.208Z (4 months ago)
- Topics: alchemy-university, exercises, hardhat, smart-contracts, solidity, tests, web3
- Language: Solidity
- Homepage:
- Size: 156 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Alchemy University - Week 4 Task
Your goal is simple! Emit the winner event on this smart contract on the Goerli testnet: [https://goerli.etherscan.io/address/0xcF469d3BEB3Fc24cEe979eFf83BE33ed50988502#code](https://goerli.etherscan.io/address/0xcF469d3BEB3Fc24cEe979eFf83BE33ed50988502#code)
If you take a look at the [Code tab in Etherscan](https://goerli.etherscan.io/address/0xcF469d3BEB3Fc24cEe979eFf83BE33ed50988502#code), you'll see that the source code for this contract looks like this:
```solidity
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;contract Contract {
event Winner(address);function attempt() external {
require(msg.sender != tx.origin, "msg.sender is equal to tx.origin");
emit Winner(msg.sender);
}
}
```How do we possibly make it so the tx.origin (the EOA who originated the transaction) is not equal to the msg.sender? 🤔
We'll leave that challenge up to you!
# Resolution
Create a simple contract and use it to make the `attempt` call to the `Winner Contract` through the interface.
```solidity
interface WinnerContract {
function attempt() external;
}contract EmitEvent {
function sendAttempt(address _contract) public {
return WinnerContract(_contract).attempt();
}
}
```Update `deploy` script to send a transaction to invoke the attempt method on the deployed contract.
```js
const hre = require("hardhat");async function main() {
const EmitEvent = await hre.ethers.getContractFactory("EmitEvent");
const emitEvent = await EmitEvent.deploy();
const addressWinnerContract = "0xcF469d3BEB3Fc24cEe979eFf83BE33ed50988502";await emitEvent.deployed();
// Call the "attempt" method of Winner Contract through the interface inside our Contract
const result = await emitEvent.sendAttempt(addressWinnerContract);console.log(`Contract deployed to ${emitEvent.address}`);
}main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
```