anchor.diff:                     ttl      => 0 %% Not used
                     }),
   
  -    ConsensusVersion = aec_hard_forks:protocol_effective_at_height(Height),
  -
  -    case aetx:check_from_contract(Tx, Trees, Height, ConsensusVersion) of
  -        {ok, Trees1} ->
  -            {ok, Trees2} = aetx:process_from_contract(Tx, Trees1, Height, ConsensusVersion),
  -            State1 = State#state{ trees = Trees2 },
  -            {ok, State1};
  -        {error, _} = E -> E
  -    end.
  -
  -oracle_extend(Oracle,_Sign, Fee, TTL,
  -              State = #state{ trees   = Trees,
  -                              height  = Height,
  -                              account = ContractKey} = State) ->
  -    AT = aec_trees:accounts(Trees),
  -    {value, Account} = aec_accounts_trees:lookup(ContractKey, AT),
  -    Nonce = aec_accounts:nonce(Account) + 1,
  +    apply_transaction(Tx, State).
  +
  +oracle_extend(Oracle,_Sign, Fee, TTL, State) ->
  +    Nonce = next_nonce(Oracle, State),
       {ok, Tx} =
  -        aeo_extend_tx:new(#{oracle    => Oracle,
  +        aeo_extend_tx:new(#{oracle     => Oracle,
                               nonce      => Nonce,
                               oracle_ttl => {delta, TTL},
                               fee        => Fee,
                               ttl        => 0 %% Not used
                              }),
  -    ConsensusVersion = aec_hard_forks:protocol_effective_at_height(Height),
  -    case aetx:check_from_contract(Tx, Trees, Height, ConsensusVersion) of
  -        {ok, Trees1} ->
  -            {ok, Trees2} =
  -                aetx:process_from_contract(Tx, Trees1, Height, ConsensusVersion),
  -            State1 = State#state{ trees = Trees2 },
  -            {ok, State1};
  -        {error, _} = E -> E
  -    end.
  +    apply_transaction(Tx, State).
   
   oracle_get_answer(OracleId, QueryId, #state{ trees = Trees } =_State) ->
       case aeo_state_tree:lookup_query(OracleId, QueryId,
  
positive.code:                                     gas_price  => 0,
                                      call_data  => CallData,
                                      call_stack => CallStack }),
              do_call_contract(CallTx, ContractKey, Target, Nonce, Trees, State, Height,
                               ConsensusVersion);
          none -> {error, {no_such_contract, Target}}
      end.
  
  do_call_contract(CallTx, ContractKey, Target, Nonce, Trees,
                   State, Height, ConsensusVersion) ->
      case aetx:check_from_contract(CallTx, Trees, Height, ConsensusVersion) of
          Err = {error, _} -> Err;
          {ok, Trees1} ->
              {ok, Trees2} =
                  aetx:process_from_contract(CallTx, Trees1,
                                             Height, ConsensusVersion),
              CallId  = aect_call:id(ContractKey, Nonce, Target),
              Call    = aect_call_state_tree:get_call(Target, CallId,
                                                      aec_trees:calls(Trees2)),
              GasUsed = aect_call:gas_used(Call),
              Result  =
                  case aect_call:return_type(Call) of
                      %% TODO: currently we don't set any
                      %%       sensible return value on exceptions
                      error ->
                          aevm_chain_api:call_exception(out_of_gas, GasUsed);
                      ok ->
                          Bin = aect_call:return_value(Call),
                          aevm_chain_api:call_result(Bin, GasUsed)
                  end,
              {ok, Result, State#state{ trees = Trees2}}
      end.
  
  
  %% -- Internal functions -----------------------------------------------------
  
  
negative.code: %% @doc Spend money from the contract account.
  -spec spend(aec_keys:pubkey(), non_neg_integer(), chain_state()) ->
            {ok, chain_state()} | {error, term()}.
  spend(Recipient, Amount, State = #state{ account = ContractKey }) ->
      Nonce = next_nonce(State),
      {ok, SpendTx} = aec_spend_tx:new(#{ sender => ContractKey
                                        , recipient => Recipient
                                        , amount => Amount
                                        , fee => 0
                                        , nonce => Nonce
                                        , payload => <<>>}),
      apply_transaction(SpendTx, State).
  
  %%    Oracle
  -spec oracle_register(aec_keys:pubkey(), binary(), non_neg_integer(),
                        non_neg_integer(), aeso_sophia:type(), aeso_sophia:type(), chain_state()) ->
      {ok, aec_keys:pubkey(), chain_state()} | {error, term()}.
  oracle_register(AccountKey,_Sign, QueryFee, TTL, QuerySpec, ResponseSpec,
                  State = #state{account = ContractKey}) ->
      Nonce = next_nonce(AccountKey, State),
      %% Note: The nonce of the account is incremented.
      %% This means that if you register an oracle for an account other than
      %% the contract account through a contract that contract nonce is incremented
      %% "behind your back".
      BinaryQuerySpec = aeso_data:to_binary(QuerySpec, 0),
      BinaryResponseSpec = aeso_data:to_binary(ResponseSpec, 0),
      Spec =
  