Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ethereum conversion function #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
65 changes: 65 additions & 0 deletions eth/web3.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,68 @@ int eth_getTransactionReceipt(web3_ctx_t *web3, const tx_hash_t *tx_hash)
WEB3_TERMINATOR();
return 0;
}

int eth_convert(const uint256_t *amount, enum ETH_UNIT from, enum ETH_UNIT to, char *buf, size_t buf_size)
{
assert(amount != NULL);
size_t shift = from > to ? from - to : to - from;

if(tostring256(amount, 10, buf, buf_size) == false) {
return -1;
}

// special case '0'
if(buf[0] == '0') return 0;

// finish if the unit doesn't change
if (shift == 0) {
return 0;
// conversion to smaller unit, add '0'
} else if (from > to) {
size_t i;
size_t len = strlen(buf);
// check buffer size
if (len + shift > buf_size) {
return -1;
}
// add zeros
for (i = len; i < len + shift; ++i) {
buf[i] = '0';
}
buf[len + shift] = '\0';
// conversion to bigger unit add '.'
} else {
size_t i;
size_t len = strlen(buf);
size_t skip = LEADING_ZERO + DECIMAL_POINT;
if (len > shift) {
// check buffer size
if (len + DECIMAL_POINT > buf_size) {
return -1;
}
// move numbers
for (i = len + skip; i > len - shift; --i) {
buf[i] = buf[i - 1];
}
buf[len - shift] = '.';
} else {
// check buffer size
if (shift + skip > buf_size) {
return -1;
}
// move numbers
for (i = len; (int) i >= 0; --i) {
buf[i + shift + skip - len] = buf[i];
}
// add '0.' at the beggining
buf[0] = '0';
buf[1] = '.';
// fill the rest with zeros
for (i = skip; i < shift - len + skip; ++i) {
buf[i] = '0';
}
}
}

return 0;
}
18 changes: 18 additions & 0 deletions eth/web3.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ int eth_call(web3_ctx_t *web3, const address_t *from, const transaction_t *tx, u
int eth_estimateGas(web3_ctx_t *web3, const address_t *from, const transaction_t *tx);
int eth_getTransactionReceipt(web3_ctx_t *web3, const tx_hash_t *tx_hash);

#define DECIMAL_POINT 1
#define LEADING_ZERO 1

enum ETH_UNIT {
ETH_UNIT_WEI = 0,
ETH_UNIT_KWEI = 3,
ETH_UNIT_MWEI = 6,
ETH_UNIT_GWEI = 9,
ETH_UNIT_SZABO = 12,
ETH_UNIT_FINNEY = 15,
ETH_UNIT_ETHER = 18,
ETH_UNIT_KETHER = 21,
ETH_UNIT_METHER = 24,
ETH_UNIT_GETHER = 27,
ETH_UNIT_TETHER = 30
};
int eth_convert(const uint256_t *amount, enum ETH_UNIT from, enum ETH_UNIT to, char *buf, size_t buf_size);

#ifdef __cplusplus
}
#endif
Expand Down
72 changes: 72 additions & 0 deletions examples/tests/src/test_web3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,75 @@ TEST(TEST_WEB3, test_decode_txreceipt)
ASSERT_EQ(receipt.blockNumber, 0x8d1447);
}

void helper_eth_convert(uint64_t value, const char *compare, enum ETH_UNIT from, enum ETH_UNIT to)
{
int rc;
uint256_t amount;
const size_t buf_len = 50;
char buf[buf_len] = {0};
set256_uint64(&amount, value);
rc = eth_convert(&amount, from, to, buf, buf_len);
ASSERT_EQ(rc, 0);
ASSERT_EQ(strcmp(buf, compare), 0);
ASSERT_EQ(strlen(buf), strlen(compare));
}

TEST(TEST_ETH_CONVERT, test_eth_convert)
{
int rc;
uint256_t amount;
const size_t buf_len = 50;
char buf[buf_len] = {0};

set256_uint64(&amount, 1234);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_FINNEY, buf, 3);
ASSERT_EQ(rc, -1);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_ETHER, buf, 4);
ASSERT_EQ(rc, 0);
ASSERT_EQ(strcmp(buf, "1234"), 0);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_FINNEY, buf, 6);
ASSERT_EQ(rc, -1);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_FINNEY, buf, 7);
ASSERT_EQ(rc, 0);
ASSERT_EQ(strcmp(buf, "1234000"), 0);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_KETHER, buf, 4);
ASSERT_EQ(rc, -1);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_KETHER, buf, 5);
ASSERT_EQ(rc, 0);
ASSERT_EQ(strcmp(buf, "1.234"), 0);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_METHER, buf, 7);
ASSERT_EQ(rc, -1);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_METHER, buf, 8);
ASSERT_EQ(rc, 0);
ASSERT_EQ(strcmp(buf, "0.001234"), 0);

helper_eth_convert(1,"1000000000000000000", ETH_UNIT_ETHER, ETH_UNIT_WEI);
helper_eth_convert(1,"1000000000000000", ETH_UNIT_ETHER, ETH_UNIT_KWEI);
helper_eth_convert(1,"1000000000000", ETH_UNIT_ETHER, ETH_UNIT_MWEI);
helper_eth_convert(1,"1000000000", ETH_UNIT_ETHER, ETH_UNIT_GWEI);
helper_eth_convert(1,"1000000", ETH_UNIT_ETHER, ETH_UNIT_SZABO);
helper_eth_convert(1,"1000", ETH_UNIT_ETHER, ETH_UNIT_FINNEY);
helper_eth_convert(1,"1", ETH_UNIT_ETHER, ETH_UNIT_ETHER);
helper_eth_convert(1,"0.001", ETH_UNIT_ETHER, ETH_UNIT_KETHER);
helper_eth_convert(1,"0.000001", ETH_UNIT_ETHER, ETH_UNIT_METHER);
helper_eth_convert(1,"0.000000001", ETH_UNIT_ETHER, ETH_UNIT_GETHER);
helper_eth_convert(1,"0.000000000001", ETH_UNIT_ETHER, ETH_UNIT_TETHER);

set256_uint64(&amount, 0);
rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_WEI, buf, buf_len);
ASSERT_EQ(rc, 0);
ASSERT_EQ(strcmp(buf, "0"), 0);

helper_eth_convert(123,"0.123", ETH_UNIT_ETHER, ETH_UNIT_KETHER);
helper_eth_convert(1234,"1.234", ETH_UNIT_ETHER, ETH_UNIT_KETHER);
helper_eth_convert(12345,"12.345", ETH_UNIT_ETHER, ETH_UNIT_KETHER);
helper_eth_convert(123456,"123.456", ETH_UNIT_ETHER, ETH_UNIT_KETHER);
}