Skip to content

Commit

Permalink
fix all examples to work on local node
Browse files Browse the repository at this point in the history
  • Loading branch information
ubergaijin committed Jan 11, 2019
1 parent 692d6af commit 9dbdc6b
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 649 deletions.
631 changes: 279 additions & 352 deletions .idea/workspace.xml

Large diffs are not rendered by default.

16 changes: 4 additions & 12 deletions examples/account.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import time
from base58 import b58decode, b58encode
from base58 import b58decode
from pyost.iost import IOST
from pyost.account import Account
from pyost.algorithm import Secp256k1, Ed25519
from pyost.signature import KeyPair
from pyost.transaction import TransactionError

if __name__ == '__main__':
iost = IOST('35.180.171.246:30002')
iost = IOST('localhost:30002')

acc_seckey = b58decode(b'58NCdrz3iUfqKnEk6AX57rGrv9qrvn8EXtiUvVXMLqkKJKSFuW6TR6iuuYBtjgzhwm9ew6e9Pjg3zx5n6ya9MHJ3')
acc_seckey = b58decode(b'1rANSfcRzr4HkhbUFZ7L1Zp69JZZHiDDq5v7dNSbbEqeU4jxy3fszV4HGiaLQEyqVpS1dKT9g7zCVRxBVzuiUzB')
acc_kp = KeyPair(Ed25519, acc_seckey)
acc = Account('iostsiri')
acc = Account('producer00001')
acc.add_key_pair(acc_kp, 'active')
acc.add_key_pair(acc_kp, 'owner')

# acc_seckey = b58decode(b'3weJNnPE16XDBncfZT68Jm13HQ68AqnvCjpNLZtVUV1FZyVQJBFpeP5TZhRhYTaDKjjpMoc7WE5V9mSayGTyCYN7')
# acc_kp = KeyPair(Ed25519, acc_seckey)
# acc = Account('iostsiri3')
# acc.add_key_pair(acc_kp, 'active')
# acc.add_key_pair(acc_kp, 'owner')
# print(acc)

print('Account Info:')
print(iost.get_account_info(acc.name))

Expand Down
31 changes: 12 additions & 19 deletions examples/buy_ram.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import time
from base58 import b58decode, b58encode
from base58 import b58decode
from pyost.iost import IOST
from pyost.account import Account
from pyost.algorithm import Ed25519
from pyost.signature import KeyPair
from pyost.transaction import TransactionError

if __name__ == '__main__':
iost = IOST('35.180.171.246:30002')
iost = IOST('localhost:30002')

acc_seckey = b58decode(b'58NCdrz3iUfqKnEk6AX57rGrv9qrvn8EXtiUvVXMLqkKJKSFuW6TR6iuuYBtjgzhwm9ew6e9Pjg3zx5n6ya9MHJ3')
acc_kp = KeyPair(Ed25519, acc_seckey)
acc = Account('iostsiri')
acc.add_key_pair(acc_kp, 'active')
acc.add_key_pair(acc_kp, 'owner')
account_seckey = b58decode(
b'4vZ8qw2MaGLVXsbW7TcyTDcEqrefAS34vuM1eJf7YrBL9Fpnq3LgRyDjnUfv7kjvPfsA5tQGnou3Bv2bYNXyorK1')
account_kp = KeyPair(Ed25519, account_seckey)
account = Account('testacc1')
account.add_key_pair(account_kp, 'active')
account.add_key_pair(account_kp, 'owner')

# acc_seckey = b58decode(b'3weJNnPE16XDBncfZT68Jm13HQ68AqnvCjpNLZtVUV1FZyVQJBFpeP5TZhRhYTaDKjjpMoc7WE5V9mSayGTyCYN7')
# acc_kp = KeyPair(Ed25519, acc_seckey)
# acc = Account('iostsiri3')
# acc.add_key_pair(acc_kp, 'active')
# acc.add_key_pair(acc_kp, 'owner')

print(f'Account RAM: {iost.get_account_info(acc.name).ram_info.available}')
print(f'Account RAM: {iost.get_account_info(account.name).ram_info.available}')

print(f'RAM price: {iost.get_ram_info().buy_price}')
tx = iost.create_call_tx('ram.iost', 'buy', acc.name, acc.name, 50000)
tx = iost.create_call_tx('ram.iost', 'buy', account.name, account.name, 50000)
tx.gas_limit = 1000000
acc.sign_publish(tx)
account.sign_publish(tx)

print('Waiting for transaction to be processed...')
try:
Expand All @@ -37,4 +30,4 @@
except RuntimeError as e:
print(f'ERROR: {e}')

print(f'Account RAM: {iost.get_account_info(acc.name).ram_info.available}')
print(f'Account RAM: {iost.get_account_info(account.name).ram_info.available}')
2 changes: 1 addition & 1 deletion examples/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from base64 import b64encode

from pyost.algorithm import Ed25519, Secp256k1
from pyost.signature import Signature, KeyPair
from pyost.signature import KeyPair

if __name__ == '__main__':
text = b'hello'
Expand Down
49 changes: 16 additions & 33 deletions examples/luckybet.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import sys
import subprocess
import os
import collections
import time
import re
import json
import argparse
import random
from base58 import b58decode
from multiprocessing.pool import ThreadPool
from pyost.iost import IOST
from pyost.account import Account, KeyPair
from pyost.algorithm import Ed25519
from pyost.transaction import TransactionError, Transaction, Action
from pyost.transaction import TransactionError
from pyost.contract import Contract

assert sys.version_info.major == 3
assert sys.version_info.minor >= 6

DEFAULT_EXPIRATION = 10
DEFAULT_GASLIMIT = 2000000
DEFAULT_GASLIMIT = 1000000
DEFAULT_GASRATIO = 1
DEFAULT_NODEIP = '35.180.171.246'
TESTID = 'iostsiri'
DEFAULT_NODEIP = 'localhost'
TESTID = 'producer00001'
initial_coin_of_bet_user = 5

iost = IOST(f'{DEFAULT_NODEIP}:30002', gas_limit=DEFAULT_GASLIMIT, gas_ratio=DEFAULT_GASRATIO,
Expand All @@ -37,12 +32,12 @@ def check_float_equal(a, b):
assert abs(a - b) < 1e-6, f"not equal {a} {b}"


def get_balance(account_name):
return iost.get_balance(account_name)
def get_balance(account):
return iost.get_balance(account.name)


def fetch_contract_state(cid, key):
return json.loads(iost.get_contract_storage(cid, key))['data']
return json.loads(iost.get_contract_storage(cid, key))


def publish_contract(js_file, js_abi_file, account):
Expand All @@ -59,40 +54,29 @@ def publish_contract(js_file, js_abi_file, account):
return json.loads(txr.returns[0])[0]
except TransactionError as e:
print(e)
return None
exit(1)


def init_account():
private_key = b58decode(b'58NCdrz3iUfqKnEk6AX57rGrv9qrvn8EXtiUvVXMLqkKJKSFuW6TR6iuuYBtjgzhwm9ew6e9Pjg3zx5n6ya9MHJ3')
private_key = b58decode(b'1rANSfcRzr4HkhbUFZ7L1Zp69JZZHiDDq5v7dNSbbEqeU4jxy3fszV4HGiaLQEyqVpS1dKT9g7zCVRxBVzuiUzB')
kp = KeyPair(Ed25519, private_key)
testid_account = Account(TESTID)
testid_account.add_key_pair(kp, 'active')
testid_account.add_key_pair(kp, 'owner')
iost.publisher = testid_account

# TODO uncomment
# print(iost.get_account_info(iost.publisher.name).ram_info)
# print(iost.get_account_info(iost.publisher.name).gas_info)
# iost.call('ram.iost', 'buy', testid_account.name, testid_account.name, 50000)
# iost.call('gas.iost', 'pledge', testid_account.name, testid_account.name, '100')
# iost.call('gas.iost', 'pledge', testid_account.name, testid_account.name, '30000')
# print(iost.get_account_info(iost.publisher.name).ram_info)
# print(iost.get_account_info(iost.publisher.name).gas_info)


def publish():
cid = 'Contract7fYG6eTZxiTj72cmABBbcSwcrQQQ4DbPDWwh63UfbktX'
# TODO uncomment this
# uploader_account = iost.new_account('sirilucky2', iost.publisher.name, 50000, 20, 0)
# print(uploader_account)
#
# # private_key = b58decode(b'3rDmfY3aFTUiMzbG7BkYmk7D5UKjSgKV46KnxibVYWCTxqRk5ZEXqYxAi4wv9yUdJUvJi2ZpHY6oHxoAf2CN8Zci')
# # kp = KeyPair(Ed25519, private_key)
# # uploader_account = Account('sirilucky2')
# # uploader_account.add_key_pair(kp, 'active')
# # uploader_account.add_key_pair(kp, 'owner')
#
# cid = publish_contract('contract/lucky_bet.js',
# 'contract/lucky_bet.js.abi', uploader_account)
uploader_account = iost.new_account(f'upacc{random.randint(0, 1000000)}', iost.publisher.name, 50000, 20, 0)
cid = publish_contract('contract/lucky_bet.js',
'contract/lucky_bet.js.abi', uploader_account)
return cid


Expand Down Expand Up @@ -129,8 +113,7 @@ def bet(idx):
lucky_number = lucky_numbers[idx]
bet_coin = bet_coins[idx]
nouce = ''
args = [bet_users[idx], lucky_number, bet_coin, nouce]
tx = iost.create_call_tx(cid, 'bet', args)
tx = iost.create_call_tx(cid, 'bet', bet_users[idx].name, lucky_number, bet_coin, nouce)
bet_users[idx].sign_publish(tx)
try:
txr = iost.send_and_wait_tx(tx)
Expand Down Expand Up @@ -162,15 +145,15 @@ def bet(idx):
all_lucky_bets = int(
sum([bet_coins[idx] for idx in range(bet_user_num) if lucky_numbers[idx] == final_lucky_number]))
for idx in range(bet_user_num):
real_reward = rewards[bet_users[idx]]
real_reward = rewards[bet_users[idx].name]
calculated_reward = 0 if lucky_numbers[idx] != final_lucky_number else total_coins_bet * 95 // 100 * bet_coins[
idx] // all_lucky_bets
check_float_equal(real_reward, calculated_reward)
assert win_user_num == bet_user_num // 10
# check balance of each user
for i in range(bet_user_num):
calculated_balance = initial_coin_of_bet_user - \
bet_coins[i] + rewards[bet_users[i]]
bet_coins[i] + rewards[bet_users[i].name]
# log(f'calculated_balance {calculated_balance} actual_balance {final_balances[i]}')
check_float_equal(calculated_balance, final_balances[i])
log('Congratulations! You have just run a smart contract on IOST!')
Expand Down
22 changes: 10 additions & 12 deletions examples/new_account.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
from base58 import b58decode, b58encode
from pyost.iost import IOST
from pyost.account import Account
Expand All @@ -7,28 +6,27 @@
from pyost.transaction import TransactionError

if __name__ == '__main__':
iost = IOST('35.180.171.246:30002')
iost = IOST('localhost:30002')

# Existing account that will pledge for the new account
acc1_seckey = b58decode(b'58NCdrz3iUfqKnEk6AX57rGrv9qrvn8EXtiUvVXMLqkKJKSFuW6TR6iuuYBtjgzhwm9ew6e9Pjg3zx5n6ya9MHJ3')
acc1_kp = KeyPair(Ed25519, acc1_seckey)
acc1 = Account('iostsiri')
acc1.add_key_pair(acc1_kp, 'active')
acc1.add_key_pair(acc1_kp, 'owner')
print(f'Account 1:\n{iost.get_account_info(acc1.name)}')
admin_seckey = b58decode(b'1rANSfcRzr4HkhbUFZ7L1Zp69JZZHiDDq5v7dNSbbEqeU4jxy3fszV4HGiaLQEyqVpS1dKT9g7zCVRxBVzuiUzB')
admin_kp = KeyPair(Ed25519, admin_seckey)
admin = Account('producer00001')
admin.add_key_pair(admin_kp, 'active')
admin.add_key_pair(admin_kp, 'owner')
print(f'Admin:\n{iost.get_account_info(admin.name)}')

# Create key pair for the new account
acc2_kp = KeyPair(Ed25519)
print('Do not forget to store the new account secret key:')
print(b58encode(acc2_kp.seckey))
acc2 = Account('iostsiri3')
acc2 = Account('testacc1')
acc2.add_key_pair(acc2_kp, 'active')
acc2.add_key_pair(acc2_kp, 'owner')

tx = iost.create_new_account_tx(acc2.name, acc1.name,
tx = iost.create_new_account_tx(acc2.name, admin.name,
acc2_kp.id, acc2_kp.id, 0, 100.0, 100.0)
tx.gas_limit = 1000000.0
acc1.sign_publish(tx)
admin.sign_publish(tx)

print('Waiting for transaction to be processed...')
try:
Expand Down
16 changes: 7 additions & 9 deletions examples/system_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@
import json
from pyost.iost import IOST
from pyost.account import Account
from pyost.algorithm import Secp256k1, Ed25519
from pyost.algorithm import Ed25519
from pyost.signature import KeyPair
from base58 import b58decode, b58encode
from pyost.transaction import TransactionError, TxReceipt
from base58 import b58decode

if __name__ == '__main__':
iost = IOST('35.180.171.246:30002', gas_limit=2000000.0, default_limit='1000')
iost = IOST('localhost:30002')

admin_seckey = b58decode(
b'58NCdrz3iUfqKnEk6AX57rGrv9qrvn8EXtiUvVXMLqkKJKSFuW6TR6iuuYBtjgzhwm9ew6e9Pjg3zx5n6ya9MHJ3')
admin_seckey = b58decode(b'1rANSfcRzr4HkhbUFZ7L1Zp69JZZHiDDq5v7dNSbbEqeU4jxy3fszV4HGiaLQEyqVpS1dKT9g7zCVRxBVzuiUzB')
admin_kp = KeyPair(Ed25519, admin_seckey)
admin = Account('iostsiri')
admin = Account('producer00001')
admin.add_key_pair(admin_kp, 'active')
admin.add_key_pair(admin_kp, 'owner')

iost.publisher = admin
iost.gas_limit = 1000000

hw_contract = '{"ID":"hw","info":{"lang":"javascript","version":"1.0.0","abi":[{"name":"hello"}, {"name":"can_update", "args": ["string"]}]},"code":"class Contract {init(){} hello(){return \\"world\\";} can_update(data){return true;}} module.exports = Contract;"}';

# Set code
# contract_id = 'ContractF2T8W8HnFDh3JdXT4fvXWTLKqhctZsBT6qEV3EVMsZ8V'
print('setting code...')
txr = iost.call('system.iost', 'SetCode', hw_contract)
contract_id = json.loads(txr.returns[0])[0]
Expand Down
Loading

0 comments on commit 9dbdc6b

Please sign in to comment.