contractsdk/cpp/example/erc20.cc

(开头部分) 6KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

#include "xchain/xchain.h"

struct ERC20 : public xchain::Contract {};

const std::string BALANCEPRE = "balanceOf_";
const std::string ALLOWANCEPRE = "allowanceOf_";

DEFINE_METHOD(ERC20, initialize) {
    xchain::Context* ctx = self.context();
    const std::string& caller = ctx->arg("caller");
    if (caller.empty()) {
        ctx->error("missing caller");
        return;
    }
    const std::string& totalSupply = ctx->arg("totalSupply");
    if (totalSupply.empty()) {
        ctx->error("missing totalSupply");
        return;
    }

    std::string key = BALANCEPRE + caller;
    ctx->put_object("totalSupply", totalSupply);
    ctx->put_object(key, totalSupply);
}

DEFINE_METHOD(ERC20, totalSupply) {
    xchain::Context* ctx = self.context();
    std::string value;
    if (ctx->get_object("totalSupply", &value)) {
        ctx->ok(value);
    } else {
        ctx->error("key not found");
    }
}

DEFINE_METHOD(ERC20, balance) {
    xchain::Context* ctx = self.context();
    const std::string& caller = ctx->arg("caller");
    if (caller.empty()) {
        ctx->error("missing caller");
        return;
    }
    
    std::string key = BALANCEPRE + caller;
    std::string value;
    if (ctx->get_object(key, &value)) {
        ctx->ok(value);
    } else {
        ctx->error("key not found");
    }
}

DEFINE_METHOD(ERC20, allowance) {
    xchain::Context* ctx = self.context();
    const std::string& from = ctx->arg("from");
    if (from.empty()) {
        ctx->error("missing from");
        return;
    }
   
后面还有 194 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。