Skip to content

Instantly share code, notes, and snippets.

@web3author
web3author / NumberStorage.sol
Last active March 3, 2023 11:28
Simple Solidity code to store a number
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;
contract NumberStorage {
uint public storedNumber;
// Storing function
function setNumber(uint _number) public {
storedNumber = _number;
@web3author
web3author / deploy.js
Last active March 3, 2023 12:42
Script for Deployment
async function main (){
const NumberStorage = await ethers.getContractFactory("NumberStorage"); // instance of contract
const numberStorage = await NumberStorage.deploy(); // deploy contract
console.log("NumberStorage deployed to:", numberStorage.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
@web3author
web3author / storage.js
Last active March 3, 2023 12:31
Testing script
const {expect} = require("chai");
describe("NumberStorage Smart Contract", function(){
it("Should set the value of storedNumber value to the value passed in", async function(){
const NumberStorage = await ethers.getContractFactory("NumberStorage");
const numberStorage = await NumberStorage.deploy();
await numberStorage.setNumber(21)
console.log(await numberStorage.storedNumber())
expect(await numberStorage.storedNumber()).to.equal(21)
})
@web3author
web3author / hardhat.config.js
Created February 15, 2023 16:23
Hardhat config file
/** @type import(‘hardhat/config’).HardhatUserConfig */
require("@nomiclabs/hardhat-waffle")
require("dotenv").config()
module.exports = {
solidity: "0.8.17",
networks: {
goerli: {
url: process.env.GOERLI_URL,
accounts: [process.env.PRIVATE_KEY]
}
@web3author
web3author / lib.rs
Last active February 25, 2023 05:10
Hello world Rust smart contract code
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint,
entrypoint::ProgramResult,
msg,
pubkey::Pubkey,
};
// Declare and export the program's entrypoint
entrypoint!(process_instruction);
@web3author
web3author / client.ts
Created February 15, 2023 23:19
Client script file to interact with a Solana Contract
const transaction = new web3.Transaction();
transaction.add(
new web3.TransactionInstruction({
keys: [],
programId: new web3.PublicKey(pg.PROGRAM_ID),
}),
);
console.log("Forwarding Transaction");
@web3author
web3author / 1_contract_migration.js
Created March 8, 2023 23:03
Smart Contract deployment script for Truffle
var NumberStorage = artifacts.require("NumberStorage");
module.exports = function(deployer) {
deployer.deploy(NumberStorage);
};
@web3author
web3author / interact.js
Created March 8, 2023 23:05
Web3.js based interaction script using Smart Contract address
const Web3 = require('web3')
const web3 = new Web3("http://10.0.2.15:8545")
const contractAddress = 'XXXXXXXXXXXXXXXXXXX'
const abi = [{"inputs":[],"name":"getNumber","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_number","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"storedNumber","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]
const NumberStorage = new web3.eth.Contract(abi, contractAddress);
@web3author
web3author / App.js
Last active March 19, 2023 08:35
React Webapp app.js file for using ether.js to interact with Smart Contract
// Import required modules and components
import './App.css'; // Importing the App.css file for styling
import { useState, useEffect } from "react"; // Importing the useState and useEffect hooks from React
import { ethers } from "ethers"; // Importing ethers library for interacting with Ethereum
import Fetch from "./components/Fetch"; // Importing the Fetch component
import Store from "./components/Store"; // Importing the Store component
function App() {
// Set initial state using the useState hook
@web3author
web3author / Fetch.js
Last active March 19, 2023 08:43
components/Fetch.js script to read stored number from blockchain by calling Smart Contract
import { useState, useEffect } from "react";
// This is a functional component named 'Fetch' that receives the 'state' object as a prop.
const Fetch = ({ state }) => {
// The 'contract' object is destructured from the 'state' object.
const { contract } = state;
// This creates a new state variable named 'storedNumber' and a function named 'setStoredNumber' to update it
const [storedNumber, setStoredNumber] = useState("Not Available");