forked from PyWaves/PyWaves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.py
45 lines (33 loc) · 1.72 KB
/
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
import requests
import pywaves as pw
class Contract(object):
def __init__(self, contractAddress, seed, pywaves=pw):
self.pw = pywaves
self.contractAddress = contractAddress
metaInfo = self.parseContractAddress()
extractedMethods = metaInfo.keys()
for method in extractedMethods:
signature = self.generateCode(method, metaInfo[method], seed)
exec(signature, globals())
setattr(self, method, eval(method))
def parseContractAddress(self):
metaInfo = requests.get(self.pwNODE + '/addresses/scriptInfo/' + self.contractAddress + '/meta').json()
return metaInfo['meta']['callableFuncTypes']
def generateCode(self, method, parameters, seed):
parameterList = ''
callParameters = 'parameters = [\n'
for parameter in parameters.keys():
parameterList += parameter + ', '
type = parameters[parameter]
if type == 'Int':
callParameters += '\t\t{ "type": "integer", "value": ' + parameter + ' },\n'
elif type == 'String':
callParameters += '\t\t{ "type": "string", "value": ' + parameter + ' },\n'
elif type == 'Boolean':
callParameters += '\t\t{ "type": "boolean", "value": ' + parameter + ' },\n'
if len(parameters.keys()) > 0:
callParameters = callParameters[0:len(callParameters) - 2] + '\n'
callParameters += '\t]'
call = 'pw.Address(seed = \'' + seed + '\').invokeScript(\'' + self.contractAddress + '\', \'' + method + '\', parameters, [])'
code = 'def ' + method + '(' + parameterList[0: len(parameterList) - 2] + '):\n\t' + callParameters + '\n\t' + call
return code