While playing a game on Dapp if we want the user to donate some tokens in order to join the game, how can we accomplish it in the smart contract?


You would add a transfer command to your token's contract as can be seen here. There is also a simple token contract example here if you don't need all the bells and whistles.


void transfer( account_name from,
    account_name to,
    asset        quantity,
    string       memo );


void token::transfer( account_name from,
                      account_name to,
                      asset        quantity,
                      string       memo )
    eosio_assert( from != to, "cannot transfer to self" );
    require_auth( from );
    eosio_assert( is_account( to ), "to account does not exist");
    auto sym = quantity.symbol.name();
    stats statstable( _self, sym );
    const auto& st = statstable.get( sym );

    require_recipient( from );
    require_recipient( to );

    eosio_assert( quantity.is_valid(), "invalid quantity" );
    eosio_assert( quantity.amount > 0, "must transfer positive quantity" );
    eosio_assert( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );
    eosio_assert( memo.size() <= 256, "memo has more than 256 bytes" );

    sub_balance( from, quantity );
    add_balance( to, quantity, from );

Then you would make an RPC API call using eosjs + wallet provider of choice (scatter) to broadcast the transaction as a part of the logic to join the game.

You can see how I'm doing this in my EOS Chess application here.

class Scatter {

    constructor() {
        this.CHAIN_PROTOCOL = 'http';
        this.CHAIN_HOST = ''; //'mainnet.eoscalgary.io' //'nodes.get-scatter.com' //'br.eosrio.io'
        this.CHAIN_PORT = '8888' //8080' //80
        // const CHAIN_ADDRESS = CHAIN_PROTOCOL + '://' + CHAIN_HOST + ':' + CHAIN_PORT;
        this.CHAIN_ID = 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f';

        /* Eos and Scatter Setup */
        this.network = {
            protocol: this.CHAIN_PROTOCOL,
            blockchain: 'eos',
            host: this.CHAIN_HOST,
            port: this.CHAIN_PORT,
            chainId: this.CHAIN_ID,
            broadcast: true,
            sign: true

        document.addEventListener('scatterLoaded', function() {
            this.scatter = window.scatter
            window.scatter = null // very important to null out

            document.getElementById("scatterLogout").addEventListener('click', function() {
                this.scatter.forgetIdentity().catch(error => {
                alert("logged out of scatter");

            document.getElementById("scatterLogin").addEventListener('click', function(event) {

                let getIdentity = () => {
                    this.scatter.getIdentity({accounts:[this.network]}).then(identity => {
                        console.log(identity, "identitySuccess")
                    }).catch(error => {
                        console.log(error, "identityCrisis")


You can also see a more polished example in MonsterEOS's index.js.

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.