1

I want to extend the Hello contract (as an exercise)

How do i execute "hi" action when notified by eosio.token contract of an EOS transfer ?

I understood i have to extend EOSIO_ABI macro to handle eosio.token transfer notification, but i cannot get it working.

Thank you

2
  • 1
    Maybe you could edit your post to include the code you have written, then we will find your error easily. Sep 10, 2018 at 14:55
  • 1
    Did you provide the permission of eosio.code to your contract? you are using eosio.hpp as a header file so first you have to provide the permission of eosio.code to your contract, as Philip said it would be better if you share code. Sep 11, 2018 at 4:13

1 Answer 1

1

I finally found a way by redefining EOSIO_ABI.

The "transfer" function in my contract get called when an EOS transfer is done through eosio.token contract.

#undef EOSIO_ABI
#define EOSIO_ABI( TYPE, MEMBERS ) \
extern "C" { \
  void apply( uint64_t receiver, uint64_t code, uint64_t action ) { \
     if( action == N(onerror)) { \
     /* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ \
     eosio_assert(code == N(eosio), "onerror action's are only valid from the \"eosio\" system account"); \
  } \
  auto self = receiver; \
  if( code == self || code == N(eosio.token) || action == N(onerror) ) { \
     TYPE thiscontract( self ); \
     switch( action ) { \
        EOSIO_API( TYPE, MEMBERS ) \
     } \
     /* does not allow destructor of thiscontract to run: eosio_exit(0); */ \
     } \
  } \
}

 EOSIO_ABI(ping_contract, (ping) (transfer) )

I used something like this for the permission:

cleos set account permission CONTRACT_ACCOUNT active '{"threshold": 1,"keys": [{"key":"CONTRACT_ACCOUNT_PUBKEY","weight":1}],"accounts": [{"permission":{"actor":"CONTRACT_ACCOUNT","permission":"eosio.code"},"weight":1}]}' owner -p CONTRACT_ACCOUNT@active
1
  • Actually you don' need to redefine EOSIO_ABI if you provided the permission , all you have to do is to make a new .cpp file and define a action that you want and use the transfer action of eosio.token. Also you have to import <eosiolib/eosio.hpp> Sep 11, 2018 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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