3

v1.0.6

I am trying to make my contract react to the token transfer. The code compiles fine, but I get the error when I do

>cleos set contract accountsale . accountsale.wast accountsale.abi
Reading WAST/WASM from accountsale.wast...
Assembling WASM...
Publishing contract...
1648512ms thread-0   main.cpp:2712                 main                 
] Failed with error: Assert Exception (10)

nodeos log:

1648511ms thread-0   http_plugin.cpp:405           handle_exception     
] FC Exception encountered while processing chain.push_transaction
1648511ms thread-0   http_plugin.cpp:406           handle_exception     
] Exception Details: 10 assert_exception: Assert Exception
!"unresolvable": env.free
{"module":"env","export":"free"}
thread-0  wasm_interface.hpp:41 resolve

{"mod_name":"env","export_name":"free"}
thread-0  wasm_interface.hpp:43 resolve

{"_pending_console_output.str()":""}
thread-0  apply_context.cpp:62 exec_one

accountsale.abi

{
  "____comment": "This file was generated by eosio-abigen. DO NOT EDIT - 2018-06-24T18:15:05",
  "version": "eosio::abi/1.0",
  "types": [],
  "structs": [{
      "name": "hi",
      "base": "",
      "fields": [{
          "name": "username",
          "type": "name"
        }
      ]
    }
  ],
  "actions": [{
      "name": "hi",
      "type": "hi",
      "ricardian_contract": ""
    }
  ],
  "tables": [],
  "ricardian_clauses": [],
  "error_messages": [],
  "abi_extensions": []
}

accountsale.cpp

#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/types.hpp>
#include <eosiolib/currency.hpp>

#include <math.h>
#include <eosiolib/dispatcher.hpp>

using namespace eosio;

class accountsale : eosio::contract {
  public:
      using contract::contract;

      accountsale(account_name self_account_name):contract(self_account_name){
      }

      /// @abi action
      void hi(account_name username){
        print( "Selling account ",name{username});  
      }

      void on(const currency::transfer& t){
         print("on\n");
      }

      void apply( account_name contract, account_name act ){
         print( "apply to contract ",name{contract}," act ",name{act}," \n");   
         switch(act){
            case N(transfer):
               print( " action transfer");
               on(unpack_action_data<currency::transfer>());
               break;
            case N(sellaccount):
               print( " call sellaccount");
               break;
            default:
            return;
         };
         if( act == N(transfer) ) {

            return;
         }
      }
};

// add extern C api?
extern "C" {
   void apply( uint64_t receiver, uint64_t code, uint64_t action ) {
      accountsale  as( receiver );
      as.apply( code, action );
      eosio_exit(0);
   }
}
1

Please take a look at MonsterEOS.io contract, I use an extended macro to support the token::transfer detection. This is the function that listens for the transfer action: https://github.com/leordev/monstereos/blob/master/contracts/pet/pet.cpp#L197-L234

The above code just add funds to an accounts table with the deposited amount.

This is the ABI extension that you need to add to your code: https://github.com/leordev/monstereos/blob/master/contracts/pet/pet.cpp#L308-L347 - You will need to keep alternating by commenting/uncommenting this macro if you want to generate ABI with eosiocpp -g

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.