diff --git a/README.md b/README.md index 973386d..9e8635d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ # warping-contracts -Example Solidity contracts transpiled to Cairo using Warp + +Transpiles in Warp with no errors in Solidity 0.8.14 with: + +```shell +bin/warp transpile exampleContracts/chainlink/pricefeed/pricefeedExample.sol +``` + +Modified: + +-put contract AggregatorV3Interface.sol +https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol +directly into the pricefeed example contract instead of importing it to get around syntax transpiler issue. + +Contract from: + +https://docs.chain.link/data-feeds/using-data-feeds/#examine-the-sample-contract + diff --git a/exampleContracts/chainlink/pricefeed/pricefeedExample.sol b/exampleContracts/chainlink/pricefeed/pricefeedExample.sol new file mode 100644 index 0000000..e67b6b7 --- /dev/null +++ b/exampleContracts/chainlink/pricefeed/pricefeedExample.sol @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.14; + +// AggregatorV3Interface contract from: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; + +interface AggregatorV3Interface { + + function decimals() external view returns (uint8); + + function description() external view returns (string memory); + + function version() external view returns (uint256); + + function getRoundData(uint80 _roundId) + external + view + returns ( + uint80 roundId, + int256 answer, + uint256 startedAt, + uint256 updatedAt, + uint80 answeredInRound + ); + + function latestRoundData() + external + view + returns ( + uint80 roundId, + int256 answer, + uint256 startedAt, + uint256 updatedAt, + uint80 answeredInRound + ); +} + +// Pricefeed example from: https://docs.chain.link/data-feeds/using-data-feeds/#examine-the-sample-contract + +contract PriceConsumerV3 { + + /** + * Network: Sepolia + * Aggregator: BTC/USD + * Address: 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43 + */ + + AggregatorV3Interface internal priceFeed = AggregatorV3Interface(address(0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43)); //Added address type wrapper to resolve Starknet type error. + + function getLatestPrice() public view returns (int) { + ( + /* uint80 roundID */, + int price, + /*uint startedAt*/, + /*uint timeStamp*/, + /*uint80 answeredInRound*/ + ) = priceFeed.latestRoundData(); + return price; + } +} diff --git a/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/AggregatorV3Interface.cairo b/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/AggregatorV3Interface.cairo new file mode 100644 index 0000000..bb01336 --- /dev/null +++ b/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/AggregatorV3Interface.cairo @@ -0,0 +1,22 @@ +%lang starknet + + +from starkware.cairo.common.uint256 import Uint256 + + +// Contract Def AggregatorV3Interface + + +@contract_interface +namespace AggregatorV3Interface{ +func decimals_313ce567()-> (__warp_0 : felt){ +} +func description_7284e416()-> (__warp_1_len : felt, __warp_1 : felt*){ +} +func version_54fd4d50()-> (__warp_2 : Uint256){ +} +func getRoundData_9a6fc8f5(_roundId : felt)-> (roundId : felt, answer : Uint256, startedAt : Uint256, updatedAt : Uint256, answeredInRound : felt){ +} +func latestRoundData_feaf968c()-> (roundId : felt, answer : Uint256, startedAt : Uint256, updatedAt : Uint256, answeredInRound : felt){ +} +} \ No newline at end of file diff --git a/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/AggregatorV3Interface_sol_abi.json b/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/AggregatorV3Interface_sol_abi.json new file mode 100644 index 0000000..ddc3755 --- /dev/null +++ b/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/AggregatorV3Interface_sol_abi.json @@ -0,0 +1,113 @@ +[ + { + "inputs": [], + "name": "decimals", + "outputs": [ + { + "internalType": "uint8", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "description", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint80", + "name": "_roundId", + "type": "uint80" + } + ], + "name": "getRoundData", + "outputs": [ + { + "internalType": "uint80", + "name": "roundId", + "type": "uint80" + }, + { + "internalType": "int256", + "name": "answer", + "type": "int256" + }, + { + "internalType": "uint256", + "name": "startedAt", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "updatedAt", + "type": "uint256" + }, + { + "internalType": "uint80", + "name": "answeredInRound", + "type": "uint80" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "latestRoundData", + "outputs": [ + { + "internalType": "uint80", + "name": "roundId", + "type": "uint80" + }, + { + "internalType": "int256", + "name": "answer", + "type": "int256" + }, + { + "internalType": "uint256", + "name": "startedAt", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "updatedAt", + "type": "uint256" + }, + { + "internalType": "uint80", + "name": "answeredInRound", + "type": "uint80" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "version", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + } +] \ No newline at end of file diff --git a/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/PriceConsumerV3.cairo b/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/PriceConsumerV3.cairo new file mode 100644 index 0000000..783813f --- /dev/null +++ b/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/PriceConsumerV3.cairo @@ -0,0 +1,122 @@ +%lang starknet + + +from starkware.cairo.common.cairo_builtins import HashBuiltin +from starkware.cairo.common.uint256 import Uint256 +from warplib.maths.external_input_check_ints import warp_external_input_check_int256 + + +func WS_WRITE0{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(loc: felt, value: felt) -> (res: felt){ + WARP_STORAGE.write(loc, value); + return (value,); +} + + +func WS0_READ_felt{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(loc: felt) ->(val: felt){ + alloc_locals; + let (read0) = WARP_STORAGE.read(loc); + return (read0,); +} + + +// Contract Def PriceConsumerV3 + + +namespace PriceConsumerV3{ + + // Dynamic variables - Arrays and Maps + + // Static variables + + const __warp_0_priceFeed = 0; + + + func __warp_init_PriceConsumerV3{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}()-> (){ + alloc_locals; + + + + WS_WRITE0(__warp_0_priceFeed, 155680397429899150176167208149732158272590442051); + + + + return (); + + } + +} + + + @view + func getLatestPrice_8e15f473{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}()-> (__warp_1 : Uint256){ + alloc_locals; + + + + let (__warp_se_0) = WS0_READ_felt(PriceConsumerV3.__warp_0_priceFeed); + + let (__warp_gv0, __warp_2_price, __warp_gv1, __warp_gv2, __warp_gv3) = AggregatorV3Interface_warped_interface.latestRoundData_feaf968c(__warp_se_0); + + warp_external_input_check_int256(__warp_2_price); + + + + return (__warp_2_price,); + + } + + + @constructor + func constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(){ + alloc_locals; + WARP_USED_STORAGE.write(1); + + + + PriceConsumerV3.__warp_init_PriceConsumerV3(); + + + + return (); + + } + +@storage_var +func WARP_STORAGE(index: felt) -> (val: felt){ +} +@storage_var +func WARP_USED_STORAGE() -> (val: felt){ +} +@storage_var +func WARP_NAMEGEN() -> (name: felt){ +} +func readId{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(loc: felt) -> (val: felt){ + alloc_locals; + let (id) = WARP_STORAGE.read(loc); + if (id == 0){ + let (id) = WARP_NAMEGEN.read(); + WARP_NAMEGEN.write(id + 1); + WARP_STORAGE.write(loc, id + 1); + return (id + 1,); + }else{ + return (id,); + } +} + + +// Contract Def AggregatorV3Interface@interface + + +@contract_interface +namespace AggregatorV3Interface_warped_interface{ +func decimals_313ce567()-> (__warp_0 : felt){ +} +func description_7284e416()-> (__warp_1_len : felt, __warp_1 : felt*){ +} +func version_54fd4d50()-> (__warp_2 : Uint256){ +} +func getRoundData_9a6fc8f5(_roundId : felt)-> (roundId : felt, answer : Uint256, startedAt : Uint256, updatedAt : Uint256, answeredInRound : felt){ +} +func latestRoundData_feaf968c()-> (roundId : felt, answer : Uint256, startedAt : Uint256, updatedAt : Uint256, answeredInRound : felt){ +} +} \ No newline at end of file diff --git a/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/PriceConsumerV3_sol_abi.json b/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/PriceConsumerV3_sol_abi.json new file mode 100644 index 0000000..11befdf --- /dev/null +++ b/warp_output/exampleContracts/chainlink/pricefeed/pricefeedExample.sol/PriceConsumerV3_sol_abi.json @@ -0,0 +1,15 @@ +[ + { + "inputs": [], + "name": "getLatestPrice", + "outputs": [ + { + "internalType": "int256", + "name": "", + "type": "int256" + } + ], + "stateMutability": "view", + "type": "function" + } +] \ No newline at end of file