-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NFT functions for general agent (#582)
- Loading branch information
Showing
5 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
97 changes: 97 additions & 0 deletions
97
prediction_market_agent/agents/microchain_agent/nft_functions.py
This file contains 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,97 @@ | ||
from microchain import Function | ||
from prediction_market_agent_tooling.loggers import logger | ||
from prediction_market_agent_tooling.tools.contract import ( | ||
ContractOwnableERC721OnGnosisChain, | ||
) | ||
from web3 import Web3 | ||
|
||
from prediction_market_agent.agents.microchain_agent.microchain_agent_keys import ( | ||
MicrochainAgentKeys, | ||
) | ||
|
||
|
||
class BalanceOfNFT(Function): | ||
@property | ||
def description(self) -> str: | ||
return "Returns the number of given NFT owned by the specified address." | ||
|
||
@property | ||
def example_args(self) -> list[str]: | ||
return [ | ||
"0xNFTAddress", | ||
"0xOwnerddress", | ||
] | ||
|
||
def __call__( | ||
self, | ||
nft_address: str, | ||
owner_address: str, | ||
) -> int: | ||
contract = ContractOwnableERC721OnGnosisChain( | ||
address=Web3.to_checksum_address(nft_address) | ||
) | ||
balance: int = contract.balanceOf(Web3.to_checksum_address(owner_address)) | ||
return balance | ||
|
||
|
||
class OwnerOfNFT(Function): | ||
@property | ||
def description(self) -> str: | ||
return "Returns the owner address of the specified NFT token ID." | ||
|
||
@property | ||
def example_args(self) -> list[str]: | ||
return ["0xNFTAddress", "1"] | ||
|
||
def __call__( | ||
self, | ||
nft_address: str, | ||
token_id: int, | ||
) -> str: | ||
contract = ContractOwnableERC721OnGnosisChain( | ||
address=Web3.to_checksum_address(nft_address) | ||
) | ||
owner_address = contract.ownerOf(token_id) | ||
return owner_address | ||
|
||
|
||
class SafeTransferFromNFT(Function): | ||
@property | ||
def description(self) -> str: | ||
return "Transfers the specified NFT token ID from one address to another." | ||
|
||
@property | ||
def example_args(self) -> list[str]: | ||
return [ | ||
"0xNFTAddress", | ||
"0xRecipientAddress", | ||
"1", | ||
] | ||
|
||
def __call__( | ||
self, | ||
nft_address: str, | ||
to_address: str, | ||
token_id: int, | ||
) -> str: | ||
keys = MicrochainAgentKeys() | ||
contract = ContractOwnableERC721OnGnosisChain( | ||
address=Web3.to_checksum_address(nft_address) | ||
) | ||
if keys.ENABLE_NFT_TRANSFER: | ||
contract.safeTransferFrom( | ||
api_keys=keys, | ||
from_address=keys.bet_from_address, | ||
to_address=Web3.to_checksum_address(to_address), | ||
tokenId=token_id, | ||
) | ||
else: | ||
logger.warning("NFT transfer is disabled in the environment.") | ||
return "Token transferred successfully." | ||
|
||
|
||
NFT_FUNCTIONS: list[type[Function]] = [ | ||
BalanceOfNFT, | ||
OwnerOfNFT, | ||
SafeTransferFromNFT, | ||
] |
This file contains 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