Skip to content

Commit 546391e

Browse files
Starting staking FLR
1 parent 8bc5e85 commit 546391e

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

src/flare_ai_defai/api/routes/chat.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,16 @@ async def handle_borrow(self, message: str, user: UserInfo) -> dict[str, str]:
552552
if (ai_response_json.get("amount") == 0.0):
553553
return {"response": "Sorry, amount must be more than 0.0 \n " + json.dumps(ai_response_json)}
554554

555+
txs = self.kinetic_market.swapFLRtoSFLR(user, 1)
556+
557+
self.blockchain.add_tx_to_queue(msg=message, txs=txs)
558+
formatted_preview = (
559+
"Transaction Preview: "
560+
+ f"Staking {Web3.from_wei(tx.get('value', 0), 'ether')} "
561+
+ f"FLR to sFLR\nType CONFIRM to proceed."
562+
)
563+
return {"response": formatted_preview}
555564

556-
# Return stringified JSON
557-
return {"response": json.dumps(ai_response_json)}
558565

559566

560567
async def handle_supply(self, message: str, user: UserInfo) -> dict[str, str]:

src/flare_ai_defai/blockchain/kinetic_market.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from web3 import Web3
1313
from web3.contract import Contract
1414
from web3.types import TxParams
15+
from flare_ai_defai.storage.fake_storage import WalletStore
1516

1617
from flare_ai_defai.models import UserInfo
1718

@@ -106,7 +107,7 @@ class KineticMarket:
106107
"type": "function"
107108
},]
108109

109-
def __init__(self, web3_provider_url: str, flare_explorer: FlareExplorer, flare_provider: FlareProvider) -> None:
110+
def __init__(self, web3_provider_url: str, flare_explorer: FlareExplorer, flare_provider: FlareProvider, wallet_store: WalletStore) -> None:
110111
"""
111112
Args:
112113
web3_provider_url (str): URL of the Web3 provider endpoint
@@ -118,6 +119,7 @@ def __init__(self, web3_provider_url: str, flare_explorer: FlareExplorer, flare_
118119
self.web3_provider_url = web3_provider_url
119120
self.flare_explorer = flare_explorer
120121
self.flare_provider = flare_provider
122+
self.wallet_store = wallet_store
121123

122124
#self.supplySFLRwithFLR(user, 1)
123125

@@ -199,7 +201,7 @@ def supplySFLRwithFLR(self,user:UserInfo, amount: float):
199201

200202
return tx_hashes
201203

202-
def swapFLRtoSFLR(self, amount: float, spender: str = None):
204+
def swapFLRtoSFLR(self, user: UserInfo, amount: float):
203205
if not self.w3.is_connected():
204206
raise Exception("Not connected to Flare blockchain")
205207

@@ -214,24 +216,19 @@ def swapFLRtoSFLR(self, amount: float, spender: str = None):
214216
self.logger.debug("Converted amount to wei", amount=amount, amount_wei=amount_wei)
215217

216218
# Check balance
217-
balance = self.w3.eth.get_balance(self.flare_provider.address)
219+
balance = self.w3.eth.get_balance(self.wallet_store.get_address(user))
218220
if balance < amount_wei:
219221
raise ValueError(f"Insufficient balance: {self.w3.from_wei(balance, 'ether')} FLR, required: {amount} FLR")
220222

221223
# Fetch current nonce
222-
current_nonce = self.w3.eth.get_transaction_count(self.flare_provider.address)
224+
current_nonce = self.w3.eth.get_transaction_count(self.wallet_store.get_address(user))
223225

224226
# Create submit transaction with value
225-
tx1 = self.flare_provider.create_contract_function_tx(
227+
tx1 = self.flare_provider.create_contract_function_tx(user,
226228
contract, "submit", 0, value=amount_wei
227229
)
228-
tx2 = self.flare_provider.create_contract_function_tx(
229-
contract, "approve", 1, spender, amount_wei
230-
)
231-
232-
233230

234-
return [tx1, tx2]
231+
return [tx1]
235232

236233

237234
def borrowUSDC(self,user_order: dict):

src/flare_ai_defai/blockchain/sparkdex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ def swap_erc20_tokens(self, token_in: str, token_out: str, amount_in: float):
351351
#token_in_abi = token_abi[token_in.lower()]
352352
#token_out_abi = token_abi[token_out.lower()]
353353

354-
token_in_abi = self.flare_explorer.get_contract_abi(contract_address=token_address_abi[token_in.lower()])
355-
token_out_abi = self.flare_explorer.get_contract_abi(contract_address=token_address_abi[token_out.lower()])
354+
#token_in_abi = self.flare_explorer.get_contract_abi(contract_address=token_address_abi[token_in.lower()])
355+
#token_out_abi = self.flare_explorer.get_contract_abi(contract_address=token_address_abi[token_out.lower()])
356356
token_in_abi = ERC20_ABI
357357
token_out_abi = ERC20_ABI
358358

src/flare_ai_defai/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def create_app() -> FastAPI:
8686
flareExplorer=flare_explorer,
8787
attestation=Vtpm(simulate=settings.simulate_attestation),
8888
prompts=PromptService(),
89-
kinetic_market=KineticMarket(settings.web3_provider_url, flare_explorer, flare_provider),
89+
kinetic_market=KineticMarket(settings.web3_provider_url, flare_explorer, flare_provider, wallet_store),
9090
sparkdex=SparkDEX(settings.web3_provider_url, flare_explorer, flare_provider, wallet_store),
9191
wallet_store=wallet_store
9292
)

0 commit comments

Comments
 (0)