kudos.utils
Define utility functions.
Copyright (C) 2021 Gitcoin Core
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.gnu.org/licenses/.
humanize_name
humanize_name(name)
Turn snake_case into Snake Case.
Returns: str: The humanized name.
computerize_name
computerize_name(name)
Turn Humanized Name into humanized_name.
Returns: str: computerized_name
get_rarity_score
get_rarity_score(num_clones_allowed)
Calculate rarity metrics based on the num_clones_allowed.
Args: num_clones_allowed (int): Number of kudos clones allowed.
Returns: str: Rarity description.
Raises: ValueError: Raises an error if the number of clones allowed in less than one.
KudosError
KudosError(self, /, *args, **kwargs)
Base class for exceptions in this module.
KudosContract
KudosContract(self, network='localhost', sockets=False)
A class represending the Kudos.sol contract.
Note: There are two types of interactions that can be done on the Solidity contract, - call() - transact()
A call() is just a getter, and does not require gas, or an account to transact with. A transact() or transaction requires gas and typically changes state on the contract.
A transaction requires an account, because it needs somewhere to pull the gas from. When working in Javascript and web3js, Metamask is used to handle this interaction. When working in Python and web3py, there is no interaction with MetaMask so this needs to be handled behind the scenes, by providing the account and private_key to web3py to create the raw transaction.
Attributes: address (str): Eth address of the kudos contract network (str): The blockchain network (localhost, rinkeby, ropsten, mainnet)
get_kudos_map
KudosContract.get_kudos_map(kudos, metadata)
Pass in a kudos array that is returned from web3, convert to dictionary.
Use this to operate on the database.
Args: kudos (list): A kudos object returned from the Kudos.sol contract. Soldidity returns the Kudos strcut as an array. metadata (dict): The metadata return from the tokenURI.
Returns: dict: Kudos dictionary with key/values to be used to interact with the database.
may_require_key
KudosContract.may_require_key(f)
Decorator to check if the operation needs a private key.
log_args
KudosContract.log_args(f)
Decorator to log out the contract args.
retry
KudosContract.retry(f)
Decorator to retry a function if it failed.
remove_kudos_orphans_db
KudosContract.remove_kudos_orphans_db(self)
DEPRECATED. This funciton must be updated to use. Sync up existing kudos from the blockchain to the database.
Then remove all "orphaned Kudos" from the database.
sync_db_without_txid
KudosContract.sync_db_without_txid(self, kudos_id)
DEPRECATED. The regular sync_db method should be preferred over this.
This method is only to be used if you are syncing kudos directly from the blockchain and don't know the txid.
The problem with not having a txid that is there is no good way to related it back to the kudos_transfer object. Which means we don't know who the original sender is.
Args: kudos_id (int): Kudos id.
sync_db
KudosContract.sync_db(self, kudos_id, txid)
Sync up the Kudos contract on the blockchain with the database.
Args: kudos_id (int): Kudos Id txid (str): The transaction hash.
mint
KudosContract.mint(self, *args, account=None, private_key=None, skip_sync=False, gas_price_gwei=None, dont_wait_for_kudos_id_return_tx_hash_instead=False)
Contract transaction method.
Mint a new Gen0 Kudos on the blockchain. Not to be confused with clone. A clone() operation is only valid for an already existing Gen0 Kudos.
From Kudos.sol:
Args: *args: From Kudos.sol: mint( address _to, uint256 _priceFinney, uint256 _numClonesAllowed, string _tokenURI, ) account (str, optional): Public account address. Not needed for localhost testing. private_key (str, optional): Private key for account. Not needed for localhost testing. skip_sync (bool, optional): If True, don't sync the database after the mint.
Returns: int: If a sync did occur, returns the kudos_id
sync_latest
KudosContract.sync_latest(self, the_buffer=0)
Contract transaction method.
Sync the latest kudos from the chain
clone
KudosContract.clone(self, *args, account=None, private_key=None, skip_sync=False)
Contract transaction method.
Args: *args: From Kudos.sol clone( address _to, uint256 _tokenId, uint256 numClonesRequested ) account (str, optional): Public account address. Not needed for localhost testing. private_key (str, optional): Private key for account. Not needed for localhost testing. skip_sync (bool, optional): If True, don't sync the database after the mint.
Returns: int: The kudos_id.
burn
KudosContract.burn(self, *args, account=None, private_key=None, skip_sync=False)
Contract transaction method.
Args: *args: From Kudos.sol burn( address _owner, uint256 _tokenId, ) account (str, optional): Public account address. Not needed for localhost testing. private_key (str, optional): Private key for account. Not needed for localhost testing. skip_sync (bool, optional): If True, don't sync the database after the mint.
Returns: int: The kudos_id.
getKudosById
KudosContract.getKudosById(self, *args, to_dict=False)
Contract call method.
Args: *args: From Kudos.sol: getKudosById(uint256 tokenId) to_dict (bool, optional): Return a dictionary mapping instead of an array.
Returns: list or dict: From Kudos.sol returns (uint256 priceFinney, uint256 numClonesAllowed, uint256 numClonesInWild, uint256 clonedFromId)
getLatestId
KudosContract.getLatestId(self)
Contract call method.
From Kudos.sol: getLatestId() view public returns (uint256 tokenId)
Returns: int: The latest token id.
gen0_exists_db
KudosContract.gen0_exists_db(self, kudos_name)
Helper method.
Args: kudos_name (TYPE): Description
Returns: bool: Whether or not the token name exists.
create_token_uri_url
KudosContract.create_token_uri_url(self, **kwargs)
Create a tokenURI object, upload it to IPFS, and return the URL.
Keyword Args: name (str): Name of the kudos. image (str): Image location of the kudos. Should be a link to an image on the web. description (str): Word description of the kudos. attributes (dict): Dictionary containing attributes of the kudos. tags (str): comma delimited tags. number_of_clones_allowed (int): self explanatory. rarity (int): integer from 0 to 100 (0 is most common). external_url (str): External link to where the Kudos lives on the Gitcoin site. background_color (str): Hex code.
Returns: str: URL location on IPFS where the URI data is stored.