Skip to content

Commit

Permalink
Merge pull request LedgerHQ#123 from LedgerHQ/feat/apa/clear_testing
Browse files Browse the repository at this point in the history
Clear testing using the updated client
  • Loading branch information
apaillier-ledger authored Dec 5, 2023
2 parents 1b529f9 + 9972339 commit eb9fca7
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_functional_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
with:
app_repository: LedgerHQ/app-ethereum
app_branch_name: develop
flags: "DEBUG=1 BYPASS_SIGNATURES=1"
flags: "DEBUG=1 CAL_TEST_KEY=1"
upload_app_binaries_artifact: ethereum_build_develop

ragger_tests_with_ethereum_develop:
Expand Down
36 changes: 36 additions & 0 deletions tests/abis/0x000102030405060708090a0b0c0d0e0f10111213.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"inputs" : [
{
"internalType" : "uint256",
"name" : "amountOutMin",
"type" : "uint256"
},
{
"internalType" : "address[]",
"name" : "path",
"type" : "address[]"
},
{
"internalType" : "address",
"name" : "to",
"type" : "address"
},
{
"internalType" : "uint256",
"name" : "deadline",
"type" : "uint256"
}
],
"name" : "swapExactETHForTokens",
"outputs" : [
{
"internalType" : "uint256[]",
"name" : "amounts",
"type" : "uint256[]"
}
],
"stateMutability" : "payable",
"type" : "function"
}
]
4 changes: 1 addition & 3 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
--extra-index-url https://test.pypi.org/simple/
ledger_app_clients.ethereum
pytest
ragger[speculos,ledgerwallet]>=1.11.5
ecdsa
simple-rlp
ragger[speculos,ledgercomm]
Binary file modified tests/snapshots/nanos/test_swap_exact_eth_for_token/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanos/test_swap_exact_eth_for_token/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanos/test_swap_exact_eth_for_token/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanos/test_swap_exact_eth_for_token/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanos/test_swap_exact_eth_for_token/00007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanos/test_swap_exact_eth_for_token/00008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanos/test_swap_exact_eth_for_token/00009.png
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/snapshots/nanosp/test_swap_exact_eth_for_token/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanosp/test_swap_exact_eth_for_token/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanosp/test_swap_exact_eth_for_token/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanox/test_swap_exact_eth_for_token/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanox/test_swap_exact_eth_for_token/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanox/test_swap_exact_eth_for_token/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/stax/test_swap_exact_eth_for_token/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/stax/test_swap_exact_eth_for_token/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 39 additions & 28 deletions tests/test_swap.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,64 @@
from pathlib import Path
import json
import os

import datetime

from web3 import Web3
from eth_typing import ChainId

from ledger_app_clients.ethereum.client import EthAppClient, StatusWord
from ledger_app_clients.ethereum.utils import get_selector_from_data
from ragger.navigator import NavInsID

from .utils import get_appname_from_makefile


ROOT_SCREENSHOT_PATH = Path(__file__).parent
ABIS_FOLDER = "%s/abis" % (os.path.dirname(__file__))

PLUGIN_NAME = get_appname_from_makefile()

PLACE_HOLDER_SIGNATURE = bytes.fromhex('3045022100f6e1a922c745e244fa3ed9a865491672808ef93f492ee0410861d748c5de201f0220160d6522499f3a84fa3e744b3b81e49e129e997b28495e58671a1169b16fa777')
with open("%s/0x000102030405060708090a0b0c0d0e0f10111213.abi.json" % (ABIS_FOLDER)) as file:
contract = Web3().eth.contract(
abi=json.load(file),
# Get address from filename
address=bytes.fromhex(os.path.basename(file.name).split(".")[0].split("x")[-1])
)

# In this test we verify "Swap Exact Eth For Tokens with beneficiary"
# Test from replayed transaction: https://etherscan.io/tx/0x0160b3aec12fd08e6be0040616c7c38248efb4413168a3372fc4d2db0e5961bb
# Function: swapExactETHForTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline)
#
# MethodID: 0x7ff36ab5
# [0]: 0000000000000000000000000000000000000000000000018b1dd9dc51b5a9f7
# [1]: 0000000000000000000000000000000000000000000000000000000000000080
# [2]: 00000000000000000000000015557c8b7246c38ee71ea6dc69e4347f5dac2104
# [3]: 0000000000000000000000000000000000000000000000000000000061533610
# [4]: 0000000000000000000000000000000000000000000000000000000000000002
# [5]: 000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
# [6]: 0000000000000000000000006b3595068778dd592e39a122f4f5a5cf09c90fe2

# EDIT THIS: build your own test
def test_swap_exact_eth_for_token(backend, firmware, navigator, test_name):
# EDIT THIS: Replace with your contract address
contract_address = bytes.fromhex('7a250d5630b4cf539739df2c5dacb4c659f2488d')
# EDIT THIS: Replace with your method selector
method_selector = bytes.fromhex('7ff36ab5')
# The rawTx of the tx up above is accessible through: https://etherscan.io/getRawTx?tx=0x0160b3aec12fd08e6be0040616c7c38248efb4413168a3372fc4d2db0e5961bb
# EDIT THIS: Replace with your Tx payload
tx_payload = bytes.fromhex('02f9015a0181d38459682f0085215d7c1e598302a4e9947a250d5630b4cf539739df2c5dacb4c659f2488d88016345785d8a0000b8e47ff36ab50000000000000000000000000000000000000000000000018b1dd9dc51b5a9f7000000000000000000000000000000000000000000000000000000000000008000000000000000000000000015557c8b7246c38ee71ea6dc69e4347f5dac210400000000000000000000000000000000000000000000000000000000615336100000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b3595068778dd592e39a122f4f5a5cf09c90fe2c001a089c5ce4ce199f7d93ea1d54c08133fab9407d8de63a9885d59d8ce69a59573dda045f6a0e0d3288dfdfddc23ad0afb9577c4011801f598d581a46cd0b0e2bd0571')

client = EthAppClient(backend)

data = contract.encodeABI("swapExactETHForTokens", [
Web3.to_wei(28.5, "ether"),
[
bytes.fromhex("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
bytes.fromhex("6B3595068778DD592e39A122f4f5a5cF09C90fE2")
],
bytes.fromhex("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"),
int(datetime.datetime(2023, 12, 25, 0, 0).timestamp())
])

# first setup the external plugin
with client.set_external_plugin(PLUGIN_NAME, contract_address, method_selector, PLACE_HOLDER_SIGNATURE):
with client.set_external_plugin(PLUGIN_NAME,
contract.address,
# Extract function selector from the encoded data
get_selector_from_data(data)):
pass

assert client.response().status == StatusWord.OK

# send the transaction
# !!!! to be replaced by a dedicated plugin sign function !!!
with client._sign("m/44'/60'/1'/0/0", tx_payload):
with client.sign("m/44'/60'/1'/0/0", {
"nonce": 20,
"maxFeePerGas": Web3.to_wei(145, "gwei"),
"maxPriorityFeePerGas": Web3.to_wei(1.5, "gwei"),
"gas": 173290,
"to": contract.address,
"value": Web3.to_wei(0.1, "ether"),
"chainId": ChainId.ETH,
"data": data
}):
# Validate the on-screen request by performing the navigation appropriate for this device
if firmware.device.startswith("nano"):
navigator.navigate_until_text_and_compare(NavInsID.RIGHT_CLICK,
Expand All @@ -60,5 +73,3 @@ def test_swap_exact_eth_for_token(backend, firmware, navigator, test_name):
"Hold to sign",
ROOT_SCREENSHOT_PATH,
test_name)

assert client.response().status == StatusWord.OK

0 comments on commit eb9fca7

Please sign in to comment.