wormhole/ethereum/forge-test/relayer/TypedUnits.t.sol

122 lines
3.8 KiB
Solidity
Raw Normal View History

Relayer: Ethereum folder Changes for Merging into Main (#3038) * gRelayer: surrounding files * modification to get compilation * restore devnet * remove generic relayer docker * remove wait for relayer engine * keep build time 20 * sh -> bash * sh -> bash * Remove comment * bash -> sh * Revert "bash -> sh" This reverts commit 5c37e92fa19bbdbefc79c8ee0dbceeb127c53373. * bash->sh * gRelayer: ethereum folder changes for generic-relayer-merge * add eth-devnet * Adds .github because workflow needs to install forge * sdk-ci-tests need to install forge * don't wait for nonexistent relayer engine * update package.json and package-lock.json * Remove unnecessary types from package.json * ts-node * gRelayer: ethereum folder changes for generic-relayer-merge * sdk-ci-tests need to install forge * don't wait for nonexistent relayer engine * update package.json and package-lock.json * remove these changes * Relayer: Natspec documentation in IWormholeRelayer (#3032) * WIP * Fixes * Updated interfaces * remove bash * Forward uses same refund chain id and refund address (#3034) * WIP * Fixes * Forward uses same refund chain id and refund address * Updated interfaces * Remove forge build warnings * Add note to interface for resend * via-ir on unless in Tilt * Correct IWormholeReceiver interface * Wormhole message fee now part of quoteDeliveryPrice (#3043) * Fix to PR 3043 * Remove compiler warning * Relayer/address drew review (#3060) * Fix typo in Create2Factory * Add event for contract upgrades * Prevent registering contract if it is already registered * Prevent allowing unset chainId for default delivery provider governance VAA * memory to calldata for external functions in WormholeRelayerSend * continue memory to calldata for external functions * Fix pricing in delivery provider * Sanity check new default delivery provider isn't 0 address * Don't save vaaKey as local variable * cache the length of array rather than iterate every time for vaaKeys * Replacing memory with calldata in few locations * Remove stale file DeliveryProviderMessages * Remove batch VAA sender script * Remove batch VAA from WormholeSimulator * Wait for a confirmation in deploy scripts * remove unnecessary comments * Fix Delivery Provider Pricing and add a test * remove console logs * Revert "continue memory to calldata for external functions" This reverts commit f322afb6c0bbd09e3d04ab42a90e592ff752f6bf. * Revert "memory to calldata for external functions in WormholeRelayerSend" This reverts commit 42fcaad8842d0c81506c9586d8d0fd98f6bb6ae1. * Revert "Don't save vaaKey as local variable" This reverts commit a9172379c564fd430a083645c1c42c78e014d68d. * Revert "cache the length of array rather than iterate every time for vaaKeys" This reverts commit d61380a9b0c0671e67e3bd5d874ae339e180dd34. * Revert "Replacing memory with calldata in few locations" This reverts commit 94e47b6e72eaaa52ac0ba2980c439180401fafd7. * Revert "Fix typo in Create2Factory" This reverts commit a9f7bdf461945c8abf020007d16bbc6b4301d051. * Update contract addresses for via-ir * Slight improvements to delivery provider implementation * typed errors for delivery provider * enable VIA-IR in CI and not in Tilt * correct contract address for via ir * WormholeRelayerSend and WormholeRelayerDelivery (#3082)
2023-06-13 14:01:43 -07:00
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "forge-std/console.sol";
import "../../contracts/interfaces/relayer/TypedUnits.sol";
contract UVDTTest is Test {
using WeiLib for Wei;
using GasLib for Gas;
using DollarLib for Dollar;
function setUp() public {}
function testWeiBasic(uint64 x) public pure {
Wei w = Wei.wrap(x);
WeiPrice p = WeiPrice.wrap(100);
Dollar value = w.toDollars(p);
require(Dollar.unwrap(value) == uint256(x) * 100, "value should be 100*x");
}
function testWeiToGas(uint64 x) public pure {
Wei w = Wei.wrap(x);
GasPrice p = GasPrice.wrap(100);
Gas value = w.toGas(p);
require(Gas.unwrap(value) == uint256(x) / 100, "value should be x/100");
}
function testGasToWei(uint64 x) public pure {
Gas w = Gas.wrap(x);
GasPrice p = GasPrice.wrap(100);
Wei value = w.toWei(p);
require(Wei.unwrap(value) == uint256(x) * 100, "value should be 100*x");
}
function convertAsset(
uint64 source,
uint64 fromPrice,
uint64 toPrice,
uint32 multNum,
uint32 multDenom,
bool roundUp
) public pure {
Wei w = Wei.wrap(source);
WeiPrice fp = WeiPrice.wrap(fromPrice);
WeiPrice tp = WeiPrice.wrap(toPrice);
Wei value = w.convertAsset(fp, tp, multNum, multDenom, roundUp);
uint256 expected;
if (roundUp) {
expected = (
uint256(source) * uint256(fromPrice) * multNum + (uint256(toPrice) * multDenom) - 1
) / (uint256(toPrice) * multDenom);
} else {
expected =
(uint256(source) * uint256(fromPrice) * multNum) / (uint256(toPrice) * multDenom);
}
require(Wei.unwrap(value) == expected, "value should be expected");
}
function sourceWeiToTargetGas(uint64 sourceWei) public pure {
Wei w = Wei.wrap(sourceWei);
// gets smaller
{
WeiPrice fp = WeiPrice.wrap(10);
WeiPrice tp = WeiPrice.wrap(100);
GasPrice gp = GasPrice.wrap(5);
Gas targetGas = w.convertAsset(fp, tp, 1, 1, false).toGas(gp);
require(Gas.unwrap(targetGas) == sourceWei / 50, "targetGas should be 2");
}
// round up
{
WeiPrice fp = WeiPrice.wrap(100);
WeiPrice tp = WeiPrice.wrap(11);
GasPrice gp = GasPrice.wrap(5);
Gas targetGas = w.convertAsset(fp, tp, 1, 1, true).toGas(gp);
require(Gas.unwrap(targetGas) == sourceWei, "round down sourceWei * 1.8 => sourceWei");
}
// round down
{
WeiPrice fp = WeiPrice.wrap(100);
WeiPrice tp = WeiPrice.wrap(11);
GasPrice gp = GasPrice.wrap(5);
Gas targetGas = w.convertAsset(fp, tp, 1, 1, false).toGas(gp);
require(
Gas.unwrap(targetGas) == sourceWei * 2, "round up sourceWei * 1.8 => sourceWei * 2"
);
}
}
function testDollarToWei(uint128 x) public pure {
Dollar d = Dollar.wrap(x);
WeiPrice p = WeiPrice.wrap(100);
Wei value = d.toWei(p, false);
require(Wei.unwrap(value) == uint256(x) / 100, "value should be x/100");
}
function testDollarToGas(uint128 x) public pure {
Dollar d = Dollar.wrap(x);
GasPrice gp = GasPrice.wrap(1 << 32);
WeiPrice wp = WeiPrice.wrap(1 << 32);
Gas value = d.toGas(gp, wp);
require(Gas.unwrap(value) == uint256(x) / (1 << 64), "value should be x/(1<<32)");
}
function testGasMin(uint64 x, uint64 y) public pure {
Gas a = Gas.wrap(x);
Gas b = Gas.wrap(y);
Gas minVal = a.min(b);
require(Gas.unwrap(minVal) == (x < y ? x : y), "minVal should be min(x, y)");
}
}