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

Use bytes32 for the Adapter underlying token #74

Merged
merged 3 commits into from
Nov 20, 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
19 changes: 10 additions & 9 deletions solidity/src/contracts/Adapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ contract Adapter is IAdapter, Ownable, ReentrancyGuard {
); // swap event custom topic0

uint256 public nonce;
address public immutable erc20;
bytes32 public immutable erc20;
address public immutable erc20Addr;
address public immutable xerc20;
address public feesManager;
address public pam;
Expand All @@ -34,14 +35,15 @@ contract Adapter is IAdapter, Ownable, ReentrancyGuard {

constructor(
address xerc20_,
address erc20_,
bytes32 erc20_,
bool isNative_,
address feesManager_,
address pam_
) Ownable(msg.sender) {
isNative = isNative_;
if (!isNative) {
erc20 = erc20_;
erc20Addr = address(uint160(uint256(erc20)));
}
xerc20 = xerc20_;
feesManager = feesManager_;
Expand All @@ -64,8 +66,7 @@ contract Adapter is IAdapter, Ownable, ReentrancyGuard {
Operation memory operation,
IPAM.Metadata calldata metadata
) external nonReentrant {
if (operation.erc20 != bytes32(abi.encode(erc20)))
revert InvalidOperation();
if (operation.erc20 != erc20) revert InvalidOperation();

(bool isAuthorized, bytes32 eventId) = IPAM(pam).isAuthorized(
operation,
Expand Down Expand Up @@ -129,7 +130,7 @@ contract Adapter is IAdapter, Ownable, ReentrancyGuard {
string memory recipient,
bytes memory data
) external payable {
if (erc20 == address(0) && isNative) {
if (erc20Addr == address(0) && isNative) {
_swapNative(destinationChainId, recipient, data);
} else {
_swapToken(token, amount, destinationChainId, recipient, data);
Expand All @@ -151,7 +152,7 @@ contract Adapter is IAdapter, Ownable, ReentrancyGuard {
bytes memory data
) internal {
if (token == address(0)) revert InvalidTokenAddress(token);
if ((token != erc20) && (token != xerc20)) revert NotAllowed();
if ((token != erc20Addr) && (token != xerc20)) revert NotAllowed();
if (amount <= 0) revert InvalidAmount();

address payable lockbox = payable(XERC20(xerc20).lockbox());
Expand All @@ -170,7 +171,7 @@ contract Adapter is IAdapter, Ownable, ReentrancyGuard {
amount
);

if (lockbox != address(0) && token == erc20) {
if (lockbox != address(0) && token == erc20Addr) {
// We are on the home chain: then we wrap the ERC20
// to the relative xERC20
IERC20(token).approve(lockbox, amount);
Expand All @@ -186,7 +187,7 @@ contract Adapter is IAdapter, Ownable, ReentrancyGuard {
bytes memory data
) internal {
uint256 amount = msg.value;
if (erc20 != address(0)) revert NotAllowed();
if (erc20Addr != address(0)) revert NotAllowed();
if (amount == 0) revert InvalidAmount();

address payable lockbox = payable(XERC20(xerc20).lockbox());
Expand Down Expand Up @@ -222,7 +223,7 @@ contract Adapter is IAdapter, Ownable, ReentrancyGuard {
uint256 topic1 = nonce;
bytes memory eventBytes = bytes.concat(
bytes32(nonce),
bytes32(abi.encode(erc20)),
erc20,
bytes32(destinationChainId),
bytes32(netAmount),
bytes32(uint256(uint160(msg.sender))),
Expand Down
4 changes: 2 additions & 2 deletions solidity/test/forge/Adapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract AdapterTest is Test, Helper {
ERC20 erc20 = ERC20(new ERC20Test(name, symbol, supply));
(XERC20 xerc20, , ) = _setupXERC20(
address(0),
address(erc20),
bytes32(abi.encode(erc20)),
string.concat("p", name),
string.concat("p", symbol),
local,
Expand All @@ -42,7 +42,7 @@ contract AdapterTest is Test, Helper {
PAM pam = new PAM();
adapter = new Adapter(
address(xerc20),
address(erc20),
bytes32(abi.encode(erc20)),
notNative,
address(feesManager),
address(pam)
Expand Down
10 changes: 7 additions & 3 deletions solidity/test/forge/DeployHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract DeployHelper {
/// chains.
function _setupXERC20(
address factory_,
address erc20,
bytes32 erc20,
string memory name,
string memory symbol,
bool local,
Expand All @@ -40,11 +40,15 @@ contract DeployHelper {
)
);

bool isNative = erc20 == address(0);
bool isNative = address(uint160(uint256(erc20))) == address(0);
XERC20Lockbox lockbox;
if (local) {
lockbox = XERC20Lockbox(
factory.deployLockbox(address(xerc20), erc20, isNative)
factory.deployLockbox(
address(xerc20),
address(uint160(uint256(erc20))),
isNative
)
);
}

Expand Down
4 changes: 2 additions & 2 deletions solidity/test/forge/Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ abstract contract Helper is Test, DeployHelper {
function _setupChain(
uint256 chain,
address owner_,
address erc20,
bytes32 erc20,
bool local,
bool freezingEnabled
)
Expand Down Expand Up @@ -104,7 +104,7 @@ abstract contract Helper is Test, DeployHelper {
feesManager = new FeesManager(securityCouncil);
adapter = new Adapter(
address(xerc20),
address(erc20),
erc20,
notNative,
address(feesManager),
address(pam)
Expand Down
4 changes: 2 additions & 2 deletions solidity/test/forge/Integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ contract IntegrationTest is Test, Helper {
(xerc20_A, lockbox_A, adapter_A, feesManager_A, pam_A) = _setupChain(
CHAIN_A,
owner_A,
address(erc20),
erc20Bytes,
LOCAL,
freezingEnabled
);

(xerc20_B, lockbox_B, adapter_B, feesManager_B, pam_B) = _setupChain(
CHAIN_B,
owner_B,
address(erc20),
erc20Bytes,
NOT_LOCAL,
freezingEnabled
);
Expand Down
2 changes: 1 addition & 1 deletion solidity/test/forge/PAM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract PAMTest is Test, Helper {
(, , adapter, , ) = _setupChain(
originChainId,
owner,
address(erc20),
bytes32(abi.encode(address(erc20))),
true,
false
);
Expand Down
2 changes: 1 addition & 1 deletion solidity/test/hardhat/Adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const deployERC1820 = () => setCode(ERC1820, ERC1820BYTES)
const PAM = await deploy(hre, 'PAM', [])
const adapter = await deploy(hre, 'Adapter', [
pTokenV2.target,
erc20.target,
erc20Bytes,
_isNative,
feesManager,
PAM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
} = require('@pnetwork/event-attestator')
const { expect } = require('chai')
const hre = require('hardhat')
const { zeroPadValue } = require('ethers')

const Operation = require('../utils/Operation.js')
const { decodeSwapEvent } = require('../utils/decode-swap-event.js')
Expand Down Expand Up @@ -81,19 +82,17 @@ conditionalDescribe(
const rpc = hre.config.networks.bscFork.url
const blockToForkFrom = 43336397 // 2024-10-22 09:25
await helpers.reset(rpc, blockToForkFrom)

user = await hre.ethers.getImpersonatedSigner(
'0x816a99530B0f272Bb6ba4913b8952249f8d2E21b',
)

await helpers.setBalance(user.address, oneEth)
ptoken = await hre.ethers.getContractAt(PTokenAbi, ADDRESS_PTOKEN)
proxyAdminOwner = await hre.ethers.getImpersonatedSigner(
ADDRESS_PTOKEN_PROXY_ADMIN_OWNER,
)
adapterBsc = await deploy(hre, 'Adapter', [
ptoken.target,
ADDRESS_ERC20_TOKEN,
zeroPadValue(ADDRESS_ERC20_TOKEN, 32),
isNative,
feesManagerBsc,
pamBsc,
Expand Down Expand Up @@ -176,7 +175,6 @@ conditionalDescribe(
const rpc = hre.config.networks.ethFork.url
const blockToForkFrom = 20369499 // 2024-07-23 15:22
await helpers.reset(rpc, blockToForkFrom)

erc20 = await hre.ethers.getContractAt(Erc20Abi, ADDRESS_ERC20_TOKEN)

const name = `p${await erc20.name()}`
Expand Down Expand Up @@ -204,7 +202,7 @@ conditionalDescribe(

adapterEth = await deploy(hre, 'Adapter', [
xerc20.target,
ADDRESS_ERC20_TOKEN,
zeroPadValue(ADDRESS_ERC20_TOKEN, 32),
isNative,
feesManagerEth,
pamEth,
Expand Down