-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (79 loc) · 3.07 KB
/
main.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
import flask
from flask_cors import CORS
from web3 import Web3
from ocean_utils.agreements.service_types import ServiceTypes
from mantaray_utilities.events import subscribe_event
from ocean_keeper import Keeper
from squid_py import (
Ocean,
Config
)
from src import default_config_dict
from src.account import get_account
from src.metadata import get_metadata
FLASK_APPLICATION_NAME = 'ocean-protocol'
TRANSACTION_RULE = '/transaction'
config_dict = default_config_dict # ? use this dict? Do we want to input more configurations? If so, how and why?
configuration = Config(config_dict=config_dict)
squid_py.ConfigProvider.set_config(configuration)
app = flask.Flask(FLASK_APPLICATION_NAME)
CORS(app)
ocn = Ocean(configuration)
keeper = Keeper.get_instance()
MARKET_PLACE_PROVIDER_ADDRESS = Web3.toChecksumAddress(configuration.provider_address)
@app.route(TRANSACTION_RULE, methods=['POST'])
def process_transaction():
"""Route for making a transaction between seller and buyer
Request JSON:
{
"publisher": {
"address": <address point>,
"private_key": <private key>
},
"consumer": {
"address": <address point>,
"private_key": <private key>
},
"price": <float representing price>,
"ipfs": {
"endpoint": <server endpoint>,
"hash": <hash value for server access>,
"type": <server access type>
}
}
"""
# initialize return dict
data = {'success': False}
if flask.request.method == 'POST':
# get file containing all information needed for transaction happen
_file = flask.request.files.get('transaction')
if _file:
publisher_acct = get_account(_file.get('publisher'))
consumer_acct = get_account(_file.get('consumer'))
metadata = get_metadata(_file)
ddo = ocn.assets.create(metadata, publisher_acct, providers=[MARKET_PLACE_PROVIDER_ADDRESS])
service_index = ddo.get_service(ServiceTypes.ASSET_ACCESS).index
agreement_id = ocn.assets.order(
ddo.did,
service_index,
consumer_acct,
provider_address=MARKET_PLACE_PROVIDER_ADDRESS
)
subscribe_event('created agreement', keeper, agreement_id)
subscribe_event('lock reward', keeper, agreement_id)
subscribe_event('access secret store', keeper, agreement_id)
subscribe_event('escrow reward', keeper, agreement_id)
ocn.assets.consume(
agreement_id,
ddo.did,
service_index,
consumer_acct,
configuration.downloads_path
)
# update request status
data.update({
'success': True
})
return flask.jsonify(data)
if __name__ == '__main__':
app.run()