Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1,095 changes: 1,095 additions & 0 deletions src/flare_ai_kit/abis/cylo.json

Large diffs are not rendered by default.

2,061 changes: 2,061 additions & 0 deletions src/flare_ai_kit/abis/kineticComptroller.json

Large diffs are not rendered by default.

116 changes: 116 additions & 0 deletions src/flare_ai_kit/abis/sceptre.json
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"
}
]
160 changes: 160 additions & 0 deletions src/flare_ai_kit/ecosystem/applications/cylo.py
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)
77 changes: 77 additions & 0 deletions src/flare_ai_kit/ecosystem/applications/kinetic.py
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:
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}")
Loading