Skip to content

Commit

Permalink
add ethereum conversion function
Browse files Browse the repository at this point in the history
Signed-off-by: Mislav Novakovic <[email protected]>
  • Loading branch information
mislavn committed Dec 12, 2018
1 parent a0ad23a commit 115ad80
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
64 changes: 64 additions & 0 deletions eth/web3.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,67 @@ 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;

// return if the conversion is the same unit
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 + 1 + shift > buf_size) {
return -1;
}
// add zeros
for (i = len; i < len + shift; ++i) {
buf[i] = '0';
}
buf[len + shift + 1] = '\0';
// conversion to bigger unit add '.'
} else {
size_t i;
size_t len = strlen(buf);
if (len > shift) {
// check buffer size
if (len + 1 + 1 > buf_size) {
return -1;
}
// move numbers
for (i = len + 1 + 1; i > len - shift; --i) {
buf[i] = buf[i - 1];
}
buf[len - shift] = '.';
} else {
// check buffer size
if (shift + 3 > buf_size) {
return -1;
}
// move numbers
for (i = len - 1; (int) i >= 0; --i) {
buf[i + shift + 2 - len] = buf[i];
}
// add '0.' at the beggining
buf[0] = '0';
buf[1] = '.';
// fill the rest with zeros
for (i = 2; i < shift - len + 2; ++i) {
buf[i] = '0';
}
}
}

return 0;
}
15 changes: 15 additions & 0 deletions eth/web3.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ 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);

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
52 changes: 52 additions & 0 deletions examples/tests/src/test_web3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,55 @@ 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, 1234567890);

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_WEI, buf, 10);
ASSERT_EQ(rc, -1);

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

rc = eth_convert(&amount, ETH_UNIT_ETHER, ETH_UNIT_TETHER, buf, 13);
ASSERT_EQ(rc, -1);

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);
}

0 comments on commit 115ad80

Please sign in to comment.