Skip to content

Commit

Permalink
added the oracle class
Browse files Browse the repository at this point in the history
  • Loading branch information
jansenmarc committed Dec 26, 2019
1 parent a5f81df commit 73583e7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,29 @@ contract = pw.Contract('3N7XfieeJ8dHyMJfs7amukzxKB1PfMXzHzi', '<seed>')
contract.faucet()
```

#### Working with oracles
Querrying oracles:
```python
import pywaves as pw

oracle = pw.Oracle(oracleAddress = '3P4PCxsJqMzQBALo8zANHtBDZRRquobHQp7')
# getting all data entries for an oracle
print(oracle.getData())
# getting data for a specific key of an oracle
print(oracle.getData('order_total_EeH5DRjdMnoYDhNbtkLsRNZq95etJUqWtvMDBCXojBoy'))
# getting all data entries of an oracle filtered by a regular expression
print(oracle.getData(regex = '^order_total_.*$'))
```
Storing data in an oracle:
```python
import pywaves as pw

pw.setNode('https://testnode1.wavesnodes.com', 'T')

oracle = pw.Oracle(seed='<your seed here>')
print(oracle.storeData('oracle_test', 'string', 'test entry from oracle class'))
```

#### Playing with Waves Matcher node (DEX):
```python
import pywaves as pw
Expand Down
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from .asset import *
from .order import *
from .contract import *
from .oracle import *

OFFLINE = False
NODE = 'https://nodes.wavesnodes.com'
Expand Down
1 change: 0 additions & 1 deletion address.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,6 @@ def issueSmartAsset(self, name, description, quantity, scriptSource, decimals=0,
"proofs": [ signature ],
"script": 'base64:' + script
})
print(data)
req = pywaves.wrapper('/transactions/broadcast', data)
if pywaves.OFFLINE:
return req
Expand Down
2 changes: 1 addition & 1 deletion contract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import pywaves as pw

class Contract(object):
class Oracle(object):

def __init__(self, contractAddress, seed):
self.contractAddress = contractAddress
Expand Down
39 changes: 39 additions & 0 deletions oracle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import pywaves as pw

class Oracle(object):

def __init__(self, oracleAddress = None, seed = None):
if seed != None:
self.oracleAddress = pw.Address(seed=seed)
else:
self.oracleAddress = oracleAddress

def getData(self, key = None, regex = None):
if key == None and regex == None:
result = self._getDataWithoutKey()
elif key != None:
result = self._getDataWithKey(key)
elif regex != None:
result = self._getDataWithRegex(regex)

return result

def _getDataWithRegex(self, regex):
print(pw.NODE + '/addresses/data/' + self.oracleAddress + '?matches=' + regex)
return requests.get(pw.NODE + '/addresses/data/' + self.oracleAddress + '?matches=' + regex).json()

def _getDataWithoutKey(self):
return requests.get(pw.NODE + '/addresses/data/' + self.oracleAddress).json()

def _getDataWithKey(self, key):
return requests.get(pw.NODE + '/addresses/data/' + self.oracleAddress + '/' + key).json()['value']

def storeData(self, key, type, dataEntry):
dataToStore = [{
'type': type,
'key': key,
'value': dataEntry
}]

return self.oracleAddress.dataTransaction(dataToStore)

0 comments on commit 73583e7

Please sign in to comment.