Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions example/erc20.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ DEFINE_METHOD(ERC20, mint) {
ctx->error("get totalSupply error");
return;
}

int increaseSupplyint = atoi(increaseSupply.c_str());
int valueint = atoi(value.c_str());
int totalSupplyint = increaseSupplyint + valueint;
char buf[32];
snprintf(buf, 32, "%d", totalSupplyint);
ctx->put_object("totalSupply", buf);
ctx->put_object("totalSupply", buf);

std::string key = BALANCEPRE + caller;
if (!ctx->get_object(key, &value)) {
ctx->error("get caller balance error");
Expand All @@ -73,8 +73,8 @@ DEFINE_METHOD(ERC20, mint) {
valueint = atoi(value.c_str());
int callerint = increaseSupplyint + valueint;
snprintf(buf, 32, "%d", callerint);
ctx->put_object(key, buf);
ctx->put_object(key, buf);

ctx->ok(buf);
}

Expand All @@ -90,12 +90,12 @@ DEFINE_METHOD(ERC20, totalSupply) {

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

std::string key = BALANCEPRE + caller;
std::string value;
if (ctx->get_object(key, &value)) {
Expand All @@ -112,7 +112,7 @@ DEFINE_METHOD(ERC20, allowance) {
ctx->error("missing from");
return;
}

const std::string& to = ctx->arg("to");
if (to.empty()) {
ctx->error("missing to");
Expand All @@ -130,12 +130,8 @@ DEFINE_METHOD(ERC20, allowance) {

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

const std::string& from = ctx->initiator();

const std::string& to = ctx->arg("to");
if (to.empty()) {
ctx->error("missing to");
Expand All @@ -153,11 +149,11 @@ DEFINE_METHOD(ERC20, transfer) {
std::string value;
int from_balance = 0;
if (ctx->get_object(from_key, &value)) {
from_balance = atoi(value.c_str());
from_balance = atoi(value.c_str());
if (from_balance < token) {
ctx->error("The balance of from not enough");
return;
}
}
} else {
ctx->error("key not found");
return;
Expand All @@ -168,11 +164,11 @@ DEFINE_METHOD(ERC20, transfer) {
if (ctx->get_object(to_key, &value)) {
to_balance = atoi(value.c_str());
}

from_balance = from_balance - token;
to_balance = to_balance + token;
char buf[32];

char buf[32];
snprintf(buf, 32, "%d", from_balance);
ctx->put_object(from_key, buf);
snprintf(buf, 32, "%d", to_balance);
Expand All @@ -188,8 +184,8 @@ DEFINE_METHOD(ERC20, transferFrom) {
ctx->error("missing from");
return;
}
const std::string& caller = ctx->arg("caller");

const std::string& caller = ctx->initiator();
if (caller.empty()) {
ctx->error("missing caller");
return;
Expand All @@ -212,11 +208,11 @@ DEFINE_METHOD(ERC20, transferFrom) {
std::string value;
int allowance_balance = 0;
if (ctx->get_object(allowance_key, &value)) {
allowance_balance = atoi(value.c_str());
allowance_balance = atoi(value.c_str());
if (allowance_balance < token) {
ctx->error("The allowance of from_to not enough");
return;
}
}
} else {
ctx->error("You need to add allowance from_to");
return;
Expand All @@ -225,11 +221,11 @@ DEFINE_METHOD(ERC20, transferFrom) {
std::string from_key = BALANCEPRE + from;
int from_balance = 0;
if (ctx->get_object(from_key, &value)) {
from_balance = atoi(value.c_str());
from_balance = atoi(value.c_str());
if (from_balance < token) {
ctx->error("The balance of from not enough");
return;
}
}
} else {
ctx->error("From no balance");
return;
Expand All @@ -240,12 +236,12 @@ DEFINE_METHOD(ERC20, transferFrom) {
if (ctx->get_object(to_key, &value)) {
to_balance = atoi(value.c_str());
}

from_balance = from_balance - token;
to_balance = to_balance + token;
allowance_balance = allowance_balance - token;

char buf[32];
char buf[32];
snprintf(buf, 32, "%d", from_balance);
ctx->put_object(from_key, buf);
snprintf(buf, 32, "%d", to_balance);
Expand All @@ -258,12 +254,12 @@ DEFINE_METHOD(ERC20, transferFrom) {

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

const std::string& to = ctx->arg("to");
if (to.empty()) {
ctx->error("missing to");
Expand All @@ -280,11 +276,11 @@ DEFINE_METHOD(ERC20, approve) {
std::string from_key = BALANCEPRE + from;
std::string value;
if (ctx->get_object(from_key, &value)) {
int from_balance = atoi(value.c_str());
int from_balance = atoi(value.c_str());
if (from_balance < token) {
ctx->error("The balance of from not enough");
return;
}
}
} else {
ctx->error("From no balance");
return;
Expand All @@ -293,17 +289,14 @@ DEFINE_METHOD(ERC20, approve) {
std::string allowance_key = ALLOWANCEPRE + from + "_" + to;
int allowance_balance = 0;
if (ctx->get_object(allowance_key, &value)) {
allowance_balance = atoi(value.c_str());
allowance_balance = atoi(value.c_str());
}

allowance_balance = allowance_balance + token;
char buf[32];

char buf[32];
snprintf(buf, 32, "%d", allowance_balance);
ctx->put_object(allowance_key, buf);

ctx->ok("approve success");
}