Skip to content

Commit

Permalink
make ERC20 more user friendly
Browse files Browse the repository at this point in the history
- proper handling of token amounts
- code cleanup
  • Loading branch information
pcppcp committed Nov 7, 2018
1 parent 6cf48aa commit 8fb0247
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 53 deletions.
26 changes: 26 additions & 0 deletions helpers/math.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @brief
* @file math.c
* @author J.H.
* @date 2018-11-07
*/

/* system includes */

/* local includes */
#include "math.h"


uint64_t ipow(uint64_t base, uint8_t exp)
{
uint64_t result = 1;
for (;;)
{
if (exp & 1) { result *= base; }
exp >>= 1;
if (!exp) { break; }
base *= base;
}

return result;
}
18 changes: 18 additions & 0 deletions helpers/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _MATH_H_
#define _MATH_H_
/* system includes */
#include <stdint.h>
/* local includes */


#ifdef __cplusplus
extern "C" {
#endif
uint64_t ipow(uint64_t base, uint8_t exp);

#ifdef __cplusplus
}
#endif

#endif /* _MATH_H_ */

11 changes: 9 additions & 2 deletions helpers/uint256.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <string.h>

#include "hextobin.h"
#include "endian.h"


static const char HEXDIGITS[] = "0123456789abcdef";
Expand Down Expand Up @@ -56,8 +57,8 @@ void readu256BE(const uint8_t *buffer, uint256_t *target) {

void writeu128BE(const uint128_t *number, uint8_t *buffer)
{
*(uint64_t*)buffer = ENDIAN_SWAP_U64(LOWER_P(number));
*(uint64_t*)(buffer + sizeof(uint64_t)) = ENDIAN_SWAP_U64(UPPER_P(number));
*(uint64_t*)buffer = ENDIAN_SWAP_U64(UPPER_P(number));
*(uint64_t*)(buffer + sizeof(uint64_t)) = ENDIAN_SWAP_U64(LOWER_P(number));
}

void writeu256BE(const uint256_t *number, uint8_t *buffer)
Expand Down Expand Up @@ -587,6 +588,12 @@ void set256_uint64(uint256_t *target, uint64_t val)
LOWER(LOWER_P(target)) = val;
}

void set256_uint64BE(uint256_t *target, uint64_t val)
{
clear256(target);
LOWER(LOWER_P(target)) = htobe64(val);
}

// connect hexencoded ASCII to an uint256
int fromstring256(const char *hexencoded, uint256_t *out)
{
Expand Down
1 change: 1 addition & 0 deletions helpers/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void writeu256BE(const uint256_t *number, uint8_t *buffer);
void writeu128BE(const uint128_t *number, uint8_t *buffer);

void set256_uint64(uint256_t *target, uint64_t val);
void set256_uint64BE(uint256_t *target, uint64_t val);

bool zero128(const uint128_t *number);
bool zero256(const uint256_t *number);
Expand Down
10 changes: 10 additions & 0 deletions tests/test_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "helpers/hextobin.h"
#include "helpers/fp2str.h"
#include "helpers/uint256.h"
#include "helpers/math.h"

#define TEST_BUF_LEN 64

Expand Down Expand Up @@ -133,3 +134,12 @@ TEST(TEST_HELPERS, TEST_STR2UINT256)
}

}

TEST(TEST_HELPERS, TEST_IPOW)
{
uint64_t res;

res = ipow(10, 18);

ASSERT_EQ(res, 1000000000000000000);
}

0 comments on commit 8fb0247

Please sign in to comment.