Пример #1
0
def test_swap_into_multiple_same_synth(swap, alice, DAI, sBTC):
    amount = (1_000_000 * 10**18) // 4
    tx = swap.swap_into_synth(DAI, sBTC, amount, 0, {'from': alice})
    token_id1 = tx.events['Transfer'][-1]['token_id']

    tx = swap.swap_into_synth(DAI, sBTC, amount, 0, {'from': alice})
    token_id2 = tx.events['Transfer'][-1]['token_id']

    assert token_id1 != token_id2
    assert swap.balanceOf(alice) == 2
    assert swap.ownerOf(token_id1) == alice
    assert swap.ownerOf(token_id2) == alice
    assert Settler.at(hex(token_id1)).synth() == sBTC
    assert Settler.at(hex(token_id2)).synth() == sBTC
    def rule_swap_into_existing(self, st_acct, st_token, st_amount, st_idx):
        """
        Increase the underyling balance of an existing NFT via a cross-asset swap.
        """
        if self.active_token_ids.get(st_acct):
            idx = int(st_idx * len(self.active_token_ids[st_acct]))
            token_id = self.active_token_ids[st_acct][idx]
        else:
            token_ids = self._all_token_ids()
            idx = int(st_idx * len(token_ids))
            token_id = token_ids[idx]

        synth = Settler.at(hex(token_id % 2**160)).synth()
        idx = int(st_idx * len(TOKENS[st_token]))
        initial = TOKENS[st_token][idx]
        amount = self._mint(st_acct, initial, st_amount)

        if self.active_token_ids.get(st_acct) and TOKENS[st_token][0] != synth:
            self.swap.swap_into_synth(initial, synth, amount, 0, st_acct,
                                      token_id, {"from": st_acct})
            chain.mine(timedelta=600)
        else:
            with brownie.reverts():
                self.swap.swap_into_synth(initial, synth, amount, 0, st_acct,
                                          token_id, {"from": st_acct})
Пример #3
0
def test_swap_into_multiple_different_synths(swap, alice, DAI, USDT, sETH,
                                             sBTC):
    amount = 1_000_000 * 10**18
    tx = swap.swap_into_synth(DAI, sBTC, amount, 0, {"from": alice})
    token_id1 = tx.events["Transfer"][-1]["token_id"]

    amount = 1_000_000 * 10**6
    tx = swap.swap_into_synth(USDT, sETH, amount, 0, {"from": alice})
    token_id2 = tx.events["Transfer"][-1]["token_id"]

    assert token_id1 != token_id2
    assert swap.balanceOf(alice) == 2
    assert swap.ownerOf(token_id1) == alice
    assert swap.ownerOf(token_id2) == alice
    assert Settler.at(hex(token_id1)).synth() == sBTC
    assert Settler.at(hex(token_id2)).synth() == sETH
Пример #4
0
def test_swap_into_deploys_settler(swap, alice, DAI, sUSD, sBTC):
    amount = 1_000_000 * 10**18
    tx = swap.swap_into_synth(DAI, sBTC, amount, 0, {'from': alice})
    token_id = tx.events['Transfer'][-1]['token_id']

    # this will fail if no bytecode exists at `token_id`
    settler = Settler.at(hex(token_id))
    assert settler.synth() == sBTC
Пример #5
0
def test_swap_into_eth(swap, alice, sETH, sBTC):
    amount = 50 * 10**18
    expected = swap.get_swap_into_synth_amount(ETH_ADDRESS, sBTC, amount)
    tx = swap.swap_into_synth(ETH_ADDRESS, sBTC, amount, 0, {
        'from': alice,
        'value': amount
    })
    token_id = tx.events['Transfer'][-1]['token_id']

    settler = Settler.at(hex(token_id))

    assert sETH.balanceOf(alice) == 0
    assert sETH.balanceOf(swap) == 0
    assert sETH.balanceOf(settler) == 0

    assert sBTC.balanceOf(alice) == 0
    assert sBTC.balanceOf(swap) == 0
    assert sBTC.balanceOf(settler) == expected
Пример #6
0
def test_swap_into_dai(swap, alice, DAI, sUSD, sBTC):
    amount = 1_000_000 * 10**18
    expected = swap.get_swap_into_synth_amount(DAI, sBTC, amount)
    tx = swap.swap_into_synth(DAI, sBTC, amount, 0, {'from': alice})
    token_id = tx.events['Transfer'][-1]['token_id']

    settler = Settler.at(hex(token_id))
    assert DAI.balanceOf(alice) == 0
    assert DAI.balanceOf(swap) == 0
    assert DAI.balanceOf(settler) == 0

    assert sUSD.balanceOf(alice) == 0
    assert sUSD.balanceOf(swap) == 0
    assert sUSD.balanceOf(settler) == 0

    assert sBTC.balanceOf(alice) == 0
    assert sBTC.balanceOf(swap) == 0
    assert sBTC.balanceOf(settler) == expected
Пример #7
0
def test_swap_into_usdt(swap, alice, USDT, sUSD, sBTC):
    # test USDT to make sure we handle tokens that return None
    amount = 1_000_000 * 10**6
    expected = swap.get_swap_into_synth_amount(USDT, sBTC, amount)
    tx = swap.swap_into_synth(USDT, sBTC, amount, 0, {'from': alice})
    token_id = tx.events['Transfer'][-1]['token_id']

    settler = Settler.at(hex(token_id))
    assert USDT.balanceOf(alice) == 0
    assert USDT.balanceOf(swap) == 0
    assert USDT.balanceOf(settler) == 0

    assert sUSD.balanceOf(alice) == 0
    assert sUSD.balanceOf(swap) == 0
    assert sUSD.balanceOf(settler) == 0

    assert sBTC.balanceOf(alice) == 0
    assert sBTC.balanceOf(swap) == 0
    assert sBTC.balanceOf(settler) == expected
    def rule_swap_from(self, st_acct, st_token, st_amount, st_idx):
        """
        Swap a synth out of an NFT.
        """
        if self.active_token_ids.get(st_acct):
            # choose from the caller's valid NFT token IDs, if there are any
            idx = int(st_idx * len(self.active_token_ids[st_acct]))
            token_id = self.active_token_ids[st_acct][idx]
        else:
            # if the caller does not own any NFTs, choose from any token ID
            token_ids = self._all_token_ids()
            idx = int(st_idx * len(token_ids))
            token_id = token_ids[idx]

        # choose a target coin for the swap
        synth = Settler.at(hex(token_id % 2**160)).synth()
        if synth == ZERO_ADDRESS:
            # if the token ID is not active, choose from any possible token - all should fail
            token_list = [x for v in TOKENS for x in v]
        else:
            # if the token ID is active, choose from the list of possible targets
            token_list = next(i for i in TOKENS if i[0] == synth)
        idx = int(st_idx * len(token_list))
        target = token_list[idx]

        amount = int(st_amount * 10**18)
        if self.active_token_ids.get(st_acct):
            # when the action is possible, don't exceed the max underlying balance
            balance = self.swap.token_info(token_id)["underlying_balance"]
            amount = min(amount, balance)

        if self.active_token_ids.get(st_acct) and synth != target:
            # sender own the NFT, target is not the same as the underlying synth
            self.swap.swap_from_synth(token_id, target, amount, 0,
                                      {"from": st_acct})
            if balance == amount:
                self.active_token_ids[st_acct].remove(token_id)
                self.used_token_ids.append(token_id)
        else:
            with brownie.reverts():
                self.swap.swap_from_synth(token_id, target, amount, 0,
                                          {"from": st_acct})