Skip to content

Instantly share code, notes, and snippets.

View duanescarlett's full-sized avatar

Duane Scarlett duanescarlett

View GitHub Profile
@duanescarlett
duanescarlett / Transaction.sol
Created December 28, 2023 02:24
This is for a solidity tutorial that explains the use of the tx object and gas in solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
contract TransactionInfo {
// State variable to store contract balance
uint256 public contractBalance;
// Variables to store transaction details
@duanescarlett
duanescarlett / MsgObject.sol
Created December 16, 2023 03:06
This is for the tutorial that teaches about the MSG object
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract TransactionInfo {
address public owner;
uint256 public contractBalance;
constructor() {
// Set the owner to the account that deploys the contract
owner = msg.sender;
@duanescarlett
duanescarlett / inheritance.sol
Created March 31, 2023 01:56
Tutorial on inheritance
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.17;
contract Parent {
function foo() public pure virtual returns (string memory) {
return "A";
}
function bar() public pure virtual returns (string memory) {
return "B";
@duanescarlett
duanescarlett / SafeMath.sol
Last active March 10, 2023 18:25
In this tutorial I explain how overflow and unchecked works in solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract SafeMath {
// uint8 public number = 255;
uint8 public number = 2**8 - 1;
function increment() public {
unchecked {
number++;
@duanescarlett
duanescarlett / visibility.sol
Created March 5, 2023 17:03
A smart contract that describes visibility
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.17;
contract Visibility {
uint256 private x = 0;
uint256 internal y = 1;
uint256 public z = 2;
// Public
// Accesed from any smart contract, function inside
@duanescarlett
duanescarlett / DataTypesTutorial.sol
Created February 28, 2023 01:14
This is for a tutorial that teaches datatypes
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.17;
contract VariablesAndDataTypes {
// State Variables
int256 public oneIng = 1;
uint256 public oneUint = 1;
// Local Variable
function getValue() public pure returns(uint256) {
@duanescarlett
duanescarlett / main.js
Last active June 16, 2022 15:36
Get Liquidity Pool address
const ethers = require('ethers')
const { toChecksumAddress } = require('ethereum-checksum-address')
require('dotenv').config()
const main = async () => {
// Connect to an EVM network
const provider = new ethers.providers.JsonRpcProvider(`https://rpc.ankr.com/bsc`)
// Create a Factory contract abi
const IPair = [
@duanescarlett
duanescarlett / main.js
Last active January 5, 2023 23:49
Send ERC-20 tokens to another wallet
const ethers = require('ethers')
const { toChecksumAddress } = require('ethereum-checksum-address')
require('dotenv').config()
const main = async () => {
// Connect to an EVM network
const provider = new ethers.providers.JsonRpcProvider(`https://polygon-mainnet.g.alchemy.com/v2/${process.env.RPC_KEY}`)
// Create a wallet
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY)
// Get a signer
@duanescarlett
duanescarlett / main.js
Created March 31, 2022 15:13
This converts from decimal to wei then from wei to decimal
const Web3 = require('web3')
require('dotenv').config()
const main = async () => {
// Connect to a EVM blockchain
const web3 = new Web3(Web3.givenProvider || process.env.INFURA_URL)
// Convert from a decimal number to a wei number
let amt = web3.utils.toWei('0.1', 'ether')
console.log('Convert from a decimal number to a wei number', amt)
@duanescarlett
duanescarlett / main.js
Created March 21, 2022 16:42
Get ether and erc-20 token balances in a wallet
const ethers = require('ethers')
const { toChecksumAddress } = require('ethereum-checksum-address')
require('dotenv').config()
const main = async () => {
// Connect to an EVM network
const provider = new ethers.providers.JsonRpcProvider(`https://polygon-mainnet.g.alchemy.com/v2/${process.env.RPC_KEY}`)
// Create a wallet
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY)
// Get a signer