anchor.diff:  	end,
       aect_state_tree:enter_contract(NewContract, ContractsTree).
   
  -do_spend(Recipient, ContractKey, Amount, Trees, Height) ->
  -    AccountTree = aec_trees:accounts(Trees),
  -    {value, Account} = aec_accounts_trees:lookup(ContractKey, AccountTree),
  -    Nonce = aec_accounts:nonce(Account) + 1,
  -    ConsensusVersion = aec_hard_forks:protocol_effective_at_height(Height),
  -    {ok, SpendTx} = aec_spend_tx:new(#{ sender => ContractKey
  -                                      , recipient => Recipient
  -                                      , amount => Amount
  -                                      , fee => 0
  -                                      , nonce => Nonce
  -                                      , payload => <<>>}),
  -    case aetx:check_from_contract(SpendTx, Trees, Height, ConsensusVersion) of
  -        {ok, Trees1} ->
  -            aetx:process_from_contract(SpendTx, Trees1, Height, ConsensusVersion);
  -        Error -> Error
  -    end.
  -
  -do_oracle_register(Spec, Height, Trees) ->
  -    {ok, Tx} = aeo_register_tx:new(Spec),
  +apply_transaction(Tx, #state{ trees = Trees, height = Height } = State) ->
       ConsensusVersion = aec_hard_forks:protocol_effective_at_height(Height),
       case aetx:check_from_contract(Tx, Trees, Height, ConsensusVersion) of
           {ok, Trees1} ->
  -            aetx:process_from_contract(Tx, Trees1, Height, ConsensusVersion);
  -        Error -> Error
  -    end.
  +            {ok, Trees2} =
  +                aetx:process_from_contract(Tx, Trees1, Height, ConsensusVersion),
  +            State1 = State#state{ trees = Trees2 },
  +            {ok, State1};
  +        {error, _} = E -> E
  +    end.
  +
  +next_nonce(State = #state{ account = ContractKey }) ->
  +    next_nonce(ContractKey, State).
  +
  +next_nonce(Addr, #state{ trees = Trees }) ->
  +    AT = aec_trees:accounts(Trees),
  +    {value, Account} = aec_accounts_trees:lookup(Addr, AT),
  +    aec_accounts:nonce(Account) + 1.
  +
  
positive.code:                            fee           => 0,
                             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 },
              {oracle_query_tx, OTx} = aetx:specialize_type(Tx),
              Query = aeo_query:new(OTx, Height),
              Id = aeo_query:id(Query),
  
              {ok, Id, State1};
  
          {error, _} = E -> E
      end.
  
  oracle_respond(Oracle, QueryId,_Sign, Response,
                 #state{ trees   = Trees,
                         height  = Height,
                         account = ContractKey} = State) ->
      %% TODO: Check signature
      AT = aec_trees:accounts(Trees),
      {value, Account} = aec_accounts_trees:lookup(ContractKey, AT),
      Nonce = aec_accounts:nonce(Account) + 1,
  
      {ok, Tx} = aeo_response_tx:new(
                   #{oracle   => Oracle,
  
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 =
  