-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
README, contract upgrades and script.
- Loading branch information
1 parent
1ce3d59
commit 15dcfc9
Showing
10 changed files
with
707 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0 <0.9.0; | ||
|
||
// Useful for debugging. Remove when deploying to a live network. | ||
import "hardhat/console.sol"; | ||
|
||
// Use openzeppelin to inherit battle-tested implementations (ERC20, ERC721, etc) | ||
// import "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
/** | ||
* A smart contract that allows changing a state variable of the contract and tracking the changes | ||
* It also allows the owner to withdraw the Ether in the contract | ||
* @author BuidlGuidl | ||
*/ | ||
contract YourContract2 { | ||
// State Variables | ||
address public immutable owner; | ||
string public greeting = "Building Unstoppable Apps!!!"; | ||
bool public premium = false; | ||
uint256 public totalCounter = 0; | ||
mapping(address => uint) public userGreetingCounter; | ||
|
||
string public farewell = "Go get 'em buidler!!!"; | ||
uint256 public totalCounterFarewell = 0; | ||
mapping(address => uint) public userFarewellCounter; | ||
|
||
// Events: a way to emit log statements from smart contract that can be listened to by external parties | ||
event GreetingChange( | ||
address indexed greetingSetter, | ||
string newGreeting, | ||
bool premium, | ||
uint256 value | ||
); | ||
|
||
event FarewellChange( | ||
address indexed farewellSetter, | ||
string newFarewell, | ||
bool premium, | ||
uint256 value | ||
); | ||
|
||
// Constructor: Called once on contract deployment | ||
// Check packages/hardhat/deploy/00_deploy_your_contract.ts | ||
constructor(address _owner) { | ||
owner = _owner; | ||
} | ||
|
||
// Modifier: used to define a set of rules that must be met before or after a function is executed | ||
// Check the withdraw() function | ||
modifier isOwner() { | ||
// msg.sender: predefined variable that represents address of the account that called the current function | ||
require(msg.sender == owner, "Not the Owner"); | ||
_; | ||
} | ||
|
||
/** | ||
* Function that allows anyone to change the state variable "greeting" of the contract and increase the counters | ||
* | ||
* @param _newGreeting (string memory) - new greeting to save on the contract | ||
*/ | ||
function setGreeting(string memory _newGreeting) public payable { | ||
// Print data to the hardhat chain console. Remove when deploying to a live network. | ||
console.log( | ||
"Setting new greeting '%s' from %s", | ||
_newGreeting, | ||
msg.sender | ||
); | ||
|
||
// Change state variables | ||
greeting = _newGreeting; | ||
totalCounter += 1; | ||
userGreetingCounter[msg.sender] += 1; | ||
|
||
// msg.value: built-in global variable that represents the amount of ether sent with the transaction | ||
if (msg.value > 0) { | ||
premium = true; | ||
} else { | ||
premium = false; | ||
} | ||
|
||
// emit: keyword used to trigger an event | ||
emit GreetingChange(msg.sender, _newGreeting, msg.value > 0, 0); | ||
} | ||
|
||
/** | ||
* Function that allows the owner to withdraw all the Ether in the contract | ||
* The function can only be called by the owner of the contract as defined by the isOwner modifier | ||
*/ | ||
function withdraw() public isOwner { | ||
(bool success, ) = owner.call{ value: address(this).balance }(""); | ||
require(success, "Failed to send Ether"); | ||
} | ||
|
||
/** | ||
* Function that allows the contract to receive ETH | ||
*/ | ||
receive() external payable {} | ||
|
||
function setFarewell(string memory _newFarewell) public payable { | ||
// Print data to the hardhat chain console. Remove when deploying to a live network. | ||
console.log( | ||
"Setting new farwell '%s' from %s", | ||
_newFarewell, | ||
msg.sender | ||
); | ||
|
||
// Change state variables | ||
farewell = _newFarewell; | ||
totalCounterFarewell += 1; | ||
userFarewellCounter[msg.sender] += 1; | ||
|
||
// msg.value: built-in global variable that represents the amount of ether sent with the transaction | ||
if (msg.value > 0) { | ||
premium = true; | ||
} else { | ||
premium = false; | ||
} | ||
|
||
// emit: keyword used to trigger an event | ||
emit FarewellChange(msg.sender, _newFarewell, msg.value > 0, 0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0 <0.9.0; | ||
|
||
// Use openzeppelin to inherit battle-tested implementations (ERC20, ERC721, etc) | ||
import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; | ||
|
||
/** | ||
* A smart contract so we can use the ABI in the frontend | ||
* The TransparentUpgradeableProxy creates a ProxyAdmin on chain, we usually use hardhat to generate the ABI | ||
* @author BuidlGuidl | ||
*/ | ||
contract YourProxyAdmin is ProxyAdmin { | ||
|
||
// Constructor: Called once on contract deployment | ||
// Check packages/hardhat/deploy/01_deploy_your_contract_upgrade.ts | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/hardhat/upgrade/deploy_script/01_deploy_your_contract_upgrade.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { HardhatRuntimeEnvironment } from "hardhat/types"; | ||
import { DeployFunction } from "hardhat-deploy/types"; | ||
|
||
/** | ||
* Deploys a contract named "YourContract" using the deployer account and | ||
* constructor arguments set to the deployer address | ||
* | ||
* @param hre HardhatRuntimeEnvironment object. | ||
*/ | ||
const deployYourContract: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { | ||
/* | ||
On localhost, the deployer account is the one that comes with Hardhat, which is already funded. | ||
When deploying to live networks (e.g `yarn deploy --network goerli`), the deployer account | ||
should have sufficient balance to pay for the gas fees for contract creation. | ||
You can generate a random account with `yarn generate` which will fill DEPLOYER_PRIVATE_KEY | ||
with a random private key in the .env file (then used on hardhat.config.ts) | ||
You can run the `yarn account` command to check your balance in every network. | ||
*/ | ||
const { deployer } = await hre.getNamedAccounts(); | ||
const { deploy } = hre.deployments; | ||
|
||
await deploy("YourContract2", { | ||
from: deployer, | ||
// Contract constructor arguments | ||
args: [deployer], | ||
log: true, | ||
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by | ||
// automatically mining the contract deployment transaction. There is no effect on live networks. | ||
autoMine: true, | ||
}); | ||
|
||
await deploy("YourProxyAdmin", { | ||
from: deployer, | ||
// Contract constructor arguments | ||
args: [], | ||
log: true, | ||
// autoMine: can be passed to the deploy function to make the deployment process faster on local networks by | ||
// automatically mining the contract deployment transaction. There is no effect on live networks. | ||
autoMine: true, | ||
}); | ||
}; | ||
|
||
export default deployYourContract; | ||
|
||
// Tags are useful if you have multiple deploy files and only want to run one of them. | ||
// e.g. yarn deploy --tags YourContract | ||
deployYourContract.tags = ["YourContract2", "YourProxyAdmin"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.