def test_native_asset(self): asset_1 = Asset("PAYDEX") asset_2 = Asset.native() assert asset_1 == asset_2 assert asset_1.code == "PAYDEX" assert asset_1.issuer is None assert asset_1.type == "native" assert asset_1.to_dict() == {"type": "native"}
def test_equals(self): assert Asset( "XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" ) == Asset("XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY") assert Asset( "XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" ) != Asset("USD", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY") assert (Asset( "XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY") != "BAD TYPE")
def test_credit_alphanum12_asset(self): code = "Banana" issuer = "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" type = "credit_alphanum12" asset = Asset(code, issuer) assert asset.code == code assert asset.issuer == issuer assert asset.type == type assert asset.to_dict() == { "type": type, "code": code, "issuer": issuer }
def test_non_native_asset_without_issuer_raise(self, code): with pytest.raises( AssetIssuerInvalidError, match= "The issuer cannot be `None` except for the native asset.", ): Asset(code)
def test_already_signed_raise(self): # GDF5O4OWEMVBY5FLDHWA5RZTYSV2U276XGKZZ6VSHDDR3THSQ6OQS7UM source = Keypair.from_secret( "SCCS5ZBI7WVIJ4SW36WGOQQIWJYCL3VOAULSXX3FB57USIO25EDOYQHH") destination = "GDJJRRMBK4IWLEPJGIE6SXD2LP7REGZODU7WDC3I2D6MR37F4XSHBKX2" amount = "1000.0" sequence = 1 memo = IdMemo(100) fee = 200 asset = Asset.native() time_bounds = TimeBounds(12345, 56789) ops = [ Payment(destination, asset, amount), ManageData("hello", "world") ] tx = Transaction(source, sequence, fee, ops, memo, time_bounds) te = TransactionEnvelope(tx, Network.PUBLIC_NETWORK_PASSPHRASE) assert binascii.hexlify(te.hash()).decode() == te.hash_hex() # te.sign(source) te.sign("SCCS5ZBI7WVIJ4SW36WGOQQIWJYCL3VOAULSXX3FB57USIO25EDOYQHH") hashx = bytes.fromhex( "94e8223a518ac16a8cb110ab1952ef14da2c10b264645c38c8b3d82bd2b20000") te.sign_hashx(hashx) with pytest.raises(SignatureExistError, match="The keypair has already signed."): te.sign(source) with pytest.raises(SignatureExistError, match="The preimage has already signed."): te.sign_hashx(hashx)
def test_to_xdr(self): # GDF5O4OWEMVBY5FLDHWA5RZTYSV2U276XGKZZ6VSHDDR3THSQ6OQS7UM source = Keypair.from_secret( "SCCS5ZBI7WVIJ4SW36WGOQQIWJYCL3VOAULSXX3FB57USIO25EDOYQHH") destination = "GDJJRRMBK4IWLEPJGIE6SXD2LP7REGZODU7WDC3I2D6MR37F4XSHBKX2" amount = "1000.0" sequence = 1 memo = IdMemo(100) fee = 200 asset = Asset.native() time_bounds = TimeBounds(12345, 56789) ops = [ Payment(destination, asset, amount), ManageData("hello", "world") ] tx = Transaction(source, sequence, fee, ops, memo, time_bounds) te = TransactionEnvelope(tx, Network.PUBLIC_NETWORK_PASSPHRASE) assert binascii.hexlify(te.hash()).decode() == te.hash_hex() te.sign(source) hashx = bytes.fromhex( "94e8223a518ac16a8cb110ab1952ef14da2c10b264645c38c8b3d82bd2b20000") te.sign_hashx(hashx) te_xdr = "AAAAAMvXcdYjKhx0qxnsDsczxKuqa/65lZz6sjjHHczyh50JAAAAyAAAAAAAAAABAAAAAQAAAAAAADA5AAAAAAAA3dUAAAACAAAAAAAAAGQAAAACAAAAAAAAAAEAAAAA0pjFgVcRZZHpMgnpXHpb/xIbLh0/YYto0PzI7+Xl5HAAAAAAAAAAAlQL5AAAAAAAAAAACgAAAAVoZWxsbwAAAAAAAAEAAAAFd29ybGQAAAAAAAAAAAAAAvKHnQkAAABAM4dg0J1LEFBmbDESJ5d+60WCuZC8lnA80g45qyEgz2oRBSNw1mOfZETnL/BgrebkG/K03oI2Wqcs9lvDKrDGDE0sOBsAAAAglOgiOlGKwWqMsRCrGVLvFNosELJkZFw4yLPYK9KyAAA=" assert te.to_xdr() == te_xdr restore_te = TransactionEnvelope.from_xdr( te_xdr, Network.PUBLIC_NETWORK_PASSPHRASE) assert restore_te.to_xdr() == te_xdr
def test_invalid_code(self, code): with pytest.raises( AssetCodeInvalidError, match= r"Asset code is invalid \(maximum alphanumeric, 12 characters at max\).", ): Asset(code, "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ")
def test_from_xdr_object_alphanum12(self): code = "Banana" issuer = "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" type = "credit_alphanum12" x = Xdr.nullclass() x.assetCode = bytearray(code, "ascii") + b"\x00" * 6 x.issuer = Keypair.from_public_key(issuer).xdr_account_id() xdr_type = Xdr.const.ASSET_TYPE_CREDIT_ALPHANUM12 xdr = Xdr.types.Asset(type=xdr_type, alphaNum12=x) asset = Asset.from_xdr_object(xdr) assert asset.code == code assert asset.issuer == issuer assert asset.type == type
from paydex_base_sdk.asset import Asset from paydex_base_sdk.keypair import Keypair from paydex_base_sdk.network import Network from paydex_base_sdk.server import Server from paydex_base_sdk.transaction_builder import TransactionBuilder server = Server(horizon_url="https://testhorizon.paydex.io//") source_keypair = Keypair.from_secret( "SA6XHAH4GNLRWWWF6TEVEWNS44CBNFAJWHWOPZCVZOUXSQA7BOYN7XHC") source_account = server.load_account(account_id=source_keypair.public_key) path = [ Asset("USD", "GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB"), Asset("EUR", "GDTNXRLOJD2YEBPKK7KCMR7J33AAG5VZXHAJTHIG736D6LVEFLLLKPDL") ] transaction = TransactionBuilder( source_account=source_account, network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE, base_fee=100) \ .append_path_payment_op(destination="GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB", send_code="PAYDEX", send_issuer=None, send_max="1000", dest_code="GBP", dest_issuer="GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW", dest_amount="5.50", path=path) \ .set_timeout(30) \ .build() transaction.sign(source_keypair) response = server.submit_transaction(transaction)
class TestAsset: @pytest.mark.parametrize("code", ["XCN", "Banana"]) def test_non_native_asset_without_issuer_raise(self, code): with pytest.raises( AssetIssuerInvalidError, match= "The issuer cannot be `None` except for the native asset.", ): Asset(code) @pytest.mark.parametrize("code", ["", "1234567890123", "ab_"]) def test_invalid_code(self, code): with pytest.raises( AssetCodeInvalidError, match= r"Asset code is invalid \(maximum alphanumeric, 12 characters at max\).", ): Asset(code, "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ") @pytest.mark.parametrize( "issuer", [ "", "GCEZWKCA5", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS67BAD" ], ) def test_invalid_issuer(self, issuer): with pytest.raises(AssetIssuerInvalidError, match="The issuer should be a correct public key."): Asset("XCN", issuer) def test_native_asset(self): asset_1 = Asset("PAYDEX") asset_2 = Asset.native() assert asset_1 == asset_2 assert asset_1.code == "PAYDEX" assert asset_1.issuer is None assert asset_1.type == "native" assert asset_1.to_dict() == {"type": "native"} def test_credit_alphanum4_asset(self): code = "XCN" issuer = "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" type = "credit_alphanum4" asset = Asset(code, issuer) assert asset.code == code assert asset.issuer == issuer assert asset.type == type assert asset.to_dict() == { "type": type, "code": code, "issuer": issuer } def test_credit_alphanum12_asset(self): code = "Banana" issuer = "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" type = "credit_alphanum12" asset = Asset(code, issuer) assert asset.code == code assert asset.issuer == issuer assert asset.type == type assert asset.to_dict() == { "type": type, "code": code, "issuer": issuer } def test_set_type_raise(self): asset = Asset.native() with pytest.raises(AttributeError, match="Asset type is immutable."): asset.type = "credit_alphanum4" def test_equals(self): assert Asset( "XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" ) == Asset("XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY") assert Asset( "XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" ) != Asset("USD", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY") assert (Asset( "XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY") != "BAD TYPE") @pytest.mark.parametrize( "asset, xdr", [ pytest.param(Asset.native(), "AAAAAA==", id="native"), pytest.param( Asset( "XCN", "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" ), "AAAAAVhDTgAAAAAAm466+JY4VR3PnqT3QyBxEGuHqw4ts9abdaU4InL3Wdg=", id="alphanum4", ), pytest.param( Asset( "CATCOIN", "GDJVFDG5OCW5PYWHB64MGTHGFF57DRRJEDUEFDEL2SLNIOONHYJWHA3Z", ), "AAAAAkNBVENPSU4AAAAAAAAAAADTUozdcK3X4scPuMNM5il78cYpIOhCjIvUltQ5zT4TYw==", id="alphanum12", ), ], ) def test_to_xdr_object(self, asset, xdr): assert asset.to_xdr_object().to_xdr() == xdr def test_from_xdr_object_native(self): xdr_type = Xdr.const.ASSET_TYPE_NATIVE xdr = Xdr.types.Asset(type=xdr_type) asset = Asset.from_xdr_object(xdr) assert asset.is_native() def test_from_xdr_object_alphanum4(self): code = "XCN" issuer = "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" type = "credit_alphanum4" x = Xdr.nullclass() x.assetCode = bytearray(code, "ascii") + b"\x00" x.issuer = Keypair.from_public_key(issuer).xdr_account_id() xdr_type = Xdr.const.ASSET_TYPE_CREDIT_ALPHANUM4 xdr = Xdr.types.Asset(type=xdr_type, alphaNum4=x) asset = Asset.from_xdr_object(xdr) assert asset.code == code assert asset.issuer == issuer assert asset.type == type def test_from_xdr_object_alphanum12(self): code = "Banana" issuer = "GCNY5OXYSY4FKHOPT2SPOQZAOEIGXB5LBYW3HVU3OWSTQITS65M5RCNY" type = "credit_alphanum12" x = Xdr.nullclass() x.assetCode = bytearray(code, "ascii") + b"\x00" * 6 x.issuer = Keypair.from_public_key(issuer).xdr_account_id() xdr_type = Xdr.const.ASSET_TYPE_CREDIT_ALPHANUM12 xdr = Xdr.types.Asset(type=xdr_type, alphaNum12=x) asset = Asset.from_xdr_object(xdr) assert asset.code == code assert asset.issuer == issuer assert asset.type == type
def test_set_type_raise(self): asset = Asset.native() with pytest.raises(AttributeError, match="Asset type is immutable."): asset.type = "credit_alphanum4"
def test_invalid_issuer(self, issuer): with pytest.raises(AssetIssuerInvalidError, match="The issuer should be a correct public key."): Asset("XCN", issuer)
def test_from_xdr_object_native(self): xdr_type = Xdr.const.ASSET_TYPE_NATIVE xdr = Xdr.types.Asset(type=xdr_type) asset = Asset.from_xdr_object(xdr) assert asset.is_native()
def test_endpoint(self): horizon_url = "https://testhorizon.paydex.io/" client = RequestsClient() with Server(horizon_url, client) as server: assert server.accounts() == AccountsCallBuilder( horizon_url, client) assert server.assets() == AssetsCallBuilder(horizon_url, client) assert server.data( "GDV6FVHPY4JH7EEBSJYPQQYZA3OC6TKTM2TAXRHWT4EEL7BJ2BTDQT5D", "hello") == DataCallBuilder( horizon_url, client, "GDV6FVHPY4JH7EEBSJYPQQYZA3OC6TKTM2TAXRHWT4EEL7BJ2BTDQT5D", "hello", ) assert server.effects() == EffectsCallBuilder(horizon_url, client) assert server.fee_stats() == FeeStatsCallBuilder( horizon_url, client) assert server.ledgers() == LedgersCallBuilder(horizon_url, client) assert server.offers() == OffersCallBuilder(horizon_url, client) assert server.operations() == OperationsCallBuilder( horizon_url, client) buying = Asset.native() selling = Asset( "MOE", "GDV6FVHPY4JH7EEBSJYPQQYZA3OC6TKTM2TAXRHWT4EEL7BJ2BTDQT5D") assert server.orderbook(buying, selling) == OrderbookCallBuilder( horizon_url, client, buying, selling) source_account = "GABUVMDURJFF477AEDAXOG5TL7JBHGDAKJQLH5K6FB5QONMLEV52C6IO" destination_account = ( "GDV6FVHPY4JH7EEBSJYPQQYZA3OC6TKTM2TAXRHWT4EEL7BJ2BTDQT5D") destination_asset = Asset.native() destination_amount = "100.0" assert server.paths( source_account, destination_account, destination_asset, destination_amount, ) == PathsCallBuilder( horizon_url, client, source_account, destination_account, destination_asset, destination_amount, ) source = "GAYSHLG75RPSMXWJ5KX7O7STE6RSZTD6NE4CTWAXFZYYVYIFRUVJIBJH" destination_asset = Asset( "EUR", "GDSBCQO34HWPGUGQSP3QBFEXVTSR2PW46UIGTHVWGWJGQKH3AFNHXHXN") destination_amount = "20.0" assert server.strict_receive_paths( source, destination_asset, destination_amount) == StrictReceivePathsCallBuilder( horizon_url, client, source, destination_asset, destination_amount) source_asset = Asset( "EUR", "GDSBCQO34HWPGUGQSP3QBFEXVTSR2PW46UIGTHVWGWJGQKH3AFNHXHXN") source_amount = "10.25" destination = "GARSFJNXJIHO6ULUBK3DBYKVSIZE7SC72S5DYBCHU7DKL22UXKVD7MXP" assert server.strict_send_paths( source_asset, source_amount, destination) == StrictSendPathsCallBuilder( horizon_url, client, source_asset, source_amount, destination) assert server.payments() == PaymentsCallBuilder( horizon_url, client) assert server.root() == RootCallBuilder(horizon_url, client) base = Asset.native() counter = Asset( "MOE", "GDV6FVHPY4JH7EEBSJYPQQYZA3OC6TKTM2TAXRHWT4EEL7BJ2BTDQT5D") resolution = 3600000 start_time = 1565272000000 end_time = 1565278000000 offset = 3600000 assert server.trade_aggregations( base, counter, resolution, start_time, end_time, offset) == TradeAggregationsCallBuilder( horizon_url, client, base, counter, resolution, start_time, end_time, offset, ) assert server.trades() == TradesCallBuilder(horizon_url, client) assert server.transactions() == TransactionsCallBuilder( horizon_url, client)