Skip to content

Commit

Permalink
send and receive working
Browse files Browse the repository at this point in the history
  • Loading branch information
dan13ram committed Sep 4, 2024
1 parent 1acd128 commit 8fd7136
Showing 1 changed file with 71 additions and 48 deletions.
119 changes: 71 additions & 48 deletions test/OFT.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ contract OFTTest is TestHelper {
abi.encodeWithSelector(L2OFTAdapterMock.initialize.selector, address(this), _rateLimitConfigs)
)
);
bERC20.grantRole(bERC20.MINTER_ROLE(), address(bOFTAdapter));
}

{
Expand All @@ -113,6 +114,7 @@ contract OFTTest is TestHelper {
abi.encodeWithSelector(L2OFTAdapterMock.initialize.selector, address(this), _rateLimitConfigs)
)
);
cERC20.grantRole(cERC20.MINTER_ROLE(), address(cOFTAdapter));
}

// config and wire the ofts
Expand Down Expand Up @@ -151,80 +153,100 @@ contract OFTTest is TestHelper {
assertEq(interfaceId, expectedId);
}

/*
function test_send_oft() public {
uint256 tokensToSend = 1 ether;
bytes memory options = OptionsBuilder.newOptions().addExecutorLzReceiveOption(200000, 0);
SendParam memory sendParam = SendParam(
bEid,
addressToBytes32(userB),
tokensToSend,
tokensToSend,
options,
"",
""
);
MessagingFee memory fee = aOFT.quoteSend(sendParam, false);
SendParam memory sendParam =
SendParam(bEid, addressToBytes32(userB), tokensToSend, tokensToSend, options, "", "");
MessagingFee memory fee = aOFTAdapter.quoteSend(sendParam, false);

assertEq(aOFT.balanceOf(userA), initialBalance);
assertEq(bOFT.balanceOf(userB), initialBalance);
assertEq(aERC20.balanceOf(userA), initialBalance);
assertEq(bERC20.balanceOf(userB), 0);

vm.prank(userA);
aOFT.send{ value: fee.nativeFee }(sendParam, fee, payable(address(this)));
verifyPackets(bEid, addressToBytes32(address(bOFT)));
vm.startPrank(userA);
aERC20.approve(address(aOFTAdapter), tokensToSend);
aOFTAdapter.send{value: fee.nativeFee}(sendParam, fee, payable(address(this)));
vm.stopPrank();
verifyPackets(bEid, addressToBytes32(address(bOFTAdapter)));

assertEq(aERC20.balanceOf(userA), initialBalance - tokensToSend);
assertEq(aERC20.balanceOf(address(aOFTAdapter)), tokensToSend);
assertEq(bERC20.balanceOf(userB), 0 + tokensToSend);
}

function test_send_oft_and_receive() public {
uint256 tokensToSend = 1 ether;
bytes memory options = OptionsBuilder.newOptions().addExecutorLzReceiveOption(200000, 0);
SendParam memory sendParam =
SendParam(bEid, addressToBytes32(userB), tokensToSend, tokensToSend, options, "", "");
MessagingFee memory fee = aOFTAdapter.quoteSend(sendParam, false);

assertEq(aERC20.balanceOf(userA), initialBalance);
assertEq(bERC20.balanceOf(userB), 0);

vm.startPrank(userA);
aERC20.approve(address(aOFTAdapter), tokensToSend);
aOFTAdapter.send{value: fee.nativeFee}(sendParam, fee, payable(address(this)));
vm.stopPrank();
verifyPackets(bEid, addressToBytes32(address(bOFTAdapter)));

assertEq(aERC20.balanceOf(userA), initialBalance - tokensToSend);
assertEq(aERC20.balanceOf(address(aOFTAdapter)), tokensToSend);
assertEq(bERC20.balanceOf(userB), 0 + tokensToSend);

assertEq(aOFT.balanceOf(userA), initialBalance - tokensToSend);
assertEq(bOFT.balanceOf(userB), initialBalance + tokensToSend);
SendParam memory receiveParam =
SendParam(aEid, addressToBytes32(userA), tokensToSend, tokensToSend, options, "", "");

MessagingFee memory receiveFee = bOFTAdapter.quoteSend(receiveParam, false);

assertEq(bERC20.balanceOf(userB), tokensToSend);
assertEq(aERC20.balanceOf(userA), initialBalance - tokensToSend);

vm.startPrank(userB);
bERC20.approve(address(bOFTAdapter), tokensToSend);
bOFTAdapter.send{value: receiveFee.nativeFee}(receiveParam, receiveFee, payable(address(this)));
vm.stopPrank();
verifyPackets(aEid, addressToBytes32(address(aOFTAdapter)));

assertEq(bERC20.balanceOf(userB), 0);
assertEq(aERC20.balanceOf(userA), initialBalance);
}

function test_send_oft_compose_msg() public {
uint256 tokensToSend = 1 ether;

OFTComposerMock composer = new OFTComposerMock();

bytes memory options = OptionsBuilder
.newOptions()
.addExecutorLzReceiveOption(200000, 0)
.addExecutorLzComposeOption(0, 500000, 0);
bytes memory options =
OptionsBuilder.newOptions().addExecutorLzReceiveOption(200000, 0).addExecutorLzComposeOption(0, 500000, 0);
bytes memory composeMsg = hex"1234";
SendParam memory sendParam = SendParam(
bEid,
addressToBytes32(address(composer)),
tokensToSend,
tokensToSend,
options,
composeMsg,
""
);
MessagingFee memory fee = aOFT.quoteSend(sendParam, false);
SendParam memory sendParam =
SendParam(bEid, addressToBytes32(address(composer)), tokensToSend, tokensToSend, options, composeMsg, "");
MessagingFee memory fee = aOFTAdapter.quoteSend(sendParam, false);

assertEq(aOFT.balanceOf(userA), initialBalance);
assertEq(bOFT.balanceOf(address(composer)), 0);
assertEq(aERC20.balanceOf(userA), initialBalance);
assertEq(bERC20.balanceOf(address(composer)), 0);

vm.prank(userA);
(MessagingReceipt memory msgReceipt, OFTReceipt memory oftReceipt) = aOFT.send{ value: fee.nativeFee }(
sendParam,
fee,
payable(address(this))
);
verifyPackets(bEid, addressToBytes32(address(bOFT)));
vm.startPrank(userA);
aERC20.approve(address(aOFTAdapter), tokensToSend);
(MessagingReceipt memory msgReceipt, OFTReceipt memory oftReceipt) =
aOFTAdapter.send{value: fee.nativeFee}(sendParam, fee, payable(address(this)));
vm.stopPrank();
verifyPackets(bEid, addressToBytes32(address(bOFTAdapter)));

// lzCompose params
uint32 dstEid_ = bEid;
address from_ = address(bOFT);
address from_ = address(bOFTAdapter);
bytes memory options_ = options;
bytes32 guid_ = msgReceipt.guid;
address to_ = address(composer);
bytes memory composerMsg_ = OFTComposeMsgCodec.encode(
msgReceipt.nonce,
aEid,
oftReceipt.amountReceivedLD,
abi.encodePacked(addressToBytes32(userA), composeMsg)
msgReceipt.nonce, aEid, oftReceipt.amountReceivedLD, abi.encodePacked(addressToBytes32(userA), composeMsg)
);
this.lzCompose(dstEid_, from_, options_, guid_, to_, composerMsg_);

assertEq(aOFT.balanceOf(userA), initialBalance - tokensToSend);
assertEq(bOFT.balanceOf(address(composer)), tokensToSend);
assertEq(aERC20.balanceOf(userA), initialBalance - tokensToSend);
assertEq(bERC20.balanceOf(address(composer)), tokensToSend);

assertEq(composer.from(), from_);
assertEq(composer.guid(), guid_);
Expand All @@ -233,6 +255,7 @@ contract OFTTest is TestHelper {
assertEq(composer.extraData(), composerMsg_); // default to setting the extraData to the message as well to test
}

/*
function test_oft_compose_codec() public {
uint64 nonce = 1;
uint32 srcEid = 2;
Expand Down

0 comments on commit 8fd7136

Please sign in to comment.