-
Notifications
You must be signed in to change notification settings - Fork 96
/
test_contract.py
156 lines (121 loc) · 5.67 KB
/
test_contract.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import asyncio
import pytest
from tronpy import Tron, Contract
from tronpy import AsyncTron, AsyncContract
from tronpy.keys import PrivateKey
def test_const_functions():
client = Tron(network='nile')
contract = client.get_contract('THi2qJf6XmvTJSpZHc17HgQsmJop6kb3ia')
assert contract
assert 'name' in dir(contract.functions)
print(dir(contract.functions))
print(repr(contract.functions.name()))
print(repr(contract.functions.decimals()))
assert contract.functions.totalSupply() > 0
for f in contract.functions:
print(f)
@pytest.mark.asyncio
async def test_async_const_functions():
async with AsyncTron(network='nile') as client:
contract = await client.get_contract('THi2qJf6XmvTJSpZHc17HgQsmJop6kb3ia')
assert contract
assert 'name' in dir(contract.functions)
print(dir(contract.functions))
print(repr(await contract.functions.name()))
print(repr(await contract.functions.decimals()))
assert await contract.functions.totalSupply() > 0
for f in contract.functions:
print(f)
def test_trc20_transfer():
# TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu
priv_key = PrivateKey(bytes.fromhex("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"))
client = Tron(network='nile')
contract = client.get_contract('THi2qJf6XmvTJSpZHc17HgQsmJop6kb3ia')
print('Balance', contract.functions.balanceOf('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu'))
txn = (
contract.functions.transfer('TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA', 1_000)
.with_owner('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu')
.fee_limit(5_000_000)
.build()
.sign(priv_key)
.inspect()
.broadcast()
)
print(txn)
# wait
receipt = txn.wait()
print(receipt)
if 'contractResult' in receipt:
print('result:', contract.functions.transfer.parse_output(receipt['contractResult'][0]))
# result
print(txn.result())
@pytest.mark.asyncio
async def test_async_trc20_transfer():
priv_key = PrivateKey(bytes.fromhex("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"))
async with AsyncTron(network='nile') as client:
contract = await client.get_contract('THi2qJf6XmvTJSpZHc17HgQsmJop6kb3ia')
print('Balance', await contract.functions.balanceOf('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu'))
txb = await contract.functions.transfer('TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA', 1_000)
txb = txb.with_owner('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu').fee_limit(5_000_000)
txn = await txb.build()
txn = txn.sign(priv_key).inspect()
txn_ret = await txn.broadcast()
print(txn)
# wait
receipt = await txn_ret.wait()
print(receipt)
if 'contractResult' in receipt:
print('result:', contract.functions.transfer.parse_output(receipt['contractResult'][0]))
# result
print(await txn_ret.result())
def test_contract_create():
# TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu
priv_key = PrivateKey(bytes.fromhex("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"))
client = Tron(network='nile')
bytecode = "608060405234801561001057600080fd5b5060c78061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea2646970667358221220c8daade51f673e96205b4a991ab6b94af82edea0f4b57be087ab123f03fc40f264736f6c63430006000033"
abi = [
{
"inputs": [],
"name": "get",
"outputs": [{"internalType": "uint256", "name": "retVal", "type": "uint256"}],
"stateMutability": "view",
"type": "function",
}
]
cntr = Contract(name="SimpleStore", bytecode=bytecode, abi=abi)
txn = (
client.trx.deploy_contract('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu', cntr)
.fee_limit(5_000_000)
.build()
.sign(priv_key)
.inspect()
.broadcast()
)
print(txn)
result = txn.wait()
print(result)
print('Created:', result['contract_address'])
@pytest.mark.asyncio
async def test_async_contract_create():
# TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu
priv_key = PrivateKey(bytes.fromhex("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"))
async with AsyncTron(network='nile') as client:
bytecode = "608060405234801561001057600080fd5b5060c78061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806360fe47b11460375780636d4ce63c146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea2646970667358221220c8daade51f673e96205b4a991ab6b94af82edea0f4b57be087ab123f03fc40f264736f6c63430006000033"
abi = [
{
"inputs": [],
"name": "get",
"outputs": [{"internalType": "uint256", "name": "retVal", "type": "uint256"}],
"stateMutability": "view",
"type": "function",
}
]
cntr = AsyncContract(name="SimpleStore", bytecode=bytecode, abi=abi)
txb = client.trx.deploy_contract('TGQgfK497YXmjdgvun9Bg5Zu3xE15v17cu', cntr).fee_limit(1_000_000)
txn = await txb.build()
txn = txn.sign(priv_key).inspect()
txn_ret = await txn.broadcast()
print(txn_ret)
result = await txn_ret.wait()
print(result)
print('Created:', result['contract_address'])