Skip to content

Commit

Permalink
Simple Rules example
Browse files Browse the repository at this point in the history
This commit enforces two rules
- Size of memo (exactly 8 characters long)
- Memo has to contain all digits.
  • Loading branch information
eosauthority committed Aug 15, 2018
1 parent 93d6e44 commit 6f795e9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
44 changes: 42 additions & 2 deletions README.md
@@ -1,2 +1,42 @@
# incoming-transfer-rules
On EOS, incoming transfers can have rules applied to them. Exchanges and accounts can enforce their memo fields to stop transfers that don't enter the correct details on memo for example.
# Transfer filter contract
EOS has a method to check incoming transfers and This contract makes token transfers to it fail, if memo in those transfers does not
satisfy certain conditions. Currently set conditions are:
* Memo has to be exactly 8 symbols long;
* Contain only digits;

You add different asserts in ontransfer action to modify these conditions.

Only transfers wher contract is receiver are currently filtered.

### Build
```commandline
eosiocpp -o transferfilter.wast transferfilter.cpp
```

### Deploy
```commandline
cleos set code <your-account-name> transferfilter.wast
```

### Test
#### Withouth memo
```commandline
$ cleos transfer eosio <your-account-name> "10.0000 SYS"
Error 3050003: eosio_assert_message assertion failure
```

#### With non digit
```commandline
$ cleos transfer eosio <your-account-name> "10.0000 SYS" "1234567a"
Error 3050003: eosio_assert_message assertion failure
```

#### Valid
```commandline
$ cleos transfer eosio <your-account-name> "10.0000 SYS" "00345670"
executed transaction: 747ceb78a0359ffd426df96d3ce4c844bfe7402e1634638a14ebc2d91c676a91 136 bytes 607 us
# eosio.token <= eosio.token::transfer {"from":"eosio","to":"<your-account-name>","quantity":"10.0000 SYS","memo":"00345670"}
# eosio <= eosio.token::transfer {"from":"eosio","to":"<your-account-name>","quantity":"10.0000 SYS","memo":"00345670"}
# <your-account-name> <= eosio.token::transfer {"from":"eosio","to":"<your-account-name>","quantity":"10.0000 SYS","memo":"00345670"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
```
41 changes: 41 additions & 0 deletions transferfilter.cpp
@@ -0,0 +1,41 @@
#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <cctype>

constexpr account_name token_contract = N(eosio.token);

using namespace eosio;

class transfer_filter : public contract {
public:
transfer_filter(account_name self) : contract(self) {}

void ontransfer(const account_name from, const account_name to, const asset& quantity,
const std::string& memo) {

if( to == _self ) {
const char* error_str = "Memo has to be exactly 8 symbols long and contain only digits";
//Rule 1: Memo has to be 8 characters long
eosio_assert(memo.length() == 8, error_str);
//Rule 2: Memo has to be all digits
eosio_assert(std::all_of(memo.begin(), memo.end(), ::isdigit), error_str);
}
}
};

extern "C" {
void apply(uint64_t receiver, uint64_t code, uint64_t action) {

auto self = receiver;
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"); \
}

if( code == token_contract && action == N(transfer) ) {
transfer_filter contract(self);
eosio::execute_action(&contract, &transfer_filter::ontransfer);
}
}
}

0 comments on commit 6f795e9

Please sign in to comment.