Skip to content

Commit

Permalink
refactor: remove named return
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou committed Sep 19, 2024
1 parent 145dec9 commit 3ce6af3
Showing 1 changed file with 30 additions and 44 deletions.
74 changes: 30 additions & 44 deletions src/CairoLib.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
pragma solidity >=0.8.0 <0.9.0;

library CairoLib {
/// @dev The Cairo precompile contract's address.
Expand All @@ -11,38 +11,37 @@ library CairoLib {
/// @param contractAddress The address of the Cairo contract.
/// @param functionSelector The function selector of the Cairo contract function to be called.
/// @param data The input data for the Cairo contract function.
/// @return returnData The return data from the Cairo contract function.
/// @return The return data from the Cairo contract function.
function callCairo(uint256 contractAddress, uint256 functionSelector, uint256[] memory data)
internal
returns (bytes memory returnData)
returns (bytes memory)
{
bytes memory callData =
abi.encodeWithSignature("call_contract(uint256,uint256,uint256[])", contractAddress, functionSelector, data);

(bool success, bytes memory result) = CAIRO_PRECOMPILE_ADDRESS.call(callData);
require(success, string(abi.encodePacked("CairoLib: call_contract failed with: ", result)));

returnData = result;
return result;
}

/// @notice Performs a low-level call to a Cairo contract deployed on the Starknet appchain.
/// @dev Used with intent to modify the state of the Cairo contract.
/// @param contractAddress The address of the Cairo contract.
/// @param functionSelector The function selector of the Cairo contract function to be called.
/// @return returnData The return data from the Cairo contract function.
function callCairo(uint256 contractAddress, uint256 functionSelector) internal returns (bytes memory returnData) {
/// @return The return data from the Cairo contract function.
function callCairo(uint256 contractAddress, uint256 functionSelector) internal returns (bytes memory) {
uint256[] memory data = new uint256[](0);
return callCairo(contractAddress, functionSelector, data);
}

/// @notice Performs a low-level call to a Cairo contract deployed on the Starknet appchain.
/// @dev Used with intent to modify the state of the Cairo contract.
/// @param functionName The name of the Cairo contract function to be called.
///
/// @return returnData The return data from the Cairo contract function.
/// @return The return data from the Cairo contract function.
function callCairo(uint256 contractAddress, string memory functionName, uint256[] memory data)
internal
returns (bytes memory returnData)
returns (bytes memory)
{
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return callCairo(contractAddress, functionSelector, data);
Expand All @@ -51,11 +50,8 @@ library CairoLib {
/// @notice Performs a low-level call to a Cairo contract deployed on the Starknet appchain.
/// @dev Used with intent to modify the state of the Cairo contract.
/// @param functionName The name of the Cairo contract function to be called.
/// @return returnData The return data from the Cairo contract function.
function callCairo(uint256 contractAddress, string memory functionName)
internal
returns (bytes memory returnData)
{
/// @return The return data from the Cairo contract function.
function callCairo(uint256 contractAddress, string memory functionName) internal returns (bytes memory) {
uint256[] memory data = new uint256[](0);
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return callCairo(contractAddress, functionSelector, data);
Expand All @@ -68,18 +64,18 @@ library CairoLib {
/// @param contractAddress The address of the Cairo contract.
/// @param functionSelector The function selector of the Cairo contract function to be called.
/// @param data The input data for the Cairo contract function.
/// @return returnData The return data from the Cairo contract function.
/// @return The return data from the Cairo contract function.
function delegatecallCairo(uint256 contractAddress, uint256 functionSelector, uint256[] memory data)
internal
returns (bytes memory returnData)
returns (bytes memory)
{
bytes memory callData =
abi.encodeWithSignature("call_contract(uint256,uint256,uint256[])", contractAddress, functionSelector, data);

(bool success, bytes memory result) = CAIRO_PRECOMPILE_ADDRESS.delegatecall(callData);
require(success, string(abi.encodePacked("CairoLib: call_contract failed with: ", result)));

returnData = result;
return result;
}

/// @notice Performs a low-level delegatecall to a Cairo contract deployed on the Starknet appchain.
Expand All @@ -88,11 +84,8 @@ library CairoLib {
/// callee contract is performed using the `msg.sender` of the calling contract.
/// @param contractAddress The address of the Cairo contract.
/// @param functionSelector The function selector of the Cairo contract function to be called.
/// @return returnData The return data from the Cairo contract function.
function delegatecallCairo(uint256 contractAddress, uint256 functionSelector)
internal
returns (bytes memory returnData)
{
/// @return The return data from the Cairo contract function.
function delegatecallCairo(uint256 contractAddress, uint256 functionSelector) internal returns (bytes memory) {
uint256[] memory data = new uint256[](0);
return delegatecallCairo(contractAddress, functionSelector, data);
}
Expand All @@ -104,10 +97,10 @@ library CairoLib {
/// @param contractAddress The address of the Cairo contract.
/// @param functionName The name of the Cairo contract function to be called.
/// @param data The input data for the Cairo contract function.
/// @return returnData The return data from the Cairo contract function.
/// @return The return data from the Cairo contract function.
function delegatecallCairo(uint256 contractAddress, string memory functionName, uint256[] memory data)
internal
returns (bytes memory returnData)
returns (bytes memory)
{
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return delegatecallCairo(contractAddress, functionSelector, data);
Expand All @@ -119,11 +112,8 @@ library CairoLib {
/// callee contract is performed using the `msg.sender` of the calling contract.
/// @param contractAddress The address of the Cairo contract.
/// @param functionName The name of the Cairo contract function to be called.
/// @return returnData The return data from the Cairo contract function.
function delegatecallCairo(uint256 contractAddress, string memory functionName)
internal
returns (bytes memory returnData)
{
/// @return The return data from the Cairo contract function.
function delegatecallCairo(uint256 contractAddress, string memory functionName) internal returns (bytes memory) {
uint256[] memory data = new uint256[](0);
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return delegatecallCairo(contractAddress, functionSelector, data);
Expand All @@ -134,31 +124,27 @@ library CairoLib {
/// @param contractAddress The address of the Cairo contract.
/// @param functionSelector The function selector of the Cairo contract function to be called.
/// @param data The input data for the Cairo contract function.
/// @return returnData The return data from the Cairo contract function.
/// @return The return data from the Cairo contract function.
function staticcallCairo(uint256 contractAddress, uint256 functionSelector, uint256[] memory data)
internal
view
returns (bytes memory returnData)
returns (bytes memory)
{
bytes memory callData =
abi.encodeWithSignature("call_contract(uint256,uint256,uint256[])", contractAddress, functionSelector, data);

(bool success, bytes memory result) = CAIRO_PRECOMPILE_ADDRESS.staticcall(callData);
require(success, string(abi.encodePacked("CairoLib: call_contract failed with: ", result)));

returnData = result;
return result;
}

/// @notice Performs a low-level call to a Cairo contract deployed on the Starknet appchain.
/// @dev Used with intent to read the state of the Cairo contract.
/// @param contractAddress The address of the Cairo contract.
/// @param functionSelector The function selector of the Cairo contract function to be called.
/// @return returnData The return data from the Cairo contract function.
function staticcallCairo(uint256 contractAddress, uint256 functionSelector)
internal
view
returns (bytes memory returnData)
{
/// @return The return data from the Cairo contract function.
function staticcallCairo(uint256 contractAddress, uint256 functionSelector) internal view returns (bytes memory) {
uint256[] memory data = new uint256[](0);
return staticcallCairo(contractAddress, functionSelector, data);
}
Expand All @@ -168,11 +154,11 @@ library CairoLib {
/// @param contractAddress The address of the Cairo contract.
/// @param functionName The name of the Cairo contract function to be called.
/// @param data The input data for the Cairo contract function.
/// @return returnData The return data from the Cairo contract function.
/// @return The return data from the Cairo contract function.
function staticcallCairo(uint256 contractAddress, string memory functionName, uint256[] memory data)
internal
view
returns (bytes memory returnData)
returns (bytes memory)
{
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return staticcallCairo(contractAddress, functionSelector, data);
Expand All @@ -182,11 +168,11 @@ library CairoLib {
/// @dev Used with intent to read the state of the Cairo contract.
/// @param contractAddress The address of the Cairo contract.
/// @param functionName The name of the Cairo contract function to be called.
/// @return returnData The return data from the Cairo contract function.
/// @return The return data from the Cairo contract function.
function staticcallCairo(uint256 contractAddress, string memory functionName)
internal
view
returns (bytes memory returnData)
returns (bytes memory)
{
uint256[] memory data = new uint256[](0);
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
Expand All @@ -205,12 +191,12 @@ library CairoLib {
/// @dev A ByteArray is represented as:
/**
* pub struct ByteArray {
* data: Array<bytes31>,
* data_len: felt252,
* data: [<bytes31>],
* pending_word: felt252,
* pending_word_len: usize,
* }
* where `data` is an array of 31-byte packed words, and `pending_word` word of size `pending_word_len`.
*
*/
/// @param data The Cairo representation of the ByteArray serialized to bytes.
function byteArrayToString(bytes memory data) internal pure returns (string memory) {
Expand Down

0 comments on commit 3ce6af3

Please sign in to comment.