-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_update_metadata.py
126 lines (104 loc) · 3.47 KB
/
test_update_metadata.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Example code to update metadata as part of TRANSFER tx
stage 1. Alice creates an asset and sets Bob as the owner with price=100
stage 2. Bob transfers the asset to Alice with price=200
stage 3. Alice transfers the asset to Alice with price=300 (only metadata is changed)
stage 4. Alice transfers the asset to Alice with price=400 (only metadata is changed)
Print the fulfilled_transfer_tx or its equivalent to see the tx items
"""
#%%
from resdb_driver import Resdb
from resdb_driver.transaction import Transaction
from copy import deepcopy
db_root_url = "https://resdb.free.beeceptor.com"
db = Resdb(db_root_url)
from resdb_driver.crypto import generate_keypair
alice, bob = generate_keypair(), generate_keypair()
#%%
# Stage 1
# create a digital asset for Alice
game_boy_token = {
"data": {
"token_for": {"game_boy": {"serial_number": "LR35902"}},
"description": "Time share token. Each token equals one hour of usage.",
},
}
#%%
# prepare the transaction with the digital asset and issue 1 token for Bob
prepared_token_tx = db.transactions.prepare(
operation="CREATE",
signers=alice.public_key,
recipients=[([bob.public_key], 1)],
asset=game_boy_token,
)
prepared_token_tx["metadata"] = {"price": "100"} #metadata has to be a dict
#%%
# fulfill the tnx
fulfilled_token_tx = db.transactions.fulfill(
prepared_token_tx, private_keys=alice.private_key
)
#%% stage 2
transfer_asset = {"id": fulfilled_token_tx["id"]}
output_index = 0
output = fulfilled_token_tx["outputs"][output_index]
transfer_input = {
"fulfillment": output["condition"]["details"],
"fulfills": {"output_index": output_index, "transaction_id": transfer_asset["id"]},
"owners_before": output["public_keys"],
}
#%%
prepared_transfer_tx = db.transactions.prepare(
operation="TRANSFER",
asset=transfer_asset,
inputs=transfer_input,
metadata={"price": "200"},
recipients=[([alice.public_key], 1)],
)
#%%
fulfilled_transfer_tx = db.transactions.fulfill(
prepared_transfer_tx, private_keys=bob.private_key
)
# stage 3
# Change the metadata
#%%
transfer_asset = {"id": fulfilled_token_tx["id"]}
output_index = 0
output = fulfilled_transfer_tx["outputs"][output_index]
transfer_input = {
"fulfillment": output["condition"]["details"],
"fulfills": {"output_index": output_index, "transaction_id": fulfilled_transfer_tx["id"]},
"owners_before": output["public_keys"],
}
prepared_transfer_tx = db.transactions.prepare(
operation="TRANSFER",
asset=transfer_asset,
inputs=transfer_input,
metadata={"price": "300"},
recipients=[([alice.public_key], 1)],
)
fulfilled_transfer_tx = db.transactions.fulfill(
prepared_transfer_tx, private_keys=alice.private_key
)
# stage 4
# chage the metadata again
#%%
transfer_asset = {"id": fulfilled_token_tx["id"]}
output_index = 0
output = fulfilled_transfer_tx["outputs"][output_index]
transfer_input = {
"fulfillment": output["condition"]["details"],
"fulfills": {"output_index": output_index, "transaction_id": fulfilled_transfer_tx["id"]},
"owners_before": output["public_keys"],
}
prepared_transfer_tx = db.transactions.prepare(
operation="TRANSFER",
asset=transfer_asset,
inputs=transfer_input,
metadata={"price": "400"},
recipients=[([alice.public_key], 1)],
)
fulfilled_transfer_tx = db.transactions.fulfill(
prepared_transfer_tx, private_keys=alice.private_key
)
#%%
sent_transfer_tx = db.transactions.send_commit(fulfilled_transfer_tx) # use this to commit whenever required