def test_create_fail_invalid_abi(testnet): with pytest.raises( erc20token.SdkConfigurationError, match="invalid token contract abi: 'abi' is not a list"): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address) with pytest.raises( erc20token.SdkConfigurationError, match="invalid token contract abi: 'abi' is not a list"): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi={}) with pytest.raises( erc20token.SdkConfigurationError, match="invalid token contract abi: 'abi' is not a list"): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi='bad') with pytest.raises( erc20token.SdkConfigurationError, match="invalid token contract abi: The elements of 'abi' " "are not all dictionaries"): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=['bad'])
def test_create_fail_empty_endpoint(): with pytest.raises( erc20token.SdkConfigurationError, match='either provider or provider endpoint must be provided'): erc20token.SDK() with pytest.raises( erc20token.SdkConfigurationError, match='either provider or provider endpoint must be provided'): erc20token.SDK(provider_endpoint_uri='')
def test_create_fail_keyfile(testnet): # file missing with pytest.raises( erc20token.SdkConfigurationError, match= "cannot load keyfile: \[Errno 2\] No such file or directory: 'missing.json'" ): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, keyfile='missing.json') # not json with open(TEST_KEYFILE, 'w+') as f: f.write('not json') with pytest.raises( erc20token.SdkConfigurationError, match="cannot load keyfile: No JSON object could be decoded"): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, keyfile=TEST_KEYFILE) # json, but invalid format with open(TEST_KEYFILE, 'w+') as f: f.write('[]') with pytest.raises(erc20token.SdkConfigurationError, match="cannot load keyfile: invalid keyfile format"): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, keyfile=TEST_KEYFILE) os.remove(TEST_KEYFILE) # good keyfile, wrong password erc20token.create_keyfile(testnet.private_key, TEST_PASSWORD, TEST_KEYFILE) with pytest.raises( erc20token.SdkConfigurationError, match='cannot load keyfile: MAC mismatch. Password incorrect?'): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, keyfile=TEST_KEYFILE, password='******') os.remove(TEST_KEYFILE)
def test_create_fail_invalid_contract_address(testnet): with pytest.raises( erc20token.SdkConfigurationError, match="invalid token contract address: '' is not an address"): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri) with pytest.raises( erc20token.SdkConfigurationError, match="invalid token contract address: '0xbad' is not an address"): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address='0xbad') with pytest.raises( erc20token.SdkConfigurationError, match= "invalid token contract address: '0x4c6527c2BEB032D46cfe0648072cAb641cA0aA81' " "has an invalid EIP55 checksum"): erc20token.SDK( provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address='0x4c6527c2BEB032D46cfe0648072cAb641cA0aA81')
def initSDK(privateKey): # Init sdk on ropsten token_sdk = erc20token.SDK( provider_endpoint_uri='http://159.89.240.246:8545', private_key=privateKey, contract_address='0xEF2Fcc998847DB203DEa15fC49d0872C7614910C', contract_abi=contract_abi) return token_sdk
def test_create_fail_bad_private_key(testnet): with pytest.raises( erc20token.SdkConfigurationError, match='cannot load private key: Unexpected private key format.' ' Must be length 32 byte string'): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, private_key='bad')
def test_create_with_gas_params(testnet): sdk = erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, private_key=testnet.private_key, gas_price=10.1, gas_limit=10000) assert sdk assert sdk._tx_manager.gas_price == 10100000000 assert sdk._tx_manager.gas_limit == 10000
def test_create_with_private_key(testnet): sdk = erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, private_key=testnet.private_key) assert sdk assert sdk.web3 assert sdk.token_contract assert sdk.private_key == testnet.private_key assert sdk.get_address() == testnet.address
def test_create_invalid_gas_params(testnet): with pytest.raises(erc20token.SdkConfigurationError, match='gas price must be either integer of float'): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, gas_price='bad') with pytest.raises(erc20token.SdkConfigurationError, match='gas price must be either integer of float'): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, gas_price='0x123') with pytest.raises(erc20token.SdkConfigurationError, match='gas limit must be integer'): erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, gas_price=10, gas_limit='bad')
def test_sdk(testnet): sdk = erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, private_key=testnet.private_key, contract_address=testnet.contract_address, contract_abi=testnet.contract_abi) assert sdk assert sdk.web3 assert sdk.token_contract assert sdk.private_key == testnet.private_key assert sdk.get_address() == testnet.address assert sdk._tx_manager.gas_price == sdk.web3.eth.gasPrice return sdk
def test_create_with_keyfile(testnet): erc20token.create_keyfile(testnet.private_key, TEST_PASSWORD, TEST_KEYFILE) sdk = erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi, keyfile=TEST_KEYFILE, password=TEST_PASSWORD) assert sdk assert sdk.web3 assert sdk.token_contract assert sdk.private_key == testnet.private_key assert sdk.get_address() == testnet.address os.remove(TEST_KEYFILE)
def post(self): address = request.json["address"] token_sdk = erc20token.SDK(provider_endpoint_uri=provider_endpoint_url, contract_address=contract_address, contract_abi=contract_abi_tbar) tbar_balance = token_sdk.get_address_token_balance(address) eth_balance = token_sdk.get_address_ether_balance(address) total_tbar_supply = token_sdk.get_token_total_supply() return { "tbar_balance": str(tbar_balance), "eth_balance": str(eth_balance), "total_tbar_supply": str(total_tbar_supply) }
def test_sdk_not_configured(testnet): sdk = erc20token.SDK(provider_endpoint_uri=testnet.provider_endpoint_uri, contract_address=testnet.address, contract_abi=testnet.contract_abi) with pytest.raises(erc20token.SdkNotConfiguredError, match='private key not configured'): sdk.get_address() with pytest.raises(erc20token.SdkNotConfiguredError, match='private key not configured'): sdk.get_ether_balance() with pytest.raises(erc20token.SdkNotConfiguredError, match='private key not configured'): sdk.get_token_balance() with pytest.raises(erc20token.SdkNotConfiguredError, match='private key not configured'): sdk.send_ether('address', 100) with pytest.raises(erc20token.SdkNotConfiguredError, match='private key not configured'): sdk.send_tokens('address', 100)
def post(self): users = mongo.db.Alphawallet email = request.json["email"] tx_id = request.json["tx_id"] if users.find_one({"email": email}) and users.find_one( {"tx_id": tx_id}): token_sdk = erc20token.SDK( provider_endpoint_uri=provider_endpoint_url, contract_address=contract_address, contract_abi=contract_abi_tbar) tx_status = token_sdk.get_transaction_status(tx_id) if tx_status == 0: return {"tx_status": "Unknown"} if tx_status == 1: return {"tx_status": "Pending"} if tx_status == 2: return {"tx_status": "Sucess"} if tx_status == 3: return {"tx_status": "Failed"}
def put(self): users = mongo.db.Alphawallet email = request.json["email"] amount = request.json["amount"] to_address = request.json["address"] private_key = request.json["Private_key"] hash_key = users.find_one({'email': email})["private_key_eth"] if users.find_one({"email": email}) and pbkdf2_sha256.verify( private_key, hash_key) == True: token_sdk = erc20token.SDK( provider_endpoint_uri=provider_endpoint_url, private_key=private_key, contract_address=contract_address, contract_abi=contract_abi_tbar) eth_balance = token_sdk.get_ether_balance() if eth_balance >= int(amount): tx_id = token_sdk.send_ether(to_address, int(amount)) users.update({"email": email}, {'$set': {'tx_id': tx_id}}) return {"tx_id": tx_id} else: abort(400, message='Something went wrong')
def test_create_fail_bad_endpoint(testnet): with pytest.raises(erc20token.SdkConfigurationError, match='cannot connect to provider endpoint'): erc20token.SDK(provider_endpoint_uri='bad', contract_address=testnet.address, contract_abi=testnet.contract_abi)