Skip to content

Commit

Permalink
When tx gets reverted marks it as done
Browse files Browse the repository at this point in the history
  • Loading branch information
KasparPeterson committed Mar 27, 2024
1 parent 701cd3b commit 86168cf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions contracts/contracts/ChatOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ contract ChatOracle {
string memory errorMessage
) public onlyWhitelisted {
require(!isPromptProcessed[promptId], "Prompt already processed");
isPromptProcessed[promptId] = true;
IChatGpt(callbackAddresses[promptId]).onOracleLlmResponse(
promptCallBackId,
response,
Expand Down Expand Up @@ -280,6 +281,7 @@ contract ChatOracle {
string memory errorMessage
) public onlyWhitelisted {
require(!isFunctionProcessed[functionId], "Function already processed");
isFunctionProcessed[functionId] = true;
IChatGpt(functionCallbackAddresses[functionId]).onOracleFunctionResponse(
functionCallBackId,
response,
Expand Down Expand Up @@ -313,6 +315,7 @@ contract ChatOracle {
string memory errorMessage
) public onlyWhitelisted {
require(!isPromptProcessed[promptId], "Prompt already processed");
isPromptProcessed[promptId] = true;
IChatGpt(callbackAddresses[promptId]).onOracleOpenAiLlmResponse(
promptCallBackId,
response,
Expand Down Expand Up @@ -346,6 +349,7 @@ contract ChatOracle {
string memory errorMessage
) public onlyWhitelisted {
require(!isPromptProcessed[promptId], "Prompt already processed");
isPromptProcessed[promptId] = true;
IChatGpt(callbackAddresses[promptId]).onOracleGroqLlmResponse(
promptCallBackId,
response,
Expand Down
59 changes: 59 additions & 0 deletions oracles/src/repositories/oracle_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ async def send_chat_response(self, chat: Chat) -> bool:
except Exception as e:
chat.is_processed = True
chat.transaction_receipt = {"error": str(e)}
await self.mark_as_done(chat)
return False
signed_tx = self.web3_client.eth.account.sign_transaction(
tx, private_key=self.account.key
Expand All @@ -106,6 +107,39 @@ async def send_chat_response(self, chat: Chat) -> bool:
chat.is_processed = bool(tx_receipt.get("status"))
return bool(tx_receipt.get("status"))

async def mark_as_done(self, chat: Chat):
nonce = await self.web3_client.eth.get_transaction_count(self.account.address)
tx_data = {
"from": self.account.address,
"nonce": nonce,
# TODO: pick gas amount in a better way
# "gas": 1000000,
"maxFeePerGas": self.web3_client.to_wei("2", "gwei"),
"maxPriorityFeePerGas": self.web3_client.to_wei("1", "gwei"),
}
if chain_id := settings.CHAIN_ID:
tx_data["chainId"] = int(chain_id)

if chat.prompt_type == PromptType.OPENAI:
tx = await self.oracle_contract.functions.markOpenAiPromptAsProcessed(
chat.id,
).build_transaction(tx_data)
elif chat.prompt_type == PromptType.GROQ:
tx = await self.oracle_contract.functions.markGroqPromptAsProcessed(
chat.id,
).build_transaction(tx_data)
else:
tx = await self.oracle_contract.functions.markPromptAsProcessed(
chat.id,
).build_transaction(tx_data)
signed_tx = self.web3_client.eth.account.sign_transaction(
tx, private_key=self.account.key
)
tx_hash = await self.web3_client.eth.send_raw_transaction(
signed_tx.rawTransaction
)
await self.web3_client.eth.wait_for_transaction_receipt(tx_hash)

async def _build_response_tx(self, chat: Chat):
nonce = await self.web3_client.eth.get_transaction_count(self.account.address)
tx_data = {
Expand Down Expand Up @@ -208,6 +242,7 @@ async def send_function_call_response(
except Exception as e:
function_call.is_processed = True
function_call.transaction_receipt = {"error": str(e)}
await self.mark_function_call_as_done(function_call)
return False
signed_tx = self.web3_client.eth.account.sign_transaction(
tx, private_key=self.account.key
Expand All @@ -220,6 +255,30 @@ async def send_function_call_response(
function_call.is_processed = bool(tx_receipt.get("status"))
return bool(tx_receipt.get("status"))

async def mark_function_call_as_done(self, function_call: FunctionCall):
nonce = await self.web3_client.eth.get_transaction_count(self.account.address)
tx_data = {
"from": self.account.address,
"nonce": nonce,
# TODO: pick gas amount in a better way
# "gas": 1000000,
"maxFeePerGas": self.web3_client.to_wei("2", "gwei"),
"maxPriorityFeePerGas": self.web3_client.to_wei("1", "gwei"),
}
if chain_id := settings.CHAIN_ID:
tx_data["chainId"] = int(chain_id)

tx = await self.oracle_contract.functions.markFunctionAsProcessed(
function_call.id,
).build_transaction(tx_data)
signed_tx = self.web3_client.eth.account.sign_transaction(
tx, private_key=self.account.key
)
tx_hash = await self.web3_client.eth.send_raw_transaction(
signed_tx.rawTransaction
)
await self.web3_client.eth.wait_for_transaction_receipt(tx_hash)

async def _get_openai_config(self, i: int) -> Optional[OpenAiConfig]:
config = await self.oracle_contract.functions.openAiConfigurations(i).call()
if not config or not config[0] or not config[0] in get_args(OpenAiModelType):
Expand Down

0 comments on commit 86168cf

Please sign in to comment.