import copy


def fillMetadata (f, txid, extra):
  """
  Retrieves a transaction by id and constructs a dict that contains
  the given "extra" fields plus all the basic metadata (like inputs
  and outputs) as retrieved from Xaya Core.
  """

  rpc = f.env.createCoreRpc ()
  tx = rpc.gettransaction (txid)["hex"]
  data = rpc.decoderawtransaction (tx)

  res = copy.deepcopy (extra)
  res["txid"] = txid
  res["btxid"] = data["btxid"]
  res["inputs"] = [
    {"txid": i["txid"], "vout": i["vout"]}
    for i in data["vin"]
  ]

  outs = {}
  burn = 0
  for o in data["vout"]:
    spk = o["scriptPubKey"]
    if "nameOp" in spk:
      continue
    if "burn" in spk and spk["burn"] == b"g/game".hex ():
      burn += o["value"]
    if "address" in spk:
      if spk["address"] not in outs:
        outs[spk["address"]] = 0.0
      outs[spk["address"]] += o["value"]
  res["out"] = outs
  res["burnt"] = burn

  return res