2

I am creating a smart contract but came to a halt when writing the code.

Is there a way, in a smart contract, to prevent me from spending tokens transferred to me by another user through the smart contract until another action is called that unlocks it for spending?

  • Do you mean EOS token or some token your smart contract has created? – TeeAttack42 Feb 21 '19 at 14:25
0

Yes this is possible.

  1. Create a table with the user's name and whether they are allowed to spend their tokens or not.
  2. Check this table in the transfer function.
  3. When the appropriate action is called, update the table entry to permit transfers.

For example:

token code

ACTION transfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo){
  transfer_not_allowed_table table(contract, scope);
  eosio::check(table.find(from.value) == table.end(), "may not transfer");
  // rest of transfer code
}

transfer allowed code

TABLE allowed{
  eosio::name user;
  uint64_t primary_key() const{return user.value;}
};
typedef eosio::multi_index< "allowed"_n, allowed > transfer_not_allowed_table;

ACTION disallow(eosio::name user){
  transfer_not_allowed_table table(contract, scope);
  eosio::check(table.find(user) == table.end(), "already not allowed to transfer");
  table.emplace(_self, [&](auto & entry){
    entry.user = user;
  });
}

ACTION allow(eosio::name user){
  transfer_not_allowed_table table(contract, scope);
  eosio::check(table.find(user.value) != table.end(), "already allowed to transfer");
  table.erase(table.find(user.value));
}
|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.