Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/hardhat/contracts/Vendor.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
pragma solidity 0.8.20; //Do not change the solidity version as it negatively impacts submission grading
// SPDX-License-Identifier: MIT

// import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./YourToken.sol";

contract Vendor {
// event BuyTokens(address buyer, uint256 amountOfETH, uint256 amountOfTokens);
contract Vendor is Ownable{
event BuyTokens(address buyer, uint256 amountOfETH, uint256 amountOfTokens);
event SellTokens(address seller, uint256 amountOfETH, uint256 amountOfTokens);

YourToken public yourToken;
uint public constant tokensPerEth = 100;

constructor(address tokenAddress) {
constructor(address tokenAddress) Ownable (msg.sender) {
yourToken = YourToken(tokenAddress);
}

// ToDo: create a payable buyTokens() function:
function buyTokens() payable public {
// msg.sender is the buyer of the tokens, they will pay some ETH in msg.data
uint ethInput = msg.value;
uint256 tokensBought = tokensPerEth * ethInput;
// check if vendor has enough
require(tokensBought<= yourToken.balanceOf(address(this)), " insuffcient tokens to dispense");
yourToken.transfer(msg.sender, tokensBought); //transfer token from
emit BuyTokens(msg.sender, ethInput, tokensBought);
}

// ToDo: create a withdraw() function that lets the owner withdraw ETH

// function withdraw
// ToDo: create a sellTokens(uint256 _amount) function:
}
8 changes: 7 additions & 1 deletion packages/hardhat/contracts/YourToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ pragma solidity 0.8.20; //Do not change the solidity version as it negatively im
// SPDX-License-Identifier: MIT

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

// learn more: https://docs.openzeppelin.com/contracts/4.x/erc20

contract YourToken is ERC20 {
constructor() ERC20("Gold", "GLD") {

constructor() ERC20("Group9", "G9") {
//_mint( ~~~YOUR FRONTEND ADDRESS HERE~~~~ , 1000 * 10 ** 18);
address initialTokenOwner = msg.sender;
_mint(initialTokenOwner, 1000* (10**18)); // 1000 tokens, msg sender aka minter will be initial Owner of all tokens
}


}
38 changes: 19 additions & 19 deletions packages/hardhat/deploy/01_deploy_vendor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
// import { Contract } from "ethers";
import { Contract } from "ethers";

/**
* Deploys a contract named "Vendor" using the deployer account and
Expand All @@ -21,25 +21,25 @@ const deployVendor: DeployFunction = async function (hre: HardhatRuntimeEnvironm
You can run the `yarn account` command to check your balance in every network.
*/
// // Deploy Vendor
// const { deployer } = await hre.getNamedAccounts();
// const { deploy } = hre.deployments;
// const yourToken = await hre.ethers.getContract<Contract>("YourToken", deployer);
// const yourTokenAddress = await yourToken.getAddress();
// await deploy("Vendor", {
// from: deployer,
// // Contract constructor arguments
// args: [yourTokenAddress],
// 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,
// });
// const vendor = await hre.ethers.getContract<Contract>("Vendor", deployer);
// const vendorAddress = await vendor.getAddress();
// // Transfer tokens to Vendor
// await yourToken.transfer(vendorAddress, hre.ethers.parseEther("1000"));
const { deployer } = await hre.getNamedAccounts();
const { deploy } = hre.deployments;
const yourToken = await hre.ethers.getContract<Contract>("YourToken", deployer);
const yourTokenAddress = await yourToken.getAddress();
await deploy("Vendor", {
from: deployer,
// Contract constructor arguments
args: [yourTokenAddress],
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,
});
const vendor = await hre.ethers.getContract<Contract>("Vendor", deployer);
const vendorAddress = await vendor.getAddress();
// // Transfer tokens to Vendor from msg sender
await yourToken.transfer(vendorAddress, hre.ethers.parseEther("1000"));
// // Transfer contract ownership to your frontend address
// await vendor.transferOwnership("**YOUR FRONTEND ADDRESS**");
await vendor.transferOwnership("0x173696F944B398d00D2176E595677102dE63C59e");
};

export default deployVendor;
Expand Down
12 changes: 6 additions & 6 deletions packages/nextjs/app/token-vendor/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const TokenVendor: NextPage = () => {

// const { data: vendorEthBalance } = useWatchBalance({ address: vendorContractData?.address });

// const { data: tokensPerEth } = useScaffoldReadContract({
// contractName: "Vendor",
// functionName: "tokensPerEth",
// });
const { data: tokensPerEth } = useScaffoldReadContract({
contractName: "Vendor",
functionName: "tokensPerEth",
});

return (
<>
Expand Down Expand Up @@ -72,7 +72,7 @@ const TokenVendor: NextPage = () => {
</div>

{/* Buy Tokens */}
{/* <div className="flex flex-col items-center space-y-4 bg-base-100 shadow-lg shadow-secondary border-8 border-secondary rounded-xl p-6 mt-8 w-full max-w-lg">
<div className="flex flex-col items-center space-y-4 bg-base-100 shadow-lg shadow-secondary border-8 border-secondary rounded-xl p-6 mt-8 w-full max-w-lg">
<div className="text-xl">Buy tokens</div>
<div>{tokensPerEth?.toString() || 0} tokens per ETH</div>

Expand All @@ -97,7 +97,7 @@ const TokenVendor: NextPage = () => {
>
Buy Tokens
</button>
</div> */}
</div>

{!!yourTokenBalance && (
<div className="flex flex-col items-center space-y-4 bg-base-100 shadow-lg shadow-secondary border-8 border-secondary rounded-xl p-6 mt-8 w-full max-w-lg">
Expand Down
Loading
Loading