Skip to content

Instantly share code, notes, and snippets.

@valterlobo
Created March 9, 2023 15:37
Show Gist options
  • Save valterlobo/df1f04538b72ab88a3f5f854605e7161 to your computer and use it in GitHub Desktop.
Save valterlobo/df1f04538b72ab88a3f5f854605e7161 to your computer and use it in GitHub Desktop.
TRANSFER ETH AND ERC 20
import os
from dotenv import load_dotenv
from web3 import Web3
load_dotenv()
# ERC 20 SEND
def send_ERC20(from_address, to_address, amount, contract , key):
amount_wei = web3.toWei(amount, 'ether')
tx = contract.functions.transfer(to_address, amount_wei).buildTransaction({
'from': from_address,
'nonce': web3.eth.getTransactionCount(from_address),
'maxFeePerGas': web3.toWei('250', 'gwei'),
'maxPriorityFeePerGas': web3.toWei('3', 'gwei'),
'value': 0,
'chainId': 80001
})
gas = web3.eth.estimateGas(tx)
tx['gas'] = gas
signed_tx = web3.eth.account.signTransaction(tx, key)
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
if tx_receipt['status'] == 1:
print('Tokens transferred successfully! Hash: {}'.format(str(web3.toHex(tx_hash))))
#else:
# print('There was an error transferring the tokens')
# ERC 20 SEND
# SEND ETH
def send_ETH(from_address, to_address, amount, private_key):
amount_wei = web3.toWei(amount, 'ether')
tx = {
'type': '0x2',
'nonce': web3.eth.getTransactionCount(from_address),
'from': from_address,
'to': to_address,
'value': amount_wei,
'maxFeePerGas': web3.toWei('250', 'gwei'),
'maxPriorityFeePerGas': web3.toWei('3', 'gwei'),
'chainId': 80001
}
gas = web3.eth.estimateGas(tx)
tx['gas'] = gas
signed_tx = web3.eth.account.signTransaction(tx, private_key)
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
if tx_receipt['status'] == 1:
print('ETH transferred successfully! Hash: {}'.format(str(web3.toHex(tx_hash))))
#else:
# print('There was an error transferring the ETH')
# SEND ETH
provider_url = os.getenv('PROVIDER_URL')
print(provider_url)
f = open("abi.json", "r")
abi = f.read()
#print(abi)
web3 = Web3(Web3.HTTPProvider(provider_url))
address_contract = '0x5cD..............'
contract = web3.eth.contract(address=address_contract, abi=abi)
#need to put .call() at the end to call the smart contract
totalSupply = contract.functions.totalSupply().call()
#convert supply to Wei witch is 18 decimal places)
print('Total Supply: ', totalSupply/1000000)
print('Contract Name: ', contract.functions.name().call())
print('Symbol: ', contract.functions.symbol().call())
balance = contract.functions.balanceOf("0x0d................").call()
print('Balance:' , balance )
print(web3.fromWei(balance, "ether"))
transfer_to = '0xf......'
transfer_from = '0x0d......'
private_key = os.getenv('PRIVATE_KEY')
balance = contract.functions.balanceOf(transfer_to).call()
print(web3.fromWei(balance, "ether"))
send_ERC20(transfer_from, transfer_to, 10, contract, private_key)
send_ETH(transfer_from, transfer_to, 1 , private_key)
print(f"Receiver {transfer_to} balance: {contract.functions.balanceOf(transfer_to).call()}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment