Skip to content

feat(lazer): Update EVM example to attach fee for verification #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lazer/evm/lib/forge-std
2 changes: 1 addition & 1 deletion lazer/evm/lib/pyth-crosschain
Submodule pyth-crosschain updated 342 files
8 changes: 6 additions & 2 deletions lazer/evm/src/ExampleReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ contract ExampleReceiver {
pythLazer = PythLazer(pythLazerAddress);
}

function updatePrice(bytes calldata update) public {
(bytes memory payload,) = pythLazer.verifyUpdate(update);
function updatePrice(bytes calldata update) payable public {
(bytes memory payload,) = pythLazer.verifyUpdate{value: pythLazer.verification_fee()}(update);
if (msg.value > pythLazer.verification_fee()) {
payable(msg.sender).transfer(msg.value - pythLazer.verification_fee());
}

(uint64 _timestamp, PythLazerLib.Channel channel, uint8 feedsLen, uint16 pos) =
PythLazerLib.parsePayloadHeader(payload);
console.log("timestamp %d", _timestamp);
Expand Down
13 changes: 9 additions & 4 deletions lazer/evm/test/ExampleReceiver.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ contract ExampleReceiverTest is Test {
address trustedSigner = 0xEfEf56cD66896f6799A90A4e4d512C330c094e44;
console.log("trustedSigner %s", trustedSigner);

address lazer = makeAddr("lazer");
PythLazer pythLazer = new PythLazer();
pythLazer.initialize(address(0x1));
vm.prank(address(0x1));
pythLazer.initialize(lazer);
vm.prank(lazer);
pythLazer.updateTrustedSigner(trustedSigner, 3000000000000000);

address consumer = makeAddr("consumer");
ExampleReceiver receiver = new ExampleReceiver(address(pythLazer));

bytes memory update =
hex"2a22999a577d3cc0202197939d736bc0dcf71b9dde7b9470e4d16fa8e2120c0787a1c0d744d0c39cc372af4d1ecf2d09e84160ca905f3f597d20e2eec144a446a0459ad600001c93c7d3750006240af373971c01010000000201000000000005f5e100";
console.logBytes(update);

receiver.updatePrice(update);
vm.deal(consumer, 1 ether);
vm.prank(consumer);
receiver.updatePrice{value: pythLazer.verification_fee()}(update);
Copy link
Contributor Author

@darunrs darunrs Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I attach more fee and call verifyUpdate{value: msg.value}() the refund fails and I get the following error:
image

If I instead call verifyUpdate{value: 1 wei}(), then all calls succeed but the ending balances are all wrong.

If I attach more fee and then comment out the refund in updatePrice, the final assert fails saying that consumer has the full amount of ether. Yet, pythLazer was still paid, and now the receiver contract got paid the 9 wei difference. So, money was made out of thin air.

I think I might be missing something simple, but not sure what it is...

Things I can think of which might matter are: Payable vs non-payable addresses and contracts with receive/fallback functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Riateche helped me out. The issue was that when I called vm.prank() before calling the updatePrice function, I was calling pythLazer.verification_fee() to get the fee to attach. This consumed the prank and led to some other default address making the call for verifyUpdate(). This default address doesn't have the receive/fallback functions so it fails. But storing the fee as a uint256 and making sure prank correctly targets the actual call, the test succeeds.

assertEq(receiver.price(), 100000000);
assertEq(receiver.timestamp(), 1728479312975644);
assertEq(address(pythLazer).balance, pythLazer.verification_fee());
assertEq(consumer.balance, 1 ether - pythLazer.verification_fee());
}
}
Loading