Skip to content

Commit

Permalink
Fix wei/gwei/eth conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Sep 16, 2024
1 parent 624f3f7 commit 888fffe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,30 @@
package tech.pegasys.teku.infrastructure.logging;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import org.apache.tuweni.units.bigints.UInt256;
import org.web3j.utils.Convert;
import org.web3j.utils.Convert.Unit;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;

public class Converter {

static BigDecimal gweiToEthFactor = BigDecimal.TEN.pow(18);
static BigDecimal gweiToEthFactor = BigDecimal.TEN.pow(9);

public static String weiToEth(final UInt256 wei) {
final BigDecimal result = Convert.fromWei(wei.toDecimalString(), Convert.Unit.ETHER);
return result.setScale(6, RoundingMode.HALF_UP).toString();
}

public static String gweiToEth(final UInt256 gwei) {
return new BigDecimal(gwei.toBigInteger())
public static String gweiToEth(final UInt64 gwei) {
return new BigDecimal(gwei.bigIntegerValue())
.divide(gweiToEthFactor, 6, RoundingMode.HALF_UP)
.toString();
}

public static UInt64 weiToGwei(final UInt256 wei) {
final BigInteger gwei = Convert.fromWei(wei.toDecimalString(), Unit.GWEI).toBigInteger();
return UInt64.valueOf(gwei);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,49 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;

class ConverterTest {

@ParameterizedTest
@MethodSource("getUInt256Values")
void test(final UInt256 wei, final String expected) {
@MethodSource("getWeiToEthArguments")
void testWeiToEth(final UInt256 wei, final String expected) {
String output = Converter.weiToEth(wei);
assertThat(output).isEqualTo(expected);
}

private static Stream<Arguments> getUInt256Values() {
@ParameterizedTest
@MethodSource("getWeiToGweiArguments")
void testWeiToGwei(final UInt256 wei, final UInt64 expected) {
UInt64 output = Converter.weiToGwei(wei);
assertThat(output).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("getGweiToEthArguments")
void testGweiToEth(final UInt64 gwei, final String expected) {
String output = Converter.gweiToEth(gwei);
assertThat(output).isEqualTo(expected);
}

private static Stream<Arguments> getWeiToEthArguments() {
return Stream.of(
Arguments.of(UInt256.valueOf(1), "0.000000"),
Arguments.of(UInt256.valueOf(1000), "0.000000"),
Arguments.of(UInt256.valueOf(3401220000000000L), "0.003401"),
Arguments.of(UInt256.valueOf(889999203452340000L), "0.889999"));
}

private static Stream<Arguments> getWeiToGweiArguments() {
return Stream.of(
Arguments.of(UInt256.valueOf(1), UInt64.valueOf(0)),
Arguments.of(UInt256.valueOf(1000), UInt64.valueOf(0)),
Arguments.of(UInt256.valueOf(3401220000000000L), UInt64.valueOf(3401220)),
Arguments.of(UInt256.valueOf(889999203452340000L), UInt64.valueOf(889999203)),
Arguments.of(UInt256.valueOf(424242424242424242L), UInt64.valueOf(424242424)));
}

private static Stream<Arguments> getGweiToEthArguments() {
return Stream.of(Arguments.of(UInt64.valueOf(424242424), "0.424242"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package tech.pegasys.teku.validator.client.duties;

import static com.google.common.base.Preconditions.checkArgument;
import static tech.pegasys.teku.infrastructure.logging.Converter.gweiToEth;
import static tech.pegasys.teku.infrastructure.logging.Converter.weiToEth;
import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ZERO;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -127,8 +127,8 @@ private SafeFuture<BlockContainer> validateBlock(
LOG.info(
"Received block for slot {}, block rewards {} ETH, execution payload value {} ETH",
slot,
gweiToEth(blockContainerAndMetaData.consensusBlockValue()),
gweiToEth(blockContainerAndMetaData.executionPayloadValue()));
weiToEth(blockContainerAndMetaData.consensusBlockValue()),
weiToEth(blockContainerAndMetaData.executionPayloadValue()));
}
return SafeFuture.completedFuture(unsignedBlockContainer);
}
Expand Down

0 comments on commit 888fffe

Please sign in to comment.