mirror of
https://github.com/autistic-symposium/blockchains-security-toolkit.git
synced 2025-08-18 02:50:28 -04:00
🍟 github vscode is so cool
This commit is contained in:
parent
0931961978
commit
5b8de9dc6d
21 changed files with 16 additions and 1 deletions
6
solidity/boilerplates/README.md
Normal file
6
solidity/boilerplates/README.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
## 🌮 boilerplates
|
||||
|
||||
|
||||
<br>
|
||||
|
||||
* [First ERC-721 project](https://github.com/bt3gl-labs/Blockchain-Development-and-Security/tree/main/Solidity-Expert/Boilerplates/erc721-solidity-101)
|
5
solidity/boilerplates/erc721-solidity-101/.env_sample
Normal file
5
solidity/boilerplates/erc721-solidity-101/.env_sample
Normal file
|
@ -0,0 +1,5 @@
|
|||
API_URL = "https://eth-rinkeby.alchemyapi.io/v2/<project>"
|
||||
PUBLIC_KEY =
|
||||
PRIVATE_KEY =
|
||||
METADATA_URL = <pinata IPFS cid dor metadata>"
|
||||
CONTRACT_ADDRESS =
|
20
solidity/boilerplates/erc721-solidity-101/README.md
Normal file
20
solidity/boilerplates/erc721-solidity-101/README.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
# One: A special summer
|
||||
|
||||
|
||||
1. Compile contract:
|
||||
|
||||
```
|
||||
npx hardhat compile
|
||||
```
|
||||
|
||||
2. Deploy contract:
|
||||
|
||||
```
|
||||
npx hardhat run scripts/deploy-contract.js --network rinkeby
|
||||
```
|
||||
|
||||
3. Mint NFT:
|
||||
|
||||
```
|
||||
node scripts/mint-nft.js
|
||||
```
|
|
@ -0,0 +1,28 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity >=0.7.3 <0.9.0;
|
||||
|
||||
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
|
||||
import "@openzeppelin/contracts/utils/Counters.sol";
|
||||
import "@openzeppelin/contracts/access/Ownable.sol";
|
||||
|
||||
|
||||
contract MiaNFT is ERC721, Ownable {
|
||||
|
||||
using Counters for Counters.Counter;
|
||||
Counters.Counter private _tokenIds;
|
||||
|
||||
constructor() public ERC721("Mia's NFT, "NFT") {}
|
||||
|
||||
function mintNFT(address recipient, string memory tokenURI)
|
||||
public onlyOwner
|
||||
returns (uint256)
|
||||
{
|
||||
_tokenIds.increment();
|
||||
|
||||
uint256 newItemId = _tokenIds.current();
|
||||
_mint(recipient, newItemId);
|
||||
_setTokenURI(newItemId, tokenURI);
|
||||
|
||||
return newItemId;
|
||||
}
|
||||
}
|
19
solidity/boilerplates/erc721-solidity-101/hardhat.config.js
Normal file
19
solidity/boilerplates/erc721-solidity-101/hardhat.config.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* @type import('hardhat/config').HardhatUserConfig
|
||||
*/
|
||||
require('dotenv').config();
|
||||
require("@nomiclabs/hardhat-ethers");
|
||||
|
||||
const { API_URL, PRIVATE_KEY } = process.env;
|
||||
|
||||
module.exports = {
|
||||
solidity: "0.7.3",
|
||||
defaultNetwork: "rinkeby",
|
||||
networks: {
|
||||
hardhat: {},
|
||||
rinkeby: {
|
||||
url: API_URL,
|
||||
accounts: [`0x${PRIVATE_KEY}`]
|
||||
}
|
||||
},
|
||||
}
|
23
solidity/boilerplates/erc721-solidity-101/package.json
Normal file
23
solidity/boilerplates/erc721-solidity-101/package.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "MiaNFT",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Mia von Steinkirch",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
||||
"@openzeppelin/contracts": "^3.1.0-solc-0.7",
|
||||
"chai": "^4.3.4",
|
||||
"ethereum-waffle": "^3.4.0",
|
||||
"ethers": "^5.4.6",
|
||||
"hardhat": "^2.6.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@alch/alchemy-web3": "^1.1.4",
|
||||
"dotenv": "^10.0.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
async function main() {
|
||||
const nft = await ethers.getContractFactory("MiaNFT");
|
||||
|
||||
const nft_deploy = await nft.deploy();
|
||||
console.log(" ⛓🧱✨ Contract address:", nft_deploy.address);
|
||||
console.log(" ➡️ (Please add this string to .env)");
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => process.exit(0))
|
||||
.catch(e => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
|
@ -0,0 +1,41 @@
|
|||
require('dotenv').config();
|
||||
|
||||
const PUBLIC_KEY = process.env.PUBLIC_KEY;
|
||||
const PRIVATE_KEY = process.env.PRIVATE_KEY;
|
||||
const API_URL = process.env.API_URL;
|
||||
const METADATA_URL = process.env.METADATA_URL;
|
||||
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
|
||||
const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
|
||||
const web3 = createAlchemyWeb3(API_URL);
|
||||
const contract = require("../artifacts/contracts/MiaNFT.sol/MiaNFT.json");
|
||||
const nftContract = new web3.eth.Contract(contract.abi, CONTRACT_ADDRESS);
|
||||
|
||||
async function mintNFT(tokenURI) {
|
||||
|
||||
const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY, 'latest');
|
||||
|
||||
const transaction = {
|
||||
'from': PUBLIC_KEY,
|
||||
'to': CONTRACT_ADDRESS,
|
||||
'nonce': nonce,
|
||||
'gas': 500000,
|
||||
'maxPriorityFeePerGas': 1999999987,
|
||||
'data': nftContract.methods.mintNFT(PUBLIC_KEY, tokenURI).encodeABI()
|
||||
};
|
||||
|
||||
const sign = web3.eth.accounts.signTransaction(transaction, PRIVATE_KEY);
|
||||
sign.then((signedTransaction) => {
|
||||
|
||||
web3.eth.sendSignedTransaction(signedTransaction.rawTransaction, function(e, hash) {
|
||||
if (!e) {
|
||||
console.log("💾 Transaction hash: ", hash);
|
||||
} else {
|
||||
console.log("ERROR:", e)
|
||||
}
|
||||
});
|
||||
}).catch((e) => {
|
||||
console.log("ERROR:", e);
|
||||
});
|
||||
}
|
||||
|
||||
mintNFT(METADATA_URL);
|
32
solidity/boilerplates/learning/hello-world.sol
Normal file
32
solidity/boilerplates/learning/hello-world.sol
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Specifies the version of Solidity, using semantic versioning.
|
||||
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/layout-of-source-files.html#pragma
|
||||
pragma solidity ^0.5.10;
|
||||
|
||||
// Defines a contract named `HelloWorld`.
|
||||
// A contract is a collection of functions and data (its state).
|
||||
// Once deployed, a contract resides at a specific address on the Ethereum blockchain.
|
||||
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/structure-of-a-contract.html
|
||||
contract HelloWorld {
|
||||
|
||||
// Declares a state variable `message` of type `string`.
|
||||
// State variables are variables whose values are permanently stored in contract storage.
|
||||
// The keyword `public` makes variables accessible from outside a contract
|
||||
// and creates a function that other contracts or clients can call to access the value.
|
||||
string public message;
|
||||
|
||||
// Similar to many class-based object-oriented languages, a constructor is
|
||||
// a special function that is only executed upon contract creation.
|
||||
// Constructors are used to initialize the contract's data.
|
||||
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#constructors
|
||||
constructor(string memory initMessage) public {
|
||||
// Accepts a string argument `initMessage` and sets the value
|
||||
// into the contract's `message` storage variable).
|
||||
message = initMessage;
|
||||
}
|
||||
|
||||
// A public function that accepts a string argument
|
||||
// and updates the `message` storage variable.
|
||||
function update(string memory newMessage) public {
|
||||
message = newMessage;
|
||||
}
|
||||
}
|
58
solidity/boilerplates/learning/token.sol
Normal file
58
solidity/boilerplates/learning/token.sol
Normal file
|
@ -0,0 +1,58 @@
|
|||
pragma solidity ^0.5.10;
|
||||
|
||||
contract Token {
|
||||
// An `address` is comparable to an email address - it's used to identify an account on Ethereum.
|
||||
// Addresses can represent a smart contract or an external (user) accounts.
|
||||
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/types.html#address
|
||||
address public owner;
|
||||
|
||||
// A `mapping` is essentially a hash table data structure.
|
||||
// This `mapping` assigns an unsigned integer (the token balance) to an address (the token holder).
|
||||
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/types.html#mapping-types
|
||||
mapping (address => uint) public balances;
|
||||
|
||||
// Events allow for logging of activity on the blockchain.
|
||||
// Ethereum clients can listen for events in order to react to contract state changes.
|
||||
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#events
|
||||
event Transfer(address from, address to, uint amount);
|
||||
|
||||
// Initializes the contract's data, setting the `owner`
|
||||
// to the address of the contract creator.
|
||||
constructor() public {
|
||||
// All smart contracts rely on external transactions to trigger its functions.
|
||||
// `msg` is a global variable that includes relevant data on the given transaction,
|
||||
// such as the address of the sender and the ETH value included in the transaction.
|
||||
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/units-and-global-variables.html#block-and-transaction-properties
|
||||
owner = msg.sender;
|
||||
}
|
||||
|
||||
// Creates an amount of new tokens and sends them to an address.
|
||||
function mint(address receiver, uint amount) public {
|
||||
// `require` is a control structure used to enforce certain conditions.
|
||||
// If a `require` statement evaluates to `false`, an exception is triggered,
|
||||
// which reverts all changes made to the state during the current call.
|
||||
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/control-structures.html#error-handling-assert-require-revert-and-exceptions
|
||||
|
||||
// Only the contract owner can call this function
|
||||
require(msg.sender == owner, "You are not the owner.");
|
||||
|
||||
// Enforces a maximum amount of tokens
|
||||
require(amount < 1e60, "Maximum issuance exceeded");
|
||||
|
||||
// Increases the balance of `receiver` by `amount`
|
||||
balances[receiver] += amount;
|
||||
}
|
||||
|
||||
// Sends an amount of existing tokens from any caller to an address.
|
||||
function transfer(address receiver, uint amount) public {
|
||||
// The sender must have enough tokens to send
|
||||
require(amount <= balances[msg.sender], "Insufficient balance.");
|
||||
|
||||
// Adjusts token balances of the two addresses
|
||||
balances[msg.sender] -= amount;
|
||||
balances[receiver] += amount;
|
||||
|
||||
// Emits the event defined earlier
|
||||
emit Transfer(msg.sender, receiver, amount);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue