Skip to content

Commit

Permalink
refactor: remove named return (#6)
Browse files Browse the repository at this point in the history
* refactor: remove named return

* chore: run test in ci

* doc: byteArrayToString docstring
  • Loading branch information
obatirou authored Sep 19, 2024
1 parent 145dec9 commit c2c7cb4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 49 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: test

on: workflow_dispatch
on:
push:
branches:
- main
pull_request:

env:
FOUNDRY_PROFILE: ci
Expand Down
83 changes: 35 additions & 48 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,13 @@ library CairoLib {
/// @dev A ByteArray is represented as:
/**
* pub struct ByteArray {
* data: Array<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`.
*
* full_words_len: felt252,
* full_words: [<bytes31>],
* pending_word: felt252,
* pending_word_len: usize,
* }
* where `full_words` is an array of 31-byte packed words, and `pending_word` word of size `pending_word_len`.
* Note that those full words are 32 bytes long, but only 31 bytes are used.
*/
/// @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 c2c7cb4

Please sign in to comment.