|
1 | | -# wrc20-examples |
| 1 | +# WRC-20 challenge examples |
| 2 | + |
2 | 3 | This repository contains examples of WRC20 tokens written in different languages. |
| 4 | + |
| 5 | +## Joining the challenge |
| 6 | + |
| 7 | +Have a look at [this gist](https://gist.github.com/axic/16158c5c88fbc7b1d09dfa8c658bc363), implement the logic for the following contract (pseudocode) in your favorite language: |
| 8 | + |
| 9 | +```javascript |
| 10 | +// main ewasm entry point |
| 11 | +function main() { |
| 12 | + if (eei_calldatasize() < 4) |
| 13 | + eei_revert(0, 0) |
| 14 | + let selector = eei_calldatacopy(0, 4) |
| 15 | + switch selector { |
| 16 | + case 0x9993021a: |
| 17 | + do_balance() |
| 18 | + case 0x5d359fbd: |
| 19 | + do_transfer() |
| 20 | + default: |
| 21 | + eei_revert(0, 0) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function do_balance() { |
| 26 | + if (eei_calldatasize() != 24) |
| 27 | + eei_revert(0, 0) |
| 28 | + |
| 29 | + let address = eei_calldatacopy(4, 20) |
| 30 | + // make sure that address is 160 bits here, |
| 31 | + // but storage key is 256 bits so need to pad it somehow |
| 32 | + let balance = eei_storageload(address) |
| 33 | + eei_return(balance) |
| 34 | +} |
| 35 | + |
| 36 | +function do_transfer() { |
| 37 | + if (eei_calldatasize() != 32) |
| 38 | + eei_revert(0, 0) |
| 39 | + |
| 40 | + let sender = eei_sender() |
| 41 | + let recipient = eei_calldatacopy(4, 20) |
| 42 | + let value = eei_calldatacopy(24, 8) |
| 43 | + let sender_balance = eei_storageload(sender) |
| 44 | + let recipient_balance = eei_storageload(recipient) |
| 45 | + |
| 46 | + if (sender_balance < value) |
| 47 | + eei_revert(0, 0) |
| 48 | + |
| 49 | + sender_balance -= value |
| 50 | + recipient_balance += value |
| 51 | + |
| 52 | + eei_storagestore(sender, sender_balance) |
| 53 | + eei_storagestore(recipient, recipient_balance) |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Then simply put it in a directory named after your language and send us a PR. |
0 commit comments