Skip to content
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

feat(router-contract): <- emit metadata event in that #8

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions contracts/PTokensRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ contract PTokensRouter is
ConvertStringToAddress,
PTokensRouterStorage
{
event Metadata(bytes, bytes4, string, bytes4, string);

function initialize (
address safeVaultAddress
)
Expand Down Expand Up @@ -170,6 +172,10 @@ contract PTokensRouter is
string memory originAddress,
string memory destinationAddress
) = decodeParamsFromUserData(_userData);
// NOTE: We emit this event to allow v2 cores to read it & thus pass through origin chain tx
// information no matter what type of bridge crossing this is.
emit Metadata(userData, originChainId, originAddress, destinationChainId, destinationAddress);

address tokenAddress = msg.sender;

// NOTE: We give the fee contract an allowance up to the total amount so that it can transfer fees...
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ptokens-router-contract",
"version": "2.2.1",
"version": "2.3.0",
"description": "The pTokens interim-chain router contract & CLI",
"main": "cli.js",
"scripts": {
Expand Down
57 changes: 57 additions & 0 deletions test/ptokens-router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ describe('pTokens Router Contract', () => {
}
})

const decodeMetadataEvent = _event =>
new Promise((resolve, reject) => {
const codec = new ethers.utils.AbiCoder()
try {
const [ userData, originChainId, originAddress, destinationChainId, destinationAddress ] = codec.decode(
[ 'bytes', 'bytes4', 'string', 'bytes4', 'string' ],
_event.data
)
return resolve({ userData, originChainId, originAddress, destinationChainId, destinationAddress })
} catch (_err) {
return reject(_err)
}
})

const getEventFromReceipt = curry((_eventSignature, _receipt) =>
new Promise((resolve, reject) => {
const eventTopic = keccakHashString(_eventSignature)
Expand All @@ -183,9 +197,28 @@ describe('pTokens Router Contract', () => {
})
)

const getMetadataEventFromReceipt = getEventFromReceipt('Metadata(bytes,bytes4,string,bytes4,string)')
const getRedeemCalledEventFromReceipt = getEventFromReceipt('RedeemCalled(uint256,bytes,string,bytes4)')
const getPegInCalledEventFromReceipt = getEventFromReceipt('PegInCalled(uint256,address,string,bytes,bytes4)')

const assertMetadataEventFromReceipt = (
_receipt,
_userData,
_originChainId,
_originAddress,
_destinationChainId,
_destinationAddress,
) =>
getMetadataEventFromReceipt(_receipt)
.then(decodeMetadataEvent)
.then(_eventParams => {
assert.strictEqual(_eventParams.userData, _userData)
assert.strictEqual(_eventParams.originChainId, _originChainId)
assert.strictEqual(_eventParams.destinationChainId, _destinationChainId)
assert.strictEqual(_eventParams.originAddress.toLowerCase(), _originAddress.toLowerCase())
assert.strictEqual(_eventParams.destinationAddress.toLowerCase(), _destinationAddress.toLowerCase())
})

describe('Peg In Route Tests', () => {
it('Should peg in successfully', async () => {
const chainId = '0xdeadbeef'
Expand All @@ -209,6 +242,14 @@ describe('pTokens Router Contract', () => {
await ROUTER_CONTRACT.addVaultAddress(SAMPLE_METADATA_CHAIN_ID_2, vaultContract.address)
const tx = await pTokenContract.send(ROUTER_CONTRACT.address, amount, metadata)
const receipt = await tx.wait()
await assertMetadataEventFromReceipt(
receipt,
userData,
originChainId,
originAddress,
destinationChainId,
destinationAddress
)
const event = await getPegInCalledEventFromReceipt(receipt)
const result = await decodePegInCalledEvent(event)
assert(BigNumber.from(amount).eq(result.amount))
Expand Down Expand Up @@ -252,6 +293,14 @@ describe('pTokens Router Contract', () => {
assert(pTokenContractBalance.eq(0))
const tx = await vaultContract.pegOut(ROUTER_CONTRACT.address, pTokenContract.address, amount, metadata)
const receipt = await tx.wait()
await assertMetadataEventFromReceipt(
receipt,
userData,
originChainId,
originAddress,
destinationChainId,
destinationAddress
)
const redeemEvent = await getRedeemCalledEventFromReceipt(receipt)
const result = await decodePegOutCalledEvent(redeemEvent)
assert(result.amount.eq(amount))
Expand Down Expand Up @@ -310,6 +359,14 @@ describe('pTokens Router Contract', () => {
metadata
)
const receipt = await tx.wait()
await assertMetadataEventFromReceipt(
receipt,
userData,
originChainId,
originAddress,
destinationChainId,
destinationAddress
)
const pegInCalledEvent = await getPegInCalledEventFromReceipt(receipt)
const result = await decodePegInCalledEvent(pegInCalledEvent)
assert(BigNumber.from(amount).eq(result.amount))
Expand Down
Loading