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

Chainlink Pricefeed Example PriceConsumerV3.sol With AggregatorV3Interface.sol Directly In Contract #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try installing chainlink inside the warp folder using npm? I think that should add the dependencies you need so you don't have to use this get around to use the interface

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

59 changes: 59 additions & 0 deletions exampleContracts/chainlink/pricefeed/pricefeedExample.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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){
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
Original file line number Diff line number Diff line change
@@ -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){
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"inputs": [],
"name": "getLatestPrice",
"outputs": [
{
"internalType": "int256",
"name": "",
"type": "int256"
}
],
"stateMutability": "view",
"type": "function"
}
]