def test_safe_creation_with_fixed_cost(self): salt_nonce = generate_salt_nonce() owners = [Account.create().address for _ in range(2)] data = { 'saltNonce': salt_nonce, 'owners': owners, 'threshold': len(owners) } fixed_creation_cost = 123 with self.settings(SAFE_FIXED_CREATION_COST=fixed_creation_cost): SafeCreationServiceProvider.del_singleton() response = self.client.post(reverse('v2:safe-creation'), data, format='json') SafeCreationServiceProvider.del_singleton() self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json['safe'] self.assertTrue(check_checksum(safe_address)) self.assertTrue(check_checksum(response_json['paymentReceiver'])) self.assertEqual(response_json['paymentToken'], NULL_ADDRESS) self.assertEqual(response_json['payment'], '123') self.assertGreater(int(response_json['gasEstimated']), 0) self.assertGreater(int(response_json['gasPriceEstimated']), 0) self.assertGreater(len(response_json['setupData']), 2)
def test_safe_creation_with_fixed_cost(self): salt_nonce = generate_salt_nonce() owners = [Account.create().address for _ in range(2)] data = { "saltNonce": salt_nonce, "owners": owners, "threshold": len(owners) } fixed_creation_cost = 456 with self.settings(SAFE_FIXED_CREATION_COST=fixed_creation_cost): SafeCreationServiceProvider.del_singleton() response = self.client.post(reverse("v3:safe-creation"), data, format="json") SafeCreationServiceProvider.del_singleton() self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json["safe"] self.assertTrue(check_checksum(safe_address)) self.assertTrue(check_checksum(response_json["paymentReceiver"])) self.assertEqual(response_json["paymentToken"], NULL_ADDRESS) self.assertEqual(response_json["payment"], str(fixed_creation_cost)) self.assertGreater(int(response_json["gasEstimated"]), 0) self.assertGreater(int(response_json["gasPriceEstimated"]), 0) self.assertGreater(len(response_json["setupData"]), 2)
def build_test_safe( self, number_owners: int = 3, threshold: Optional[int] = None, owners: Optional[List[str]] = None, fallback_handler: Optional[str] = None, ) -> SafeCreate2Tx: salt_nonce = generate_salt_nonce() owners = (owners if owners else [Account.create().address for _ in range(number_owners)]) threshold = threshold if threshold else len(owners) - 1 gas_price = self.ethereum_client.w3.eth.gas_price return Safe.build_safe_create2_tx( self.ethereum_client, self.safe_contract_address, self.proxy_factory.address, salt_nonce, owners, threshold, fallback_handler=fallback_handler, gas_price=gas_price, payment_token=None, fixed_creation_cost=0, )
def create2_test_safe_in_db(self, owners=None, number_owners=3, threshold=None, payment_token=None, salt_nonce=None) -> SafeCreation2: salt_nonce = salt_nonce or generate_salt_nonce() owners = owners or [Account.create().address for _ in range(number_owners)] threshold = threshold if threshold else len(owners) return self.safe_creation_service.create2_safe_tx(salt_nonce, owners, threshold, payment_token)
def test_safe_creation_with_payment_token(self): salt_nonce = generate_salt_nonce() owners = [Account.create().address for _ in range(2)] payment_token = Account.create().address data = { "saltNonce": salt_nonce, "owners": owners, "threshold": len(owners), "paymentToken": payment_token, } response = self.client.post(reverse("v3:safe-creation"), data=data, format="json") self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) response_json = response.json() self.assertIn("InvalidPaymentToken", response_json["exception"]) self.assertIn(payment_token, response_json["exception"]) fixed_eth_conversion = 0.1 token_model = TokenFactory(address=payment_token, fixed_eth_conversion=fixed_eth_conversion) response = self.client.post(reverse("v3:safe-creation"), data, format="json") self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json["safe"] self.assertTrue(check_checksum(safe_address)) self.assertTrue(check_checksum(response_json["paymentReceiver"])) self.assertEqual(response_json["paymentToken"], payment_token) self.assertEqual( int(response_json["payment"]), int(response_json["gasEstimated"]) * int(response_json["gasPriceEstimated"]) * (1 / fixed_eth_conversion), ) self.assertGreater(int(response_json["gasEstimated"]), 0) self.assertGreater(int(response_json["gasPriceEstimated"]), 0) self.assertGreater(len(response_json["setupData"]), 2) self.assertTrue(SafeContract.objects.filter(address=safe_address)) self.assertTrue( SafeCreation2.objects.filter(owners__contains=[owners[0]])) safe_creation = SafeCreation2.objects.get(safe=safe_address) self.assertEqual(safe_creation.payment_token, payment_token) # Payment includes deployment gas + gas to send eth to the deployer self.assertEqual( safe_creation.payment, safe_creation.wei_estimated_deploy_cost() * (1 / fixed_eth_conversion), )
def test_safe_creation(self): salt_nonce = generate_salt_nonce() owners = [Account.create().address for _ in range(2)] data = { 'saltNonce': salt_nonce, 'owners': owners, 'threshold': len(owners) } response = self.client.post(reverse('v3:safe-creation'), data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json['safe'] self.assertTrue(check_checksum(safe_address)) self.assertTrue(check_checksum(response_json['paymentReceiver'])) self.assertEqual(response_json['paymentToken'], NULL_ADDRESS) self.assertEqual(int(response_json['payment']), int(response_json['gasEstimated']) * int(response_json['gasPriceEstimated'])) self.assertGreater(int(response_json['gasEstimated']), 0) self.assertGreater(int(response_json['gasPriceEstimated']), 0) self.assertGreater(len(response_json['setupData']), 2) self.assertEqual(response_json['masterCopy'], settings.SAFE_CONTRACT_ADDRESS) self.assertTrue(SafeContract.objects.filter(address=safe_address)) self.assertTrue(SafeCreation2.objects.filter(owners__contains=[owners[0]])) safe_creation = SafeCreation2.objects.get(safe=safe_address) self.assertEqual(safe_creation.payment_token, None) # Payment includes deployment gas + gas to send eth to the deployer self.assertEqual(safe_creation.payment, safe_creation.wei_estimated_deploy_cost()) # Deploy the Safe to check it self.send_ether(safe_address, int(response_json['payment'])) safe_creation2 = SafeCreationServiceProvider().deploy_create2_safe_tx(safe_address) self.ethereum_client.get_transaction_receipt(safe_creation2.tx_hash, timeout=20) safe = Safe(safe_address, self.ethereum_client) self.assertEqual(safe.retrieve_master_copy_address(), response_json['masterCopy']) self.assertEqual(safe.retrieve_owners(), owners) # Test exception when same Safe is created response = self.client.post(reverse('v3:safe-creation'), data, format='json') self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) self.assertIn('SafeAlreadyExistsException', response.json()['exception']) data = { 'salt_nonce': -1, 'owners': owners, 'threshold': 2 } response = self.client.post(reverse('v3:safe-creation'), data, format='json') self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY)
def test_safe_creation(self): salt_nonce = generate_salt_nonce() owners = [Account.create().address for _ in range(2)] data = { 'saltNonce': salt_nonce, 'owners': owners, 'threshold': len(owners) } response = self.client.post(reverse('v2:safe-creation'), data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json['safe'] self.assertTrue(check_checksum(safe_address)) self.assertTrue(check_checksum(response_json['paymentReceiver'])) self.assertEqual(response_json['paymentToken'], NULL_ADDRESS) self.assertEqual(int(response_json['payment']), int(response_json['gasEstimated']) * int(response_json['gasPriceEstimated'])) self.assertGreater(int(response_json['gasEstimated']), 0) self.assertGreater(int(response_json['gasPriceEstimated']), 0) self.assertGreater(len(response_json['setupData']), 2) self.assertTrue(SafeContract.objects.filter(address=safe_address)) self.assertTrue(SafeCreation2.objects.filter(owners__contains=[owners[0]])) safe_creation = SafeCreation2.objects.get(safe=safe_address) self.assertEqual(safe_creation.payment_token, None) # Payment includes deployment gas + gas to send eth to the deployer self.assertEqual(safe_creation.payment, safe_creation.wei_estimated_deploy_cost()) # Test exception when same Safe is created response = self.client.post(reverse('v2:safe-creation'), data, format='json') self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) self.assertIn('SafeAlreadyExistsException', response.json()['exception']) data = { 'salt_nonce': -1, 'owners': owners, 'threshold': 2 } response = self.client.post(reverse('v2:safe-creation'), data, format='json') self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY)