-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_t.py
166 lines (131 loc) · 5.73 KB
/
test_t.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
157
158
159
160
161
162
163
164
165
166
import base58check
from cssdk import TClient as Client
# checking wallet balance
def checkWalletBalance():
balance = client.amountToFloat(client.getBalance(Key))
print('Key balance = ', balance)
def getTransactionData():
transactionsData = client.getTransactions(Key, 0, 10)
cnt = 0
if transactionsData != None and transactionsData.status.code == 0:
for a in transactionsData.transactions:
cnt += 1
print(cnt, '. ', a)
else:
print(transactionsData.status.message)
defaultTransactionId = transactionsData.transactions[0].id
print('Default transaction id = (', defaultTransactionId.poolSeq, ', ' , defaultTransactionId.index, ')')
defaultTransaction = client.getTransaction(defaultTransactionId.poolSeq, defaultTransactionId.index)
if defaultTransaction != None and transactionsData.status.code == 0:
print('Default pool value: ', defaultTransaction.transaction)
def getPoolData():
defaultPool = client.getPool(defaultTransactionId.poolSeq)
if defaultPool != None and defaultPool.status.code == 0:
print('Default pool value: ', defaultPool)
def getContractData():
pKeyContracts = client.getUserContracts(Key, 0, 100)
if pKeyContracts != None and pKeyContracts.status.code == 0:
cnt = 0
print(Key,' contracts:')
for a in pKeyContracts.smartContractsList:
cnt += 1
print(cnt, '. ', base58check.b58encode(a.address).decode('UTF-8'), ', deployer: ' , base58check.b58encode(a.deployer).decode('UTF-8'))
defaultContract = pKeyContracts.smartContractsList[0]
print('Default contract source code: \n', defaultContract.smartContractDeploy.sourceCode)
defaultContractSourceCode = client.getContractCode(base58check.b58encode(defaultContract.address).decode('UTF-8'))
print('Default contract source code by address: \n', defaultContractSourceCode)
def sendTransaction():
amount = client.getAPI().general.Amount()
# or
amount = client.floatToamount(1.001)
fee =client.double_to_fee(0.1)
# this version of SDK doesn't work with transactions user fields, this will be awailable in next version
# UserFields: id(32 bit integer, value(integer, text, amount))
# userFields = {}
# sending coins to new created account
client.sendAmount(Key, secureKey, newKey, amount, fee)
balance = client.amountToFloat(client.getBalance(newKey))
print(newKey, ': balance = ', balance)
# sending coins from new created account
amount = client.floatToamount(0.02)
client.sendAmount(newKey, newSKey, Key, amount, fee)
balance = client.amountToFloat(client.getBalance(newKey))
print(newKey, ': balance = ', balance)
# deploing new contract
def deployContract():
with open("smart.txt", "r") as contract_file:
newContractCode = contract_file.read()
print(newContractCode)
contract = client.prepareContract(newContractCode)
fee = client.double_to_fee(0.1)
uf_text = ''
client.deployContract(Key, secureKey, fee, contract, uf_text)
# executing contract
def executeContract():
pKeyContracts = client.getUserContracts(Key, 0, 100)
contractAddress =''
if pKeyContracts == None or pKeyContracts.status.code != 0:
return
num = len(pKeyContracts.smartContractsList)-1
curContractDepoyData = pKeyContracts.smartContractsList[num]
contractAddress = base58check.b58encode(curContractDepoyData.address).decode('UTF-8')
curContractMethods = client.getContractMethods(contractAddress)
if curContractMethods == None or len(curContractMethods.methods) == 0:
print('Contract ', contractAddress, ' has no methods')
return
contractMethod = curContractMethods.methods[0].name
methodParameters = curContractMethods.methods[0].arguments
fee = fee = client.double_to_fee(0.1)
ufText = ''
used = []
save_to_bch = True
client.executeContract(Key,secureKey, contractAddress, contractMethod, methodParameters, fee, ufText, used, save_to_bch)
# deploing new token
def deployToken():
with open("token.txt", "r") as contract_file:
newContractCode = contract_file.read()
newContractCode = newContractCode.replace('_Token_Symbol_', 'NNN')
newContractCode = newContractCode.replace('_Token_Name_', 'New Token')
newContractCode = newContractCode.replace('_Token_decimal_', '3')
newContractCode = newContractCode.replace('_Token_supply_', '1000')
print(newContractCode)
contract = client.prepareContract(newContractCode)
if contract ==None:
return
fee = client.double_to_fee(0.5)
uf_text = ''
client.deployContract(Key, secureKey, fee, contract, uf_text)
# transfering token
def transferToken():
contractAddress = '_TokenKey_'
curContractMethods = client.getContractMethods(contractAddress)
if curContractMethods == None or len(curContractMethods.methods) == 0:
print('Contract ', contractAddress, ' has no methods')
return
contractMethod = 'transfer'
methodParameters = {'to':{'String':'_ReceiverKey_'}, 'amount':{'String':'_token_transfer_value_'}}
fee = fee = client.double_to_fee(0.1)
ufText = ''
used = []
save_to_bch = True
client.executeContract(Key,secureKey, contractAddress, contractMethod, methodParameters, fee, ufText, used, save_to_bch)
# initializing api client
client = Client(port = 9013)
# initializing wallet Keys
Key = 'PublicKey'
secureKey = 'Privatekey'
defaultTransactionId = client.getAPI().TransactionId()
# creating new wallet Keys
newKeyPair = client.createKeyPair()
print(newKeyPair)
newKey = newKeyPair[1]
newSKey = newKeyPair[0]
checkWalletBalance()
getTransactionData()
getPoolData()
getContractData()
# sendTransaction()
# deployContract()
# executeContract()
# deployToken()
# transferToken()