Exemplo n.º 1
0
    def test_smart_contract_balance(self):
        txids, smart_contract_account = SmartContract.generate(self.investment)

        for txid in txids:
            self.assertTrue(utils.wait_for_confirmation(txid))
        
        sm = SmartContract.objects.first()
        self.assertTrue(sm.check_if_sufficient_balance())

        txid = Transaction.transfer(
            smart_contract_account, 
            self.beneficiary_account, 
            0.00004, 
            Transaction.USDC, 
            Transaction.REWARD)

        self.assertTrue(utils.wait_for_confirmation(txid.txid))

        self.assertTrue(sm.check_if_sufficient_balance())

        txid = Transaction.transfer(
            smart_contract_account, 
            self.beneficiary_account, 
            0.00005, 
            Transaction.USDC, 
            Transaction.REWARD)

        self.assertTrue(utils.wait_for_confirmation(txid.txid))

        self.assertFalse(sm.check_if_sufficient_balance())
Exemplo n.º 2
0
    def test_smart_contract(self):
        txids, smart_contract_account = SmartContract.generate(self.investment)

        for txid in txids:
            self.assertTrue(utils.wait_for_confirmation(txid))

        txid = Transaction.transfer(
            smart_contract_account, 
            self.beneficiary_account, 
            0.00001, 
            Transaction.USDC, 
            Transaction.REWARD)

        self.assertTrue(utils.wait_for_confirmation(txid.txid))

        txid = Transaction.transfer(
            smart_contract_account, 
            self.facililator_account, 
            0.00001, 
            Transaction.USDC, 
            Transaction.TRANSFER)

        self.assertTrue(utils.wait_for_confirmation(txid.txid))

        txid = Transaction.transfer(
            smart_contract_account, 
            self.investor_account, 
            0.00001, 
            Transaction.USDC, 
            Transaction.CLOSE,
            self.investor_account)

        self.assertTrue(utils.wait_for_confirmation(txid.txid))
Exemplo n.º 3
0
    def test_retry_atomic(self):
        main_account = Account.get_main_account()
        txn0 = Transaction.objects.create(
            from_account=main_account,
            to_account=self.user_account,
            amount=0.21,
            currency=Transaction.ALGO,
            action=Transaction.OPT_IN,
            status=Transaction.REJECTED,
            atomic=True,
        )
        txn1 = Transaction.objects.create(from_account=self.user_account,
                                          to_account=self.user_account,
                                          amount=0,
                                          currency=Transaction.USDC,
                                          action=Transaction.OPT_IN,
                                          atomic=True,
                                          atomic_prev=txn0)
        txn2 = Transaction.objects.create(from_account=main_account,
                                          to_account=self.user_account,
                                          amount=0.1,
                                          currency=Transaction.USDC,
                                          action=Transaction.FUELING,
                                          atomic=True,
                                          atomic_prev=txn1)

        txids = txn0.retry()
        for txid in txids:
            self.assertTrue(utils.wait_for_confirmation(txid))

        self.assertEqual(Transaction.objects.count(), 6)
Exemplo n.º 4
0
 def test_confirmed(self):
     main_account = Account.get_main_account()
     tx = Transaction.transfer(main_account, self.user_account, 0.1,
                               Transaction.ALGO, Transaction.OPT_IN)
     self.assertTrue(utils.wait_for_confirmation(tx.txid))
     verify_transactions()
     tx.refresh_from_db()
     self.assertEqual(tx.status, Transaction.CONFIRMED)
Exemplo n.º 5
0
    def test_opt_in_chain(self):
        main_account = Account.get_main_account()
        txids = Transaction.opt_in(self.user_account, main_account, [
            Transaction.prepare_transfer(main_account, self.user_account,
                                         0.0001, Transaction.USDC,
                                         Transaction.FUELING),
        ])

        for txid in txids:
            self.assertTrue(utils.wait_for_confirmation(txid))
Exemplo n.º 6
0
    def test_finish(self):
        self.investment = Investment.objects.create(
            investor=self.investor,
            project=self.project,
            start="2020-03-01",
            end="2020-03-01",
            amount="0.0001")
        self.investment.refresh_from_db()
        txids, _ = SmartContract.generate(self.investment)
        for txid in txids:
            self.assertTrue(utils.wait_for_confirmation(txid))

        self.investment.refresh_from_db()
        txids = self.investment.finish()

        for txid in txids:
            self.assertTrue(utils.wait_for_confirmation(txid))

        self.investment.refresh_from_db()
        self.assertEqual(self.investment.status, Investment.FINISHED)
Exemplo n.º 7
0
 def test_rejected_pool_error(self):
     main_account = Account.get_main_account()
     with patch('algorand.utils.transaction_info') as mock_transaction_info:
         mock_transaction_info.return_value = {'pool-error': 'error'}
         tx = Transaction.transfer(main_account, self.user_account, 0.1,
                                   Transaction.ALGO, Transaction.OPT_IN)
         self.assertTrue(utils.wait_for_confirmation(tx.txid))
         verify_transactions()
         tx.refresh_from_db()
         self.assertEqual(tx.status, Transaction.REJECTED)
         self.assertEqual(Transaction.objects.count(), 2)
Exemplo n.º 8
0
    def test_retry_single(self):
        main_account = Account.get_main_account()
        txn = Transaction.objects.create(from_account=main_account,
                                         to_account=self.user_account,
                                         amount=0.1,
                                         currency=Transaction.ALGO,
                                         action=Transaction.FUELING,
                                         retries=3)

        with self.assertRaises(Exception):
            txn.retry()

        txn.retries = 0
        txn.save()

        with self.assertRaises(Exception):
            txn.retry()

        txn.status = Transaction.REJECTED
        txid = txn.retry()
        self.assertTrue(utils.wait_for_confirmation(txid))
Exemplo n.º 9
0
    def test_retrieve(self):
        self.investment = Investment.objects.create(investor=self.investor,
                                                    project=self.project,
                                                    start="2020-03-01",
                                                    end="2020-03-01",
                                                    amount="0.1")

        self.investment.refresh_from_db()

        txids, _ = SmartContract.generate(self.investment)
        for txid in txids:
            self.assertTrue(utils.wait_for_confirmation(txid))

        self._retrieve(self.beneficiary,
                       f"/api/v1/projects/{self.project.id}/accounts/",
                       status.HTTP_200_OK)
        self._retrieve(self.facililator,
                       f"/api/v1/projects/{self.project.id}/accounts/",
                       status.HTTP_200_OK)
        self._retrieve(self.investor,
                       f"/api/v1/projects/{self.project.id}/accounts/",
                       status.HTTP_200_OK)
Exemplo n.º 10
0
    def test_rejected_atomic(self):
        main_account = Account.get_main_account()
        with patch('algorand.utils.transaction_info') as mock_transaction_info:
            mock_transaction_info.return_value = {
                'pool-error': '',
                'txn': {
                    'txn': {
                        'lv': utils.status()['last-round'] - 1000
                    }
                }
            }
            tx0 = Transaction.prepare_transfer(main_account, self.user_account,
                                               0.21, Transaction.ALGO,
                                               Transaction.OPT_IN)
            tx1 = Transaction.prepare_transfer(self.user_account,
                                               self.user_account, 0,
                                               Transaction.USDC,
                                               Transaction.OPT_IN)
            tx2 = Transaction.prepare_transfer(main_account, self.user_account,
                                               0.1, Transaction.USDC,
                                               Transaction.OPT_IN)
            txids = Transaction.atomic_transfer([tx0, tx1, tx2])
            for txid in txids:
                self.assertTrue(utils.wait_for_confirmation(txid))

            verify_transactions()

            self.assertEqual(
                Transaction.objects.filter(
                    status=Transaction.REJECTED).count(), 3)
            self.assertEqual(Transaction.objects.count(), 6)

            one_without_prev = False
            for txn in Transaction.objects.filter(retries=1):
                if not one_without_prev and txn.atomic_prev is None:
                    one_without_prev = True
                    continue

                self.assertFalse(txn.atomic_prev is None)
Exemplo n.º 11
0
    def test_create(self):
        self.investment = Investment.objects.create(investor=self.investor,
                                                    project=self.project,
                                                    start="2020-03-01",
                                                    end="2020-03-01",
                                                    amount="0.1")
        self.investment.refresh_from_db()

        txids, _ = SmartContract.generate(self.investment)
        for txid in txids:
            self.assertTrue(utils.wait_for_confirmation(txid))

        small_jpg = (
            b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x00\x00\x00\x21\xf9\x04'
            b'\x01\x0a\x00\x01\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02'
            b'\x02\x4c\x01\x00\x3b')
        file = SimpleUploadedFile('small.jpg',
                                  small_jpg,
                                  content_type='image/jpeg')
        self._create(
            self.facililator,
            f"/api/v1/projects/{self.project.id}/tasks/{self.task.id}/activities/",
            {
                "photo": file,
            }, status.HTTP_403_FORBIDDEN, 'multipart')

        file = SimpleUploadedFile('small.jpg',
                                  small_jpg,
                                  content_type='image/jpeg')
        self._create(
            self.investor,
            f"/api/v1/projects/{self.project.id}/tasks/{self.task.id}/activities/",
            {
                "photo": file,
            }, status.HTTP_403_FORBIDDEN, 'multipart')

        file = SimpleUploadedFile('small.jpg',
                                  small_jpg,
                                  content_type='image/jpeg')
        self._create(
            self.beneficiary,
            f"/api/v1/projects/{self.project.id}/tasks/{self.task.id}/activities/",
            {
                "photo": file,
            }, status.HTTP_403_FORBIDDEN, 'multipart')

        Transaction.objects.all().update(status=Transaction.CONFIRMED)

        file = SimpleUploadedFile('small.jpg',
                                  small_jpg,
                                  content_type='image/jpeg')
        self._create(
            self.beneficiary,
            f"/api/v1/projects/{self.project.id}/tasks/{self.task.id}/activities/",
            {
                "photo": file,
            }, status.HTTP_201_CREATED, 'multipart')

        transaction = Transaction.objects.get(
            to_account=self.beneficiary_account, action=Transaction.REWARD)
        self.assertTrue(utils.wait_for_confirmation(transaction.txid))

        file = SimpleUploadedFile('small.jpg',
                                  small_jpg,
                                  content_type='image/jpeg')
        self._create(
            self.beneficiary,
            f"/api/v1/projects/{self.project.id}/tasks/{self.task.id}/activities/",
            {
                "photo": file,
            }, status.HTTP_400_BAD_REQUEST, 'multipart')

        self.investment.refresh_from_db()
        self.assertEqual(self.investment.status, Investment.FINISHED)
Exemplo n.º 12
0
 def test_transfer(self):
     main_account = Account.get_main_account()
     txid = Transaction.transfer(main_account, self.user_account, 0.1,
                                 Transaction.ALGO, Transaction.OPT_IN)
     self.assertTrue(utils.wait_for_confirmation(txid.txid))
Exemplo n.º 13
0
 def test_opt_in(self):
     main_account = Account.get_main_account()
     txids = Transaction.opt_in(self.user_account, main_account)
     for txid in txids:
         self.assertTrue(utils.wait_for_confirmation(txid))