Skip to content

Instantly share code, notes, and snippets.

@jyeshe
Last active October 6, 2021 10:54
Show Gist options
  • Save jyeshe/135c36287a642ff03837d8045350a6dd to your computer and use it in GitHub Desktop.
Save jyeshe/135c36287a642ff03837d8045350a6dd to your computer and use it in GitHub Desktop.
defmodule AeMdwWeb.DryRun.Runner do
@moduledoc """
Simulates transactions with dry run.
"""
alias AeMdw.Node.Db, as: DBN
# alias AeMdw.DryRun.Contract
alias AeMdw.DryRun.Runner.Contract
defmodule Contract do
@moduledoc """
Contract transactions for dry-running.
"""
alias AeMdw.Node.Db, as: DBN
alias AeMdw.Util
# @vm_fate_sophia_2 7
@abi_fate_sophia_1 3
# @sophia_contract_vsn_3 3
@balances_args []
@gas 1_000_000
# @gas_price 1_000_000_000
def new_balances_call(caller_pk, contract_pk) do
new_balances_call_tx(caller_pk, contract_pk, DBN.top_height_hash(true))
end
defp new_balances_call_tx(caller_pk, contract_pk, {_type, _height, block_hash}) do
{_tx_env, trees} = :aetx_env.tx_env_and_trees_from_hash(:aetx_contract, block_hash)
contracts = :aec_trees.contracts(trees)
contract_id =
contract_pk
|> :aect_state_tree.get_contract(contracts)
|> :aect_contracts.id()
call_data = :aeb_fate_abi.create_calldata('balances', @balances_args) |> Util.ok!
:aect_call_tx.new(%{
caller_id: :aeser_id.create(:account, caller_pk),
nonce: 0,
contract_id: contract_id,
abi_version: abi_version(),
amount: 0,
gas: @gas,
gas_price: min_gas_price(),
call_data: call_data,
fee: 100_000 * min_gas_price()
})
|> Util.ok!
end
def min_gas_price do
protocol = :aec_hard_forks.protocol_effective_at_height(1)
:erlang.max(
:aec_governance.minimum_gas_price(protocol),
:aec_tx_pool.minimum_miner_gas_price()
)
end
# defp vm_version, do: @vm_fate_sophia_2
defp abi_version, do: @abi_fate_sophia_1
end
@runner_pk <<13, 24, 60, 171, 170, 28, 99, 114, 174, 14, 112, 19, 49, 53, 233, 194, 46, 149,
172, 14, 114, 22, 38, 51, 153, 136, 58, 149, 27, 56, 30, 105>>
# @runner_sk <<230, 169, 29, 99, 60, 119, 207, 87, 113, 50, 157, 51, 84, 179, 188, 239, 27, 197,
# 224, 50, 196, 61, 112, 182, 211, 90, 249, 35, 206, 30, 183, 77, 206, 167, 173, 228,
# 112, 201, 249, 157, 157, 78, 64, 8, 128, 168, 111, 29, 73, 187, 68, 75, 98, 241,
# 26, 158, 187, 100, 187, 207, 235, 115, 254, 243>>
# @caller_pubkey <<0::256>>
@amount 1_000_000_000_000_000
def run(tx) do
{_type, _height, top_hash} = DBN.top_height_hash(false) |> IO.inspect
# {_type, _height, top_hash} = DBN.top_height_hash(true) |> IO.inspect
accounts = [%{pub_key: @runner_pk, amount: @amount}]
# accounts = []
txs = [tx]
:aec_dry_run.dry_run(top_hash, accounts, txs, tx_events: true)
end
def encode_new_balances_call(contract_pk) do
_call_tx = Contract.new_balances_call(@runner_pk, contract_pk)
# |> sign_tx()
#ser_tx = :aetx.serialize_to_binary(call_tx)
#:aeser_api_encoder.encode(:transaction, ser_tx)
end
#
# Private functions
#
#defp sign_tx(tx) do
# bin_network =
# tx
# |> :aetx.serialize_to_binary()
# |> :aec_governance.add_network_id()
# signatures = [:enacl.sign_detached(bin_network, @runner_sk)]
# tx
# |> :aetx_sign.new(signatures)
#end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment