Python 3, Ethereum - how to send ERC20 Tokens? - python

i have some script using to sending Ethers from address to addres. Im using Parity, and Python 3.6. It is using Flask looks like:
from flask import Flask, render_template, json, request
import urllib
import requests
import binascii
from decimal import *
app = Flask(__name__)
def Eth(method,params=[]):
data = {"method":method,"params":params,"id":1,"jsonrpc":"2.0"}
headers = {'Content-type': 'application/json'}
r = requests.post(ethrpc, data=json.dumps(data), headers=headers)
r = r.text
response = json.loads(r)
return(response)
hot = str("XXXXXXX")
#app.route('/')
def index():
ethnumbers = int(10)**int(18)
hot = str("XXXXX")
balance = Eth("eth_getBalance",[hot,'latest'])
balance = balance["result"]
balance = int(balance, 16)
balance = float(balance)
balance = balance / ethnumbers
balance = str(balance)
return render_template('index.html',hot = hot,balance=balance)
#app.route('/send/',methods=['POST','GET'])
def send():
getcontext().prec = 28
ethnumbers = Decimal(10)**Decimal(18)
print(ethnumbers)
if request.method == "POST":
_myaddress = request.form['_myaddress']
_youraddress = request.form['_youraddress']
_amount = request.form['_amount']
_gas = request.form['_gas']
_gas = hex(int(_gas))
passy = str("XXXXXXXXX")
getcontext().prec = 28
_amount = Decimal(_amount)
getcontext().prec = 28
_amount = _amount * ethnumbers
getcontext().prec = 28
_amount = int(_amount)
_amount = hex(_amount)
r = [{'from':_myaddress,"to":_youraddress,"value":_amount,"gas":_gas,},str("XXXXXXXXXX!")]
print(r)
json.dumps(r)
resultio = Eth("personal_sendTransaction",r)
try:
resultio["result"]
return render_template('sent.html',resultio=resultio["result"])
except: KeyError
return render_template('sent.html',resultio=resultio["error"]["message"])
else:
return render_template('index.html')
if __name__ == "__main__":
app.run()
Im pretty sure, that i have to use "data" to do this, but i have no idea how to send via this script ERC20 tokens. Structure of tokens transaction looks like "my address -> token address -> token receiver".
Any ideas?

Guys it was simpler than it looks like.
You just have to put:
contract address
as receiver and make a long "data" field, it represents string as:
// method name, its constans in erc20
0xa9059cbb
//receiver address (you have to do it without "0x" because its needed only when
//you have to tell "im using hexadecimal". You did it above, in method field.
//so, receiver address:
5b7b3b499fb69c40c365343cb0dc842fe8c23887
// and fill it with zeros, it have to be lenght 64. So fill rest of address
0000000000000000000000005b7b3b499fb69c40c365343cb0dc842fe8c23887
// then you need amount, please convert it to hexadecimal, delete "0x" and
// remember, you need to integer first, so if token has 18 "decimals" it need
// to be amount / 10**18 first!!
//1e27786570c272000 and fill that with zeros, like above:
000000000000000000000000000000000000000000000001e27786570c272000
//just add string, to string, to string, and you have data field:
0xa9059cbb0000000000000000000000005b7b3b499fb69c40c365343cb0dc842fe8c23887000000000000000000000000000000000000000000000001e27786570c272000
Sample transaction: https://etherscan.io/tx/0x9c27df8af24e06edb819a8d7a380f548fad637de5edddd6155d15087d1619964

web3.py is definitely the way to go. If you want to do it by hand and you just want to call the standard ERC-20 transfer method, the from address should stay the same, the to address should be the token contract, and then the data should be the following concatenated together and formatted as hexadecimal:
The first 4 bytes of the keccak256 hash of "transfer(address,uint256)", which is the function's signature.
The address of the recipient, left-zero-padded to be 32 bytes.
The amount to be transferred. (Be sure to take into account the token's decimals... 1 token is often 10**18, but the number of decimal places varies from token to token and can be retrieved by calling the decimals() function.) This should also be formatted as a 32-byte number (so left-zero-padded).
web3.py would be much easier. :-) Something like:
web3.eth.contract(address, abi=standard_token_abi).sendTransaction({
'from': from_address
}).transfer(to_address, amount)

Related

Discord API: Providing messages from incorrect time period

Im trying to get all messages from a channel from 2 days ago till now. instead, it's giving me the messages from the inception of the channel. Don't know what I'm doing wrong here...
here is the code
import requests
import json
import datetime
def retrieve_messages(channelid, after=None, before=None):
headers = {
'authorization': "<TOKEN>"
}
# Build the query string, including the after and before parameters
# if they were specified
query_string = '?'
if after:
query_string += f'&after={after}'
if before:
query_string += f'&before={before}'
# Make the API call, including the query string
r = requests.get(
f'https://discord.com/api/v9/channels/{channelid}/messages{query_string}', headers=headers)
jsonn = json.loads(r.text)
for value in jsonn:
print(value['content'], '\n')
# Retrieve messages from the past two days
two_days_ago = datetime.datetime.utcnow() - datetime.timedelta(days=2)
after = int(two_days_ago.timestamp())
retrieve_messages("<CHANNEL_ID>", after=after)
I finally figured it out. It is in the documentation
# Define the DISCORD_EPOCH constant
DISCORD_EPOCH = 1420070400000
# Convert the x_days_ago variable to a timestamp in milliseconds
timestamp_ms = int(x_days_ago.timestamp() * 1000)
# Subtract the DISCORD_EPOCH constant from the timestamp to get the correct format for the after parameter
after = (timestamp_ms - DISCORD_EPOCH) << 22
The discord.py API uses timestamps in milliseconds, not seconds. So:
# ...
if after:
query_string += f'&after={after * 1000}' # Multiply by 1000 (convert to ms)
if before:
query_string += f'&before={before}'

web3py with 0x API

I have been trying to perform a swap on Ropsten using the 0x API to get the best quote but every time I try to perform a swap the transaction fails without any feedback(at least from Etherscan) while the approve tx goes through fine.
I am probably missing something here, but I'm getting blind to keep staring at my code.
import requests
import json
import time
from web3 import Web3
walletAddress = 'mywalletaddress'
wpk = 'myprivatekey'
# Setup Ropsten provider
w3 = Web3(Web3.HTTPProvider('https://ropsten.infura.io/v3/'))
# Get the nonce to prevent from sending the transaction twice
nonce = w3.eth.getTransactionCount(walletAddress)
print('Nonce:', nonce)
# Store the generic ERC20 abi for later use
ABI = json.load(open('abi.json'))
# ROPSTEN ADDRESSES
WETH = Web3.toChecksumAddress('0xc778417e063141139fce010982780140aa0cd5ab')
DAI = Web3.toChecksumAddress('0xad6d458402f60fd3bd25163575031acdce07538d')
USDC = Web3.toChecksumAddress('0x07865c6e87b9f70255377e024ace6630c1eaa37f')
def calcAmount(initialAmount, contractAddress):
contract = w3.eth.contract(address=contractAddress, abi=ABI)
decimals = contract.functions.decimals().call()
amount = int(initialAmount * 10**decimals)
return amount
print(decimals, amount)
def getTokenSymbol(contractAddress):
contract = w3.eth.contract(address=contractAddress, abi=ABI)
tokenSymbol = contract.functions.symbol().call()
return tokenSymbol
print('Symbol:', tokenSymbol)
# Define query parameters
initialAmount = 100
slippage = '0.01' # 0.01 = 1% slippage
sellAmount = calcAmount(initialAmount, USDC)
print('Sell:', sellAmount)
def approve(token, spenderAddress, walletAddress, wpk):
token = Web3.toChecksumAddress(token)
contract = w3.eth.contract(address=token, abi=ABI)
spender = Web3.toChecksumAddress(spenderAddress)
max_amount = Web3.toWei(2**64 - 1, 'ether')
nonce = w3.eth.getTransactionCount(walletAddress)
tx = contract.functions.approve(spender, max_amount).buildTransaction({
'from': walletAddress,
'nonce': nonce
})
signed_tx = w3.eth.account.signTransaction(tx, wpk)
tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
print("Approve", w3.toHex(tx_hash))
w3.eth.wait_for_transaction_receipt(tx_hash)
def getQuote(buyToken, slippage, sellToken, sellAmount):
# Make the query https://ropsten.api.0x.org/
query = 'https://ropsten.api.0x.org/swap/v1/quote?buyToken={}&slippage={}&sellToken={}&sellAmount={}'.format(buyToken, slippage, sellToken, sellAmount)
# Get the query and store the response in data
response = requests.get(query)
data = response.json()
# Print a prettiefied version of the response (optional)
print(json.dumps(data, indent=2))
# Convert the buyAmmount to integer
buyAmount = int(data['buyAmount'])
# Convert the buyAmount to a readable number
finalPrice = calcAmount(buyAmount, buyToken)
# Get the symbol of the 2 tokens
buyTokenSymbol = getTokenSymbol(buyToken)
sellTokenSymbol = getTokenSymbol(sellToken)
print(sellTokenSymbol, '->', buyTokenSymbol)
approve(data['sellTokenAddress'], data['allowanceTarget'], walletAddress, wpk)
tx = {
"from": walletAddress,
"to": Web3.toChecksumAddress(data['to']),
"data": data['data'],
"value": 0,
"gas": int(data['gas']),
"gasPrice": int(data['gasPrice']),
"nonce": nonce + 1,
"chainId": data['chainId']
}
# sign the transaction
signed_tx = w3.eth.account.sign_transaction(tx, wpk)
#
# send transaction
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
#
# get transaction hash
print(w3.toHex(tx_hash))
# Use only for testing the query
getQuote(DAI, slippage, USDC, sellAmount)
As you can see I am trying to swap USDC for DAI using the 0x API.
The first tx generated is approved
Etherscan approval tx
But the actual swap fails without any feedback
Etherscan swap tx
I honestly don't know what to try anymore...I've been digging both documentation and reading anything I could find on Stackoverflow without any luck.
Any help is greatly appreciate.
Gas Limit & Usage by Txn: 134,523 | 132,823 (98.74%)
Im 99,9% sure it is because ur running out of gas, try to increase it

I want to send my response to an API using TOTP?

I want to sent my assignment gist in the JSON Response :
{
gist :"",
email :""
}
to an API using TOTP(Time-based one-time password) method . Is there any solution available for that?
Although you have to do it by yourself but still if you invested lot's of time and didn't find solution then you can use my solution for that :
import requests
import hmac
import hashlib
import time
import sys
import struct
import json
from requests.auth import HTTPBasicAuth
root = "YOUR API RESIDES HERE"
content_type = "application/json"
userid = "YOUR USERID"
secret_suffix = "YOUR SECRET SUFFIX OR KEY"
shared_secret = userid+secret_suffix
timestep = 30
T0 = 0
def HOTP(K, C, digits=10):
"""HTOP:
K is the shared key
C is the counter value
digits control the response length
"""
K_bytes = str.encode(K)
C_bytes = struct.pack(">Q", C)
hmac_sha512 = hmac.new(key = K_bytes, msg=C_bytes, digestmod=hashlib.sha512).hexdigest()
return Truncate(hmac_sha512)[-digits:]
def Truncate(hmac_sha512):
"""truncate sha512 value"""
offset = int(hmac_sha512[-1], 16)
binary = int(hmac_sha512[(offset *2):((offset*2)+8)], 16) & 0x7FFFFFFF
return str(binary)
def TOTP(K, digits=10, timeref = 0, timestep = 30):
"""TOTP, time-based variant of HOTP
digits control the response length
the C in HOTP is replaced by ( (currentTime - timeref) / timestep )
"""
C = int ( time.time() - timeref ) // timestep
return HOTP(K, C, digits = digits)
data = {
"github_url": "YOUR GITHUB URL",
"contact_email": "YOUR EMAIL",
"solution_language": "IT IS OPTIONAL"
}
passwd = TOTP(shared_secret, 10, T0, timestep).zfill(10)
resp = requests.post(root, auth=HTTPBasicAuth(userid, passwd), data=json.dumps(data))
print(resp.json())
I hope this works !!
Happy Coding !!

Microsoft LUIS: unable to set time zone (datetimeReference) for datetimeV2 entities

I am using the V3 API to get predictions from a LUIS endpoint and I need a way to tell LUIS my time zone, so that relative time expressions (e.g. "in the past two hours", "in 10 minutes") are resolved properly by the datetimeV2 entity.
Everything works perfectly if I use the V2 API with the timezoneOffset option, but I am unable to make the V3 API work with the new option datetimeReference (which is supposed to replace timezoneOffset). Actually, I could not even figure out which value I should set for datetimeReference (an integer number? A datetime?).
Here are my attempts with Python. Can anyone tell me if there is anything wrong?
from datetime import datetime
import requests
appId = # my app id
subscriptionKey = # my subscription key
query = "tra 10 minuti" # = "in 10 minutes" (my app speaks Italian)
# ATTEMPT 1
# based on https://learn.microsoft.com/en-us/azure/cognitive-services/luis/luis-concept-data-alteration?tabs=V2#change-time-zone-of-prebuilt-datetimev2-entity,
# assuming it works the same way as timezoneOffset
endpoint = 'https://westeurope.api.cognitive.microsoft.com/luis/prediction/v3.0/apps/{appId}/slots/staging/predict?datetimeReference=120&subscription-key={subscriptionKey}&query={query}'
endpoint = endpoint.format(appId = appId, subscriptionKey = subscriptionKey, query = query)
response = requests.get(endpoint)
# ATTEMPT 2
# according to https://learn.microsoft.com/en-us/azure/cognitive-services/luis/luis-migration-api-v3
endpoint = 'https://westeurope.api.cognitive.microsoft.com/luis/prediction/v3.0/apps/{appId}/slots/staging/predict?'
endpoint = endpoint.format(appId = appId)
json = {
"query" : query,
"options":{
"datetimeReference": datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), # e.g. "2020-05-07T13:54:33". Not clear if that's what it wants
"preferExternalEntities": True
},
"externalEntities":[],
"dynamicLists":[]
}
response = requests.post(endpoint, json, headers = {'Ocp-Apim-Subscription-Key' : subscriptionKey})
UPDATE: the correct way of sending the request in ATTEMPT 2 is
response = requests.post(endpoint, json = json, headers = {'Ocp-Apim-Subscription-Key' : subscriptionKey})
As you've discovered, your JSON should go in the json argument and not the data argument:
response = requests.post(endpoint, json = json, headers = {'Ocp-Apim-Subscription-Key' : subscriptionKey})

How can I limit python script to 1 API trade per day?

I'm putting together a python script to make trades on poloniex with the API, and so far I've got it to make trades when certain conditions are met, but I still need it to NOT place anymore trades for the rest of that day (I have the entire script looping every 60 seconds).
So far I have this script:
import requests
import urllib.request
import urllib.parse
import http.client
import hashlib
import hmac
import time
import json
from urllib.request import urlopen
The_Currency_Pair = input('Which Currency Pair?\nPAIRS TO CHOOSE FROM:\nUSDT_BTC\nUSDT_XRP\nUSDT_ETH\nUSDT_BCH\nUSDT_STR\nUSDT_LTC\nUSDT_ETC\nUSDT_XMR\n')
api = 'https://poloniex.com/tradingApi'
key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
secret = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
def main():
poloniexPrices = urlopen('https://poloniex.com/public?command=returnTicker').read()
poloniexjson = json.loads(poloniexPrices)
poloniexlastP = poloniexjson[The_Currency_Pair]['last']
poloniexOCHL = urlopen('https://poloniex.com/public?command=returnChartData&currencyPair=USDT_BCH&start=1538352000&period=86400').read()
poloniexOCHLjson = json.loads(poloniexOCHL)
poloniexlasthigh = poloniexOCHLjson[-2]['high']
print ('Last Price')
print (poloniexlastP)
print ('----------------------------------------')
print ('Last Day High')
print (poloniexlasthigh)
print ('----------------------------------------')
data = {
'command': 'returnBalances',
'nonce' : int(time.time() * 1000)
}
data = urllib.parse.urlencode(data).encode()
signature = hmac.new(secret.encode(), data, hashlib.sha512)
headers = {
'Key' : key,
'Sign': signature.hexdigest()
}
request = urllib.request.Request(
url=api, data=data, headers=headers, method='POST'
)
text = urllib.request.urlopen(request).read().decode()
print ('MY ACCOUNT BALANCE')
try:
print(json.loads(text)['USDT'])
except:
print(text)
print ('-----------------------------------------')
if float(poloniexlastP) > 0:
print ('PLACING TRADE')
print ('-----------------------------------------------')
parms = {"command":"buy",
"currencyPair":The_Currency_Pair,
"rate":100,
"immediateOrCancel":1,
"amount":0.01,
"nonce":int(time.time() * 1000)}
parms = urllib.parse.urlencode(parms).encode()
signature = hmac.new(secret.encode(), parms, hashlib.sha512)
headers = {'Key' : key,
'Sign': signature.hexdigest()}
request = urllib.request.Request(
url=api, data=parms, headers=headers, method='POST')
text = urllib.request.urlopen(request).read().decode()
ordernumber = (json.loads(text)['orderNumber'])
print ('Order Number:')
print (ordernumber)
while True:
main()
time.sleep(60)
Anyway, after a trade has been placed, I need it to make sure that after the 60 second sleep, it doesn't make a second trade unless it is a new day/the day after the trade was made. (Could I use poloniex server time for this?)
So, if it has got as far as print (ordernumber) that means it has placed a trade. But how do I mark it as placed trade for the day or something and use it in the if float(poloniexlastP) > 0: for the next loop to make sure it doesn't place another one?
Maybe you can use Python to get the date, and create a global variable, and after the print statement, you can set the variable to the current date, and the coffee will check if it has already sent, that way it doesn't execute more than once in a day.
import datetime
# This Gets The Day Of The Month
todaysDateNumber = int(datetime.datetime.now().strftime("%d"))
dateNumberTradeSent = 0
if todaysDateNumber == dateNumberTradeSent:
print("The API has already been used once today, try again tomorrow!")
return
else:
# Call Your Trade Sending Code Here
# After The Print Statement That Shows That The Trade Was Sent:
global dateNumberTradeSent
dateNumberTradeSent = int(datetime.datetime.now().strftime("%d"))

Categories