Skip to content

Commit

Permalink
Merge pull request #60 from jansenmarc/master
Browse files Browse the repository at this point in the history
Smart Asset functionality
  • Loading branch information
PyWaves committed Nov 18, 2018
2 parents 27e878e + 4e24faa commit 2900de9
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Expand Up @@ -85,6 +85,10 @@ __pywaves.Address(address, publicKey, privateKey, seed)__ _Creates a new Address

`dataTransaction(data, timestamp=0)` sets data for the account. data should be a json array with entries including type (bool, binary, int, string), key and value

`setScript(scriptSource, txFee=pywaves.DEFAULT_SCRIPT_FEE, timestamp=0)` issue a smart asset

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

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

Expand Down Expand Up @@ -345,6 +349,48 @@ for address in lines:
myAddress.sendAsset(pw.Address(address.strip()), myToken, amount)
```

#### Add a script to an account:
```python
import pywaves as pw
import base64

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

script = 'match tx { \n' + \
' case _ => true\n' + \
'}'
address = pw.Address(privateKey = "<private key>")
tx = address.setScript(script, txFee=1000000)
```

#### Issue a Smart Asset
```python
imort pywaves as pw
import base64

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

script = 'match tx { \n' + \
' case _ => true\n' + \
'}'
address = pw.Address(privateKey = '<private key>')
tx = address.issueSmartAsset('smartTestAsset', 'an asset for testingsmart assets', 1000, script, 2)
```

#### Set a new script for a Smart Asset
```python
import pywaves as pw
import base64

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

script = 'match tx { \n' + \
' case _ => true\n' + \
'}'
address = pw.Address(privateKey = '<private key>')
tx = address.setAssetScript(pw.Asset('<asset id>'), script)
```

#### Playing with Waves Matcher node (DEX):
```python
import pywaves as pw
Expand Down
1 change: 1 addition & 0 deletions __init__.py
Expand Up @@ -18,6 +18,7 @@
DEFAULT_ALIAS_FEE = 100000
DEFAULT_SPONSOR_FEE = 100000000
DEFAULT_SCRIPT_FEE = 100000
DEFAULT_ASSET_SCRIPT_FEE = 100000000
VALID_TIMEFRAMES = (5, 15, 30, 60, 240, 1440)
MAX_WDF_REQUEST = 100

Expand Down
89 changes: 89 additions & 0 deletions address.py
Expand Up @@ -970,3 +970,92 @@ def setScript(self, scriptSource, txFee=pywaves.DEFAULT_SCRIPT_FEE, timestamp=0)
})

return pywaves.wrapper('/transactions/broadcast', data)

def setAssetScript(self, asset, scriptSource, txFee=pywaves.DEFAULT_ASSET_SCRIPT_FEE, timestamp=0):
script = pywaves.wrapper('/utils/script/compile', scriptSource)['script'][7:]
if not self.privateKey:
logging.error('Private key required')
else:
compiledScript = base64.b64decode(script)
scriptLength = len(compiledScript)
if timestamp == 0:
timestamp = int(time.time() * 1000)
sData = b'\x0f' + \
b'\1' + \
crypto.str2bytes(str(pywaves.CHAIN_ID)) + \
base58.b58decode(self.publicKey) + \
base58.b58decode(asset.assetId) + \
struct.pack(">Q", txFee) + \
struct.pack(">Q", timestamp) + \
b'\1' + \
struct.pack(">H", scriptLength) + \
compiledScript
signature = crypto.sign(self.privateKey, sData)

data = json.dumps({
"type": 15,
"version": 1,
"assetId": asset.assetId,
"senderPublicKey": self.publicKey,
"fee": txFee,
"timestamp": timestamp,
"script": 'base64:' + script,
"proofs": [
signature
]
})
print(data)

return pywaves.wrapper('/transactions/broadcast', data)

def issueSmartAsset(self, name, description, quantity, scriptSource, decimals=0, reissuable=False, txFee=pywaves.DEFAULT_ASSET_FEE):
script = pywaves.wrapper('/utils/script/compile', scriptSource)['script'][7:]
if not self.privateKey:
msg = 'Private key required'
logging.error(msg)
pywaves.throw_error(msg)
elif len(name) < 4 or len(name) > 16:
msg = 'Asset name must be between 4 and 16 characters long'
logging.error(msg)
pywaves.throw_error(msg)
else:
compiledScript = base64.b64decode(script)
scriptLength = len(compiledScript)
timestamp = int(time.time() * 1000)
sData = b'\3' + \
b'\2' + \
crypto.str2bytes(str(pywaves.CHAIN_ID)) + \
base58.b58decode(self.publicKey) + \
struct.pack(">H", len(name)) + \
crypto.str2bytes(name) + \
struct.pack(">H", len(description)) + \
crypto.str2bytes(description) + \
struct.pack(">Q", quantity) + \
struct.pack(">B", decimals) + \
(b'\1' if reissuable else b'\0') + \
struct.pack(">Q", txFee) + \
struct.pack(">Q", timestamp) + \
b'\1' + \
struct.pack(">H", scriptLength) + \
compiledScript
signature = crypto.sign(self.privateKey, sData)
data = json.dumps({
"type": 3,
"senderPublicKey": self.publicKey,
"name": name,
"version": 2,
"quantity": quantity,
"timestamp": timestamp,
"description": description,
"decimals": decimals,
"reissuable": reissuable,
"fee": txFee,
"proofs": [ signature ],
"script": 'base64:' + script
})
print(data)
req = pywaves.wrapper('/transactions/broadcast', data)
if pywaves.OFFLINE:
return req
else:
return req

0 comments on commit 2900de9

Please sign in to comment.