Skip to content

Commit 5e3ba82

Browse files
authored
Merge pull request #48 from tinymanorg/v2-updates
Tinyman V2
2 parents cd1a7db + ddf1e01 commit 5e3ba82

76 files changed

Lines changed: 6259 additions & 416 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
ignore = E501,F403,F405,E126,E121,W503,E203

.github/workflows/tests.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Lint & Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python-version: ["3.10", "3.9", "3.8"]
12+
py-algorand-sdk-version: ["1.18", "1.17", "1.16", "1.15", "1.14", "1.13"]
13+
14+
steps:
15+
- uses: actions/checkout@v3
16+
17+
- name: Set up Python ${{ matrix.python-version }}
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
22+
- name: Install dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install flake8 black py-algorand-sdk==${{ matrix.py-algorand-sdk-version }}
26+
27+
- name: Run flake8
28+
run: flake8 ${{ github.workspace }} --ignore=E501,F403,F405,E126,E121,W503,E203
29+
30+
- name: Run Black
31+
run: black ${{ github.workspace }} --check
32+
33+
- name: Run Unit tests
34+
run: python -m unittest

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
.idea
23

34
# Byte-compiled / optimized / DLL files
45
__pycache__/
@@ -129,3 +130,7 @@ dmypy.json
129130

130131
# Pyre type checker
131132
.pyre/
133+
134+
# Tutorials
135+
account*.json
136+
assets*.json

.pre-commit-config.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
repos:
2+
- repo: https://github.com/pycqa/flake8
3+
rev: '3.9.2' # pick a git hash / tag to point to
4+
hooks:
5+
- id: flake8
6+
args: ['--ignore=E501,F403,F405,E126,E121,W503,E203', '.']
7+
exclude: ^(env|venv)
8+
9+
- repo: https://github.com/psf/black
10+
rev: 22.8.0
11+
hooks:
12+
- id: black
13+
args: ['.', '--check']
14+
exclude: ^(env|venv)

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Change Log
2+
3+
## 2.0.0
4+
5+
### Added
6+
7+
* Added Tinyman V2 support (`tinyman.v2`).
8+
* Added Staking support (`tinyman.staking`).
9+
- It allows creating commitment transaction by `prepare_commit_transaction` and tracking commitments by `parse_commit_transaction`.
10+
* Added `calculate_price_impact` function to `tinyman.utils`.
11+
* Improved `TransactionGroup` class.
12+
- Added `+` operator support for composability, it allows creating a new transaction group (`txn_group_1 + txn_group_2`).
13+
- Added `id` property, it returns the transactions group id.
14+
- Added `TransactionGroup.sign_with_logicsig` function and deprecated `TransactionGroup.sign_with_logicisg` because of the typo.
15+
16+
### Changed
17+
18+
* `get_program` (V1) is moved from `tinyman.utils` to `tinyman.v1.contracts`.
19+
* `get_state_from_account_info` (V1) is moved from `tinyman.utils` to `tinyman.v1.utils`.
20+
21+
### Removed
22+
23+
* Deprecated `wait_for_confirmation` function is removed. `wait_for_confirmation` is added to [Algorand SDK](https://github.com/algorand/py-algorand-sdk).
24+
* Drop Python 3.7 support.
25+

README.md

Lines changed: 148 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,150 @@ The SDK has been updated for Tinyman V1.1.
1313

1414

1515
## Installation
16-
tinyman-py-sdk is not yet released on PYPI. It can be installed directly from this repository with pip:
16+
tinyman-py-sdk is not released on PYPI. It can be installed directly from this repository with pip:
1717

1818
`pip install git+https://github.com/tinymanorg/tinyman-py-sdk.git`
1919

20+
## V2
21+
22+
## Sneak Preview
23+
24+
```python
25+
# examples/v2/sneak_preview.py
26+
27+
from examples.v2.utils import get_algod
28+
from tinyman.v2.client import TinymanV2TestnetClient
29+
30+
algod = get_algod()
31+
client = TinymanV2TestnetClient(algod_client=algod)
32+
33+
# Fetch our two assets of interest
34+
USDC = client.fetch_asset(10458941)
35+
ALGO = client.fetch_asset(0)
36+
37+
# Fetch the pool we will work with
38+
pool = client.fetch_pool(USDC, ALGO)
39+
print(f"Pool Info: {pool.info()}")
40+
41+
# Get a quote for a swap of 1 ALGO to USDC with 1% slippage tolerance
42+
quote = pool.fetch_fixed_input_swap_quote(amount_in=ALGO(1_000_000), slippage=0.01)
43+
print(quote)
44+
print(f"USDC per ALGO: {quote.price}")
45+
print(f"USDC per ALGO (worst case): {quote.price_with_slippage}")
46+
```
47+
48+
## Tutorial
49+
50+
You can find a tutorial under the `examples/v2/tutorial` folder.
51+
52+
To run a step use `python <file_name>` such as `python 01_generate_account.py`.
53+
54+
#### Prerequisites
55+
1. [Generating an account](examples/v2/01_generate_account.py)
56+
2. [Creating assets](examples/v2/02_create_assets.by)
57+
58+
#### Steps
59+
60+
3. [Bootstrapping a pool](examples/v2/03_bootstrap_pool.py)
61+
4. [Adding initial liquidity to the pool](examples/v2/04_add_initial_liquidity.py)
62+
5. [Adding flexible (add two asset with a flexible rate) liquidity to the pool](examples/v2/05_add_flexible_liquidity.py)
63+
6. [Adding single asset (add only one asset) liquidity to the pool](examples/v2/06_add_single_asset_liquidity.py)
64+
7. [Removing liquidity to the pool](examples/v2/07_remove_liquidity.py)
65+
8. [Removing single asset(receive single asset) liquidity to the pool](examples/v2/08_single_asset_remove_liquidity.py)
66+
9. [Swapping fixed-input](examples/v2/09_fixed_input_swap.py)
67+
10. [Swapping fixed-output](examples/v2/10_fixed_output_swap.py)
68+
69+
## Example Operations
70+
71+
### Bootstrap
72+
73+
```python
74+
txn_group = pool.prepare_bootstrap_transactions()
75+
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
76+
txn_info = txn_group.submit(algod, wait=True)
77+
```
78+
79+
### Add Liquidity
80+
81+
#### Initial Add Liquidity
82+
83+
```python
84+
quote = pool.fetch_initial_add_liquidity_quote(
85+
amount_a=<AssetAmount>,
86+
amount_b=<AssetAmount>,
87+
)
88+
txn_group = pool.prepare_add_liquidity_transactions_from_quote(quote)
89+
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
90+
txn_info = txn_group.submit(algod, wait=True)
91+
```
92+
93+
#### Flexible Add Liquidity
94+
95+
```python
96+
quote = pool.fetch_flexible_add_liquidity_quote(
97+
amount_a=<AssetAmount>,
98+
amount_b=<AssetAmount>,
99+
)
100+
txn_group = pool.prepare_add_liquidity_transactions_from_quote(quote=quote)
101+
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
102+
txn_info = txn_group.submit(algod, wait=True)
103+
```
104+
105+
#### Single Asset Add Liquidity
106+
107+
```python
108+
quote = pool.fetch_single_asset_add_liquidity_quote(amount_a=<AssetAmount>)
109+
txn_group = pool.prepare_add_liquidity_transactions_from_quote(quote=quote)
110+
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
111+
txn_info = txn_group.submit(algod, wait=True)
112+
```
113+
114+
### Remove Liquidity
115+
116+
#### Remove Liquidity
117+
118+
```python
119+
quote = pool.fetch_remove_liquidity_quote(
120+
pool_token_asset_in=<AssetAmount>,
121+
)
122+
txn_group = pool.prepare_remove_liquidity_transactions_from_quote(quote=quote)
123+
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
124+
txn_info = txn_group.submit(algod, wait=True)
125+
```
126+
127+
#### Single Asset Remove Liquidity
128+
129+
```python
130+
quote = pool.fetch_single_asset_remove_liquidity_quote(
131+
pool_token_asset_in=<AssetAmount>,
132+
output_asset=<Asset>,
133+
)
134+
txn_group = pool.prepare_remove_liquidity_transactions_from_quote(quote=quote)
135+
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
136+
txn_info = txn_group.submit(algod, wait=True)
137+
```
138+
139+
### Swap
140+
141+
#### Fixed Input Swap
142+
143+
```python
144+
quote = pool.fetch_fixed_input_swap_quote(amount_in=<AssetAmount>)
145+
txn_group = pool.prepare_swap_transactions_from_quote(quote=quote)
146+
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
147+
txn_info = txn_group.submit(algod, wait=True)
148+
```
149+
150+
#### Fixed Output Swap
151+
152+
```python
153+
quote = pool.fetch_fixed_output_swap_quote(amount_in=<AssetAmount>)
154+
txn_group = pool.prepare_swap_transactions_from_quote(quote=quote)
155+
txn_group.sign_with_private_key(<ADDRESS>, <PRIVATE_KEY>)
156+
txn_info = txn_group.submit(algod, wait=True)
157+
```
158+
159+
## V1.1
20160

21161
## Sneak Preview
22162

@@ -48,7 +188,7 @@ print(f'TINYUSDC per ALGO (worst case): {quote.price_with_slippage}')
48188
## Examples
49189

50190
### Basic Swapping
51-
[swapping1.py](examples/swapping1.py)
191+
[swapping1.py](examples/v1/swapping1.py)
52192
This example demonstrates basic functionality including:
53193
* retrieving Pool details
54194
* getting a swap quote
@@ -58,16 +198,16 @@ This example demonstrates basic functionality including:
58198
* checking excess amounts
59199
* preparing redeem transactions
60200

61-
[swapping1_less_convenience.py](examples/swapping1_less_convenience.py)
62-
This example has exactly the same functionality as [swapping1.py](examples/swapping1.py) but is purposely more verbose, using less convenience functions.
201+
[swapping1_less_convenience.py](examples/v1/swapping1_less_convenience.py)
202+
This example has exactly the same functionality as [swapping1.py](examples/v1/swapping1.py) but is purposely more verbose, using less convenience functions.
63203

64204

65205
### Basic Pooling
66-
[pooling1.py](examples/pooling1.py)
206+
[pooling1.py](examples/v1/pooling1.py)
67207
This example demonstrates retrieving the current pool position/share for an address.
68208

69209
### Basic Add Liquidity (Minting)
70-
[add_liquidity1.py](examples/add_liquidity1.py)
210+
[add_liquidity1.py](examples/v1/add_liquidity1.py)
71211
This example demonstrates add liquidity to an existing pool.
72212

73213
### Basic Burning
@@ -119,9 +259,9 @@ for i, txn in enumerate(transaction_group.transactions):
119259
transaction_group.signed_transactions[i] = kmd.sign_transaction(handle, KMD_WALLET_PASSWORD, txn)
120260
```
121261

122-
A User account LogicSig can also be used in a similar way or using the `sign_with_logicisg` convenience method:
262+
A User account LogicSig can also be used in a similar way or using the `sign_with_logicsig` convenience method:
123263
```python
124-
transaction_group.sign_with_logicisg(logicsig)
264+
transaction_group.sign_with_logicsig(logicsig)
125265
```
126266

127267
### Submission

asc.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"repo": "https://github.com/tinymanorg/tinyman-staking",
3+
"ref": "main",
4+
"contracts": {
5+
"staking_app": {
6+
"type": "app",
7+
"approval_program": {
8+
"bytecode": "BSADAAEIJgcIZW5kX3RpbWUGYXNzZXRzBG1pbnMMdmVyaWZpY2F0aW9uCGJhbGFuY2UgAmlkD3Byb2dyYW1fY291bnRlcjEZIhJAAC8xGSMSQAAZMRmBAhJAAicxGYEEEkACITEZgQUSQAIfADYaAIAFc2V0dXASQAGZADYaAIAGY3JlYXRlEkAAijYaAIAGY29tbWl0EkAAfTYaAIAFY2xhaW0SQAEDNhoAgAZ1cGRhdGUSQAE8NhoAgA51cGRhdGVfcmV3YXJkcxJAAN82GgCADXVwZGF0ZV9hc3NldHMSQAD4NhoAgAtlbmRfcHJvZ3JhbRJAAPI2GgArEkAA9TYaAIALbG9nX2JhbGFuY2USQADvACNDIyhiMgcNRCMpYiJKJAtbNjAAEkAACiMISYEODERC/+tMSDUBIypiNAEkC1s1AjYaARdBAAg2GgEXNAIPRCI2MABwAERJFicETFCwNhoBFw9EgBN0aW55bWFuU3Rha2luZy92MTpiMQVRABMSRDEFVxMANQE0ASJbIycFYhJENAEkWzYwABJENAGBEFs2GgEXEkQjQyNDIoACcjE2GgFmIoACcjI2GgJmIoACcjM2GgNmIoACcjQ2GgRmIoACcjU2GgVmI0MiKTYaAWYiKjYaAmYjQyIoNhoBF2YjQyNDMgkxABJEIis2GgFmI0MiNjAAcABESUQWJwRMULAjQycGZCMINQEnBjQBZyInBTQBZiKAA3VybDYaAWYigA9yZXdhcmRfYXNzZXRfaWQ2GgIXZiKADXJld2FyZF9wZXJpb2Q2GgMXZiKACnN0YXJ0X3RpbWU2GgQXZiIoNhoFF2YiKTYaBmYiKjYaB2YjQyNDMgkxABJDMgkxABJDAA==",
9+
"address": "KJ3W4IB66Q4ZITCNVABJXAV4I4HKWSZIJMD6BTFATQAWO5AKOV5VMZ6OFI",
10+
"size": 658,
11+
"variables": [],
12+
"source": "https://github.com/tinymanorg/tinyman-staking/tree/main/contracts/staking.teal"
13+
},
14+
"clear_program": {
15+
"bytecode": "BIEB",
16+
"address": "P7GEWDXXW5IONRW6XRIRVPJCT2XXEQGOBGG65VJPBUOYZEJCBZWTPHS3VQ",
17+
"size": 3,
18+
"variables": [],
19+
"source": "https://github.com/tinymanorg/tinyman-staking/tree/main/contracts/clear_state.teal"
20+
},
21+
"global_state_schema": {
22+
"num_uints": 2,
23+
"num_byte_slices": 2
24+
},
25+
"local_state_schema": {
26+
"num_uints": 5,
27+
"num_byte_slices": 11
28+
},
29+
"name": "staking_app"
30+
}
31+
}
32+
}

examples/__init__.py

Whitespace-only changes.

examples/staking/commitments.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import requests
2+
from tinyman.staking import parse_commit_transaction
3+
from tinyman.staking.constants import TESTNET_STAKING_APP_ID
4+
5+
app_id = TESTNET_STAKING_APP_ID
6+
result = requests.get(
7+
f"https://indexer.testnet.algoexplorerapi.io/v2/transactions?application-id={app_id}&latest=50"
8+
).json()
9+
for txn in result["transactions"]:
10+
commit = parse_commit_transaction(txn, app_id)
11+
if commit:
12+
print(commit)
13+
print()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This sample is provided for demonstration purposes only.
2+
# It is not intended for production use.
3+
# This example does not constitute trading advice.
4+
5+
from tinyman.v1.client import TinymanTestnetClient
6+
7+
from tinyman.staking import prepare_commit_transaction
8+
9+
# Hardcoding account keys is not a great practice. This is for demonstration purposes only.
10+
# See the README & Docs for alternative signing methods.
11+
account = {
12+
"address": "ALGORAND_ADDRESS_HERE",
13+
"private_key": "base64_private_key_here", # Use algosdk.mnemonic.to_private_key(mnemonic) if necessary
14+
}
15+
16+
client = TinymanTestnetClient(user_address=account["address"])
17+
18+
# Fetch our two assets of interest
19+
TINYUSDC = client.fetch_asset(21582668)
20+
ALGO = client.fetch_asset(0)
21+
22+
# Fetch the pool we will work with
23+
pool = client.fetch_pool(TINYUSDC, ALGO)
24+
25+
26+
sp = client.algod.suggested_params()
27+
28+
txn_group = prepare_commit_transaction(
29+
app_id=client.staking_app_id,
30+
program_id=1,
31+
program_account="B4XVZ226UPFEIQBPIY6H454YA4B7HYXGEM7UDQR2RJP66HVLOARZTUTS6Q",
32+
pool_asset_id=pool.liquidity_asset.id,
33+
amount=600_000_000,
34+
sender=account["address"],
35+
suggested_params=sp,
36+
)
37+
38+
txn_group.sign_with_private_key(account["address"], account["private_key"])
39+
result = client.submit(txn_group, wait=True)
40+
print(result)

0 commit comments

Comments
 (0)