def __init__(self): self.neb = Neb("https://testnet.nebulas.io") #account & address self.from_account = Account( "6c41a31b4e689e1441c930ce4c34b74cc037bd5e68bbd6878adb2facf62aa7f3") self.from_addr = self.from_account.get_address_obj() #block height self.height_begin = 0 self.height_next = 0 #time_skip, 300seconds self.time_skip = 300 #times checking the balance,3 times one day self.check_times = 3 #period height self.period_height = 6000 #contract address self.distribute = "n1sZLHKWuXAz13oLyF775c38Z9PcR4bTXbk" self.staking_proxy = 'n1pff2q7R3bz3vu3SdEu4PvmJEJHxijJmEF'
# @File : TransactionExample.py # @Software: PyCharm from nebpysdk.src.account.Account import Account from nebpysdk.src.core.Address import Address from nebpysdk.src.core.Transaction import Transaction from nebpysdk.src.core.TransactionBinaryPayload import TransactionBinaryPayload from nebpysdk.src.core.TransactionCallPayload import TransactionCallPayload from nebpysdk.src.client.Neb import Neb import json neb = Neb("https://testnet.nebulas.io") keyJson = '{"version":4,"id":"814745d0-9200-42bd-a4df-557b2d7e1d8b","address":"n1H2Yb5Q6ZfKvs61htVSV4b1U2gr2GA9vo6","crypto":{"ciphertext":"fb831107ce71ed9064fca0de8d514d7b2ba0aa03aa4fa6302d09fdfdfad23a18","cipherparams":{"iv":"fb65caf32f4dbb2593e36b02c07b8484"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"dddc4f9b3e2079b5cc65d82d4f9ecf27da6ec86770cb627a19bc76d094bf9472","n":4096,"r":8,"p":1},"mac":"1a66d8e18d10404440d2762c0d59d0ce9e12a4bbdfc03323736a435a0761ee23","machash":"sha3256"}}' password = '******' # prepare from&to addr from_account = Account.from_key(keyJson, bytes(password.encode())) from_addr = from_account.get_address_obj() to_addr = Address.parse_from_string("n1JmhE82GNjdZPNZr6dgUuSfzy2WRwmD9zy") print("from_addr", from_addr.string()) print("to_addr ", to_addr.string()) # prepare transaction, get nonce first resp = neb.api.getAccountState(from_addr.string()).text print(resp) resp_json = json.loads(resp) print(resp_json) nonce = int(resp_json['result']['nonce']) chain_id = 1001 # PayloadType
# -*- coding: utf-8 -*- # @Time : 2018/6/6 下午3:30 # @Author : GuoXiaoMin # @File : AccountExample.py # @Software: PyCharm from nebpysdk.src.account.Account import Account # generate a new account account = Account.new_account() # export account account_json = account.to_key("passphrase") print(account_json) # load account account = Account.from_key(account_json, "passphrase".encode()) print(account.get_address_str()) print(account.get_private_key()) print(account.get_public_key())
def newAccount(json_list, password_list, addrno, amount): # generate a new account account = Account() # export account password = ''.join(random.sample(string.ascii_letters + string.digits, 10)) account_json = account.to_key(bytes(password.encode())) print(account_json) json_list[addrno] = account_json password_list[addrno] = password # load account account = Account.from_key(account_json, bytes(password.encode())) print(account.get_address_str()) print(account.get_private_key()) print(account.get_public_key()) neb = Neb("https://mainnet.nebulas.io") keyJson = '{"version": 4, "id": "661f6b70-b9b3-11e9-9fee-8c859052a951", "address": "n1ae3JErG9V779Tnx8jWy11JYnaBiN5Y5S9", "crypto": {"ciphertext": "4ebba0473c262492bbb4013326e54ce06bf0595acfe93b54aefa04dca03b8cf4", "cipherparams": {"iv": "9e702902fddb5679ca996caa2fb52faf"}, "cipher": "aes-128-ctr", "kdf": "scrypt", "kdfparams": {"dklen": 32, "salt": "0f48ca8c4b5267061aa9f619db5fd076a74fc4862fd69ca99d7aa8667c323271", "n": 4096, "r": 8, "p": 1}, "mac": "468c115d184c999463de48c5a6f361005e86dae956cfb544bd6795e201449b40", "machash": "sha3256"}}' password = "******" # prepare from&to addr from_account = Account.from_key(keyJson, bytes(password.encode())) from_addr = from_account.get_address_obj() to_addr = Address.parse_from_string(account.get_address_str()) print("from_addr", from_addr.string()) print("to_addr ", to_addr.string()) # prepare transaction, get nonce first resp = neb.api.getAccountState(from_addr.string()).text print(resp) resp_json = json.loads(resp) print(resp_json) nonce = int(resp_json['result']['nonce']) chain_id = 1 # PayloadType payload_type = Transaction.PayloadType("binary") # payload payload = TransactionBinaryPayload("binary").to_bytes() # gasPrice gas_price = 20000000000 # gasLimit gas_limit = 60000 # binary transaction example tx = Transaction(chain_id, from_account, to_addr, amount, nonce + 1, payload_type, payload, gas_price, gas_limit) tx.calculate_hash() tx.sign_hash() res = neb.api.sendRawTransaction(tx.to_proto()).text print(res) obj = json.loads(res) txhash = obj["result"]["txhash"] while (True): try: res = neb.api.getTransactionReceipt(txhash).text obj = json.loads(res) status = obj["result"]["status"] except: continue if status != 2: print(res) break else: time.sleep(1) print("Waiting the transaction to be confirmed.") return json_list, password_list
# -*- coding: utf-8 -*- # @Time : 2018/6/6 下午3:30 # @Author : GuoXiaoMin # @File : AccountExample.py # @Software: PyCharm from nebpysdk.src.account.Account import Account # generate a new account # account = Account.new_account() account = Account.new_account() print("new account", account.get_address_str()) account = Account() print("new account", account.get_address_str()) account = Account.new_account("6c41a31b4e689e1441c930ce4c34b74cc037bd5e68bbd6878adb2facf62aa7f3") print("new account with given priv_key", account.get_address_str()) account = Account.new_account(bytes.fromhex("6c41a31b4e689e1441c930ce4c34b74cc037bd5e68bbd6878adb2facf62aa7f3")) print("new account with given priv_key", account.get_address_str()) account = Account("6c41a31b4e689e1441c930ce4c34b74cc037bd5e68bbd6878adb2facf62aa7f3") print("new account with given priv_key", account.get_address_str()) # export account account_json = account.to_key("passphrase") print(account_json) # load account account = Account.from_key(account_json, "passphrase".encode())
class Call_trigger: def __init__(self): self.neb = Neb("https://testnet.nebulas.io") #account & address self.from_account = Account( "6c41a31b4e689e1441c930ce4c34b74cc037bd5e68bbd6878adb2facf62aa7f3") self.from_addr = self.from_account.get_address_obj() #block height self.height_begin = 0 self.height_next = 0 #time_skip, 300seconds self.time_skip = 300 #times checking the balance,3 times one day self.check_times = 3 #period height self.period_height = 6000 #contract address self.distribute = "n1sZLHKWuXAz13oLyF775c38Z9PcR4bTXbk" self.staking_proxy = 'n1pff2q7R3bz3vu3SdEu4PvmJEJHxijJmEF' def get_nonce(self): # get nonce resp = self.neb.api.getAccountState(self.from_addr.string()).text print(resp) resp_json = json.loads(resp) print(resp_json) nonce = int(resp_json['result']['nonce']) + int( resp_json['result']['pending']) return nonce def getReceipt(self, txhash): while (True): try: res = self.neb.api.getTransactionReceipt(txhash).text obj = json.loads(res) status = obj["result"]["status"] except: continue if status != 2: return res else: time.sleep(5) print("Waiting the transaction to be confirmed.") def call_contract(self, func, args, contract_addr): # payload payload = TransactionCallPayload(func, args).to_bytes() # PayloadType payload_type = Transaction.PayloadType("call") # gasPrice gas_price = 20000000000 # gasLimit gas_limit = 100000 # prepare to addr to_addr = Address.parse_from_string(contract_addr) print("from_addr", self.from_addr.string()) print("to_addr ", contract_addr) #nonce nonce = self.get_nonce() # calls chain_id = 1001 tx = Transaction(chain_id, self.from_account, to_addr, 0, nonce + 1, payload_type, payload, gas_price, gas_limit) tx.calculate_hash() tx.sign_hash() res = self.neb.api.sendRawTransaction(tx.to_proto()).text print(res) obj = json.loads(res) txhash = obj["result"]["txhash"] return txhash def check_balance(self): #nonce nonce = self.get_nonce() #get current page count re1 = self.neb.api.call(self.from_addr.string(), self.staking_proxy, "0", nonce + 1, "200000", "200000", { 'function': 'getCurrentPageCount', 'args': '[]' }).text print(re1) obj = json.loads(re1) pages = int(obj['result']['result']) # check for page in range(pages): result = self.neb.api.call(self.from_addr.string(), self.staking_proxy, "0", nonce + 1, "200000", "200000", { 'function': 'check', 'args': '[%d]' % page }).text print(result) def calculate(self, sessionid): #nonce nonce = self.get_nonce() #calculate print(sessionid) txhash = self.call_contract("calculateTotalValue", "[%s]" % str(sessionid), self.staking_proxy) res = self.getReceipt(txhash) print(res) obj = json.loads(res) status = obj["result"]["status"] if status == 1: hasNext = obj["result"]['execute_result']["hasNext"] sessionid = obj['result']['execute_result']['sessionid'] if hasNext: # next calculate self.calculate(sessionid) def distribute_trigger(self): #call the trigger txhash = self.call_contract("trigger", "[]", self.distribute) res = self.getReceipt(txhash) print(res) obj = json.loads(res) status = obj["result"]["status"] if status == 1: hasNext = obj["result"]['execute_result']["hasNext"] if hasNext: self.distribute_trigger() def daily_timer(self): #time_skip, 300seconds, 288 times a day seconds_one_day = 24 * 60 * 60 times_one_day = int(seconds_one_day / self.time_skip) #check the balance possibility = self.check_times / times_one_day rand = 0 #random.random() if rand < possibility: res = self.check_balance() #block height now results = self.neb.api.getNebState().text obj = json.loads(results) height_now = int(obj["result"]["height"]) print("height now: %d, height next: %d" % (height_now, self.height_next)) #Run the trigger self.height_next = 0 if height_now > self.height_next: #calculate self.calculate('null') #distribute self.distribute_trigger() #change the height_next self.height_next += self.period_height #call next Timer print('curreent threading:{}'.format(threading.activeCount())) timer = threading.Timer(self.time_skip, self.daily_timer) timer.start() def core(self): #block height now and height of next circle results = self.neb.api.getNebState().text obj = json.loads(results) self.height_begin = int(obj["result"]["height"]) self.height_next = self.height_begin + self.period_height threading.Timer(1, self.daily_timer).start()