Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust 'stamp calculator' to be a 'tx simulator' #312

Merged
merged 7 commits into from
Oct 24, 2024
Merged

Conversation

Endogen
Copy link
Contributor

@Endogen Endogen commented Oct 22, 2024

Description

This will give us the possibility to simulate transactions by only supplying the encoded payload dict. No signing needed. Since this is more than just a stamp calculator, I renamed it to 'simulator'.

This is tested and working. I've also reworked xian-py to reflect these adjustments and explicitly added a method to simulate transactions: xian-network/xian-py#2

To have it really clean and making sense, I've added a simulate_tx endpoint to query.py that should over time replace the calculate_stamps endpoint.

Our Chrome wallet and possibly JS tools would need to be adjusted

Example script

from xian_py.wallet import Wallet
from xian_py.xian import Xian
from xian_py.transaction import (
    get_nonce,
    simulate_tx
)

node_url = 'http://127.0.0.1:26657'
wallet = Wallet('cd6cc45ffe7cebf09c6c6025575d50bb42c6c70c07e1dbc5150aaadc98705c2b')

xian = Xian(
    node_url,
    wallet=wallet
)

payload = {
    "contract": "currency",
    "function": "transfer",
    "kwargs":
    {
        "to": "8bf21c7dc3a4ff32996bf56a665e1efe3c9261cc95bbf82552c328585c863829",
        "amount": 1.11,
    },
    "nonce": get_nonce(node_url, wallet.public_key),
    "stamps": 0,
    "chain_id": xian.get_chain_id(),
    "sender": wallet.public_key
}
print(f'payload: {payload}')

simulated_tx = simulate_tx(node_url, payload)
print(f'simulated tx: {simulated_tx}')

Endpoint INPUT

{
    'contract': 'currency',
    'function': 'transfer',
    'kwargs':
    {
        'to': '8bf21c7dc3a4ff32996bf56a665e1efe3c9261cc95bbf82552c328585c863829',
        'amount': 1.11
    },
    'nonce': 2,
    'stamps': 0,
    'chain_id': 'unique-chain-id-goes-here',
    'sender': 'ee06a34cf08bf72ce592d26d36b90c79daba2829ba9634992d034318160d49f9'
}

Endpoint OUTPUT

{
    'payload':
    {
        'chain_id': 'unique-chain-id-goes-here',
        'contract': 'currency',
        'function': 'transfer',
        'kwargs':
        {
            'amount': 1.11,
            'to': '8bf21c7dc3a4ff32996bf56a665e1efe3c9261cc95bbf82552c328585c863829'
        },
        'nonce': 2,
        'sender': 'ee06a34cf08bf72ce592d26d36b90c79daba2829ba9634992d034318160d49f9',
        'stamps': 0
    },
    'result': 'None',
    'stamps_used': 10,
    'state': [
    {
        'key': 'currency.balances:ee06a34cf08bf72ce592d26d36b90c79daba2829ba9634992d034318160d49f9',
        'value': '61111008.32100000'
    },
    {
        'key': 'currency.balances:8bf21c7dc3a4ff32996bf56a665e1efe3c9261cc95bbf82552c328585c863829',
        'value': '2.22'
    }],
    'status': 0
}

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist

  • I have performed a self-review of my own code
  • I have tested this change in my development environment.
  • I have added tests to prove that this change works
  • All existing tests pass after this change
  • I have added / updated documentation related to this change

@Endogen Endogen marked this pull request as ready for review October 24, 2024 00:27
@Endogen
Copy link
Contributor Author

Endogen commented Oct 24, 2024

@duelingbenjos The only thing i see that we should think more about: If the provided payload doesn't include everything needed, it will not tell you.

@Endogen Endogen self-assigned this Oct 24, 2024
@Endogen Endogen added the enhancement New feature or request label Oct 24, 2024
@Endogen Endogen changed the title Adjust to accept encoded payload only Adjust 'stamp calculator' to be a 'tx simulator' Oct 24, 2024
@duelingbenjos duelingbenjos self-requested a review October 24, 2024 08:33
Copy link
Contributor

@duelingbenjos duelingbenjos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, we must keep the old endpoint intact though.

src/xian/methods/query.py Show resolved Hide resolved
@duelingbenjos duelingbenjos self-requested a review October 24, 2024 13:58
@Endogen Endogen merged commit 0cde32b into master Oct 24, 2024
4 checks passed
@duelingbenjos duelingbenjos deleted the stamp_calc_payload branch October 24, 2024 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants