Skip to content

Commit

Permalink
Port Solana tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeutin-ledger committed Jul 26, 2023
1 parent 65d5ad9 commit b3ddccb
Show file tree
Hide file tree
Showing 273 changed files with 67 additions and 504 deletions.
23 changes: 12 additions & 11 deletions test/python/apps/exchange_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from . import cal as cal
from .signing_authority import SigningAuthority, LEDGER_SIGNER

from ..utils import handle_lib_call_start_or_stop
from ..utils import handle_lib_call_start_or_stop, int_to_minimally_sized_bytes

# When adding a new test, have it prefixed by this string in order to have it automatically parametrized for currencies tests
TEST_METHOD_PREFIX="perform_test_"
Expand Down Expand Up @@ -57,7 +57,7 @@ def _perform_valid_exchange(self, subcommand, tx_infos, fees, ui_validation):
ex = ExchangeClient(self.backend, Rate.FIXED, subcommand)

# The partner we will perform the exchange with
partner = SigningAuthority(curve=get_partner_curve(subcommand), name="Default name")
partner = SigningAuthority(curve=get_partner_curve(subcommand), name=self.partner_name)

# Initialize a new transaction request
transaction_id = ex.init_transaction().data
Expand Down Expand Up @@ -105,7 +105,7 @@ def perform_valid_swap_from_custom(self, destination, send_amount, fees, memo, r
"payout_extra_id": b"", # Default
"currency_from": self.currency_ticker,
"currency_to": "ETH", # Default
"amount_to_provider": int.to_bytes(send_amount, length=8, byteorder='big'),
"amount_to_provider": int_to_minimally_sized_bytes(send_amount),
"amount_to_wallet": b"\246\333t\233+\330\000", # Default
}
self._perform_valid_exchange(SubCommand.SWAP, tx_infos, fees, ui_validation=ui_validation)
Expand All @@ -120,28 +120,28 @@ def perform_valid_swap_to_custom(self, destination, send_amount, fees, memo, ui_
"payout_extra_id": memo.encode(),
"currency_from": "ETH", # Default
"currency_to": self.currency_ticker,
"amount_to_provider": int.to_bytes(send_amount, length=8, byteorder='big'),
"amount_to_provider": int_to_minimally_sized_bytes(send_amount),
"amount_to_wallet": b"\246\333t\233+\330\000", # Default
}
self._perform_valid_exchange(SubCommand.SWAP, tx_infos, fees, ui_validation=ui_validation)

def perform_valid_fund_from_custom(self, destination, send_amount, fees):
tx_infos = {
"user_id": "Jon Wick", # Default
"account_name": "My account 00", # Default
"user_id": self.fund_user_id,
"account_name": self.fund_account_name,
"in_currency": self.currency_ticker,
"in_amount": int.to_bytes(send_amount, length=4, byteorder='big'),
"in_amount": int_to_minimally_sized_bytes(send_amount),
"in_address": destination,
}
self._perform_valid_exchange(SubCommand.FUND, tx_infos, fees, ui_validation=True)

def perform_valid_sell_from_custom(self, destination, send_amount, fees):
tx_infos = {
"trader_email": "john@doe.lost", # Default
"out_currency": "USD", # Default
"out_amount": {"coefficient": b"\x01", "exponent": 3}, # Default
"trader_email": self.sell_trader_email,
"out_currency": self.sell_out_currency,
"out_amount": self.sell_out_amount,
"in_currency": self.currency_ticker,
"in_amount": int.to_bytes(send_amount, length=4, byteorder='big'),
"in_amount": int_to_minimally_sized_bytes(send_amount),
"in_address": destination,
}
self._perform_valid_exchange(SubCommand.SELL, tx_infos, fees, ui_validation=True)
Expand Down Expand Up @@ -338,6 +338,7 @@ def perform_test_sell_wrong_amount(self):
# Remove prefix to have nice snapshots directories
ALL_TESTS = [str(i).replace(TEST_METHOD_PREFIX, '') for i in _all_test_methods_prefixed]
ALL_TESTS_EXCEPT_MEMO = [test for test in ALL_TESTS if not "memo" in test]
ALL_TESTS_EXCEPT_MEMO_AND_FEES = [test for test in ALL_TESTS if (not "memo" in test and not "fees" in test)]
SWAP_TESTS = [test for test in ALL_TESTS if "swap" in test]
FUND_TESTS = [test for test in ALL_TESTS if "fund" in test]
SELL_TESTS = [test for test in ALL_TESTS if "sell" in test]
3 changes: 3 additions & 0 deletions test/python/apps/solana_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def lamports_to_bytes(lamports: int) -> str:
FEES = sol_to_lamports(0.00000564)
FEES_BYTES = lamports_to_bytes(FEES)

FEES_2 = sol_to_lamports(0.0005543)
FEES_2_BYTES = lamports_to_bytes(FEES_2)


### Proposed foreign and owned addresses ###

Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
52 changes: 52 additions & 0 deletions test/python/test_solana.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

from .apps.exchange_test_runner import ExchangeTestRunner, ALL_TESTS_EXCEPT_MEMO_AND_FEES
from .apps.solana import SolanaClient, ErrorType
from .apps.solana_utils import SOL_PACKED_DERIVATION_PATH
from .apps.solana_cmd_builder import SystemInstructionTransfer, Message, verify_signature
from .apps import solana_utils as SOL

# A bit hacky but way less hassle than actually writing an actual address decoder
SOLANA_ADDRESS_DECODER = {
SOL.FOREIGN_ADDRESS: SOL.FOREIGN_PUBLIC_KEY,
SOL.FOREIGN_ADDRESS_2: SOL.FOREIGN_PUBLIC_KEY_2,
}

# ExchangeTestRunner implementation for Stellar
class SolanaTests(ExchangeTestRunner):
currency_ticker = "SOL"
valid_destination_1 = SOL.FOREIGN_ADDRESS
valid_destination_memo_1 = ""
valid_destination_2 = SOL.FOREIGN_ADDRESS_2
valid_destination_memo_2 = ""
valid_refund = SOL.OWNED_ADDRESS
valid_refund_memo = ""
valid_send_amount_1 = SOL.AMOUNT
valid_send_amount_2 = SOL.AMOUNT_2
valid_fees_1 = SOL.FEES
valid_fees_2 = SOL.FEES_2
fake_refund = SOL.FOREIGN_ADDRESS
fake_refund_memo = ""
fake_payout = SOL.FOREIGN_ADDRESS
fake_payout_memo = ""
signature_refusal_error_code = ErrorType.SOLANA_SUMMARY_FINALIZE_FAILED

partner_name = "Partner name"
fund_user_id = "Daft Punk"
fund_account_name = "Account 0"

def perform_final_tx(self, destination, send_amount, fees, memo):
decoded_destination = SOLANA_ADDRESS_DECODER[destination]
instruction: SystemInstructionTransfer = SystemInstructionTransfer(SOL.OWNED_PUBLIC_KEY, decoded_destination, send_amount)
message: bytes = Message([instruction]).serialize()
sol = SolanaClient(self.backend)
with sol.send_async_sign_message(SOL_PACKED_DERIVATION_PATH, message):
pass
signature: bytes = sol.get_async_response().data
verify_signature(SOL.OWNED_PUBLIC_KEY, message, signature)


# Use a class to reuse the same Speculos instance
@pytest.mark.parametrize('test_to_run', ALL_TESTS_EXCEPT_MEMO_AND_FEES)
def test_solana(backend, exchange_navigation_helper, test_to_run):
SolanaTests(backend, exchange_navigation_helper).run_test(test_to_run)
105 changes: 0 additions & 105 deletions test/python/test_solana_fund.py

This file was deleted.

107 changes: 0 additions & 107 deletions test/python/test_solana_sell.py

This file was deleted.

Loading

0 comments on commit b3ddccb

Please sign in to comment.