uuosio / ascdk

MIT License
12 stars 9 forks source link

Add a @apply decorator #62

Closed jafri closed 2 years ago

jafri commented 2 years ago

In some cases it would be very useful to provide a custom apply function that the contract uses instead of the generated one

For example a contract with just this:

export function apply(receiver: u64, firstReceiver: u64, action: u64): void {
    if (receiver != firstReceiver) {
        requireRecipient(Name.fromString("account"))
    }
    return;
}
learnforpractice commented 2 years ago

commit aa450d7555d001b908a4d3c44147d8624abdb404 should satisfy your need.

jafri commented 2 years ago

Yup that works!

One interesting thing though is that WASM from ascdk here is 4KB while same code from blanc++ is 124 bytes. Roughly 30-40x larger.

ascdk code

import { Name, requireRecipient } from "as-chain"

@contract
class ForwardedContract {}

export function apply(receiver: u64, firstReceiver: u64, action: u64): void {
    if (receiver != firstReceiver) {
            requireRecipient(Name.fromString("sf"))
    }
    return;
}

blanc++

#include <eosio/eosio.hpp>

class [[eosio::contract]] forwarder : public eosio::contract {
public:
   using contract::contract;
};

extern "C" {
   [[eosio::wasm_entry]]
   void apply( uint64_t receiver, uint64_t code, uint64_t action ) {
      if(code != receiver) {
         require_recipient("sf"_n);
      }
   }
}
jafri commented 2 years ago

Ah I can achieve same result with this:

import * as env from 'as-chain/env'

@contract
class ForwardedContract {}

export function apply(receiver: u64, firstReceiver: u64, action: u64): void {
    if (receiver != firstReceiver) {
        env.require_recipient(14033216438886465536); // proton encode:name sf
    }
}