Skip to content

Commit

Permalink
🔧 Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
z0r0z committed Feb 9, 2024
1 parent 0c0e75a commit 7add30c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 49 deletions.
23 changes: 15 additions & 8 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
IETest:testBalanceIn() (gas: 61352)
IETest:testDeploy() (gas: 1811938)
IETest:testNameFromHelper() (gas: 29061)
IETest:testNameOwnership() (gas: 109019)
IETest:testNameSetting() (gas: 8174)
IETest:testPreviewCommand() (gas: 79033)
IETest:testPreviewCommandDecimals() (gas: 145004)
IETest:testTotalSupply() (gas: 14695)
IETest:testBalanceInERC20() (gas: 62541)
IETest:testBalanceInETH() (gas: 53683)
IETest:testCommandSendERC0() (gas: 117712)
IETest:testCommandSendETH() (gas: 83838)
IETest:testCommandSendUSDC() (gas: 150633)
IETest:testDeploy() (gas: 1799284)
IETest:testENSNameFromENSHelper() (gas: 29125)
IETest:testENSNameOwnership() (gas: 109154)
IETest:testIENameSetting() (gas: 8174)
IETest:testPreviewCommandSendDecimals() (gas: 120253)
IETest:testPreviewCommandSendUSDC() (gas: 80750)
IETest:testPreviewSend() (gas: 54699)
IETest:testPreviewSendCommand() (gas: 70150)
IETest:testSendETH() (gas: 70975)
IETest:testTotalSupply() (gas: 14810)
58 changes: 25 additions & 33 deletions src/IE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ contract IE {
/// @dev The canonical wrapped ETH address.
address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

/// @dev The popular wrapped BTC address.
address internal constant WBTC = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;

/// @dev The Circle USD stablecoin address.
address internal constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;

Expand Down Expand Up @@ -162,12 +165,13 @@ contract IE {

/// @dev Checks and returns the canonical constant for a matched intent string.
function _returnConstant(bytes32 asset) internal view virtual returns (address _asset) {
if (asset == "eth" || msg.value != 0) return ETH;
if (asset == "eth" || asset == "ether" || msg.value != 0) return ETH;
if (asset == "usdc") return USDC;
if (asset == "usdt") return USDT;
if (asset == "dai") return DAI;
if (asset == "weth") return WETH;
if (asset == "nani") return NANI;
if (asset == "weth") return WETH;
if (asset == "wbtc" || asset == "bitcoin") return WBTC;
}

/// ===================== COMMAND EXECUTION ===================== ///
Expand Down Expand Up @@ -328,45 +332,33 @@ contract IE {
}

/// @dev Split the intent into an array of words.
function _split(string memory base, bytes1 value)
function _split(string memory base, bytes1 delimiter)
internal
pure
virtual
returns (string[] memory)
{
uint256 index;
uint256 count = 1;
bytes memory baseBytes = bytes(base);
for (uint256 i; i != baseBytes.length; ++i) {
if (baseBytes[i] == value) ++count;
}
string[] memory array = new string[](count);
for (uint256 i; i != baseBytes.length; ++i) {
if (baseBytes[i] == value) {
++index;
} else {
array[index] = _concat(array[index], baseBytes[i]);
}
}
return array;
}

/// @dev Perform string concatenation on base.
function _concat(string memory base, bytes1 value)
internal
pure
virtual
returns (string memory)
{
unchecked {
uint256 len = bytes(base).length;
bytes memory baseBytes = bytes(base);
bytes memory result = new bytes(len + 1);
for (uint256 i; i != len; ++i) {
result[i] = baseBytes[i];
uint256 count = 1;
for (uint256 i; i != baseBytes.length; ++i) {
if (baseBytes[i] == delimiter) {
++count;
}
}
string[] memory parts = new string[](count);
uint256 partIndex;
bytes memory tempPart;
for (uint256 i; i <= baseBytes.length; ++i) {
if (i == baseBytes.length || baseBytes[i] == delimiter) {
parts[partIndex] = string(tempPart);
++partIndex;
tempPart = "";
} else {
tempPart = abi.encodePacked(tempPart, baseBytes[i]);
}
}
result[len] = value;
return string(result);
return parts;
}
}

Expand Down
86 changes: 78 additions & 8 deletions test/IE.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ contract IETest is Test {
address internal constant Z0R0Z_DOT_ETH = 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20;
address internal constant NANI_DOT_ETH = 0x7AF890Ca7262D6accdA5c9D24AC42e35Bb293188;

address internal constant USDC_WHALE = 0xD6153F5af5679a75cC85D8974463545181f48772;

IE internal ie; // Intents Engine.

function setUp() public payable {
Expand Down Expand Up @@ -57,7 +59,7 @@ contract IETest is Test {
new IE();
}

function testNameOwnership() public payable {
function testENSNameOwnership() public payable {
(, address receiver,) = ie.whatIsTheAddressOf("vitalik");
assertEq(receiver, VITALIK_DOT_ETH);
(, receiver,) = ie.whatIsTheAddressOf("z0r0z");
Expand All @@ -66,21 +68,36 @@ contract IETest is Test {
assertEq(receiver, NANI_DOT_ETH);
}

function testNameFromHelper() public payable {
function testENSNameFromENSHelper() public payable {
(address result,) =
IENSHelper(0x4A5cae3EC0b144330cf1a6CeAD187D8F6B891758).owner("vitalik.eth");
assertEq(result, VITALIK_DOT_ETH);
}

function testPreviewCommand() public payable {
function testPreviewSendCommand() public payable {
string memory command = "send vitalik 20 dai";
(address to, uint256 amount, address asset,,) = ie.previewCommand(command);
assertEq(to, VITALIK_DOT_ETH);
assertEq(amount, 20 ether);
assertEq(asset, DAI);
}

function testPreviewCommandDecimals() public payable {
function testPreviewSend() public payable {
(address to, uint256 amount, address asset,,) = ie.previewSend("vitalik", "20", "dai");
assertEq(to, VITALIK_DOT_ETH);
assertEq(amount, 20 ether);
assertEq(asset, DAI);
}

function testPreviewCommandSendUSDC() public payable {
string memory command = "send z0r0z 20 usdc";
(address to, uint256 amount, address asset,,) = ie.previewCommand(command);
assertEq(to, Z0R0Z_DOT_ETH);
assertEq(amount, 20000000);
assertEq(asset, USDC);
}

function testPreviewCommandSendDecimals() public payable {
string memory command = "send vitalik 20.2 dai";
(address to, uint256 amount, address asset,,) = ie.previewCommand(command);
assertEq(to, VITALIK_DOT_ETH);
Expand All @@ -93,7 +110,7 @@ contract IETest is Test {
assertEq(asset, ETH);
}

function testNameSetting() public payable {
function testIENameSetting() public payable {
assertEq(ie.assets("uni"), UNI);
}

Expand All @@ -103,9 +120,62 @@ contract IETest is Test {
assertEq(adjustedSupply, 1000000000);
}

function testBalanceIn() public payable {
function testBalanceInERC20() public payable {
uint256 vBal = IERC20(OMG).balanceOf(VITALIK_DOT_ETH);
(uint256 balance, uint256 adjustedBalance) = ie.whatIsTheBalanceOf("VITALIK", "omg");
assertEq(balance, 123646253428532080615067);
assertEq(adjustedBalance, 123646);
assertEq(balance, vBal);
assertEq(adjustedBalance, vBal / 10 ** 18);
}

function testBalanceInETH() public payable {
uint256 vBal = VITALIK_DOT_ETH.balance;
(uint256 balance, uint256 adjustedBalance) = ie.whatIsTheBalanceOf("VITALIK", "eth");
assertEq(balance, vBal);
assertEq(adjustedBalance, vBal / 10 ** 18);
}

function testCommandSendETH() public payable {
uint256 vBal = VITALIK_DOT_ETH.balance;
uint256 zBal = Z0R0Z_DOT_ETH.balance;
vm.prank(VITALIK_DOT_ETH);
ie.command{value: 1 ether}("send z0r0z 1 ETH");
assertEq(VITALIK_DOT_ETH.balance, vBal - 1 ether);
assertEq(Z0R0Z_DOT_ETH.balance, zBal + 1 ether);
}

function testCommandSendERC0() public payable {
vm.prank(VITALIK_DOT_ETH);
IERC20(OMG).approve(address(ie), 100 ether);
uint256 vBal = IERC20(OMG).balanceOf(VITALIK_DOT_ETH);
uint256 zBal = IERC20(OMG).balanceOf(Z0R0Z_DOT_ETH);
vm.prank(VITALIK_DOT_ETH);
ie.command("send z0r0z 100 OMG");
assertEq(IERC20(OMG).balanceOf(VITALIK_DOT_ETH), vBal - 100 ether);
assertEq(IERC20(OMG).balanceOf(Z0R0Z_DOT_ETH), zBal + 100 ether);
}

function testCommandSendUSDC() public payable {
vm.prank(USDC_WHALE);
IERC20(USDC).approve(address(ie), 100 ether);
uint256 wBal = IERC20(USDC).balanceOf(USDC_WHALE);
uint256 zBal = IERC20(USDC).balanceOf(Z0R0Z_DOT_ETH);
vm.prank(USDC_WHALE);
ie.command("send z0r0z 100 USDC");
assertEq(IERC20(USDC).balanceOf(USDC_WHALE), wBal - 100000000);
assertEq(IERC20(USDC).balanceOf(Z0R0Z_DOT_ETH), zBal + 100000000);
}

function testSendETH() public payable {
uint256 vBal = VITALIK_DOT_ETH.balance;
uint256 zBal = Z0R0Z_DOT_ETH.balance;
vm.prank(VITALIK_DOT_ETH);
ie.send{value: 1 ether}("z0r0z", "1", "ETH");
assertEq(VITALIK_DOT_ETH.balance, vBal - 1 ether);
assertEq(Z0R0Z_DOT_ETH.balance, zBal + 1 ether);
}
}

interface IERC20 {
function approve(address, uint256) external; // unsafe lol.
function balanceOf(address) external view returns (uint256);
}

0 comments on commit 7add30c

Please sign in to comment.