Skip to content

Commit

Permalink
turn on network check with auto switch button, clean up contract so i…
Browse files Browse the repository at this point in the history
…t is super simple, updates s3 script
  • Loading branch information
Austin Griffith committed Jul 8, 2021
1 parent 1e4793d commit 74ea431
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"start": "yarn workspace @scaffold-eth/react-app start",
"compile": "yarn workspace @scaffold-eth/hardhat compile",
"deploy": "yarn workspace @scaffold-eth/hardhat deploy",
"verify": "yarn workspace @scaffold-eth/hardhat etherscan-verify",
"watch": "yarn workspace @scaffold-eth/hardhat watch",
"accounts": "yarn workspace @scaffold-eth/hardhat accounts",
"balance": "yarn workspace @scaffold-eth/hardhat balance",
Expand Down
17 changes: 4 additions & 13 deletions packages/hardhat/contracts/YourContract.sol
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
pragma solidity >=0.8.0 <0.9.0;
//SPDX-License-Identifier: MIT

import "hardhat/console.sol";
//import "hardhat/console.sol";
//import "@openzeppelin/contracts/access/Ownable.sol"; //https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol

contract YourContract {

event SetPurpose(address sender, string purpose);
//event SetPurpose(address sender, string purpose);

string public purpose = "Building Unstoppable Apps";

error EmptyPurposeError(uint code, string message);

constructor() {
// what should we do on deploy?
}

function setPurpose(string memory newPurpose) public {
if(bytes(newPurpose).length == 0){
revert EmptyPurposeError({
code: 1,
message: "Purpose can not be empty"
});
}

purpose = newPurpose;
console.log(msg.sender,"set purpose to",purpose);
emit SetPurpose(msg.sender, purpose);
//console.log(msg.sender,"set purpose to",purpose);
//emit SetPurpose(msg.sender, purpose);
}
}
70 changes: 62 additions & 8 deletions packages/react-app/scripts/s3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const s3FolderUpload = require("s3-folder-upload");
const fs = require("fs");

const directoryName = "build";

const BUCKETNAME = ""; // <<---- SET YOUR BUCKET NAME AND CREATE aws.json ** see below vvvvvvvvvv
const BUCKETNAME = "testbucket.xyz"; // <<---- SET YOUR BUCKET NAME AND CREATE aws.json ** see below vvvvvvvvvv

/*
const invalidation = {
awsDistributionId: "E224H0HK9AWILY",
awsInvalidationPath: "/*"
}
*/

if (!BUCKETNAME) {
console.log("☢️ Enter a bucket name in packages/react-app/scripts/s3.js ");
Expand All @@ -20,7 +28,6 @@ try {
);
process.exit(1);
}
console.log("credentials", credentials);

credentials.bucket = BUCKETNAME;

Expand All @@ -30,10 +37,57 @@ const options = {
useIAMRoleCredentials: false,
};

// optional cloudfront invalidation rule
// const invalidation = {
// awsDistributionId: "<Your CloudFront Distribution Id>",
// awsInvalidationPath: "/*"
// }
/// //////////
/// ////////// First, let's automatically create the bucket if it doesn't exist...
/// //////////

// eslint-disable-next-line import/no-extraneous-dependencies
const AWS = require("aws-sdk");
// Load credentials and set Region from JSON file
AWS.config.loadFromPath("./aws.json");

// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });

// Create params JSON for S3.createBucket
const bucketParams = {
Bucket: BUCKETNAME,
ACL: "public-read",
};

// Create params JSON for S3.setBucketWebsite
const staticHostParams = {
Bucket: BUCKETNAME,
WebsiteConfiguration: {
ErrorDocument: {
Key: "index.html",
},
IndexDocument: {
Suffix: "index.html",
},
},
};

// Call S3 to create the bucket
s3.createBucket(bucketParams, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Bucket URL is ", data.Location);
// Set the new policy on the newly created bucket
s3.putBucketWebsite(staticHostParams, function (err, data) {
if (err) {
// Display error message
console.log("Error", err);
} else {
// Update the displayed policy for the selected bucket
console.log("Success... UPLOADING!", data);

s3FolderUpload(directoryName, credentials, options /* , invalidation */);
///
/// After the bucket is created, we upload to it:
///
s3FolderUpload(directoryName, credentials, options /* , invalidation */);
}
});
}
});
25 changes: 23 additions & 2 deletions packages/react-app/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const targetNetwork = NETWORKS.localhost; // <------- select your target fronten

// 😬 Sorry for all the console logging
const DEBUG = true;
const NETWORKCHECK = false;
const NETWORKCHECK = true;

// 🛰 providers
if (DEBUG) console.log("📡 Connecting to Mainnet Ethereum");
Expand Down Expand Up @@ -262,7 +262,28 @@ function App(props) {
description={
<div>
You have <b>{networkSelected && networkSelected.name}</b> selected and you need to be on{" "}
<b>{networkLocal && networkLocal.name}</b>.
<Button
onClick={async () => {
const ethereum = window.ethereum;
const data = [
{
chainId: "0x" + targetNetwork.chainId.toString(16),
chainName: targetNetwork.name,
nativeCurrency: targetNetwork.nativeCurrency,
rpcUrls: [targetNetwork.rpcUrl],
blockExplorerUrls: [targetNetwork.blockExplorer],
},
];
console.log("data", data);
const tx = await ethereum.request({ method: "wallet_addEthereumChain", params: data }).catch();
if (tx) {
console.log(tx);
}
}}
>
<b>{networkLocal && networkLocal.name}</b>
</Button>
.
</div>
}
type="error"
Expand Down

0 comments on commit 74ea431

Please sign in to comment.