Пример #1
0
    def buy_at_fixed_rate(
        self,
        amount: float,
        wallet: Wallet,
        max_OCEAN_amount: float,
        exchange_id: str = "",
        data_token: str = "",
        exchange_owner: str = "",
    ) -> bool:

        exchange, exchange_id = self.get_exchange_id_fallback_dt_and_owner(
            exchange_id, exchange_owner, data_token)

        amount_base = to_base_18(amount)
        max_OCEAN_amount_base = to_base_18(max_OCEAN_amount)

        # Figure out the amount of ocean tokens to approve before triggering the exchange function to do the swap
        ocean_amount_base = exchange.get_base_token_quote(
            exchange_id, amount_base)
        if ocean_amount_base > max_OCEAN_amount_base:
            raise ValidationError(
                f"Buying {amount} datatokens requires {from_base_18(ocean_amount_base)} OCEAN "
                f"tokens which exceeds the max_OCEAN_amount {max_OCEAN_amount}."
            )
        ocean_token = DataToken(self.ocean_address)
        ocean_token.get_tx_receipt(
            ocean_token.approve(self._exchange_address, ocean_amount_base,
                                wallet))
        tx_id = exchange.buy_data_token(exchange_id,
                                        data_token_amount=amount_base,
                                        from_wallet=wallet)
        return bool(exchange.get_tx_receipt(tx_id).status)
Пример #2
0
def test_ERC20(alice_ocean, alice_wallet, alice_address, bob_wallet, bob_address):
    """Tests DataToken minting, allowance and transfer."""
    token = alice_ocean.create_data_token(
        "DataToken1", "DT1", from_wallet=alice_wallet, blob="foo_blob"
    )

    assert token.symbol()[:2] == "DT"
    assert token.decimals() == 18
    assert token.balanceOf(alice_address) == 0

    token.mint(alice_address, to_base_18(100.0), from_wallet=alice_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 100.0

    assert token.allowance(alice_address, bob_address) == 0
    token.approve(bob_address, to_base_18(1.0), from_wallet=alice_wallet)
    assert token.allowance(alice_address, bob_address) == int(1e18)

    token.transfer(bob_address, to_base_18(5.0), from_wallet=alice_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 95.0
    assert from_base_18(token.balanceOf(bob_address)) == 5.0

    token.transfer(alice_address, to_base_18(3.0), from_wallet=bob_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 98.0
    assert from_base_18(token.balanceOf(bob_address)) == 2.0

    # assert transfers were successful
    block = alice_ocean.web3.eth.blockNumber
    all_transfers = token.get_all_transfers_from_events(block - 1, block + 1, chunk=1)
    assert len(all_transfers[0]) == 2
Пример #3
0
def test_joinSwapExternAmountIn(network, T1, T2, alice_wallet, alice_address):
    init_T1balance = from_base_18(T1.balanceOf(alice_address))
    T2balance = from_base_18(T2.balanceOf(alice_address))
    pool = _createPoolWith2Tokens(network, T1, T2, alice_wallet, 90.0, 10.0, 9.0, 1.0)
    T1.approve(pool.address, to_base_18(100.0), from_wallet=alice_wallet)

    # pool's not public
    with pytest.raises(Exception):
        pool.swapExactAmountOut(
            tokenIn_address=T1.address,
            maxAmountIn_base=to_base_18(100.0),
            tokenOut_address=T2.address,
            tokenAmountOut_base=to_base_18(10.0),
            maxPrice_base=HUGEINT,
            from_wallet=alice_wallet,
        )

    # pool's public
    pool.setPublicSwap(True, from_wallet=alice_wallet)
    pool.swapExactAmountOut(
        tokenIn_address=T1.address,
        maxAmountIn_base=to_base_18(100.0),
        tokenOut_address=T2.address,
        tokenAmountOut_base=to_base_18(1.0),
        maxPrice_base=HUGEINT,
        from_wallet=alice_wallet,
    )
    new_balance = init_T1balance - 91.055
    assert (
        (new_balance - 0.005)
        <= from_base_18(T1.balanceOf(alice_address))
        <= (new_balance + 0.005)
    )
    assert from_base_18(T2.balanceOf(alice_address)) == (T2balance - 9.0)
Пример #4
0
def test_ERC20(network, alice_wallet, alice_address, bob_wallet, bob_address,
               OCEAN_address):
    """Tests an OCEAN token approval, allowance and transfers."""
    token = BToken(OCEAN_address)

    token.approve(bob_address, 0, from_wallet=alice_wallet)
    # generating ERC20 Tokens, so the symbol is irrelevant
    assert token.symbol() == "DTT"
    assert token.decimals() == 18
    assert token.balanceOf(alice_address) > util.to_base_18(10.0)
    assert token.balanceOf(bob_address) > util.to_base_18(10.0)

    assert token.allowance(alice_address, bob_address) == 0
    token.approve(bob_address, int(1e18), from_wallet=alice_wallet)
    assert token.allowance(alice_address, bob_address) == int(1e18)

    # alice sends all her OCEAN to Bob, then Bob sends it back
    alice_OCEAN = token.balanceOf(alice_address)
    bob_OCEAN = token.balanceOf(bob_address)
    token.transfer(bob_address, alice_OCEAN, from_wallet=alice_wallet)
    assert token.balanceOf(alice_address) == 0
    assert token.balanceOf(bob_address) == (alice_OCEAN + bob_OCEAN)

    token.transfer(alice_address, alice_OCEAN, from_wallet=bob_wallet)
    assert token.balanceOf(alice_address) == alice_OCEAN
    assert token.balanceOf(bob_address) == bob_OCEAN
Пример #5
0
def _deployAndMintToken(symbol: str, to_address: str) -> btoken.BToken:
    wallet = get_factory_deployer_wallet(_NETWORK)
    dt_address = DataToken.deploy(
        wallet.web3,
        wallet,
        None,
        "Template Contract",
        "TEMPLATE",
        wallet.address,
        to_base_18(1000),
        DTFactory.FIRST_BLOB,
        to_address,
    )
    dt_factory = DTFactory(
        DTFactory.deploy(wallet.web3, wallet, None, dt_address, to_address)
    )
    token_address = dt_factory.get_token_address(
        dt_factory.createToken(
            symbol, symbol, symbol, DataToken.DEFAULT_CAP_BASE, wallet
        )
    )
    token = DataToken(token_address)
    token.mint(to_address, to_base_18(1000), wallet)

    return btoken.BToken(token.address)
Пример #6
0
    def buy_data_tokens(self, pool_address: str, amount: float,
                        max_OCEAN_amount: float, from_wallet: Wallet) -> str:
        """
        Buy data tokens from this pool, paying `max_OCEAN_amount_base` of OCEAN tokens.
        If total spent <= max_OCEAN_amount_base.
        - Caller is spending OCEAN tokens, and receiving `amount_base` DataTokens
        - OCEAN tokens are going into pool, DataTokens are going out of pool

        The transaction fails if total spent exceeds `max_OCEAN_amount_base`.

        :param pool_address: str address of pool contract
        :param amount: int number of data tokens to add to this pool in *base*
        :param max_OCEAN_amount:
        :param from_wallet:
        :return: str transaction id/hash
        """
        ocean_tok = DataToken(self.ocean_address)
        ocean_tok.approve_tokens(pool_address,
                                 max_OCEAN_amount,
                                 from_wallet,
                                 wait=True)

        dtoken_address = self.get_token_address(pool_address)
        pool = BPool(pool_address)
        return pool.swapExactAmountOut(
            tokenIn_address=self.ocean_address,  # entering pool
            maxAmountIn_base=to_base_18(max_OCEAN_amount),  # ""
            tokenOut_address=dtoken_address,  # leaving pool
            tokenAmountOut_base=to_base_18(amount),  # ""
            maxPrice_base=2**255,  # here we limit by max_num_OCEAN, not price
            from_wallet=from_wallet,
        )
Пример #7
0
    def buy_at_fixed_rate(self,
                          amount: float,
                          wallet: Wallet,
                          max_OCEAN_amount: float,
                          exchange_id: str = '',
                          data_token: str = '',
                          exchange_owner: str = '') -> bool:

        exchange = self._exchange_contract()
        if not exchange_id:
            assert exchange_owner and data_token, f'exchange_owner and data_token are required when exchange_id is not given.'
            exchange_id = exchange.generateExchangeId(self.ocean_address,
                                                      data_token,
                                                      exchange_owner)

        amount_base = to_base_18(amount)
        max_OCEAN_amount_base = to_base_18(max_OCEAN_amount)

        # Figure out the amount of ocean tokens to approve before triggering the exchange function to do the swap
        ocean_amount_base = exchange.get_base_token_quote(
            exchange_id, amount_base)
        if ocean_amount_base > max_OCEAN_amount_base:
            raise AssertionError(
                f'Buying {amount} datatokens requires {from_base_18(ocean_amount_base)} OCEAN '
                f'tokens which exceeds the max_OCEAN_amount {max_OCEAN_amount}.'
            )
        ocean_token = DataToken(self.ocean_address)
        ocean_token.get_tx_receipt(
            ocean_token.approve(self._exchange_address, ocean_amount_base,
                                wallet))
        tx_id = exchange.buy_data_token(exchange_id,
                                        data_token_amount=amount_base,
                                        from_wallet=wallet)
        return bool(exchange.get_tx_receipt(tx_id).status)
Пример #8
0
def test_gulp(network, T1, alice_wallet):
    """Test pool gulp."""
    pool = _deployBPool(network, alice_wallet)

    # bind T1 to the pool, with a balance of 2.0
    T1.approve(pool.address, to_base_18(50.0), from_wallet=alice_wallet)
    pool.bind(T1.address,
              to_base_18(2.0),
              to_base_18(50.0),
              from_wallet=alice_wallet)

    # T1 is now pool's (a) ERC20 balance (b) _records[token].balance
    assert T1.balanceOf(pool.address) == to_base_18(2.0)  # ERC20 balance
    assert pool.getBalance(T1.address) == to_base_18(2.0)  # records[]

    # but then some joker accidentally sends 5.0 tokens to the pool's address
    #  rather than binding / rebinding. So it's in ERC20 bal but not records[]
    T1.transfer(pool.address, to_base_18(5.0), from_wallet=alice_wallet)
    assert T1.balanceOf(pool.address) == to_base_18(2.0 + 5.0)  # ERC20 bal
    assert pool.getBalance(T1.address) == to_base_18(2.0)  # records[]

    # so, 'gulp' gets the pool to absorb the tokens into its balances.
    # i.e. to update _records[token].balance to be in sync with ERC20 balance
    pool.gulp(T1.address, from_wallet=alice_wallet)
    assert T1.balanceOf(pool.address) == to_base_18(2.0 + 5.0)  # ERC20
    assert pool.getBalance(T1.address) == to_base_18(2.0 + 5.0)  # records[]
Пример #9
0
def test_ERC20(alice_ocean, alice_wallet, alice_address, bob_wallet,
               bob_address):
    """Tests DataToken minting, allowance and transfer."""
    token = alice_ocean.create_data_token("DataToken1",
                                          "DT1",
                                          from_wallet=alice_wallet,
                                          blob="foo_blob")

    assert token.symbol()[:2] == "DT"
    assert token.decimals() == 18
    assert token.balanceOf(alice_address) == 0

    token.mint(alice_address, to_base_18(100.0), from_wallet=alice_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 100.0

    assert token.allowance(alice_address, bob_address) == 0
    token.approve(bob_address, to_base_18(1.0), from_wallet=alice_wallet)
    assert token.allowance(alice_address, bob_address) == int(1e18)

    token.transfer(bob_address, to_base_18(5.0), from_wallet=alice_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 95.0
    assert from_base_18(token.balanceOf(bob_address)) == 5.0

    token.transfer(alice_address, to_base_18(3.0), from_wallet=bob_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 98.0
    assert from_base_18(token.balanceOf(bob_address)) == 2.0
Пример #10
0
def test_transfer_event(alice_ocean, alice_wallet, alice_address, bob_wallet,
                        bob_address):
    """Tests that a transfer event is registered."""
    token = alice_ocean.create_data_token("DataToken1",
                                          "DT1",
                                          from_wallet=alice_wallet,
                                          blob="foo_blob")

    block = alice_ocean.web3.eth.blockNumber
    transfer_event = token.get_transfer_event(block, alice_address,
                                              bob_address)
    # different way of retrieving
    transfer_events = token.get_event_logs("Transfer", None, block, block)
    assert transfer_events == []

    token.mint(alice_address, to_base_18(100.0), from_wallet=alice_wallet)
    token.approve(bob_address, to_base_18(1.0), from_wallet=alice_wallet)
    token.transfer(bob_address, to_base_18(5.0), from_wallet=alice_wallet)

    block = alice_ocean.web3.eth.blockNumber
    transfer_event = token.get_transfer_event(block, alice_address,
                                              bob_address)
    assert transfer_event["args"]["from"] == alice_address
    assert transfer_event["args"]["to"] == bob_address

    # same transfer event, different way of retrieving
    transfer_event = token.get_event_logs("Transfer", None, block, block)[0]
    assert transfer_event["args"]["from"] == alice_address
    assert transfer_event["args"]["to"] == bob_address
Пример #11
0
def test_ERC20(alice_ocean, alice_wallet, alice_address, bob_wallet,
               bob_address):
    token = alice_ocean.create_data_token('DataToken1',
                                          'DT1',
                                          from_wallet=alice_wallet,
                                          blob='foo_blob')

    assert token.symbol()[:2] == 'DT'
    assert token.decimals() == 18
    assert token.balanceOf(alice_address) == 0

    token.mint(alice_address, to_base_18(100.0), from_wallet=alice_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 100.0

    assert token.allowance(alice_address, bob_address) == 0
    token.approve(bob_address, to_base_18(1.0), from_wallet=alice_wallet)
    assert token.allowance(alice_address, bob_address) == int(1e18)

    token.transfer(bob_address, to_base_18(5.0), from_wallet=alice_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 95.0
    assert from_base_18(token.balanceOf(bob_address)) == 5.0

    token.transfer(alice_address, to_base_18(3.0), from_wallet=bob_wallet)
    assert from_base_18(token.balanceOf(alice_address)) == 98.0
    assert from_base_18(token.balanceOf(bob_address)) == 2.0
Пример #12
0
def test_calcSpotPrice_base(network, T1, T2, alice_address, alice_wallet):
    pool = _deployBPool(network, alice_wallet)
    x = pool.calcSpotPrice(tokenBalanceIn_base=to_base_18(10.0),
                           tokenWeightIn_base=to_base_18(1.0),
                           tokenBalanceOut_base=to_base_18(11.0),
                           tokenWeightOut_base=to_base_18(1.0),
                           swapFee_base=0)
    assert round(from_base_18(x), 3) == 0.909
Пример #13
0
def test_calcInGivenOut_base(network, alice_wallet):
    pool = _deployBPool(network, alice_wallet)
    x = pool.calcInGivenOut(tokenBalanceIn_base=to_base_18(10.0),
                            tokenWeightIn_base=to_base_18(1.0),
                            tokenBalanceOut_base=to_base_18(10.1),
                            tokenWeightOut_base=to_base_18(1.0),
                            tokenAmountOut_base=to_base_18(1.0),
                            swapFee_base=0)
    assert round(from_base_18(x), 3) == 1.099
Пример #14
0
def test_calcSingleOutGivenPoolIn_base(network, alice_wallet):
    pool = _deployBPool(network, alice_wallet)
    x = pool.calcSingleOutGivenPoolIn(tokenBalanceOut_base=to_base_18(10.0),
                                      tokenWeightOut_base=to_base_18(1.0),
                                      poolSupply_base=to_base_18(120.0),
                                      totalWeight_base=to_base_18(2.0),
                                      poolAmountIn_base=to_base_18(10.0),
                                      swapFee_base=0)
    assert round(from_base_18(x), 3) == 1.597
Пример #15
0
def test_calcPoolInGivenSingleOut_base(network, alice_wallet):
    pool = _deployBPool(network, alice_wallet)
    x = pool.calcPoolInGivenSingleOut(tokenBalanceOut_base=to_base_18(1000.0),
                                      tokenWeightOut_base=to_base_18(5.0),
                                      poolSupply_base=to_base_18(100.0),
                                      totalWeight_base=to_base_18(10.0),
                                      tokenAmountOut_base=to_base_18(0.1),
                                      swapFee_base=0)
    assert round(from_base_18(x), 3) == 0.005
Пример #16
0
def test_get_token_minter(alice_wallet, dtfactory_address, alice_address):
    """Tests proper retrieval of token minter from DTFactory."""
    dtfactory = DTFactory(dtfactory_address)

    dt_address = dtfactory.createToken(
        "foo_blob", "DT1", "DT1", to_base_18(1000.0), from_wallet=alice_wallet
    )
    dt = DataToken(dtfactory.get_token_address(dt_address))
    dt.mint(alice_address, to_base_18(10.0), from_wallet=alice_wallet)
    assert dtfactory.get_token_minter(dt.address) == alice_address
Пример #17
0
def test_calculateFee(alice_ocean, alice_wallet):
    """Tests the DataToken calculateFee method."""
    token = alice_ocean.create_data_token("DataToken1",
                                          "DT1",
                                          from_wallet=alice_wallet,
                                          blob="foo_blob")

    fee = token.calculateFee(to_base_18(100.0),
                             to_base_18(DataToken.OPF_FEE_PERCENTAGE))
    assert from_base_18(fee) == 0.1
Пример #18
0
def test_setSwapFee_fails(network, alice_wallet, alice_address, bob_wallet,
                          bob_address):
    factory = BFactory(get_bfactory_address(network))
    pool_address = factory.newBPool(alice_wallet)
    pool = BPool(pool_address)
    with pytest.raises(Exception):
        pool.setSwapFee(to_base_18(0.011),
                        from_wallet=bob_wallet)  # not ok, bob isn't controller
    pool.setController(bob_address, from_wallet=alice_wallet)
    pool.setSwapFee(to_base_18(0.011), from_wallet=bob_wallet)  # ok now
Пример #19
0
def test_calcPoolOutGivenSingleIn_base(network, alice_wallet):
    pool = _deployBPool(network, alice_wallet)
    x = pool.calcPoolOutGivenSingleIn(
        tokenBalanceIn_base=to_base_18(10.0),
        tokenWeightIn_base=to_base_18(1.0),
        poolSupply_base=to_base_18(120.0),
        totalWeight_base=to_base_18(2.0),
        tokenAmountIn_base=to_base_18(0.1),
        swapFee_base=0,
    )
    assert round(from_base_18(x), 3) == 0.599
Пример #20
0
def test_calcSingleInGivenPoolOut_base(network, alice_wallet):
    """Tests pricing with calcSingleInGivenPoolOut."""
    pool = _deployBPool(network, alice_wallet)
    x = pool.calcSingleInGivenPoolOut(
        tokenBalanceIn_base=to_base_18(10.0),
        tokenWeightIn_base=to_base_18(1.0),
        poolSupply_base=to_base_18(120.0),
        totalWeight_base=to_base_18(2.0),
        poolAmountOut_base=to_base_18(10.0),
        swapFee_base=0,
    )
    assert round(from_base_18(x), 3) == 1.736
Пример #21
0
def test_calcOutGivenIn_base(network, alice_wallet):
    """Tests pricing with calcOutGivenIn."""
    pool = _deployBPool(network, alice_wallet)
    x = pool.calcOutGivenIn(
        tokenBalanceIn_base=to_base_18(10.0),
        tokenWeightIn_base=to_base_18(1.0),
        tokenBalanceOut=to_base_18(10.1),
        tokenWeightOut_base=to_base_18(1.0),
        tokenAmountIn_base=to_base_18(1.0),
        swapFee_base=0,
    )
    assert round(from_base_18(x), 3) == 0.918
Пример #22
0
def test_setMinter(alice_ocean, alice_wallet, alice_address, bob_wallet,
                   bob_address):
    """Tests that a minter can be assigned for a Datatoken."""
    ocean = alice_ocean
    token = ocean.create_data_token("DataToken1",
                                    "DT1",
                                    from_wallet=alice_wallet,
                                    blob="foo_blob")

    # alice is the minter
    token.mint(alice_address, to_base_18(10.0), from_wallet=alice_wallet)
    token.mint(bob_address, to_base_18(10.0), from_wallet=alice_wallet)
    with pytest.raises(Exception):
        token.mint(alice_address, to_base_18(10.0), from_wallet=bob_wallet)

    # switch minter to bob
    token.proposeMinter(bob_address, from_wallet=alice_wallet)
    time.sleep(5)
    token.approveMinter(from_wallet=bob_wallet)
    token.mint(alice_address, to_base_18(10.0), from_wallet=bob_wallet)
    with pytest.raises(Exception):
        token.mint(alice_address, to_base_18(10.0), from_wallet=alice_wallet)
    with pytest.raises(Exception):
        token.mint(bob_address, to_base_18(10.0), from_wallet=alice_wallet)

    # switch minter back to alice
    token.proposeMinter(alice_address, from_wallet=bob_wallet)
    time.sleep(5)
    token.approveMinter(from_wallet=alice_wallet)
    token.mint(alice_address, to_base_18(10.0), from_wallet=alice_wallet)
    with pytest.raises(Exception):
        token.mint(alice_address, to_base_18(10.0), from_wallet=bob_wallet)
Пример #23
0
def test_setMinter(alice_ocean, alice_wallet, alice_address, bob_wallet,
                   bob_address):
    ocean = alice_ocean
    token = ocean.create_data_token('DataToken1',
                                    'DT1',
                                    from_wallet=alice_wallet,
                                    blob='foo_blob')

    #alice is the minter
    token.mint(alice_address, to_base_18(10.0), from_wallet=alice_wallet)
    token.mint(bob_address, to_base_18(10.0), from_wallet=alice_wallet)
    with pytest.raises(Exception):
        token.mint(alice_address, to_base_18(10.0), from_wallet=bob_wallet)

    #switch minter to bob
    token.proposeMinter(bob_address, from_wallet=alice_wallet)
    time.sleep(5)
    token.approveMinter(from_wallet=bob_wallet)
    token.mint(alice_address, to_base_18(10.0), from_wallet=bob_wallet)
    with pytest.raises(Exception):
        token.mint(alice_address, to_base_18(10.0), from_wallet=alice_wallet)
    with pytest.raises(Exception):
        token.mint(bob_address, to_base_18(10.0), from_wallet=alice_wallet)

    #switch minter back to alice
    token.proposeMinter(alice_address, from_wallet=bob_wallet)
    time.sleep(5)
    token.approveMinter(from_wallet=alice_wallet)
    token.mint(alice_address, to_base_18(10.0), from_wallet=alice_wallet)
    with pytest.raises(Exception):
        token.mint(alice_address, to_base_18(10.0), from_wallet=bob_wallet)
Пример #24
0
def test_rebind_more_tokens(network, T1, T2, alice_wallet):
    pool = _createPoolWith2Tokens(network, T1, T2, alice_wallet, 90.0, 10.0, 9.0, 1.0)

    # insufficient allowance
    with pytest.raises(Exception):
        pool.rebind(
            T1.address, to_base_18(120.0), to_base_18(9.0), from_wallet=alice_wallet
        )

    # sufficient allowance
    T1.approve(pool.address, to_base_18(30.0), from_wallet=alice_wallet)
    pool.rebind(
        T1.address, to_base_18(120.0), to_base_18(9.0), from_wallet=alice_wallet
    )
Пример #25
0
def test_exitswapExternAmountOut(network, T1, T2, alice_address, alice_wallet):
    T1balance = from_base_18(T1.balanceOf(alice_address))
    pool = _createPoolWith2Tokens(network, T1, T2, alice_wallet, 90.0, 10.0, 9.0, 1.0)
    BPT = pool
    pool.finalize(from_wallet=alice_wallet)
    pool_balance = from_base_18(BPT.balanceOf(alice_address))
    assert from_base_18(T1.balanceOf(alice_address)) == T1balance - 90
    pool.exitswapExternAmountOut(
        tokenOut_address=T1.address,
        tokenAmountOut_base=to_base_18(2.0),  # T1 wanted
        maxPoolAmountIn_base=to_base_18(10.0),  # max BPT spent
        from_wallet=alice_wallet,
    )
    assert from_base_18(T1.balanceOf(alice_address)) == (T1balance - 90 + 2.0)
    assert from_base_18(BPT.balanceOf(alice_address)) >= (pool_balance - 10.0)
Пример #26
0
def test_exitswapPoolAmountIn(network, T1, T2, alice_address, alice_wallet):
    T1balance = from_base_18(T1.balanceOf(alice_address))
    pool = _createPoolWith2Tokens(network, T1, T2, alice_wallet, 90.0, 10.0,
                                  9.0, 1.0)
    BPT = pool
    pool.finalize(from_wallet=alice_wallet)
    pool_balance = from_base_18(BPT.balanceOf(alice_address))
    assert from_base_18(T1.balanceOf(alice_address)) == (T1balance - 90)
    pool.exitswapPoolAmountIn(
        tokenOut_address=T1.address,
        poolAmountIn_base=to_base_18(10.0),  #BPT spent
        minAmountOut_base=to_base_18(1.0),  #min T1 wanted
        from_wallet=alice_wallet)
    assert from_base_18(T1.balanceOf(alice_address)) >= (T1balance - 90 + 1.0)
    assert from_base_18(BPT.balanceOf(alice_address)) == (pool_balance - 10.0)
Пример #27
0
    def get_info(self, web3, from_block, to_block, include_holders=False):
        contract = self.contract_concise
        minter = contract.minter()
        all_transfers, _ = self.get_all_transfers_from_events(
            from_block, to_block)
        order_logs = self.get_start_order_logs(web3,
                                               from_block=from_block,
                                               to_block=to_block)
        holders = []
        if include_holders:
            a_to_balance = DataToken.calculate_balances(all_transfers)
            _min = to_base_18(0.000001)
            holders = sorted(
                [(a, from_base_18(b))
                 for a, b in a_to_balance.items() if b > _min],
                key=lambda x: x[1],
                reverse=True,
            )

        return {
            "address": self.address,
            "name": contract.name(),
            "symbol": contract.symbol(),
            "decimals": contract.decimals(),
            "cap": from_base_18(contract.cap()),
            "totalSupply": from_base_18(contract.totalSupply()),
            "minter": minter,
            "minterBalance": self.token_balance(minter),
            "numHolders": len(holders),
            "holders": holders,
            "numOrders": len(order_logs),
        }
Пример #28
0
    def create(self, data_token: str, exchange_rate: float,
               wallet: Wallet) -> str:
        assert exchange_rate > 0, "Invalid exchange rate, must be > 0"
        exchange = self._exchange_contract()
        exchange_rate_base = to_base_18(exchange_rate)
        tx_id = exchange.create(self.ocean_address,
                                data_token,
                                exchange_rate_base,
                                from_wallet=wallet)
        # get tx receipt
        tx_receipt = exchange.get_tx_receipt(tx_id)
        # get event log from receipt
        logs = exchange.contract.events.ExchangeCreated().processReceipt(
            tx_receipt)
        if not logs:
            raise VerifyTxFailed(
                f"Create new datatoken exchange failed, transaction receipt for tx {tx_id} is not found."
            )

        exchange_id = logs[0].args.exchangeId  # get from event log args
        # compare exchange_id to generateExchangeId() value
        assert exchange_id == exchange.generateExchangeId(
            self.ocean_address, data_token, wallet.address)

        return exchange_id
Пример #29
0
    def setRate(
        self,
        new_rate: float,
        wallet: Wallet,
        exchange_id: str = "",
        data_token: str = "",
        exchange_owner: str = "",
    ) -> bool:
        assert new_rate > 0, "Invalid exchange rate, must be > 0"
        exchange_rate_base = to_base_18(new_rate)

        exchange, exchange_id = self.get_exchange_id_fallback_dt_and_owner(
            exchange_id, exchange_owner, data_token)

        tx_id = exchange.setRate(exchange_id,
                                 exchange_rate_base,
                                 from_wallet=wallet)
        # get tx receipt
        tx_receipt = exchange.get_tx_receipt(tx_id)
        # get event log from receipt
        logs = exchange.contract.events.ExchangeRateChanged().processReceipt(
            tx_receipt)
        if not logs:
            raise VerifyTxFailed(
                f"Set rate for exchange_id {exchange_id} failed, transaction receipt for tx {tx_id} is not found."
            )

        return True
Пример #30
0
def setup_all():
    config = ExampleConfig.get_config()
    ConfigProvider.set_config(config)
    Web3Provider.init_web3(
        provider=get_web3_connection_provider(config.network_url))
    ContractHandler.set_artifacts_path(config.artifacts_path)

    network = Web3Helper.get_network_name()
    wallet = get_ganache_wallet()
    if network in ["ganache", "development"] and wallet:

        print(
            f"sender: {wallet.key}, {wallet.address}, {wallet.password}, {wallet.keysStr()}"
        )
        print(
            f"sender balance: {Web3Helper.from_wei(Web3Helper.get_ether_balance(wallet.address))}"
        )
        assert Web3Helper.from_wei(Web3Helper.get_ether_balance(
            wallet.address)) > 10

        from ocean_lib.models.data_token import DataToken

        OCEAN_token = DataToken(get_ocean_token_address(network))
        amt_distribute = 1000
        amt_distribute_base = to_base_18(float(amt_distribute))
        for w in (get_publisher_wallet(), get_consumer_wallet()):
            if Web3Helper.from_wei(Web3Helper.get_ether_balance(
                    w.address)) < 2:
                Web3Helper.send_ether(wallet, w.address, 4)

            if OCEAN_token.token_balance(w.address) < 100:
                OCEAN_token.transfer(w.address,
                                     amt_distribute_base,
                                     from_wallet=wallet)