-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated: migrating to ts boilerplate with lock contract and tests
- Loading branch information
1 parent
ce06e5d
commit de422b6
Showing
12 changed files
with
310 additions
and
159 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 was deleted.
Oops, something went wrong.
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,34 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.9; | ||
|
||
// Uncomment this line to use console.log | ||
// import "hardhat/console.sol"; | ||
|
||
contract Lock { | ||
uint public unlockTime; | ||
address payable public owner; | ||
|
||
event Withdrawal(uint amount, uint when); | ||
|
||
constructor(uint _unlockTime) payable { | ||
require( | ||
block.timestamp < _unlockTime, | ||
"Unlock time should be in the future" | ||
); | ||
|
||
unlockTime = _unlockTime; | ||
owner = payable(msg.sender); | ||
} | ||
|
||
function withdraw() public { | ||
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal | ||
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp); | ||
|
||
require(block.timestamp >= unlockTime, "You can't withdraw yet"); | ||
require(msg.sender == owner, "You aren't the owner"); | ||
|
||
emit Withdrawal(address(this).balance, block.timestamp); | ||
|
||
owner.transfer(address(this).balance); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,95 @@ | ||
import { HardhatUserConfig, task } from "hardhat/config"; | ||
import "@nomicfoundation/hardhat-toolbox"; | ||
import "dotenv/config"; | ||
|
||
const accounts = process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : []; | ||
|
||
// https://hardhat.org/guides/create-task.html | ||
task( | ||
"accounts", | ||
"Prints the list of accounts with balances", | ||
async (_, hre) => { | ||
const accounts = await hre.ethers.getSigners(); | ||
const provider = hre.ethers.provider; | ||
|
||
for (const account of accounts) { | ||
const balance = await provider.getBalance(account.address); | ||
console.log( | ||
`${account.address} - ${hre.ethers.formatEther(balance)} ETH` | ||
); | ||
} | ||
} | ||
); | ||
|
||
task("deploy", "Deploys Contract", async (_, hre) => { | ||
const currentTimestampInSeconds = Math.round(Date.now() / 1000); | ||
const unlockTime = currentTimestampInSeconds + 300; | ||
const lockedAmount = hre.ethers.parseEther("0.001"); | ||
|
||
const lockInstance = await hre.ethers.deployContract("Lock", [unlockTime], { | ||
value: lockedAmount | ||
}); | ||
|
||
await lockInstance.waitForDeployment(); | ||
console.log("contract deployed at:", lockInstance.target); | ||
}); | ||
|
||
task("balance", "Prints an account's balance") | ||
.addParam("account", "The account's address") | ||
.setAction(async ({ account }, hre) => { | ||
const provider = hre.ethers.provider; | ||
const balance = await provider.getBalance(account); | ||
console.log(hre.ethers.formatEther(balance), "ETH"); | ||
}); | ||
|
||
const config: HardhatUserConfig = { | ||
defaultNetwork: "local", | ||
networks: { | ||
hardhat: { | ||
chainId: 1337 | ||
}, | ||
local: { | ||
url: "http://127.0.0.1:8545" | ||
}, | ||
mumbai: { | ||
url: | ||
process.env.POLYGON_MUMBAI_RPC_URL || | ||
"https://rpc-mumbai.maticvigil.com", | ||
accounts | ||
}, | ||
polygon: { | ||
url: | ||
process.env.POLYGON_MAINNET_RPC_URL || | ||
"https://rpc-mainnet.maticvigil.com", | ||
accounts | ||
} | ||
}, | ||
etherscan: { | ||
// API key for Polygonscan | ||
apiKey: process.env.ETHERSCAN_API_KEY | ||
}, | ||
gasReporter: { | ||
enabled: true, | ||
currency: "USD" | ||
}, | ||
solidity: { | ||
version: "0.8.19", | ||
settings: { | ||
optimizer: { | ||
enabled: true, | ||
runs: 200 | ||
} | ||
} | ||
}, | ||
paths: { | ||
sources: "./contracts", | ||
tests: "./test", | ||
cache: "./cache", | ||
artifacts: "./artifacts" | ||
}, | ||
mocha: { | ||
timeout: 20000 | ||
} | ||
}; | ||
|
||
export default config; |
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 was deleted.
Oops, something went wrong.
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,31 @@ | ||
import { ethers } from "hardhat"; | ||
|
||
async function main() { | ||
const currentTimestampInSeconds = Math.round(Date.now() / 1000); | ||
const unlockTime = currentTimestampInSeconds + 300; | ||
const lockedAmount = ethers.parseEther("0.001"); | ||
|
||
const lockInstance = await ethers.deployContract("Lock", [unlockTime], { | ||
value: lockedAmount | ||
}); | ||
|
||
await lockInstance.waitForDeployment(); | ||
return lockInstance; | ||
} | ||
|
||
main() | ||
.then(async (lockInstance) => { | ||
console.log("Lock Contract deployed to:", lockInstance.target); | ||
// Read from the contract | ||
const unlockTime = await lockInstance.unlockTime(); | ||
console.log("Unlock time:", unlockTime.toString()); | ||
|
||
// Write to the contract | ||
// const tx = await lockInstance.withdraw(); | ||
// await tx.wait(); | ||
// console.log("Withdrawn!"); | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
process.exitCode = 1; | ||
}); |
Oops, something went wrong.