forked from tinymanorg/tinyman-py-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_flash_loan_2_multiple_assets.py
87 lines (72 loc) · 2.72 KB
/
12_flash_loan_2_multiple_assets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# This sample is provided for demonstration purposes only.
# It is not intended for production use.
# This example does not constitute trading advice.
from pprint import pprint
from urllib.parse import quote_plus
from tinyman.assets import AssetAmount
from examples.v2.tutorial.common import get_account, get_assets
from examples.v2.utils import get_algod
from tinyman.v2.client import TinymanV2TestnetClient
from tinyman.compat import AssetTransferTxn, PaymentTxn
account = get_account()
algod = get_algod()
client = TinymanV2TestnetClient(algod_client=algod, user_address=account["address"])
ASSET_A_ID, ASSET_B_ID = get_assets()["ids"]
ASSET_A = client.fetch_asset(ASSET_A_ID)
ASSET_B = client.fetch_asset(ASSET_B_ID)
pool = client.fetch_pool(ASSET_A_ID, ASSET_B_ID)
position = pool.fetch_pool_position()
quote = pool.fetch_flash_loan_quote(
loan_amount_a=AssetAmount(pool.asset_1, 3_000_000),
loan_amount_b=AssetAmount(pool.asset_2, 2_000_000),
)
print("\nQuote:")
print(quote)
account_info = algod.account_info(account["address"])
for asset in account_info["assets"]:
if asset["asset-id"] == pool.asset_1.id:
asset_1_balance = asset["amount"]
if pool.asset_2.id == 0:
# Algo
asset_2_balance = (
account_info["amount"] - account_info["min-balance"] - 4000
) # 4 tnx fee
elif asset["asset-id"] == pool.asset_2.id:
asset_2_balance = asset["amount"]
# Transfer amounts are equal to sum of initial account balance and loan amount
# These transactions demonstrate that you can use the total amount
transactions = [
AssetTransferTxn(
sender=account["address"],
sp=algod.suggested_params(),
receiver=account["address"],
amt=asset_1_balance + quote.amounts_out[pool.asset_1].amount,
index=pool.asset_1.id,
),
AssetTransferTxn(
sender=account["address"],
sp=algod.suggested_params(),
receiver=account["address"],
amt=asset_2_balance + quote.amounts_out[pool.asset_2].amount,
index=pool.asset_2.id,
)
if pool.asset_2.id
else PaymentTxn(
sender=account["address"],
sp=algod.suggested_params(),
receiver=account["address"],
amt=asset_2_balance + quote.amounts_out[pool.asset_2].amount,
),
]
txn_group = pool.prepare_flash_loan_transactions_from_quote(
quote=quote, transactions=transactions
)
# Sign
txn_group.sign_with_private_key(account["address"], account["private_key"])
# Submit transactions to the network and wait for confirmation
txn_info = client.submit(txn_group, wait=True)
print("Transaction Info")
pprint(txn_info)
print(
f"Check the transaction group on Algoexplorer: https://testnet.algoexplorer.io/tx/group/{quote_plus(txn_group.id)}"
)