-
Notifications
You must be signed in to change notification settings - Fork 30
feat: create Ecosystem connectors #82
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
Open
bernardev254
wants to merge
3
commits into
flare-foundation:main
Choose a base branch
from
bernardev254:Feat/Ecosystem-Engine-Connectors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| [ | ||
| { | ||
| "inputs": [ | ||
| { | ||
| "internalType": "bytes21", | ||
| "name": "_feedId", | ||
| "type": "bytes21" | ||
| }, | ||
| { | ||
| "internalType": "bytes21", | ||
| "name": "_referenceFeedId", | ||
| "type": "bytes21" | ||
| }, | ||
| { | ||
| "internalType": "contract IFlareContractRegistry", | ||
| "name": "_flareContractRegistry", | ||
| "type": "address" | ||
| }, | ||
| { | ||
| "internalType": "contract ISFlr", | ||
| "name": "_sFlr", | ||
| "type": "address" | ||
| } | ||
| ], | ||
| "stateMutability": "nonpayable", | ||
| "type": "constructor" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "calculateFee", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "uint256", | ||
| "name": "_fee", | ||
| "type": "uint256" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "feedId", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "bytes21", | ||
| "name": "", | ||
| "type": "bytes21" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "flareContractRegistry", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "contract IFlareContractRegistry", | ||
| "name": "", | ||
| "type": "address" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "getCurrentFeed", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "uint256", | ||
| "name": "_value", | ||
| "type": "uint256" | ||
| }, | ||
| { | ||
| "internalType": "int8", | ||
| "name": "_decimals", | ||
| "type": "int8" | ||
| }, | ||
| { | ||
| "internalType": "uint64", | ||
| "name": "_timestamp", | ||
| "type": "uint64" | ||
| } | ||
| ], | ||
| "stateMutability": "payable", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "referenceFeedId", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "bytes21", | ||
| "name": "", | ||
| "type": "bytes21" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "sFlr", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "contract ISFlr", | ||
| "name": "", | ||
| "type": "address" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| from eth_typing import ChecksumAddress | ||
| from web3 import AsyncHTTPProvider, AsyncWeb3 | ||
| from web3.middleware import ( | ||
| ExtraDataToPOAMiddleware, # pyright: ignore[reportUnknownVariableType] | ||
| ) | ||
|
|
||
| from flare_ai_kit.common.utils import load_abi | ||
| from flare_ai_kit.config import AppSettings | ||
| from flare_ai_kit.ecosystem.settings import Contracts | ||
|
|
||
|
|
||
| class CyloClient: | ||
| """ | ||
| Client for interacting with the Cylo protocol. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self.settings = AppSettings().ecosystem | ||
| self.client = AsyncWeb3( | ||
| AsyncHTTPProvider(str(self.settings.web3_provider_url)), | ||
| ) | ||
| self.client.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0) # pyright: ignore[reportUnknownArgumentType] | ||
| self.contracts = Contracts() | ||
| self.address = self.settings.account_address | ||
| self.private_key = self.settings.account_private_key | ||
| self.contract_address = self.contracts.flare.cylo_cysflr | ||
| self.abi = load_abi("cylo") | ||
| self.contract = self.client.eth.contract( | ||
| address=self.contract_address, | ||
| abi=self.abi, | ||
| ) | ||
|
|
||
| def describe_cylo_services(self) -> str: | ||
| return """ | ||
| Cylo is a decentralized protocol on the Flare Network, | ||
| enabling users to create and manage synthetic assets. | ||
| It allows for the creation of synthetic assets that track the value of real-world assets, | ||
| providing a bridge between traditional finance and decentralized finance (DeFi). | ||
| Cylo leverages the Flare Time Series Oracle (FTSOv2) for accurate price feeds, | ||
| ensuring that synthetic assets are always pegged to their underlying assets. | ||
| """ | ||
|
|
||
| async def convert_to_shares(self, assets: int, id: int) -> int: | ||
| return await self.contract.functions.convertToShares(assets, id).call() | ||
|
|
||
| async def convert_to_assets(self, shares: int, id: int) -> int: | ||
| return await self.contract.functions.convertToAssets(shares, id).call() | ||
|
|
||
| async def deposit( | ||
| self, | ||
| amount: int, | ||
| receiver: ChecksumAddress, | ||
| min_share_ratio: int, | ||
| receipt_data: bytes, | ||
| ): | ||
| """ | ||
| Deposit assets into the Cylo protocol. | ||
| """ | ||
| if self.address is None: | ||
| raise ValueError("Account address is not set") | ||
| nonce = await self.client.eth.get_transaction_count(self.address) | ||
| tx = await self.contract.functions.deposit( | ||
| amount, receiver, min_share_ratio, receipt_data | ||
| ).build_transaction( | ||
| { | ||
| "from": self.address, | ||
| "nonce": nonce, | ||
| "gas": 3000000, | ||
| "gasPrice": self.client.to_wei(50, "gwei"), | ||
| } | ||
| ) | ||
| if self.private_key is None: | ||
| raise ValueError("Private key is not set") | ||
| signed = self.client.eth.account.sign_transaction( | ||
| tx, self.private_key.get_secret_value() | ||
| ) | ||
| return await self.client.eth.send_raw_transaction(signed.raw_transaction) | ||
|
|
||
| async def mint( | ||
| self, | ||
| shares: int, | ||
| receiver: ChecksumAddress, | ||
| min_share_ratio: int, | ||
| receipt_data: bytes, | ||
| ): | ||
| if self.address is None: | ||
| raise ValueError("Account address is not set") | ||
| nonce = await self.client.eth.get_transaction_count(self.address) | ||
| tx = await self.contract.functions.mint( | ||
| shares, receiver, min_share_ratio, receipt_data | ||
| ).build_transaction( | ||
| { | ||
| "from": self.address, | ||
| "nonce": nonce, | ||
| "gas": 3000000, | ||
| "gasPrice": self.client.to_wei(50, "gwei"), | ||
| } | ||
| ) | ||
| if self.private_key is None: | ||
| raise ValueError("Private key is not set") | ||
| signed = self.client.eth.account.sign_transaction( | ||
| tx, self.private_key.get_secret_value() | ||
| ) | ||
| return await self.client.eth.send_raw_transaction(signed.raw_transaction) | ||
|
|
||
| async def withdraw( | ||
| self, | ||
| assets: int, | ||
| receiver: ChecksumAddress, | ||
| owner: ChecksumAddress, | ||
| id: int, | ||
| receipt_data: bytes, | ||
| ): | ||
| if self.address is None: | ||
| raise ValueError("Account address is not set") | ||
| nonce = await self.client.eth.get_transaction_count(self.address) | ||
| tx = await self.contract.functions.withdraw( | ||
| assets, receiver, owner, id, receipt_data | ||
| ).build_transaction( | ||
| { | ||
| "from": self.address, | ||
| "nonce": nonce, | ||
| "gas": 3000000, | ||
| "gasPrice": self.client.to_wei(50, "gwei"), | ||
| } | ||
| ) | ||
| if self.private_key is None: | ||
| raise ValueError("Private key is not set") | ||
| signed = self.client.eth.account.sign_transaction( | ||
| tx, self.private_key.get_secret_value() | ||
| ) | ||
| return await self.client.eth.send_raw_transaction(signed.raw_transaction) | ||
|
|
||
| async def redeem( | ||
| self, | ||
| shares: int, | ||
| receiver: ChecksumAddress, | ||
| owner: ChecksumAddress, | ||
| id: int, | ||
| receipt_data: bytes, | ||
| ): | ||
| if self.address is None: | ||
| raise ValueError("Account address is not set") | ||
| nonce = await self.client.eth.get_transaction_count(self.address) | ||
| tx = await self.contract.functions.redeem( | ||
| shares, receiver, owner, id, receipt_data | ||
| ).build_transaction( | ||
| { | ||
| "from": self.address, | ||
| "nonce": nonce, | ||
| "gas": 3000000, | ||
| "gasPrice": self.client.to_wei(50, "gwei"), | ||
| } | ||
| ) | ||
| if self.private_key is None: | ||
| raise ValueError("Private key is not set") | ||
| signed = self.client.eth.account.sign_transaction( | ||
| tx, self.private_key.get_secret_value() | ||
| ) | ||
| return await self.client.eth.send_raw_transaction(signed.raw_transaction) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| from eth_typing import ChecksumAddress | ||
| from web3 import AsyncHTTPProvider, AsyncWeb3 | ||
|
|
||
| from flare_ai_kit.common.utils import load_abi | ||
| from flare_ai_kit.config import AppSettings | ||
| from flare_ai_kit.ecosystem.settings import Contracts | ||
|
|
||
|
|
||
| class KineticClient: | ||
bernardev254 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def __init__(self): | ||
| self.settings = AppSettings().ecosystem | ||
| self.client = AsyncWeb3( | ||
| AsyncHTTPProvider(str(self.settings.web3_provider_url)), | ||
| ) | ||
| self.contracts = Contracts() | ||
| self.contract_address = self.contracts.flare.kinetic_comptroller | ||
| self.abi = load_abi("kineticComptroller") | ||
| self.contract = self.client.eth.contract( | ||
| address=self.contract_address, | ||
| abi=self.abi, | ||
| ) | ||
|
|
||
| async def describe_kinetic_services(self) -> str: | ||
| return """ | ||
| Kinetic is a leading lending protocol on the Flare Network, | ||
| reshaping decentralized finance (DeFi) with cutting-edge technology | ||
| and strategic partnerships. | ||
| Kinetic makes lending and borrowing digital assets like BTC, DOGE, | ||
| and XRP simple and secure. | ||
| Leveraging the Flare Time Series Oracle (FTSOv2) and FAssets, | ||
| we enable users to unlock the value of non-native assets | ||
| within a decentralized, trustless framework. | ||
| FTSOv2 provides reliable, decentralized price feeds, while | ||
| FAssets expand the range of assets available for lending and borrowing. | ||
| """ | ||
|
|
||
| async def get_kinetic_contract_address(self) -> ChecksumAddress | None: | ||
| """ | ||
| Get the Kinetic contract address for the current network. | ||
| """ | ||
| return self.contract_address | ||
|
|
||
| async def get_assets_in(self, user: ChecksumAddress) -> list[str]: | ||
| """ | ||
| Returns a list of markets the user has entered. | ||
| """ | ||
| try: | ||
| return await self.contract.functions.getAssetsIn(user).call() | ||
| except Exception as e: | ||
| raise RuntimeError(f"Failed to fetch assets in: {e}") | ||
|
|
||
| async def get_account_liquidity(self, user: ChecksumAddress) -> dict[str, int]: | ||
| """ | ||
| Returns the account's liquidity and shortfall values. | ||
| """ | ||
| try: | ||
| ( | ||
| err, | ||
| liquidity, | ||
| shortfall, | ||
| ) = await self.contract.functions.getAccountLiquidity(user).call() | ||
| return {"error_code": err, "liquidity": liquidity, "shortfall": shortfall} | ||
| except Exception as e: | ||
| raise RuntimeError(f"Failed to fetch account liquidity: {e}") | ||
|
|
||
| async def check_membership( | ||
| self, user: ChecksumAddress, ctoken_address: ChecksumAddress | ||
| ) -> bool: | ||
| """ | ||
| Checks whether a user has entered a specific market. | ||
| """ | ||
| try: | ||
| return await self.contract.functions.checkMembership( | ||
| user, ctoken_address | ||
| ).call() | ||
| except Exception as e: | ||
| raise RuntimeError(f"Failed to check membership: {e}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.