Skip to content

Instantly share code, notes, and snippets.

@jsyqrt
Created August 7, 2019 08:27
Show Gist options
  • Save jsyqrt/bb7247842628e1bd9d7abd24074b5ac2 to your computer and use it in GitHub Desktop.
Save jsyqrt/bb7247842628e1bd9d7abd24074b5ac2 to your computer and use it in GitHub Desktop.
send raw transaction to cpchain testmainnet or mainnet.
import sys
from cpc_fusion import Web3 as w
def sendRawTransactionTestMainnet():
chainID = 42
endpoint = "http://13.229.143.32:8501"
myAddress = "abb528bffc707c2c507307e426ce810a7ad93ed6"
toAddress = "abb528bffc707c2c507307e426ce810a7ad93ed6"
keystoreFilePath = "/home/jsyqrt/go/src/bitbucket.org/cpchain/chain/examples/testmainnet/data/data22/keystore/key22"
password = "password"
value = 0
data = "你好,世界".encode()
cf = w(w.HTTPProvider(endpoint))
sendRawTransaction(cf, chainID, myAddress, toAddress, keystoreFilePath, password, value, data)
def sendRawTransactionMainnet():
chainID = 337
endpoint = "http://13.229.143.32:8501"
myAddress = "e3f205ad1f2f3ab6f8a3b70f52c4c52e36aa6c71"
toAddress = "e3f205ad1f2f3ab6f8a3b70f52c4c52e36aa6c71"
keystoreFilePath = "/home/ubuntu/"
password = "password"
value = 0
data = b""
cf = w(w.HTTPProvider(endpoint))
sendRawTransaction(cf, chainID, myAddress, toAddress, keystoreFilePath, password, value, data)
def sendRawTransaction(cf, chainID, myAddress, toAddress, keystoreFilePath, password, value, data):
# check balance
from_addr = cf.toChecksumAddress(myAddress)
print("from address", from_addr)
print("balance", cf.cpc.getBalance(from_addr))
# read keystore file
with open(keystoreFilePath) as keyfile:
encrypted_key = keyfile.read()
print("keystore file", encrypted_key)
# decrypt private key
privKey = cf.cpc.account.decrypt(encrypted_key, password)
print("private key", privKey)
# get toAddress
to_addr = cf.toChecksumAddress(toAddress)
print("to address", to_addr)
# get gas price
gas_price = cf.cpc.gasPrice
print("gasPrice", gas_price)
# get nonce
nonce = cf.cpc.getTransactionCount(from_addr)
print("nonce", nonce)
# compose the tx
tx_dict = dict(
type=0,
nonce=nonce,
gasPrice=gas_price,
gas=90000,
to=to_addr,
value=value,
data=data,
chainId=chainID,
)
# sign the tx with priv key
signed_txn = cf.cpc.account.signTransaction(tx_dict, privKey)
print("signed txn", signed_txn)
tx_hash = cf.cpc.sendRawTransaction(signed_txn.rawTransaction)
print("send tx", cf.toHex(tx_hash))
receipt = cf.cpc.waitForTransactionReceipt(tx_hash)
print("receipt", receipt)
if __name__ == '__main__':
sendRawTransactionTestMainnet()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment