39#define AMOUNT_CENTS_MAX 999999ULL
41#define AMOUNT_UNITS_MAX (AMOUNT_CENTS_MAX * 10000ULL)
46 const uint64_t n = (cents * 10ULL) +
static_cast<uint64_t
>(digit);
47 return (n <= cap) ? n : cents;
53 const uint64_t n = cents * 100ULL;
54 return (n > cap) ? cap : n;
66 return cents * 10000ULL;
72 uint64_t whole = units / 1000000ULL;
73 uint64_t cents = (units % 1000000ULL) / 10000ULL;
74 snprintf(out, n,
"%" PRIu64
".%02" PRIu64, whole, cents);
87 for (
size_t i = 0U; i <
WEI_LEN; i++) {
89 (i < 8U) ?
static_cast<uint8_t
>((v >> (8U * i)) & 0xFFU) : 0U;
98 for (
size_t i =
WEI_LEN; i > 0U; i--) {
99 const unsigned s =
static_cast<unsigned>(a[i - 1U]) + b[i - 1U] + carry;
100 a[i - 1U] =
static_cast<uint8_t
>(s & 0xFFU);
108 return memcmp(a, b,
WEI_LEN) < 0;
125 const uint64_t x = units * 1000000ULL;
131 (void)memmove(hi, hi + 4U,
WEI_LEN - 4U);
132 (void)memset(hi + (
WEI_LEN - 4U), 0, 4U);
150 bool polygon, uint32_t floor_gwei,
151 uint64_t *max_fee, uint64_t *prio_fee)
155 uint64_t max_fee_wei = (uint64_t)max_gwei * 1000000000ULL;
156 uint64_t prio_fee_wei = (uint64_t)prio_gwei * 1000000000ULL;
157 if (prio_fee_wei > max_fee_wei) { prio_fee_wei = max_fee_wei; }
163 const uint64_t floor_wei = (uint64_t)floor_gwei * 1000000000ULL;
164 if (prio_fee_wei < floor_wei) { prio_fee_wei = floor_wei; }
165 if (max_fee_wei < prio_fee_wei) { max_fee_wei = prio_fee_wei; }
167 *max_fee = max_fee_wei;
168 *prio_fee = prio_fee_wei;
188 const uint8_t have_wei[
WEI_LEN],
189 uint64_t gas_cost, uint64_t units)
207static inline void fmt_coin(
char *out,
size_t n, uint64_t v,
unsigned dec,
211 for (
unsigned i = 6U; i < dec; i++) { div *= 10U; }
212 const uint64_t micro = (v / div) + (((v % div) != 0U) ? 1U : 0U);
214 (void)snprintf(frac,
sizeof(frac),
"%06" PRIu64, micro % 1000000U);
215 size_t f = strlen(frac);
216 while ((f > 0U) && (frac[f - 1U] ==
'0')) { frac[--f] =
'\0'; }
217 (void)snprintf(out, n,
"%" PRIu64
"%s%s %s", micro / 1000000U,
218 (f > 0U) ?
"." :
"", frac, coin);
227#define ABI_SELECTOR_LEN 4U
228#define ABI_WORD_LEN 32U
229#define USDC_CALLDATA_LEN (ABI_SELECTOR_LEN + (2U * ABI_WORD_LEN))
230#define ABI_TO_OFFSET (ABI_SELECTOR_LEN + (ABI_WORD_LEN - ETH_ADDR_LEN))
258 for (j = 0U; j <
sizeof(amount); j++) {
260 static_cast<uint8_t
>((amount >> (8U * j)) & 0xFFU);
Hex Ethereum-address parsing — a pure, dependency-free unit so it can be unit-tested and fuzzed on th...
#define ETH_ADDR_LEN
Length of a raw (binary) Ethereum address, in bytes.
static const uint8_t TRANSFER_SELECTOR[4]
#define WEI_LEN
How wei is carried: a uint256, 32 bytes big-endian, as on chain.
static void wei_add(uint8_t a[WEI_LEN], const uint8_t b[WEI_LEN])
a += b. A carry out of the top byte is dropped; every caller here stays below 2^80.
static void wei_from_u64(uint64_t v, uint8_t out[WEI_LEN])
v as a uint256.
static evm_funds_t evm_funds_check(bool native, const uint8_t have_wei[WEI_LEN], uint64_t gas_cost, uint64_t units)
Can have_wei pay for this sale?
static void evm_fees_from_gwei(uint32_t max_gwei, uint32_t prio_gwei, bool polygon, uint32_t floor_gwei, uint64_t *max_fee, uint64_t *prio_fee)
The EIP-1559 fees one EVM sale will offer, in wei per gas, from the operator's Gwei settings.
static void fmt_coin(char *out, size_t n, uint64_t v, unsigned dec, const char *coin)
"0.0013 ETH": v base units of a dec-decimal coin, to six places, rounded UP — it is a ceiling,...
static bool wei_lt(const uint8_t a[WEI_LEN], const uint8_t b[WEI_LEN])
a < b. Equal-length big-endian, so byte order is numeric order.
evm_funds_t
What the pre-flight balance check concluded.
static uint64_t amount_key_00(uint64_t cents, uint64_t cap)
The "00" key: two zeroes shifted in, clamped to cap.
static void build_usdc_calldata(uint8_t out[USDC_CALLDATA_LEN], const uint8_t to[ETH_ADDR_LEN], uint64_t amount)
Build the 68-byte ABI-encoded calldata for a USDC transfer call.
static uint64_t amount_key_back(uint64_t cents)
The backspace key: the last digit dropped.
static void amount_format(uint64_t units, char *out, size_t n)
"12.50": 6-decimal base units to two places, truncated.
static uint64_t amount_key_digit(uint64_t cents, unsigned digit, uint64_t cap)
A digit shifted in from the right; refused whole if it passes cap.
static uint64_t amount_cents_to_units(uint64_t cents)
Keypad cents to 6-decimal base units.
#define USDC_CALLDATA_LEN
static bool evm_units_to_wei(uint64_t units, uint8_t wei[WEI_LEN])
6-decimal keypad units -> wei, for the 18-decimal coins only.