Skip to content

Commit 3e96ffa

Browse files
xraymemoryMichael Anzuoni
andauthored
Add boolean parameter to enable multisig output (#16)
* Add boolean parameter to enable multisig output * Output instruction bytes rather than transaction * Add proper payload output * Dump all instructions as list of json objects * Explicitly write to sys.stdout Co-authored-by: Michael Anzuoni <[email protected]>
1 parent 350b80c commit 3e96ffa

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

program_admin/__init__.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import json
12
import os
3+
import sys
4+
from dataclasses import asdict
25
from pathlib import Path
36
from typing import Dict, List, Literal, Tuple
47

@@ -151,6 +154,7 @@ async def send_transaction(
151154
self,
152155
instructions: List[TransactionInstruction],
153156
signers: List[Keypair],
157+
dump_instructions: bool = False,
154158
):
155159
if not instructions:
156160
return
@@ -166,6 +170,25 @@ async def send_transaction(
166170

167171
ix_index = 1
168172

173+
if dump_instructions:
174+
dump_output = []
175+
for instruction in instructions:
176+
instruction_output = {
177+
"program_id": instruction.program_id,
178+
"data": instruction.data.decode(),
179+
}
180+
accounts = []
181+
for account in instruction.keys:
182+
account_data = {
183+
"pubkey": str(account.pubkey),
184+
"is_signer": account.is_signer,
185+
"is_writable": account.is_writable,
186+
}
187+
accounts.append(account_data)
188+
instruction_output["accounts"] = accounts
189+
dump_output.append(instruction_output)
190+
sys.stdout.write(json.dumps(dump_output))
191+
169192
# FIXME: Ideally, we would compute the exact additional size of each
170193
# instruction, add it to the current transaction size and compare
171194
# that with PACKET_DATA_SIZE. But there is currently no method that
@@ -187,21 +210,25 @@ async def send_transaction(
187210
transaction.sign(*signers)
188211
ix_index += 1
189212

190-
response = await client.send_raw_transaction(
191-
transaction.serialize(),
192-
opts=TxOpts(
193-
skip_confirmation=False, preflight_commitment=self.commitment
194-
),
195-
)
213+
if not dump_instructions:
214+
response = await client.send_raw_transaction(
215+
transaction.serialize(),
216+
opts=TxOpts(
217+
skip_confirmation=False, preflight_commitment=self.commitment
218+
),
219+
)
220+
logger.debug(f"Transaction: {response['result']}")
196221

197222
logger.debug(f"Sent {ix_index} instructions")
198-
logger.debug(f"Transaction: {response['result']}")
199223

200224
remaining_instructions = instructions[ix_index:]
201225

202226
if remaining_instructions:
203227
logger.debug("Sending remaining instructions in separate transaction")
204228
await self.send_transaction(remaining_instructions, signers)
229+
else:
230+
if dump_instructions:
231+
return dump_output
205232

206233
async def sync(
207234
self,

program_admin/cli.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ def cli():
3535
)
3636
@click.option("--product", help="Public key of the product account")
3737
@click.option("--price", help="Public key of the price account")
38-
def delete_price(network, rpc_endpoint, program_key, keys, commitment, product, price):
38+
@click.option(
39+
"--dump",
40+
help="Output instructions rather than transact",
41+
envvar="DUMP",
42+
default=False,
43+
)
44+
def delete_price(
45+
network, rpc_endpoint, program_key, keys, commitment, product, price, dump
46+
):
3947
program_admin = ProgramAdmin(
4048
network=network,
4149
rpc_endpoint=rpc_endpoint,
@@ -57,6 +65,7 @@ def delete_price(network, rpc_endpoint, program_key, keys, commitment, product,
5765
program_admin.send_transaction(
5866
[instruction],
5967
[funding_keypair, product_keypair, price_keypair],
68+
dump_instructions=dump,
6069
)
6170
)
6271

@@ -74,8 +83,14 @@ def delete_price(network, rpc_endpoint, program_key, keys, commitment, product,
7483
)
7584
@click.option("--price", help="Public key of the price account")
7685
@click.option("--min-pub", help="Minimum publishers value to set for this price")
86+
@click.option(
87+
"--dump",
88+
help="Output instructions rather than transact",
89+
envvar="DUMP",
90+
default=False,
91+
)
7792
def set_minimum_publishers_for_price(
78-
network, rpc_endpoint, program_key, keys, commitment, price, min_pub
93+
network, rpc_endpoint, program_key, keys, commitment, price, min_pub, dump
7994
):
8095
program_admin = ProgramAdmin(
8196
network=network,
@@ -92,8 +107,7 @@ def set_minimum_publishers_for_price(
92107

93108
asyncio.run(
94109
program_admin.send_transaction(
95-
[instruction],
96-
[funding_keypair, price_keypair],
110+
[instruction], [funding_keypair, price_keypair], dump_instructions=dump
97111
)
98112
)
99113

@@ -111,8 +125,14 @@ def set_minimum_publishers_for_price(
111125
)
112126
@click.option("--mapping", help="Public key of the mapping account")
113127
@click.option("--product", help="Public key of the product account")
128+
@click.option(
129+
"--dump",
130+
help="Output instructions rather than transact",
131+
envvar="DUMP",
132+
default=False,
133+
)
114134
def delete_product(
115-
network, rpc_endpoint, program_key, keys, commitment, mapping, product
135+
network, rpc_endpoint, program_key, keys, commitment, mapping, product, dump
116136
):
117137
program_admin = ProgramAdmin(
118138
network=network,
@@ -135,6 +155,7 @@ def delete_product(
135155
program_admin.send_transaction(
136156
[instruction],
137157
[funding_keypair, mapping_keypair, product_keypair],
158+
dump_instructions=dump,
138159
)
139160
)
140161

@@ -338,6 +359,7 @@ def sync(
338359
cli.add_command(list_accounts)
339360
cli.add_command(restore_links)
340361
cli.add_command(sync)
362+
cli.add_command(set_minimum_publishers_for_price)
341363

342364

343365
logger.remove()

tests/test_sync.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ async def test_sync(
381381
min_pub_instruction = instructions.set_minimum_publishers(
382382
pyth_program, funding_key.public_key, price_keypair.public_key, 10
383383
)
384+
385+
# Test instruction print output
386+
instruction_output = await program_admin.send_transaction(
387+
[min_pub_instruction], [funding_key, price_keypair], dump_instructions=True
388+
)
389+
assert isinstance(instruction_output, list)
390+
384391
await program_admin.send_transaction(
385392
[min_pub_instruction], [funding_key, price_keypair]
386393
)

0 commit comments

Comments
 (0)