Skip to content

Commit

Permalink
tests update, before lzdevtools install
Browse files Browse the repository at this point in the history
  • Loading branch information
princetonbishop committed May 5, 2024
1 parent aa896db commit 2078776
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 45 deletions.
22 changes: 14 additions & 8 deletions protocol/contracts/templegold/TempleGold.sol
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ import { TempleMath } from "contracts/common/TempleMath.sol";
* Emits a {Transfer} event.
*/
function _update(address from, address to, uint256 value) internal override {
/// @notice can only transfer to or from whitelisted addreess
if (!authorized[from] && !authorized[to]) { revert ITempleGold.NonTransferrable(from, to); }
/// can only transfer to or from whitelisted addreess
/// @dev skip check on mint and burn. function `send` checks from == to
// if (from != address(0) && to != address(0)) {
// if (!authorized[from] && !authorized[to]) { revert ITempleGold.NonTransferrable(from, to); }
// }
super._update(from, to, value);
}

Expand Down Expand Up @@ -268,13 +271,16 @@ import { TempleMath } from "contracts/common/TempleMath.sol";
address _refundAddress
) external payable virtual override(IOFT, OFTCore) returns (MessagingReceipt memory msgReceipt, OFTReceipt memory oftReceipt) {
/// cast bytes32 to address
address _to = address(uint160(uint256(_sendParam.to)));
// address _to = address(uint160(uint256(_sendParam.to)));
address _to = _sendParam.to.bytes32ToAddress();
/// @dev user can cross-chain transfer to either whitelisted or self
if (!authorized[msg.sender] || authorized[_to]) {
if (msg.sender != _to) {
revert ITempleGold.NonTransferrable(msg.sender, _to);
}
}
// if (!authorized[msg.sender] || authorized[_to]) {
// if (msg.sender != _to) {
// revert ITempleGold.NonTransferrable(msg.sender, _to);
// }
// }
// if (!authorized[msg.sender] && !authorized[_to]) { revert ITempleGold.NonTransferrable(msg.sender, _to); }
// if (msg.sender != _to) { revert ITempleGold.NonTransferrable(msg.sender, _to); }

// @dev Applies the token transfers regarding this send() operation.
// - amountSentLD is the amount in local decimals that was ACTUALLY sent/debited from the sender.
Expand Down
3 changes: 3 additions & 0 deletions protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"@ethersproject/bytes": "^5.7.0",
"@gnosis.pm/safe-contracts": "^1.3.0",
"@layerzerolabs/lz-definitions": "^2.2.1",
"@layerzerolabs/lz-evm-messagelib-v2": "^2.3.5",
"@layerzerolabs/lz-evm-oapp-v2": "^2.1.24",
"@layerzerolabs/lz-evm-protocol-v2": "^2.1.24",
"@layerzerolabs/test-devtools-evm-foundry": "^0.2.3",
"@layerzerolabs/toolbox-hardhat": "^0.2.12",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.4",
"@nomicfoundation/hardhat-network-helpers": "^1.0.6",
Expand Down Expand Up @@ -39,6 +41,7 @@
"hardhat": "^2.17.1",
"hardhat-contract-sizer": "^2.10.0",
"hardhat-gas-reporter": "^1.0.9",
"solidity-bytes-utils": "^0.8.2",
"solidity-coverage": "^0.8.4",
"ts-node": "^10.9.1",
"typechain": "^8.1.1",
Expand Down
83 changes: 75 additions & 8 deletions protocol/test/forge/templegold/TempleGold.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { FakeERC20 } from "contracts/fakes/FakeERC20.sol";
import { ITempleGold } from "contracts/interfaces/templegold/ITempleGold.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { CommonEventsAndErrors } from "contracts/common/CommonEventsAndErrors.sol";
import { IERC20Errors } from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";
import { SendParam, OFTReceipt } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oft/interfaces/IOFT.sol";
import { MessagingReceipt, MessagingFee } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/OAppSender.sol";
import { MessagingParams, MessagingFee, MessagingReceipt, ILayerZeroEndpointV2 } from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";
import { SendParam } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oft/interfaces/IOFT.sol";
import { MessagingFee, MessagingReceipt } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/OAppSender.sol";
import { OptionsBuilder } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/libs/OptionsBuilder.sol";
import { Origin, ILayerZeroEndpointV2 } from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";
// import { EndpointV2 } from "@layerzerolabs/lz-evm-protocol-v2/contracts/EndpointV2.sol";

contract TempleGoldTestBase is TempleGoldCommon {
using OptionsBuilder for bytes;

event Transfer(address indexed from, address indexed to, uint256 value);
event ContractAuthorizationSet(address indexed _contract, bool _whitelisted);
event VestingFactorSet(uint128 numerator, uint128 denominator);
Expand Down Expand Up @@ -61,6 +63,8 @@ contract TempleGoldTestBase is TempleGoldCommon {
voteToken.setAuthorized(address(staking), true);
templeGold.setEscrow(address(daiGoldAuction));
_configureTempleGold();
vm.deal(alice, 100 ether);
vm.deal(bob, 100 ether);
vm.stopPrank();
}

Expand Down Expand Up @@ -93,7 +97,7 @@ contract TempleGoldTestBase is TempleGoldCommon {
fork("mainnet", mainnetForkBlockNumber);
mainnetForkId = forkId;
ITempleGold.InitArgs memory initArgs = _getTempleGoldInitArgs();
initArgs.layerZeroEndpoint = 0x1a44076050125825900e736c501f859c50fE728c;
initArgs.layerZeroEndpoint = layerZeroEndpointEthereum;
templeGoldMainnet = new TempleGold(initArgs);
return templeGoldMainnet;
}
Expand All @@ -102,9 +106,9 @@ contract TempleGoldTestBase is TempleGoldCommon {
templeGoldMainnet = _deployContractsOnMainnet();
vm.startPrank(executor);
vm.selectFork(mainnetForkId);
templeGoldMainnet.setPeer(ARBITRUM_ONE_LZ_EID, bytes32(uint256(uint160(address(templeGold)))));
templeGoldMainnet.setPeer(ARBITRUM_ONE_LZ_EID, _addressToBytes32(address(templeGold)));
vm.selectFork(arbitrumOneForkId);
templeGold.setPeer(MAINNET_LZ_EID, bytes32(uint256(uint160(address(templeGoldMainnet)))));
templeGold.setPeer(MAINNET_LZ_EID, _addressToBytes32(address(templeGoldMainnet)));
vm.stopPrank();
}
}
Expand Down Expand Up @@ -148,6 +152,12 @@ contract TempleGoldAccessTest is TempleGoldTestBase {
}

contract TempleGoldViewTest is TempleGoldTestBase {
function test_oftVersion_tgld() public {
(bytes4 interfaceId, ) = templeGold.oftVersion();
bytes4 expectedId = 0x02e49c2c;
assertEq(interfaceId, expectedId);
}

function test_getVestingFactor_tgld() public {
ITempleGold.VestingFactor memory _factor = _getVestingFactor();
vm.startPrank(executor);
Expand Down Expand Up @@ -216,6 +226,63 @@ contract TempleGoldViewTest is TempleGoldTestBase {
}

contract TempleGoldTest is TempleGoldTestBase {
using OptionsBuilder for bytes;
function test_send_oft() public {
// templeGoldMainnet = _deployContractsOnMainnet();
_setupPeers();
// vm.selectFork(arbitrumOneForkId);
// get some TGOLDgld from staking
vm.warp(block.timestamp+3 days);
templeGold.mint();
uint256 stakingBalance = templeGold.balanceOf(address(staking));
staking.distributeRewards();
vm.startPrank(alice);
deal(address(templeToken), alice, 1000 ether, true);
_approve(address(templeToken), address(staking), type(uint).max);
staking.stake(100 ether);
vm.warp(block.timestamp+ 5 days);
staking.getReward(alice);
uint256 aliceTgldBalance = templeGold.balanceOf(alice);

uint256 tokensToSend = 1 ether;
bytes memory options = OptionsBuilder.newOptions().addExecutorLzReceiveOption(200000, 0);
SendParam memory sendParam = SendParam(
MAINNET_LZ_EID,
_addressToBytes32(alice),
tokensToSend,
tokensToSend,
options,
"",
""
);
MessagingFee memory fee = templeGold.quoteSend(sendParam, false);

// assertEq(aOFT.balanceOf(userA), initialBalance);
// assertEq(bOFT.balanceOf(userB), initialBalance);

vm.startPrank(alice);
vm.selectFork(arbitrumOneForkId);
MessagingReceipt memory _receipt;
(_receipt,) = templeGold.send{ value: fee.nativeFee }(sendParam, fee, payable(address(this)));
templeGold.balanceOf(alice);
Origin memory origin = Origin(ARBITRUM_ONE_LZ_EID, _addressToBytes32(address(templeGold)), 1); // srcEid, sender, nonce
// EndpointV2 endpoint = EndpointV2(layerZeroEndpointEthereum); // destination endpoint
ILayerZeroEndpointV2 endpoint = ILayerZeroEndpointV2(layerZeroEndpointEthereum);
bytes memory _msg = abi.encodePacked(_addressToBytes32(alice), uint64(tokensToSend));
vm.selectFork(mainnetForkId);
vm.deal(address(this), 1000 ether);
// vm.deal(layerZeroEndpointEthereum, 1000 ether);
// vm.startPrank(layerZeroEndpointEthereum);
// endpoint.lzReceive{value: 10 ether, gas: 1500000}(origin, address(templeGoldMainnet), _receipt.guid, _msg, bytes(""));
// _verifyPackets(MAINNET_LZ_EID, _addressToBytes32(address(templeGoldMainnet)));
// vm.selectFork(mainnetForkId);
// assertEq(templeGoldMainnet.balanceOf(alice), aliceTgldBalance);
// assertEq(aOFT.balanceOf(userA), initialBalance - tokensToSend);
// assertEq(bOFT.balanceOf(userB), initialBalance + tokensToSend);
// emit PacketSent(encodedPayload: 0x0100000000000000010000759e0000000000000000000000005615deb798bb3e4dfa0139dfa1b3d433cc23b72f00007595000000000000000000000000a0cb889707d426a7a386870a03bc70d1b069759884ed493bd0a19c91a23045b2807beda78bca4c251db9581614867213d59d3c46000000000000000000000000328809bc894f92807417d2dad6b7c998c1afdac600000000000f4240,
// options: 0x00030100110100000000000000000000000000030d40, sendLibrary: 0x975bcD720be66659e3EB3C0e4F1866a3020E493A)
}

function test_setStaking_tgld() public {
vm.startPrank(executor);
vm.expectRevert(abi.encodeWithSelector(CommonEventsAndErrors.InvalidAddress.selector));
Expand Down
50 changes: 50 additions & 0 deletions protocol/test/forge/templegold/TempleGoldCommon.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contract TempleGoldCommon is TempleTest {
address public treasury = makeAddr("treasury");
address public teamGnosis = makeAddr("teamGnosis");
address public layerZeroEndpointArbitrumOne = 0x1a44076050125825900e736c501f859c50fE728c;
address public layerZeroEndpointEthereum = 0x1a44076050125825900e736c501f859c50fE728c;
uint256 public layerZeroEndpointArbitrumId = 30110;
address public usdcToken = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831; // arb USDC
address public daiToken = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1; //arb DAI
Expand All @@ -32,6 +33,8 @@ contract TempleGoldCommon is TempleTest {
string public constant VOTE_TOKEN_NAME = "Staked Temple Vote Token";
string public constant VOTE_TOKEN_SYMBOL = "stTemple";

receive() external payable {}

function _approve(address _token, address _spender, uint256 _amount) internal {
IERC20(_token).approve(_spender, _amount);
}
Expand All @@ -57,4 +60,51 @@ contract TempleGoldCommon is TempleTest {
_factor.numerator = 10 ether;
_factor.denominator = 100 ether;
}

function _addressToBytes32(address _addr) internal pure returns (bytes32) {
return bytes32(uint256(uint160(_addr)));
}

// function _verifyPackets(uint32 _dstEid, bytes32 _dstAddress) public {
// verifyPackets(_dstEid, _dstAddress, 0, address(0x0));
// }

// function _verifyPackets(uint32 _dstEid, address _dstAddress) public {
// _verifyPackets(_dstEid, bytes32(uint256(uint160(_dstAddress))), 0, address(0x0));
// }

function _verifyPackets(uint32 _dstEid, bytes32 _dstAddress, uint256 _packetAmount, address _composer) public {
// require(endpoints[_dstEid] != address(0), "endpoint not yet registered");

// DoubleEndedQueue.Bytes32Deque storage queue = packetsQueue[_dstEid][_dstAddress];
// uint256 pendingPacketsSize = queue.length();
// uint256 numberOfPackets;
// if (_packetAmount == 0) {
// numberOfPackets = queue.length();
// } else {
// numberOfPackets = pendingPacketsSize > _packetAmount ? _packetAmount : pendingPacketsSize;
// }
// while (numberOfPackets > 0) {
// numberOfPackets--;
// // front in, back out
// bytes32 guid = queue.popBack();
// bytes memory packetBytes = packets[guid];
// this.assertGuid(packetBytes, guid);
// this.validatePacket(packetBytes);

// bytes memory options = optionsLookup[guid];
// if (_executorOptionExists(options, ExecutorOptions.OPTION_TYPE_NATIVE_DROP)) {
// (uint256 amount, bytes32 receiver) = _parseExecutorNativeDropOption(options);
// address to = address(uint160(uint256(receiver)));
// (bool sent, ) = to.call{ value: amount }("");
// require(sent, "Failed to send Ether");
// }
// if (_executorOptionExists(options, ExecutorOptions.OPTION_TYPE_LZRECEIVE)) {
// this.lzReceive(packetBytes, options);
// }
// if (_composer != address(0) && _executorOptionExists(options, ExecutorOptions.OPTION_TYPE_LZCOMPOSE)) {
// this.lzCompose(packetBytes, options, guid, _composer);
// }
// }
}
}
64 changes: 64 additions & 0 deletions protocol/test/forge/templegold/TempleGoldLayerZero.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
pragma solidity 0.8.20;
// SPDX-License-Identifier: AGPL-3.0-or-later
// (tests/forge/templegold/TempleGoldLayerZero.t.sol)

import { Packet } from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ISendLib.sol";
import { OptionsBuilder } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/libs/OptionsBuilder.sol";
import { MessagingFee } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/OApp.sol";
import { MessagingReceipt } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/OAppSender.sol";
// The unique path location of your OApp
// import { MyOApp } from "../../contracts/MyOApp.sol";
import { TempleGold } from "contracts/templegold/TempleGold.sol";
import { TestHelperOz5 } from "@layerzerolabs/test-devtools-evm-foundry/contracts/TestHelperOz5.sol";

import "forge-std/console.sol";

contract TempleGoldLayerZeroTestBase is TestHelperOz5 {
using OptionsBuilder for bytes;

// Declaration of mock endpoint IDs.
uint16 aEid = 1;
uint16 bEid = 2;

// Declaration of mock contracts.
TempleGold public aTempleGold; // OApp A
TempleGold public bTempleGold; // OApp B


/// @notice Calls setUp from TestHelper and initializes contract instances for testing.
function setUp() public virtual override {
super.setUp();

// Setup function to initialize 2 Mock Endpoints with Mock MessageLib.
setUpEndpoints(2, LibraryType.UltraLightNode);

// Initializes 2 OApps; one on chain A, one on chain B.
address[] memory sender = setupOApps(type(TempleGold).creationCode, 1, 2);
aMyOApp = TempleGold(payable(sender[0]));
bMyOApp = TempleGold(payable(sender[1]));
}

/// @notice Tests the send and multi-compose functionality of MyOApp.
/// @dev Simulates message passing from A -> B and checks for data integrity.
function test_send() public {
// Setup variable for data values before calling send().
string memory dataBefore = aTempleGold.data();

// Generates 1 lzReceive execution option via the OptionsBuilder library.
// STEP 0: Estimating message gas fees via the quote function.
bytes memory options = OptionsBuilder.newOptions().addExecutorLzReceiveOption(150000, 0);
MessagingFee memory fee = aTempleGold.quote(bEid, "test message", options, false);

// STEP 1: Sending a message via the _lzSend() method.
MessagingReceipt memory receipt = aTempleGold.send{ value: fee.nativeFee }(bEid, "test message", options);

// Asserting that the receiving OApps have NOT had data manipulated.
assertEq(bTempleGold.data(), dataBefore, "shouldn't be changed until lzReceive packet is verified");

// STEP 2 & 3: Deliver packet to bTempleGold manually.
verifyPackets(bEid, addressToBytes32(address(bMyOApp)));

// Asserting that the data variable has updated in the receiving OApp.
assertEq(bMyOApp.data(), "test message", "lzReceive data assertion failure");
}
}
Loading

0 comments on commit 2078776

Please sign in to comment.