-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbasic_transfer.py
More file actions
91 lines (77 loc) · 2.96 KB
/
basic_transfer.py
File metadata and controls
91 lines (77 loc) · 2.96 KB
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
88
89
90
91
import algokit_utils
from algokit_utils import OnSchemaBreak, \
OnUpdate, transfer, TransferParameters, TransferAssetParameters, get_account_from_mnemonic, Account
from algosdk.transaction import SuggestedParams
from algosdk.v2client.algod import AlgodClient
from algosdk.v2client.indexer import IndexerClient
from SolarChain.projects.SolarChain.smart_contracts.artifacts.unit_transfer.asset_purchase_client import \
AssetPurchaseClient
from account_constants import ACCOUNTS_LOCAL, ACCOUNTS_TEST_NET, ASSET_ID_TEST_NET, \
ASSET_ID_LOCAL_NET
# LocalNet configuration
# Make sure all accounts are properly setup
LOCAL_NET: bool = False # Set to False for Test Net, and True to use Local Net
if LOCAL_NET:
TOKEN = "a" * 64 # Default AlgoKit LocalNet token
SERVER_ADDRESS = "http://localhost:4001" # Default AlgoKit LocalNet endpoint
INDEXER_ADDRESS = "http://localhost:8980"
INDEXER_TOKEN = TOKEN
else:
TOKEN = "" # Default AlgoKit LocalNet token
SERVER_ADDRESS = "https://testnet-api.4160.nodely.dev" # Default AlgoKit LocalNet endpoint
INDEXER_ADDRESS = "https://testnet-idx.4160.nodely.dev"
INDEXER_TOKEN = ""
# Initialize the Algod client
algod_client: AlgodClient = AlgodClient(TOKEN, SERVER_ADDRESS)
print("Algod Client Created")
# Check the connection by fetching the node status
try:
status = algod_client.status()
print("Connected to Algorand Network.")
print("Network Status:", status)
except Exception as e:
print("Failed to connect:", e)
# Generate address and mnemonic if needed
if LOCAL_NET:
acc1: Account = get_account_from_mnemonic(ACCOUNTS_LOCAL[0])
acc2: Account = get_account_from_mnemonic(ACCOUNTS_LOCAL[1])
else:
acc1: Account = get_account_from_mnemonic(ACCOUNTS_TEST_NET[0])
acc2: Account = get_account_from_mnemonic(ACCOUNTS_TEST_NET[1])
params: SuggestedParams = algod_client.suggested_params()
params.min_fee = 0
params.flat_fee = True
created_asset = ASSET_ID_TEST_NET if not LOCAL_NET else ASSET_ID_LOCAL_NET
app_indexer_client: IndexerClient = IndexerClient(indexer_address=INDEXER_ADDRESS, indexer_token=INDEXER_TOKEN)
app_client = AssetPurchaseClient(
algod_client=algod_client,
creator=acc1,
indexer_client=app_indexer_client
)
app_client.deploy(
on_schema_break=OnSchemaBreak.AppendApp,
on_update=OnUpdate.AppendApp,
)
price = 2_000
qty = 1
FEES = 1_000
transfer(
algod_client,
TransferParameters(
from_account=acc2,
to_address=app_client.app_address,
micro_algos=(FEES+price) * qty,
)
)
app_client.contract(seller=acc1.address, buyer=acc2.address, price=price, qty=qty, asset=created_asset)
app_client.asset_opt_in(asset=created_asset)
algokit_utils.transfer_asset(
algod_client,
TransferAssetParameters(
from_account=acc1,
to_address=app_client.app_address,
asset_id=created_asset,
amount=qty
)
)
app_client.begin_transfer(asset=created_asset, seller=acc1.address, buyer=acc2.address, price=price, qty=qty)