Skip to content

Commit

Permalink
unit: change the signature of eth_unit_convert
Browse files Browse the repository at this point in the history
  • Loading branch information
mhw0 committed May 23, 2023
1 parent 9d0e59a commit ff0e509
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion include/ethc/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C" {
#define ETH_UNIT_GETHER "1000000000000000000000000000"
#define ETH_UNIT_TETHER "1000000000000000000000000000000"

ETHC_EXPORT char *eth_unit_convert(const char *amount, const char *from, const char *to);
ETHC_EXPORT int eth_unit_convert(char **dest, const char *amount, const char *from, const char *to);

#ifdef __cplusplus
}
Expand Down
19 changes: 10 additions & 9 deletions src/unit.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <ethc/unit.h>
#include <gmp.h>
#include <ethc/unit.h>

char *eth_unit_convert(const char *amount, const char *from, const char *to) {
int eth_unit_convert(char **dest, const char *amount, const char *from, const char *to) {
mpf_t j, k, l;
char *buff, *fmt;
char *buf, *fmt;

if (amount == NULL)
return NULL;
if (dest == NULL || amount == NULL || from == NULL || to == NULL)
return -1;

if (mpf_init_set_str(j, amount, 10) == -1) {
mpf_clear(j);
return NULL;
return -1;
}

mpf_init_set_str(k, from, 10);
Expand All @@ -20,11 +20,12 @@ char *eth_unit_convert(const char *amount, const char *from, const char *to) {
mpf_div(j, j, l);

fmt = mpf_integer_p(j) ? "%F.0f" : "%F.18f";
if (gmp_asprintf(&buff, fmt, j) == -1) {
if (gmp_asprintf(&buf, fmt, j) == -1) {
mpf_clears(j, k, l, NULL);
return NULL;
return -1;
}

mpf_clears(j, k, l, NULL);
return buff;
*dest = buf;
return 1;
}
26 changes: 14 additions & 12 deletions test/test-eth-unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
#include <ethc/unit.h>

void test_eth_unit_convert(void) {
char *out1 = eth_unit_convert("1", ETH_UNIT_ETHER, ETH_UNIT_WEI);
const char *exp1 = "1000000000000000000";
is(out1, exp1);
char *r0, *r1, *r2, *r3 = NULL;

char *out2 = eth_unit_convert("1", ETH_UNIT_WEI, ETH_UNIT_ETHER);
const char *exp2 = "0.000000000000000001";
is(out2, exp2);
ok(eth_unit_convert(&r0, "1", ETH_UNIT_ETHER, ETH_UNIT_WEI) == 1);
is(r0, "1000000000000000000");
free(r0);

char *out3 = eth_unit_convert("1000000000000000000", ETH_UNIT_WEI, ETH_UNIT_ETHER);
const char *exp3 = "1";
is(out3, exp3);
ok(eth_unit_convert(&r1, "1", ETH_UNIT_WEI, ETH_UNIT_ETHER) == 1);
is(r1, "0.000000000000000001");
free(r1);

char *out4 = eth_unit_convert("abc", ETH_UNIT_ETHER, ETH_UNIT_WEI);
const char *exp4 = NULL;
is(out4, exp4);
ok(eth_unit_convert(&r2, "1000000000000000000", ETH_UNIT_WEI, ETH_UNIT_ETHER) == 1);
is(r2, "1");
free(r2);

ok(eth_unit_convert(&r3, "abc", ETH_UNIT_ETHER, ETH_UNIT_WEI) == -1);
is(r3, NULL);
free(r3);
}

0 comments on commit ff0e509

Please sign in to comment.