0bps

Strategy vault / Tools & templates

BSC Transaction

Then... Recent research on new technologies, automatic on-chain transfer... Well... nothing really, everyone just play around~ Publicly Share Here~ I wish everyone fortune!!

Author
扁豆子
Complexity
Intermediate · 103 lines
Parameters
8

Then... Recent research on new technologies, automatic on-chain transfer... Well... nothing really, everyone just play around~ Publicly Share Here~ I wish everyone fortune!!

Parameters

NameDefaultWhat it controls
my_address1my_address
to_address1to_address
contract_coinBNBtransaction coin
contract_address0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeetransaction coin address
transaction_time1615997422transaction time
private_key1private_key
send0.001amount
gas10gas

Source Python


from web3 import Web3
import json
import time
import urllib.request

def transaction(my_address, to_address, contract_address, private_key, send, gas):
    Log("start trans " + contract_coin)
    bsc = "https://bsc-dataseed.binance.org/"
    web3 = Web3(Web3.HTTPProvider(bsc))

    Log(web3.isConnected())

    balance = web3.eth.get_balance(my_address)
    humanReadalbe = web3.fromWei(balance, 'ether')
    Log(humanReadalbe)

    abi = json.loads('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"spender","type":"address"},{"name":"tokens","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"from","type":"address"},{"name":"to","type":"address"},{"name":"tokens","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"tokenOwner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"safeSub","outputs":[{"name":"c","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"tokens","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"safeDiv","outputs":[{"name":"c","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"safeMul","outputs":[{"name":"c","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"tokenOwner","type":"address"},{"name":"spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"safeAdd","outputs":[{"name":"c","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"tokens","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"tokenOwner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"tokens","type":"uint256"}],"name":"Approval","type":"event"}]')

    nonce = web3.eth.getTransactionCount(my_address)

    contract_address = web3.toChecksumAddress(contract_address)
    contract = web3.eth.contract(address=contract_address, abi=abi)

    amount = web3.toWei(send, 'ether')
    Log(amount)

    token_tx = contract.functions.transfer(to_address, amount).buildTransaction({
        'chainId':56, 'gas': 150000,'gasPrice': web3.toWei(gas,'gwei'), 'nonce':nonce
    })
    sign_txn = web3.eth.account.signTransaction(token_tx, private_key=private_key)
    web3.eth.sendRawTransaction(sign_txn.rawTransaction)
    Log("Transaction has been sent to " + to_address)

def transaction_bnb(my_address, to_address, private_key, send, gas):
    Log("start trans " + contract_coin)
    bsc = "https://bsc-dataseed.binance.org/"
    web3 = Web3(Web3.HTTPProvider(bsc))

    Log(web3.isConnected())

    balance = web3.eth.get_balance(my_address)
    humanReadalbe = web3.fromWei(balance, 'ether')
    Log(humanReadalbe)

    nonce = web3.eth.getTransactionCount(my_address)

    tx = {
        'nonce': nonce,
        'to': to_address,
        'value': web3.toWei(send, 'ether'),
        'gas': 150000,
        'gasPrice': web3.toWei(gas, 'gwei')
    }

    try:
        signed_tx = web3.eth.account.signTransaction(tx, private_key)
        tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
        trans = web3.toHex(tx_hash)
        Log(trans)
        transaction = web3.eth.get_transaction(trans)
        Log("get " + trans + " status!!")
    except IOError:
        Sleep(100)
        signed_tx = web3.eth.account.signTransaction(tx, private_key)
        tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
        trans = web3.toHex(tx_hash)
        Log("get hash error retry!!")
        transaction = web3.eth.get_transaction(trans)
        Log("get " + trans + " status!!")
    else:
        trans_reslut = web3.eth.waitForTransactionReceipt(tx_hash, timeout=30, poll_latency=0.1)
        trans_reslut = web3.toJSON(trans_reslut)
        Log(trans_reslut)
        trans_reslut_status = json.loads(trans_reslut)['status']
        Log(trans_reslut_status)
        if trans_reslut_status == 0:
            Log("trans failed retry!!")
            transaction_bnb(my_address, to_address, private_key, send, gas)


def doAction():
    Log("Start trasaction!!!")
    if contract_coin != 'BNB':
        transaction(my_address, to_address, contract_address, private_key, send, gas)
    else:
        transaction_bnb(my_address, to_address, private_key, send, gas)
    pass
    Log("trasaction Done!!!")

def main():
    LogReset()
    while 1:
        if time.time() > transaction_time:
            Log("Is time to gogogo !!!")
            Log(_D(time.time()))
            doAction()
            exit("End!!!")
        pass


    

Published by 扁豆子 on the FMZ strategy square · last modified 2021-03-23. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a tested, production-ready module from strategies/BSC-Transaction.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open