Skip to content

Instantly share code, notes, and snippets.

@web3author
web3author / interact.py
Created March 18, 2023 08:04
web3.py script to interact with a deployed Smart Contract using address and ABI
from web3 import Web3
import codecs
# Connect to a Web3 provider
w3 = Web3(Web3.HTTPProvider('http://10.0.2.15:8545'))
# Address of the smart contract
contract_address = "0x4235d917d3420BF5adf03aE5146701B87D179752"
# ABI of the smart contract
@web3author
web3author / deploy.py
Created March 18, 2023 08:03
Brownie based deployment file for Vyper Smart Contract
#!/usr/bin/python3
from brownie import NumberStorage, accounts
def main():
return NumberStorage.deploy({'from': accounts[0]})
@web3author
web3author / NumberStorage.vy
Created March 18, 2023 08:02
Hello world Smart Contract NumberStorage.vy written in Vyper
# SPDX-License-Identifier: UNLICENSED
# @version ^0.3.7
storedNumber: public(uint256)
# Storing function
@external
def setNumber(_number: uint256):
self.storedNumber = _number
@web3author
web3author / Store.js
Last active March 19, 2023 08:49
components/Store.js script to set stored number from blockchain by calling Smart Contract
// Define a functional component named 'Store' that takes a single prop named 'state'
const Store = ({ state }) => {
// Define an async function named 'storeNum' that takes an 'event' object as its parameter.
const storeNum = async (event) => {
// Prevent the default form submission behavior.
event.preventDefault();
// Retrieve the contract object from the 'state' prop.
@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");
@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 / 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 / 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 / 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 / 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);