Skip to content

Commit

Permalink
Adding tests for [OZ] ERC-2771 Context (#562)
Browse files Browse the repository at this point in the history
Adding tests for ERC-2771 Context

Signed-off-by: Stefan Stefanov <[email protected]>
  • Loading branch information
stefan-stefanooov authored Nov 9, 2023
1 parent 3be2095 commit 39749a7
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
23 changes: 23 additions & 0 deletions contracts/solidity/oz/ERC-2771/ERC2771Context.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/metatx/ERC2771Context.sol";

contract ERC2771ContextTest is ERC2771Context {
string public message;
address public sender;
bytes public msgData;

constructor(address trustedForward) ERC2771Context(trustedForward) {
}

function msgSenderTest() public returns (address) {
sender = _msgSender();
return _msgSender();
}

function msgDataTest() public returns (bytes memory) {
msgData = _msgData();
return _msgData();
}
}
92 changes: 92 additions & 0 deletions test/solidity/oz/erc-2771/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

const { expect } = require('chai')
const { ethers } = require('hardhat')

describe('@solidityequiv3 OZ ERC-2771 Context', function () {
let signers, wallet2, wallet
let contract, msgDataTestFuncSig

before(async function () {
signers = await ethers.getSigners()
wallet2 = signers[1]
wallet = signers[0]

const factory = await ethers.getContractFactory('ERC2771ContextTest')
contract = await factory.deploy(wallet2.address)

const iface = new ethers.utils.Interface([
'function msgDataTest()'
]);
msgDataTestFuncSig = iface.getSighash("msgDataTest");
})

it('should deploy the contract', async function () {
const res = await contract.deployed()

expect(res).to.exist
})

it('should have the correct trusted forwarder', async function () {
const res2 = await contract.isTrustedForwarder(wallet2.address)
const res = await contract.isTrustedForwarder(wallet.address)

expect(res2).to.be.true
expect(res).to.be.false
})

it('should return Pure message sender when not correct request is sent to _msgSender', async function () {
const res = await contract.callStatic.msgSenderTest()

expect(res).to.be.equal(wallet.address)
})

it('should return Pure message data when not correct request is sent to _msgData', async function () {
const res = await contract.callStatic.msgDataTest()

expect(res).to.be.equal(msgDataTestFuncSig)
})

it('should extract message sender from the request', async function () {
const trx = await contract.connect(wallet2).populateTransaction.msgSenderTest()
trx.data = trx.data + wallet2.address.substring(2)

const signedTrx = await wallet2.sendTransaction(trx)
await signedTrx.wait()

const msgData = await contract.sender()
expect(msgData).to.be.equal(wallet2.address)
})

it('should extract message data from the request', async function () {
const trx = await contract.connect(wallet2).populateTransaction.msgDataTest()
const initialData = trx.data
trx.data = initialData + wallet2.address.substring(2)

const signedTrx = await wallet2.sendTransaction(trx)
await signedTrx.wait()

const msgData = await contract.msgData()
expect(msgData).to.be.equal(initialData)
})


})

0 comments on commit 39749a7

Please sign in to comment.