def test_sdk_create_success(setup, test_sdk): # test defaults from stellar_base.horizon import HORIZON_LIVE, HORIZON_TEST sdk = kin.SDK() assert sdk.horizon.horizon_uri == HORIZON_LIVE sdk = kin.SDK(network='TESTNET') assert sdk.horizon.horizon_uri == HORIZON_TEST # test test_sdk fixture assert test_sdk.horizon assert test_sdk.horizon.horizon_uri == setup.horizon_endpoint_uri assert test_sdk.network == setup.network assert test_sdk.base_keypair.verifying_key == setup.sdk_keypair.verifying_key assert test_sdk.base_keypair.signing_key == setup.sdk_keypair.signing_key assert test_sdk.channel_manager
def make_payment(address, amount): sdk = kin.SDK( horizon_endpoint_uri='https://horizon-playground.kininfrastructure.com', network='Kin Playground Network ; June 2018', secret_key=get_ssm_parameter('/config/stage/stellar/base-seed')) tx_hash = sdk.send_kin(address, amount, memo_text='testmemo') return tx_hash
def test_sdk(setup): # create and fund sdk account Helpers.fund_account(setup, setup.sdk_keypair.address().decode()) # create and fund issuer account Helpers.fund_account(setup, setup.issuer_keypair.address().decode()) # create a trustline from sdk to asset Helpers.trust_asset(setup, setup.sdk_keypair.seed()) # init sdk sdk = kin.SDK(secret_key=setup.sdk_keypair.seed(), horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset) assert sdk print("""test_sdk fixture created with the following setup: type: {} network: {} sdk keypair: {} {} issuer keypair: {} {} asset: {} {} horizon uri: {} """.format(setup.type, setup.network, setup.sdk_keypair.seed(), setup.sdk_keypair.address().decode(), setup.issuer_keypair.seed(), setup.issuer_keypair.address().decode(), setup.test_asset.code, setup.test_asset.issuer, setup.horizon_endpoint_uri)) return sdk
def trust_kin(private_seed): kin_sdk = kin.SDK( secret_key=private_seed, horizon_endpoint_uri=config.STELLAR_HORIZON_URL, network=get_network_name(config.STELLAR_NETWORK), kin_asset=kin.Asset.native()) kin_asset = stellar_base.asset.Asset(config.STELLAR_KIN_TOKEN_NAME, config.STELLAR_KIN_ISSUER_ADDRESS) kin_sdk._trust_asset(kin_asset)
def test_sdk_create_fail(): # bad endpoint with pytest.raises(kin.SdkConfigurationError, match='cannot connect to horizon'): kin.SDK(horizon_endpoint_uri='bad') with pytest.raises(kin.SdkConfigurationError, match='cannot connect to horizon'): kin.SDK(horizon_endpoint_uri='http://localhost:666') # bad seeds (without Nick Cave) with pytest.raises(kin.SdkConfigurationError, match='invalid base seed'): kin.SDK(base_seed='bad') keypair = Keypair.random() with pytest.raises(kin.SdkConfigurationError, match='invalid channel seed'): kin.SDK(base_seed=keypair.seed(), channel_seeds=['bad'])
def test_sdk_create_fail(setup, helpers, test_sdk): with pytest.raises(ValueError, match='invalid secret key: bad'): kin.SDK(secret_key='bad', horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset) keypair = Keypair.random() secret_key = keypair.seed() address = keypair.address().decode() with pytest.raises(ValueError, match='invalid channel key: bad'): kin.SDK(secret_key=secret_key, channel_secret_keys=['bad'], horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset) # wallet account does not exist with pytest.raises(kin.AccountNotFoundError): kin.SDK(secret_key=secret_key, horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset) helpers.fund_account(setup, address) # wallet account exists but not yet activated with pytest.raises(kin.AccountNotActivatedError): kin.SDK(secret_key=secret_key, horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset) helpers.trust_asset(setup, secret_key) channel_keypair = Keypair.random() channel_secret_key = channel_keypair.seed() # channel account does not exist with pytest.raises(kin.AccountNotFoundError): kin.SDK(secret_key=secret_key, channel_secret_keys=[channel_secret_key], horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset) # bad Horizon endpoint with pytest.raises(kin.NetworkError): kin.SDK(secret_key=secret_key, horizon_endpoint_uri='bad', network=setup.network, kin_asset=setup.test_asset) # no Horizon on endpoint with pytest.raises(kin.NetworkError): kin.SDK(secret_key=secret_key, horizon_endpoint_uri='http://localhost:666', network=setup.network, kin_asset=setup.test_asset)
def test_sdk_create_success(): keypair = Keypair.random() sdk = kin.SDK(base_seed=keypair.seed()) assert sdk assert sdk.horizon assert sdk.network == 'PUBLIC' assert sdk.base_keypair.verifying_key == keypair.verifying_key assert sdk.base_keypair.signing_key == keypair.signing_key assert sdk.channel_manager
def main(): global sdk primary, channels = get_seeds() NETWORKS['CUSTOM'] = os.environ['NETWORK_PASSPHRASE'] sdk = kin.SDK(network='CUSTOM', secret_key=primary, channel_secret_keys=channels, horizon_endpoint_uri=os.environ['HORIZON_ENDPOINT'], kin_asset=Asset('KIN', os.environ['KIN_ISSUER']))
def test_channels(setup): # prepare channel accounts channel_keypairs = [ Keypair.random(), Keypair.random(), Keypair.random(), Keypair.random() ] channel_seeds = [ channel_keypair.seed() for channel_keypair in channel_keypairs ] channel_addresses = [ channel_keypair.address().decode() for channel_keypair in channel_keypairs ] for channel_address in channel_addresses: fund(setup, channel_address) # init sdk with these channels sdk = kin.SDK(base_seed=setup.sdk_keypair.seed(), horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, channel_seeds=channel_seeds) assert sdk assert sdk.channel_manager assert sdk.channel_manager.channel_builders.qsize() == len( channel_keypairs) def channel_worker(): # create an account using a channel address = Keypair.random().address().decode() tx_hash = sdk.create_account(address, starting_balance=100) assert tx_hash sleep(1) tx_data = sdk.get_transaction_data(tx_hash) assert tx_data # transaction envelope source is some channel account assert tx_data.source_account in channel_addresses # operation source is the base account assert tx_data.operations[0].source_account == sdk.get_address() # now issue parallel transactions threads = [] for channel_keypair in channel_keypairs: t = threading.Thread(target=channel_worker) threads.append(t) for t in threads: t.start() # wait for all to finish for t in threads: t.join()
def test_get_status(setup, test_sdk): # bad Horizon endpoint sdk = kin.SDK(horizon_endpoint_uri='bad') status = sdk.get_status() assert status['horizon'] assert status['horizon']['online'] is False assert status['horizon']['error'].startswith( "Invalid URL 'bad': No schema supplied") # no Horizon on endpoint sdk = kin.SDK(horizon_endpoint_uri='http://localhost:666') status = sdk.get_status() assert status['horizon'] assert status['horizon']['online'] is False assert status['horizon']['error'].find('Connection refused') > 0 # success status = test_sdk.get_status() assert status['network'] == setup.network assert status['address'] == setup.sdk_keypair.address().decode() assert status['kin_asset'] assert status['kin_asset']['code'] == setup.test_asset.code assert status['kin_asset']['issuer'] == setup.test_asset.issuer assert status['horizon'] assert status['horizon']['uri'] == setup.horizon_endpoint_uri assert status['horizon']['online'] assert status['horizon']['error'] is None assert status['channels'] assert status['channels']['all'] == 1 assert status['channels']['free'] == 1 assert status['transport'] assert status['transport']['pool_size'] == sdk.horizon.pool_size assert status['transport']['num_retries'] == sdk.horizon.num_retries assert status['transport'][ 'request_timeout'] == sdk.horizon.request_timeout assert status['transport'][ 'retry_statuses'] == sdk.horizon.status_forcelist assert status['transport']['backoff_factor'] == sdk.horizon.backoff_factor
def test_sdk(setup): # create and fund sdk account fund(setup, setup.sdk_keypair.address().decode()) # create and fund issuer account fund(setup, setup.issuer_keypair.address().decode()) # override KIN with our test asset # TODO: does not work? kin.KIN_ASSET = setup.test_asset # init sdk sdk = kin.SDK(base_seed=setup.sdk_keypair.seed(), horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network) assert sdk return sdk
def test_sdk_not_configured(setup): sdk = kin.SDK(horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network) with pytest.raises(kin.SdkError, match='address not configured'): sdk.get_address() with pytest.raises(kin.SdkError, match='address not configured'): sdk.get_native_balance() with pytest.raises(kin.SdkError, match='address not configured'): sdk.get_kin_balance() with pytest.raises(kin.SdkError, match='address not configured'): sdk.create_account('address') with pytest.raises(kin.SdkError, match='address not configured'): sdk.monitor_kin_payments(None) with pytest.raises(kin.SdkError, match='address not configured'): sdk._trust_asset(Asset('TMP', 'tmp')) with pytest.raises(kin.SdkError, match='address not configured'): sdk._send_asset(Asset('TMP', 'tmp'), 'address', 1)
def test_sdk_not_configured(): sdk = kin.SDK() with pytest.raises(kin.SdkNotConfiguredError, match='address not configured'): sdk.get_address() with pytest.raises(kin.SdkNotConfiguredError, match='address not configured'): sdk.get_lumen_balance() with pytest.raises(kin.SdkNotConfiguredError, match='address not configured'): sdk.get_kin_balance() with pytest.raises(kin.SdkNotConfiguredError, match='address not configured'): sdk.create_account('address') with pytest.raises(kin.SdkNotConfiguredError, match='address not configured'): sdk.trust_asset(Asset('TMP', 'tmp')) with pytest.raises(kin.SdkNotConfiguredError, match='address not configured'): sdk.send_asset('address', Asset('TMP', 'tmp'), 1) with pytest.raises(kin.SdkNotConfiguredError, match='address not configured'): sdk.monitor_transactions(None)
import kin from stellar_base.network import NETWORKS from config import NETWORK, HORIZON_URL, KIN_ASSET, TEST_USER_SK, PUBLIC_KEY NETWORKS['CUSTOM'] = 'private testnet' sdk = kin.SDK(network=NETWORK, horizon_endpoint_uri=HORIZON_URL, secret_key=TEST_USER_SK, kin_asset=KIN_ASSET) # Get the address of my wallet account. The address is derived from the secret key the SDK was inited with. my_addr = sdk.get_address() print('Address: %s' % my_addr) native_balance = sdk.get_native_balance() # Get KIN balance of the SDK wallet kin_balance = sdk.get_kin_balance() print('Native balance: %r, KIN balance: %r' % (native_balance, kin_balance)) data = sdk.get_account_data(my_addr) tx_hash = sdk.send_kin(PUBLIC_KEY, 1, memo_text='for_tournament') print('tx_hash=%r' % tx_hash) def process_payment(address, tx_data):
print("stellar horizon: %s" % config.STELLAR_HORIZON_URL) # define an asset to forward to the SDK because sometimes we're using a custom issuer from stellar_base.asset import Asset kin_asset = Asset('KIN', config.STELLAR_KIN_ISSUER_ADDRESS) if config.STELLAR_NETWORK != 'TESTNET': log.info('starting the sdk in a private network') network = 'CUSTOM' NETWORKS[network] = config.STELLAR_NETWORK else: print('starting the sdk on the public testnet') network = config.STELLAR_NETWORK app.kin_sdk = kin.SDK(secret_key=base_seed, horizon_endpoint_uri=config.STELLAR_HORIZON_URL, network=network, channel_secret_keys=channel_seeds, kin_asset=kin_asset) # get (and print) the current balance for the account: from stellar_base.keypair import Keypair log.info( 'the current KIN balance on the base-seed: %s' % stellar.get_kin_balance(Keypair.from_seed(base_seed).address().decode())) # get (and print) the current balance for the account: log.info( 'the current XLM balance on the base-seed: %s' % stellar.get_xlm_balance(Keypair.from_seed(base_seed).address().decode())) for channel in channel_seeds: address = Keypair.from_seed(channel).address().decode()
def trust_kin(private_seed: str): sdk = kin.SDK(network='TESTNET', secret_key=private_seed, kin_asset=kin.Asset.native()) sdk._trust_asset(kin.KIN_ASSET_TEST)
import threading import time from datetime import datetime import kin from mongoengine import connect from stellar_base.network import NETWORKS from config import NETWORK, HORIZON_URL, KIN_ASSET, SECRET_KEY, MONGODB_URI, PUBLIC_KEY, TOURNEY_PAY_TIMEOUT from misc import logs from schema import Tourney, TourneyStatus, TourneyMemberED NETWORKS['CUSTOM'] = 'private testnet' sdk = kin.SDK(network=NETWORK, horizon_endpoint_uri=HORIZON_URL, secret_key=SECRET_KEY, kin_asset=KIN_ASSET) # TX_HASH = 'e3f4b6167243118d60284cd18c7d9e16be776a4cec0713516239d49c680928c7' # # tx_dat = sdk.get_transaction_data(TX_HASH) # # print(tx_dat) # def store_tourney_err(tourney, err_msg, status): logging.info(err_msg) tourney.status = status tourney.ended = datetime.utcnow() tourney.error_message = err_msg
def test_channels(setup, helpers): # prepare channel accounts channel_keypairs = [ Keypair.random(), Keypair.random(), Keypair.random(), Keypair.random() ] channel_keys = [ channel_keypair.seed() for channel_keypair in channel_keypairs ] channel_addresses = [ channel_keypair.address().decode() for channel_keypair in channel_keypairs ] for channel_address in channel_addresses: helpers.fund_account(setup, channel_address) # init sdk with these channels sdk = kin.SDK(secret_key=setup.sdk_keypair.seed(), channel_secret_keys=channel_keys, horizon_endpoint_uri=setup.horizon_endpoint_uri, network=setup.network, kin_asset=setup.test_asset) assert sdk assert sdk.channel_manager assert sdk.channel_manager.channel_builders.qsize() == len( channel_keypairs) thread_ex = [] def channel_worker(thread_ex_holder): try: # create an account using a channel address = Keypair.random().address().decode() tx_hash1 = sdk.create_account(address, starting_balance=100) assert tx_hash1 # send lumens tx_hash2 = sdk.send_native(address, 1) assert tx_hash2 # send more lumens tx_hash3 = sdk.send_native(address, 1) assert tx_hash3 sleep(1) # check transactions tx_data = sdk.get_transaction_data(tx_hash1) assert tx_data # transaction envelope source is some channel account assert tx_data.source_account in channel_addresses # operation source is the base account assert tx_data.operations[0].source_account == sdk.get_address() tx_data = sdk.get_transaction_data(tx_hash2) assert tx_data assert tx_data.source_account in channel_addresses assert tx_data.operations[0].source_account == sdk.get_address() tx_data = sdk.get_transaction_data(tx_hash3) assert tx_data assert tx_data.source_account in channel_addresses assert tx_data.operations[0].source_account == sdk.get_address() except Exception as e: thread_ex_holder.append(e) # now issue parallel transactions threads = [] for channel_keypair in channel_keypairs: t = threading.Thread(target=channel_worker, args=(thread_ex, )) threads.append(t) for t in threads: t.start() # wait for all to finish for t in threads: t.join() # check thread errors assert not thread_ex