Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve cosmetics
  • Loading branch information
obrok committed Jan 13, 2023
commit 8cba8dadc922b09d7eab8195df128c3599fd12f9
25 changes: 14 additions & 11 deletions scripts/pricing/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import random
import subprocess
import json
from subprocess import PIPE
from tabulate import tabulate
import urllib.request

Expand All @@ -31,16 +30,16 @@ def random_salt():


def deploy(directory):
res = subprocess.run(['cargo', 'contract', 'instantiate', '--salt',
random_salt()] + COMMON_ARGS, cwd=directory, check=True, stdout=PIPE)
return json.loads(res.stdout.decode('utf-8'))
res = subprocess.check_output(['cargo', 'contract', 'instantiate', '--salt',
random_salt()] + COMMON_ARGS, cwd=directory)
return json.loads(res.decode('utf-8'))


def call(directory, contract, message, *args):
args = [x for a in args for x in ['--args', a]]
res = subprocess.run(['cargo', 'contract', 'call', '--contract', contract,
'--message', message] + args + COMMON_ARGS, cwd=directory, check=True, stdout=PIPE)
return json.loads(res.stdout.decode('utf-8'))
res = subprocess.check_output(['cargo', 'contract', 'call', '--contract', contract,
'--message', message] + args + COMMON_ARGS, cwd=directory)
return json.loads(res.decode('utf-8'))


def event_field(event, field):
Expand All @@ -49,6 +48,13 @@ def event_field(event, field):
return f['value']


def deployer_account_id(deploy_result):
setup_event = next(filter(
lambda e: e['name'] == 'Transfer' and account_id(event_field(e, 'to')) == adder_address, deploy_result['events']), None)

return account_id(event_field(setup_event, 'from'))


def account_id(value):
match value:
case {'Literal': account_id}: return account_id
Expand Down Expand Up @@ -79,10 +85,7 @@ def format_fee(fee):
deploy_result = deploy(args.adder_dir)

adder_address = deploy_result['contract']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach will return invalid address if there are contracts instantiated in the constructor use-ink/cargo-contract#777

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, given it's a bug in cargo contract, I say ignore until they fix it, then update cargo contract, no?

Copy link
Collaborator

@deuszx deuszx Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can inspect the .events[] field in the JSON and use the last element where .pallet == "Contracts" and .name = "Instantiated".

You will handle it however you want, I'm just raising a problem this code has with some contracts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep that in mind if I run into this problem.

setup_event = next(filter(
lambda e: e['name'] == 'Transfer' and account_id(event_field(e, 'to')) == adder_address, deploy_result['events']), None)

suri_address = account_id(event_field(setup_event, 'from'))
suri_address = deployer_account_id(deploy_result)
instantiate_fee = find_fee(deploy_result['events'], suri_address)

events = call(args.adder_dir, adder_address, 'add', '42')
Expand Down