async def create_contract(session: Session, address: str, active: bool, chain: str) -> None: """Create contract entry in db. Args: session (Session): sqlalchemy session address (str): contract address active (bool): contract status chain (str): chain the contract is associated with Raises: ChainNotFound: raised if the chain specified does not exist in the db InvalidAddress: raised if the address is not valid for the chain specified """ chain_data = await Chain.get_chain(session, chain) if not chain_data: raise ChainNotFound(f"Chain not found: {chain}") if (chain_data.type == "evm" and not web3.Web3.isAddress(address)) or ( chain_data.type == "substrate" and not is_valid_ss58_address( address, SubstrateChain.get_ss58_prefix(chain))): raise InvalidAddress( f"Invalid address for chain: {chain} - address: {address}") await session.execute( sa.insert(Contract).values(address=address, active=active, chain=chain)) await session.commit()
def is_valid_polkadot_address(value: str) -> bool: return is_valid_ss58_address(value=value, valid_ss58_format=0)
def test_is_valid_ss58_address(self): self.assertTrue(is_valid_ss58_address('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')) self.assertTrue(is_valid_ss58_address('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', valid_ss58_format=42)) self.assertFalse(is_valid_ss58_address('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', valid_ss58_format=2)) self.assertTrue(is_valid_ss58_address('GLdQ4D4wkeEJUX8DBT9HkpycFVYQZ3fmJyQ5ZgBRxZ4LD3S', valid_ss58_format=2)) self.assertFalse(is_valid_ss58_address('GLdQ4D4wkeEJUX8DBT9HkpycFVYQZ3fmJyQ5ZgBRxZ4LD3S', valid_ss58_format=42)) self.assertFalse(is_valid_ss58_address('GLdQ4D4wkeEJUX8DBT9HkpycFVYQZ3fmJyQ5ZgBRxZ4LD3S', valid_ss58_format=0)) self.assertTrue(is_valid_ss58_address('12gX42C4Fj1wgtfgoP624zeHrcPBqzhb4yAENyvFdGX6EUnN', valid_ss58_format=0)) self.assertFalse(is_valid_ss58_address('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQ')) self.assertFalse(is_valid_ss58_address('6GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY')) self.assertFalse(is_valid_ss58_address('0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d')) self.assertFalse(is_valid_ss58_address('d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d')) self.assertFalse(is_valid_ss58_address('incorrect_string'))
def is_valid_kusama_address(value: str) -> bool: return is_valid_ss58_address(value=value, valid_ss58_format=2)