Skip to content

Commit

Permalink
Merge pull request #14 from pnetwork-association/chore/test-userdata
Browse files Browse the repository at this point in the history
Add test for userdata
  • Loading branch information
gitmp01 authored Jul 27, 2024
2 parents 3f2449e + ee95689 commit 9545030
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
2 changes: 1 addition & 1 deletion solidity/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
forge coverage --skip src/ptoken-v1/*.sol src/ptoken-v1/*.sol --report lcov

# Generate coverage/lcov.info
yarn hardhat coverage --solcoverjs .solcover.cjs
FORK=1 yarn hardhat coverage --solcoverjs .solcover.cjs

# Generate report
genhtml -o lcov lcov.info coverage/lcov.info
Expand Down
1 change: 1 addition & 0 deletions solidity/src/Adapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ contract Adapter is IAdapter, Ownable {
0,
data
);

if (!success) emit ReceiveUserDataFailed();
}

Expand Down
2 changes: 1 addition & 1 deletion solidity/src/PAM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ contract PAM is Ownable, IPAM {
function _doesContentMatchOperation(
bytes calldata content,
IAdapter.Operation memory operation
) internal pure returns (bool) {
) internal view returns (bool) {
// Event Bytes content (see _finalizeSwap() in Adapter)
// | nonce | erc20 | destination | amount | sender | recipientLen | recipient | data |
// | 32B | 32B | 32B | 32B | 32B | 32B | varlen | varlen |
Expand Down
11 changes: 11 additions & 0 deletions solidity/src/test/DataReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {IPReceiver} from "../interfaces/IPReceiver.sol";

contract DataReceiver is IPReceiver {
event DataReceived(bytes userdata);
function receiveUserData(bytes calldata userdata) external {
emit DataReceived(userdata);
}
}
29 changes: 23 additions & 6 deletions solidity/test/forge/Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,31 @@ abstract contract Helper is Test {
bytes32 originChainId,
bytes32 blockHash,
bytes32 txHash
) internal returns (IAdapter.Operation memory operation) {
return
_getOperationFromRecordedLogs(
originChainId,
blockHash,
txHash,
false
);
}

function _getOperationFromRecordedLogs(
bytes32 originChainId,
bytes32 blockHash,
bytes32 txHash,
bool print
) internal returns (IAdapter.Operation memory operation) {
Vm.Log[] memory entries = vm.getRecordedLogs();
uint256 last = entries.length - 1;
// console.log("////////////////////////////////");
// console.log(entries[last].emitter); // address
// console.log(vm.toString(entries[last].data)); // data
// console.log(vm.toString(entries[last].topics[0])); // topic0
// console.log(vm.toString(entries[last].topics[1])); // topic1
if (print) {
console.log("////////////////////////////////");
console.log(entries[last].emitter); // address
console.log(vm.toString(entries[last].data)); // data
console.log(vm.toString(entries[last].topics[0])); // topic0
console.log(vm.toString(entries[last].topics[1])); // topic1
}

bytes memory content = abi
.decode(entries[last].data, (IAdapter.EventBytes))
Expand All @@ -190,7 +207,7 @@ abstract contract Helper is Test {
_hexStringToAddress(
string(BytesLib.slice(content, 192, recipientLen)) // recipient
),
BytesLib.slice(content, 224, dataLen) // data
BytesLib.slice(content, 192 + recipientLen, dataLen) // data
);
}

Expand Down
53 changes: 53 additions & 0 deletions solidity/test/forge/Integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {IAdapter} from "../../src/interfaces/IAdapter.sol";

import {XERC20} from "../../src/xerc20/XERC20.sol";
import {ERC20Test} from "../../src/test/ERC20Test.sol";
import {DataReceiver} from "../../src/test/DataReceiver.sol";
import {XERC20Lockbox} from "../../src/xerc20/XERC20Lockbox.sol";
import {ExcessivelySafeCall} from "../../src/libraries/ExcessivelySafeCall.sol";

import "forge-std/console.sol";

Expand Down Expand Up @@ -338,4 +340,55 @@ contract IntegrationTest is Test, Helper {
prevBalanceFeesManager_A + fees
);
}

function test_settle_e2e_withUserData() public {
uint256 amount = 10000;
DataReceiver receiver = new DataReceiver();
bytes memory data = vm.parseBytes("0xC0FFEE");

vm.recordLogs();
_performERC20Swap(
CHAIN_A,
address(erc20_A),
user,
address(adapter_A),
CHAIN_B,
address(receiver), // recipient
amount,
data
);

IAdapter.Operation memory operation = _getOperationFromRecordedLogs(
bytes32(CHAIN_A),
DEFAULT_BLOCK_HASH,
DEFAULT_TX_HASH
);

metadata.preimage = vm.parseBytes(
"0x01010000000000000000000000000000000000000000000000000000000000007a69a880cb2ab67ec9140db0f6de238b34d4108f6fab99315772ee987ef9002e0e6311365bbee18058f12c27236e891a66999c4325879865303f785854e9169c257a0000000000000000000000006d411e0a54382ed43f02410ce1c7a7c122afa6e1a68959eed8a7e77ce926c4c04ee06434559ae1db7f636ceacd659f5c9126f1c300000000000000000000000000000000000000000000000000000000000000000000000000000000000000002946259e0334f33a064106302415ad3391bed3840000000000000000000000000000000000000000000000000000000000007a6a00000000000000000000000000000000000000000000000000000000000026fc0000000000000000000000002b5ad5c4795c026514f8317c7a215e218dccd6cf000000000000000000000000000000000000000000000000000000000000002a307835363135644542373938424233453464466130313339644661316233443433334363323362373266c0ffee"
);
metadata.signature = vm.parseBytes(
"0x4d677785f125d2b6fbeb56c8360fbff583f504e5d4af23e02f0b0e7dc6a0f1c756b85935bdea9c32786a2671faf2b12fedce0874b67b7b60ade4c891219237ee1c"
);

bytes32 eventId = _getEventId(metadata.preimage);

vm.chainId(CHAIN_B);

vm.expectEmit(address(xerc20_B));
uint256 fees = (amount * 20) / 10000;
emit IERC20.Transfer(address(0), address(receiver), amount - fees);
vm.expectEmit(address(receiver));
emit DataReceiver.DataReceived(data);
vm.expectEmit(address(adapter_B));
emit IAdapter.Settled(eventId);

adapter_B.settle(operation, metadata);

uint256 R = xerc20_B.balanceOf(address(receiver));
uint256 A = xerc20_B.balanceOf(address(adapter_B));

assertEq(R, amount - fees);
assertEq(A, 0);
}
}

0 comments on commit 9545030

Please sign in to comment.