-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmint.py
166 lines (138 loc) · 5.95 KB
/
mint.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from flow_config import *
class CreateCollection():
def __init__(self, account: Address):
self.account_address = Address.from_hex(account)
async def run(self, ctx: Config):
async with flow_client(
host=ctx.access_node_host, port=ctx.access_node_port
) as client:
account = await client.get_account(address=self.account_address)
account_address = self.account_address
new_signer =ctx.service_account_signer
latest_block = await client.get_latest_block()
proposer = await client.get_account_at_latest_block(
address=account_address.bytes
)
transaction = Tx(
code=open('CreateCollection.cdc').read(),
reference_block_id=latest_block.id,
payer=account_address,
proposal_key=ProposalKey(
key_address=account_address,
key_id=ctx.service_account_key_id,
key_sequence_number=proposer.keys[0].sequence_number,
),
).add_authorizers(account_address).with_envelope_signature(
account_address,0,new_signer,
)
response = await client.send_transaction(transaction=transaction.to_signed_grpc())
transaction_id = response.id
transaction = await client.get_transaction(id=transaction_id)
print("transaction ID: {}".format(transaction_id.hex()))
print("transaction payer: {}".format(transaction.payer.hex()))
print(
"transaction proposer: {}".format(
transaction.proposal_key.address.hex()
)
)
class MintNFT():
def __init__(self, sender: Address, receiver: Address, metadata: dict):
self.account_address = Address.from_hex(sender)
self.receiver_address = Address.from_hex(receiver)
kvpair= []
for key, value in metadata.items():
kvpair.append(KeyValuePair(String(key), String(value)))
self.metadata = Dictionary(kvpair)
async def run(self, ctx: Config):
async with flow_client(
host=ctx.access_node_host, port=ctx.access_node_port
) as client:
account = await client.get_account(address=self.account_address)
account_address = self.account_address
new_signer =ctx.service_account_signer
latest_block = await client.get_latest_block()
proposer = await client.get_account_at_latest_block(
address=account_address.bytes
)
transaction = Tx(
code=open("MintNFT.cdc").read(),
reference_block_id=latest_block.id,
payer=account_address,
proposal_key=ProposalKey(
key_address=account_address,
key_id=0,
key_sequence_number=proposer.keys[0].sequence_number,
),
).add_arguments(self.receiver_address, self.metadata).add_authorizers(account_address).with_envelope_signature(
account_address,
0,
new_signer,
)
response = await client.send_transaction(transaction=transaction.to_signed_grpc())
transaction_id = response.id
transaction = await client.get_transaction(id=transaction_id)
print("transaction ID: {}".format(transaction_id.hex()))
print("transaction payer: {}".format(transaction.payer.hex()))
print(
"transaction proposer: {}".format(
transaction.proposal_key.address.hex()
)
)
return transaction_id.hex()
class RetrieveMetadata():
def __init__(self, receiver: Address, id: int) -> None:
self.receiver_address = Address.from_hex(receiver)
self.id=id
async def run(self, ctx: Config):
script = Script(
code=open("ViewNFT.cdc").read(),
arguments=[self.receiver_address, UInt64(self.id)],
)
async with flow_client(
host=ctx.access_node_host, port=ctx.access_node_port
) as client:
complex_script = await client.execute_script(
script=script
# , block_id
# , block_height
)
if not complex_script:
raise Exception("Script execution failed")
script_result: Value = complex_script
m = script_result.as_type(Dictionary).value
result = {}
for obj in m:
result[str(obj.key)]=str(obj.value)
print(result)
return result
class RetrieveAllNFTs():
def __init__(self, receiver: Address) -> None:
self.receiver_address = Address.from_hex(receiver)
async def run(self, ctx: Config):
script = Script(
code=open("RetrieveAll.cdc").read(),
arguments=[self.receiver_address],
)
async with flow_client(
host=ctx.access_node_host, port=ctx.access_node_port
) as client:
complex_script = await client.execute_script(
script=script
# , block_id
# , block_height
)
if not complex_script:
raise Exception("Script execution failed")
script_result: Value = complex_script
m = script_result.as_type(Array).value
result = []
for obj in m:
result.append(obj.value)
print(result)
return result
# a = CreateCollection('0x429b022e4a4860c5')
# asyncio.run(a.run(ctx = Config("./flow/flow.json", 'web3hacks')))
# b = MintCert('0x429b022e4a4860c5', '0x429b022e4a4860c5', {"Name": "Moiz", "IPFS": "SFDSDFSDF"})
# asyncio.run(b.run(ctx = Config("./flow/flow.json", 'web3hacks')))
c = RetrieveAllNFTs('0x429b022e4a4860c5')
asyncio.run(c.run(ctx = Config("./flow/flow.json", 'web3hacks')))