Skip to content

Commit

Permalink
Created wrapper call contract useful for batching calls and checking …
Browse files Browse the repository at this point in the history
…validity at the same time
  • Loading branch information
scx1332 authored May 24, 2024
1 parent da03e5c commit 386cedd
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
8 changes: 7 additions & 1 deletion gnt2-contracts/.waffle-MultiTransfer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"compilerVersion": "v0.8.13+commit.abaa5c0e",
"sourceDirectory": "./src/contracts/MultiTransfer"
"sourceDirectory": "./src/contracts/MultiTransfer",
"compilerOptions": {
"optimizer": {
"enabled": true,
"runs": 200
}
}
}
10 changes: 10 additions & 0 deletions gnt2-contracts/.waffle-WrapperCall.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerVersion": "v0.8.13+commit.abaa5c0e",
"sourceDirectory": "./src/contracts/WrapperCall",
"compilerOptions": {
"optimizer": {
"enabled": true,
"runs": 200
}
}
}
3 changes: 2 additions & 1 deletion gnt2-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"build:gntd": "pnpm waffle .waffle-GNTD.json",
"build:multisig": "pnpm waffle .waffle-MultiSigWallet.json",
"build:multitransfer": "pnpm waffle .waffle-MultiTransfer.json",
"build:wrappercall": "pnpm waffle .waffle-WrapperCall.json",
"build:zksync": "pnpm waffle .waffle-zkSync.json",
"build:lock": "pnpm waffle .waffle-LockPayment.json",
"build:types": "typechain --target ethers-v5 --out-dir build/contract-types 'build/**/*.json'",
"build": "pnpm build:lock && pnpm build:gnt && pnpm build:gnt2 && pnpm build:gntb && pnpm build:gntd && pnpm build:multisig && pnpm build:zksync && pnpm build:multitransfer && pnpm build:types",
"build": "pnpm build:wrappercall && pnpm build:lock && pnpm build:gnt && pnpm build:gnt2 && pnpm build:gntb && pnpm build:gntd && pnpm build:multisig && pnpm build:zksync && pnpm build:multitransfer && pnpm build:types",
"clean": "rimraf build dist",
"deploy:ngt2:dev": "ts-node src/deployment/deployAllRinkeby.ts"
},
Expand Down
85 changes: 85 additions & 0 deletions gnt2-contracts/src/contracts/WrapperCall/WrapperCall.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract WrapperCall {
struct CallWithDetailsResult {
uint256 chainId;
uint256 blockNumber;
uint256 blockTimestamp;
uint256 callerBalance;
bytes callResult;
}

// Call an external contract with the provided call data and return the result along with useful details
function callWithDetails(address externalContractAddress, bytes calldata callData) external returns (CallWithDetailsResult memory) {
bytes memory result;
if (externalContractAddress != address(0)) {
(bool success, bytes memory res) = externalContractAddress.call(callData);
// Check if the call was successful
require(success, "External call failed");
result = res;
} else {
result = "";
}

uint256 chainId;
assembly {
chainId := chainid()
}

return CallWithDetailsResult(
chainId,
block.number,
block.timestamp,
msg.sender.balance,
result
);
}

struct MultiCallWithDetailsResult {
uint256 chainId;
uint256 blockNumber;
uint256 blockTimestamp;
uint256 callerBalance;
bytes[] callResults;
}

// Call multiple external contracts with the provided calls data and return the result along with useful details
function multiCallWithDetails(address[] calldata externalContractAddresses, bytes[] calldata callDataArray) external returns (MultiCallWithDetailsResult memory) {
require(externalContractAddresses.length == callDataArray.length, "Arrays must be of equal length");
require(externalContractAddresses.length > 0, "Arrays must not be empty");

bytes[] memory results = new bytes[](externalContractAddresses.length);
for (uint256 i = 0; i < externalContractAddresses.length; i++) {
(bool success, bytes memory data) = externalContractAddresses[i].call(callDataArray[i]);
// Check if the call was successful
require(success, "External call failed");
results[i] = data;
}

uint256 chainId;
assembly {
chainId := chainid()
}

return MultiCallWithDetailsResult(
chainId,
block.number,
block.timestamp,
msg.sender.balance,
results
);
}

// Standard multicall for chaining transactions
function multiCall(address[] calldata externalContractAddresses, bytes[] calldata callDataArray) external {
require(externalContractAddresses.length == callDataArray.length, "Arrays must be of equal length");
require(externalContractAddresses.length > 0, "Arrays must not be empty");

for (uint256 i = 0; i < externalContractAddresses.length; i++) {
(bool success,) = externalContractAddresses[i].call(callDataArray[i]);
// Check if the call was successful
require(success, "External call failed");
}
}
}
2 changes: 2 additions & 0 deletions gnt2-docker-yagna/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
deploy_*
index_*

0 comments on commit 386cedd

Please sign in to comment.