Skip to content

Commit

Permalink
added invoke script functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jansenmarc committed May 6, 2019
1 parent f3b102e commit df8833e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ __pywaves.Address(address, publicKey, privateKey, seed)__ _Creates a new Address

`setAssetScript(asset, scriptSource, txFee=pywaves.DEFAULT_ASSET_SCRIPT_FEE, timestamp=0)` set a new script for a smart asset

`invokeScript(dappAddress, functionName, params, feeAsset = None, txFee=pywaves.DEFAULT_INVOKE_SCRIPT_FEE)` invoke a script on a given dapp address

### Asset Class
__pywaves.Asset(assetId)__ _Creates a new Asset object_

Expand Down Expand Up @@ -377,6 +379,16 @@ address = pw.Address(privateKey = '<private key>')
tx = address.setAssetScript(pw.Asset('<asset id>'), script)
```

#### Invoking a script on a dapp address
```python
import pywaves as pw

pw.setNode(node='<node>', chain='testnet')

address = pw.Address(privateKey = '<private key>')
tx = address.invokeScript('3N5Wq22bLSf3gt5VwHTCRbRnETeSwpuT8kK', 'fundRecipient', [{"type": "integer", "value": 100, }, { "type": "string", "value": "test" }, { "type": "boolean", "value": True }])
```

#### Playing with Waves Matcher node (DEX):
```python
import pywaves as pw
Expand Down
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
DEFAULT_SPONSOR_FEE = 100000000
DEFAULT_SCRIPT_FEE = 100000
DEFAULT_ASSET_SCRIPT_FEE = 100000000
DEFAULT_SET_SCRIPT_FEE = 1000000
DEFAULT_INVOKE_SCRIPT_FEE = 500000
VALID_TIMEFRAMES = (5, 15, 30, 60, 240, 1440)
MAX_WDF_REQUEST = 100

Expand Down
78 changes: 78 additions & 0 deletions address.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ def buy(self, assetPair, amount, price, maxLifetime=30 * 86400, matcherFee=pywav
def sell(self, assetPair, amount, price, maxLifetime=30 * 86400, matcherFee=pywaves.DEFAULT_MATCHER_FEE, timestamp=0):
assetPair.refresh()
normPrice = int(pow(10, 8 + assetPair.asset2.decimals - assetPair.asset1.decimals) * price)
print('normPrice: ' + str(normPrice))
id = self._postOrder(assetPair.asset1, assetPair.asset2, b'\1', amount, normPrice, maxLifetime, matcherFee, timestamp)
if pywaves.OFFLINE:
return id
Expand Down Expand Up @@ -1077,3 +1078,80 @@ def issueSmartAsset(self, name, description, quantity, scriptSource, decimals=0,
return req
else:
return req

# def invokeScript(self, dappAddress, functionName, params, payments, feeAsset = None, txFee=pywaves.DEFAULT_INVOKE_SCRIPT_FEE):
def invokeScript(self, dappAddress, functionName, params, feeAsset=None, txFee=pywaves.DEFAULT_INVOKE_SCRIPT_FEE):
# nasty workaround until the bug in the node is fixed
payments = []
if not self.privateKey:
msg = 'Private key required'
logging.error(msg)
pywaves.throw_error(msg)
else:
timestamp = int(time.time() * 1000)
parameterBytes = b''
for param in params:
if param['type'] == 'integer':
parameterBytes += b'\0' + struct.pack(">Q", param['value'])
elif param['type'] == 'binary':
parameterBytes += b'\1' + struct.pack(">I", len(param['value'])) + crypto.str2bytes(param['value'])
elif param['type'] == 'string':
parameterBytes += b'\2' + struct.pack(">I", len(crypto.str2bytes(param['value']))) + crypto.str2bytes(param['value'])
elif param['type'] == 'boolean':
if param['value'] == True:
parameterBytes += b'\6'
else:
parameterBytes += b'\7'
paymentBytes = b''
for payment in payments:
currentPaymentBytes = b''
if ('assetId' in payment and payment['assetId'] != None and payment['assetId'] != ''):
currentPaymentBytes += struct.pack(">Q", payment['amount']) + b'\x01' + base58.b58decode(payment['assetId'])
else:
currentPaymentBytes += struct.pack(">Q", payment['amount']) + b'\x00'
paymentBytes += struct.pack(">H", len(currentPaymentBytes)) + currentPaymentBytes
assetIdBytes = b''
if (feeAsset):
assetIdBytes += b'\x01' + base58.b58decode(feeAsset)
else:
assetIdBytes += b'\x00'

sData = b'\x10' + \
b'\x01' + \
crypto.str2bytes(str(pywaves.CHAIN_ID)) + \
base58.b58decode(self.publicKey) + \
base58.b58decode(dappAddress) + \
b'\x09' + \
b'\x01' + \
struct.pack(">L", len(crypto.str2bytes(functionName))) +\
crypto.str2bytes(functionName) + \
struct.pack(">I", len(params)) + \
parameterBytes + \
struct.pack(">H", len(payments)) + \
paymentBytes + \
struct.pack(">Q", txFee) + \
assetIdBytes + \
struct.pack(">Q", timestamp)

signature = crypto.sign(self.privateKey, sData)

data = json.dumps({
"type": 16,
"senderPublicKey": self.publicKey,
"version": 1,
"timestamp": timestamp,
"fee": txFee,
"proofs": [ signature ],
"feeAssetId": feeAsset,
"dappAddress": dappAddress,
"call": {
"function": functionName,
"args": params
},
"payment": payments
})
req = pywaves.wrapper('/transactions/broadcast', data)
if pywaves.OFFLINE:
return req
else:
return req

0 comments on commit df8833e

Please sign in to comment.