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

[VEN-2243]: override sendAndCall function and introduce sendAndCallEnabled with its setter #9

Merged
merged 3 commits into from
Dec 20, 2023
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
43 changes: 43 additions & 0 deletions contracts/Bridge/BaseXVSProxyOFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract contract BaseXVSProxyOFT is Pausable, ExponentialNoError, BaseOFTV2 {
using SafeERC20 for IERC20;
IERC20 internal immutable innerToken;
uint256 internal immutable ld2sdRate;
bool public sendAndCallEnabled;

/**
* @notice The address of ResilientOracle contract wrapped in its interface.
Expand Down Expand Up @@ -98,6 +99,11 @@ abstract contract BaseXVSProxyOFT is Pausable, ExponentialNoError, BaseOFTV2 {
*/
event InnerTokenAdded(address indexed innerToken);

/**
* @notice Event emitted when SendAndCallEnabled updated successfully.
*/
event UpdateSendAndCallEnabled(bool indexed enabled);

/**
* @param tokenAddress_ Address of the inner token.
* @param sharedDecimals_ No of shared decimals.
Expand Down Expand Up @@ -238,6 +244,43 @@ abstract contract BaseXVSProxyOFT is Pausable, ExponentialNoError, BaseOFTV2 {
emit TrustedRemoteRemoved(remoteChainId_);
}

/**
* @notice It enables or disables sendAndCall functionality for the bridge.
* @param enabled_ Boolean indicating whether the sendAndCall function should be enabled or disabled.
*/
function updateSendAndCallEnabled(bool enabled_) external onlyOwner {
sendAndCallEnabled = enabled_;
emit UpdateSendAndCallEnabled(enabled_);
}

/**
* @notice Initiates a cross-chain token transfer and triggers a call on the destination chain.
* @dev This internal override function enables the contract to send tokens and invoke calls on the specified
* destination chain. It checks whether the sendAndCall feature is enabled before proceeding with the transfer.
* @param from_ Address from which tokens will be debited.
* @param dstChainId_ Destination chain id on which tokens will be send.
* @param toAddress_ Address on which tokens will be credited on destination chain.
* @param amount_ Amount of tokens that will be transferred.
* @param payload_ Additional data payload for the call on the destination chain.
* @param dstGasForCall_ The amount of gas allocated for the call on the destination chain.
* @param callparams_ Additional parameters, including refund address, ZRO payment address,
* and adapter params.
*/

function sendAndCall(
address from_,
uint16 dstChainId_,
bytes32 toAddress_,
uint256 amount_,
bytes calldata payload_,
uint64 dstGasForCall_,
LzCallParams calldata callparams_
) public payable override {
require(sendAndCallEnabled, "sendAndCall is disabled");

super.sendAndCall(from_, dstChainId_, toAddress_, amount_, payload_, dstGasForCall_, callparams_);
}

/**
* @notice Empty implementation of renounce ownership to avoid any mishappening.
*/
Expand Down
1 change: 1 addition & 0 deletions helpers/deploymentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const xvsBridgeMethods = [
"setPayloadSizeLimit(uint16,uint256)",
"setWhitelist(address,bool)",
"setConfig(uint16,uint16,uint256,bytes)",
"updateSendAndCallEnabled(bool)",
];

export const bridgeAdminMethods = ["setTrustedRemoteAddress(uint16,bytes)", "transferBridgeOwnership(address)"];
Expand Down
62 changes: 62 additions & 0 deletions test/proxyOFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe("Proxy OFTV2: ", function () {
"setPayloadSizeLimit(uint16,uint256)",
"setUseCustomAdapterParams(bool)",
"removeTrustedRemote(uint16)",
"updateSendAndCallEnabled(bool)",
];
const activeArray = new Array(functionregistry.length).fill(true);
await bridgeAdminRemote.upsertSignature(functionregistry, activeArray);
Expand Down Expand Up @@ -718,4 +719,65 @@ describe("Proxy OFTV2: ", function () {
});
expect(await localOFT.trustedRemoteLookup(remoteChainId)).equals("0x");
});
it("Reverts when sendAndCall is disabled", async function () {
const amount = ethers.utils.parseEther("2", 18);
const dstGasForCall_ = 0;
const uint160Value = BigInt("0x" + acc3.address.slice(2));
const bytes32Value = uint160Value << BigInt(96);
const acc3AddressBytes32 = "0x" + bytes32Value.toString(16).padStart(32, "0");

await expect(
localOFT
.connect(acc1)
.sendAndCall(acc3.address, remoteChainId, acc3AddressBytes32, amount, "0x", dstGasForCall_, [
acc1.address,
acc1.address,
"0x",
]),
).to.be.revertedWith("sendAndCall is disabled");
});

it("Successfully call sendAndCall", async function () {
const uint160Value = BigInt("0x" + acc3.address.slice(2));
const bytes32Value = uint160Value << BigInt(96);
const acc3AddressBytes32 = "0x" + bytes32Value.toString(16).padStart(32, "0");
let data = localOFT.interface.encodeFunctionData("setMinDstGas", [remoteChainId, 1, 300000]);
const amount = ethers.utils.parseEther("2", 18);
const dstGasForCall_ = 0;
await acc1.sendTransaction({
to: bridgeAdminLocal.address,
data: data,
});

await acc1.sendTransaction({
to: bridgeAdminLocal.address,
data: data,
});
data = localOFT.interface.encodeFunctionData("updateSendAndCallEnabled", [true]);
await acc1.sendTransaction({
to: bridgeAdminLocal.address,
data: data,
});

const adapterParams = ethers.utils.solidityPack(["uint16", "uint256"], [1, 300000]);
await localToken.connect(acc1).faucet(amount);
await localToken.connect(acc1).approve(localOFT.address, amount);
expect(await localOFT.sendAndCallEnabled()).to.be.true;

const nativeFee = (await localOFT.estimateSendFee(remoteChainId, acc3AddressBytes32, amount, false, adapterParams))
.nativeFee;

await localOFT
.connect(acc1)
.sendAndCall(
acc1.address,
remoteChainId,
acc3AddressBytes32,
amount,
"0x",
dstGasForCall_,
[acc1.address, acc1.address, adapterParams],
{ value: nativeFee },
);
});
});