2024-06-09 01:59:27 +03:00

88 lines
3.6 KiB
Python

from giza.agents import AgentResult, GizaAgent
from starknet_py.contract import Contract
from starknet_py.net.account.account import Account
from starknet_py.net.models import StarknetChainId
from starknet_py.net.signer.stark_curve_signer import KeyPair
from starknet_py.net.full_node_client import FullNodeClient
class MyAgent(GizaAgent):
def __init__(self, agent_id, contracts, chain, account_alias):
super().__init__(agent_id, contracts, chain, account_alias)
self.account = Account(
address=account_alias,
client=FullNodeClient(node_url="<your Alchemy API Key URL / Node URL>"),
key_pair=KeyPair.from_private_key("<your private key>"),
chain=StarknetChainId.SEPOLIA,
)
self.contract = Contract.from_address(provider=self.account, address="<your contract address>")
async def predict(self, input_feed):
# Make a prediction using the input data
X = input_feed["input"]
# Perform some complex computation on X
result = await self.contract.functions["compute"].invoke_v1(X)
return result
async def transfer_funds(self, sender, recipient, amount):
# Transfer funds from sender to recipient
invocation = await self.contract.functions["transferFrom"].invoke_v1(
sender=sender, recipient=recipient, amount=amount, max_fee=int(1e16)
)
return invocation
async def execute(self, input_feed):
# Execute a complex workflow
prediction = await self.predict(input_feed)
if prediction > 0.5:
# Transfer funds from sender to recipient
await self.transfer_funds("321", "123", 10000)
# Update the contract state
await self.contract.functions["update_state"].invoke_v1("new_state")
else:
# Transfer funds from sender to recipient
await self.transfer_funds("123", "321", 5000)
# Update the contract state
await self.contract.functions["update_state"].invoke_v1("old_state")
return AgentResult(success=True)
async def analyze_data(self, input_feed):
# Analyze the input data
X = input_feed["input"]
# Perform some complex analysis on X
result = await self.contract.functions["analyze"].invoke_v1(X)
return result
async def make_decision(self, input_feed):
# Make a decision based on the analysis
analysis = await self.analyze_data(input_feed)
if analysis > 0.5:
# Execute a specific action
await self.execute_specific_action(input_feed)
else:
# Execute a different action
await self.execute_different_action(input_feed)
return AgentResult(success=True)
async def execute_specific_action(self, input_feed):
# Execute a specific action
await self.transfer_funds("321", "123", 10000)
await self.contract.functions["update_state"].invoke_v1("new_state")
return AgentResult(success=True)
async def execute_different_action(self, input_feed):
# Execute a different action
await self.transfer_funds("123", "321", 5000)
await self.contract.functions["update_state"].invoke_v1("old_state")
return AgentResult(success=True)
agent = MyAgent(
agent_id="1",
contracts={"my_contract": " 0x01342e21e7af743f4741565b5d137fc10dbe702a420bf2cc51a9a0e42a814526"},
chain="Sepolia",
account_alias="0x01342e21e7af743f4741565b5d137fc10dbe702a420bf2cc51a9a0e42a814526"
)
input_feed = {"input": [1, 2, 3, 4, 5]}
result = agent.make_decision(input_feed)
print(result)