Ejemplo n.º 1
0
Archivo: code.py Proyecto: eos21/pyeos
def apply(code, action):
#    print(eoslib.n2s(code),eoslib.n2s(action))
    if code == test:
        eoslib.requireAuth(test)
        if action == N(b'transfer'):
            msg = eoslib.readMessage()
            result = struct.unpack('QQQ', msg)
            print(result)
            from_ = result[0]
            to_ = result[1]
            amount = result[2]
        elif action == N(b'test'):
            test_rw_db()
        elif action == N(b'testdb'):
            test_db()
        elif action == N(b'callwasm'):
        		test_call_wasm_function()
        elif action == N(b'testmsg'):
            print('testmsg')
            test_message()
        elif action == N(b'testts'):
            test_transaction()
        elif action == N(b'testmem'):
            test_memory_limit()
        elif action == N(b'testtimeout'):
            test_time_out()
Ejemplo n.º 2
0
def apply_exchange_buy():
    order = BuyOrder()
    bid = order
    print(eoslib.n2s(bid.buyer.name))
    eoslib.requireAuth(bid.buyer.name)
    assert bid.quantity > 0, "invalid quantity"
    assert bid.expiration > eoslib.now(), "order expired"
    print(eoslib.n2s(bid.buyer.name), " created bid for ", order.quantity,
          " currency at price: ", order.price, "\n")
    buyer_account = Account(bid.buyer.name)
    buyer_account.eos_balance -= bid.quantity

    print('buyer_account:', buyer_account)
    lowest_ask = Ask.front_by_price()
    if not lowest_ask:
        print("\n No asks found, saving buyer account and storing bid\n")
        assert not order.fill_or_kill, "order not completely filled"
        bid.store()
        buyer_account.open_orders += 1
        buyer_account.save()
        return

    print("ask: ", lowest_ask, "\n")
    print("bid: ", bid, "\n")

    seller_account = Account(lowest_ask.seller.name)

    while lowest_ask.price <= bid.price:
        print("lowest ask <= bid.price\n", lowest_ask.price, bid.price)
        match(bid, buyer_account, lowest_ask, seller_account)

        if lowest_ask.quantity == 0:
            seller_account.open_orders -= 1
            seller_account.save()
            buyer_account.save()
            lowest_ask.remove()
            lowest_ask = Ask.front_by_price()
            if not lowest_ask:
                break
            seller_account = Account(lowest_ask.seller.name)
        else:
            break
            # buyer's bid should be filled
    print("lowest_ask >= bid.price or buyer's bid has been filled\n")

    if bid.quantity and not order.fill_or_kill:
        buyer_account.open_orders += 1
    buyer_account.save()
    print("saving buyer's account\n")
    if bid.quantity:
        print(bid.quantity, " eos left over")
        assert not order.fill_or_kill, "order not completely filled"
        bid.store()
        return
    print("bid filled\n")
Ejemplo n.º 3
0
def apply_exchange_sell():
    order = SellOrder()
    ask = order
    requireAuth(ask.seller.name)

    assert ask.quantity > 0, "invalid quantity"
    assert ask.expiration > now(), "order expired"

    print("\n\n", eoslib.n2s(ask.seller.name), " created sell for ",
          order.quantity, " currency at price: ", order.price, "\n")

    existing_ask = Ask.load_by_order_id(ask.seller)
    assert not existing_ask, "order with this id already exists"

    seller_account = Account(ask.seller.name)
    seller_account.currency_balance -= ask.quantity

    highest_bid = Bid.back_by_price()
    if not highest_bid:
        assert not order.fill_or_kill, "order not completely filled"
        print("\n No bids found, saving seller account and storing ask\n")
        ask.store()
        seller_account.open_orders += 1
        seller_account.save()
        return

    print("\n bids found, lets see what matches\n")
    buyer_account = Account(highest_bid.buyer.name)

    while highest_bid.price >= ask.price:
        match(highest_bid, buyer_account, ask, seller_account)
        if highest_bid.quantity == 0:
            buyer_account.open_orders -= 1
            seller_account.save()
            buyer_account.save()
            highest_bid.remove()
            highest_bid = Bid.back_by_price()
            if not highest_bid:
                break
            buyer_account = Account(highest_bid.buyer.name)
        else:
            break
            # buyer's bid should be filled

    if ask.quantity and not order.fill_or_kill:
        seller_account.open_orders += 1
    seller_account.save()
    if ask.quantity:
        assert not order.fill_or_kill, "order not completely filled"
        print("saving ask\n")
        ask.store()
        return
    print("ask filled\n")
Ejemplo n.º 4
0
def apply_eos_transfer():
    print('apply_eos_transfer')
    transfer = Transfer()
    if transfer.to_ == exchange:
        account = Account(transfer.from_)
        account.eos_balance += transfer.amount
        account.save()
    elif transfer.from_ == exchange:
        eoslib.requireAuth(transfer.to_)
        account = Account(transfer.to_)
        account.eos_balance -= transfer.amount
        account.save()
    else:
        assert False, "notified on transfer that is not relevant to this exchange"
Ejemplo n.º 5
0
def apply_exchange_cancel_sell():
    msg = readMessage()
    order = OrderID()
    requireAuth(order.name)

    bid_to_cancel = Bid.load_by_order_id(order)
    assert bid_to_cancel, "bid with this id does not exists"
    buyer_account = Account(order.name)
    buyer_account.eos_balance += bid_to_cancel.quantity
    if buyer_account.open_orders > 0:
        buyer_account.open_orders -= 1
    bid_to_cancel.remove()
    buyer_account.save()
    print("bid removed\n")
Ejemplo n.º 6
0
Archivo: mnist.py Proyecto: eos21/pyeos
def train():
    eoslib.requireAuth(eoslib.N(b'mnist'))
    data = eoslib.readMessage()
    data = eoslib.unpack(data)
    print(len(data))
    data = zlib.decompress(data)
    data = pickle.loads(data)
    print('training start...')
    #    print(data)
    #    data0 = np.reshape(data[0], (784, 1))
    #    data1 = vectorized_result(data[1])
    net = Network([784, 30, 10])
    net.SGD(data, 1, 1, 3.0, test_data=None)
    print('training end...')
Ejemplo n.º 7
0
def dotest():
    msg = readMessage()

    name = uint64(msg[:8])
    print(name)
    print(eoslib.n2s(name))

    id = uint64(msg[8:])
    requireAuth(name)

    ask = Ask.front_by_price()
    print(ask)

    bid = Bid.front_by_price()
    print(bid)
Ejemplo n.º 8
0
def apply_currency_transfer():
    print('apply_currency_transfer')
    transfer = Transfer()
    if transfer.to_ == exchange:
        account = Account(transfer.from_)
        account.currency_balance += transfer.amount
        account.save()
    elif transfer.from_ == exchange:
        requireAuth(transfer.to_)
        # require the receiver of funds (account owner) to authorize this transfer
        account = Account(transfer.to_)
        assert account.currency_balance >= transfer.amount
        account.currency_balance -= transfer.amount
        account.save()
    else:
        assert False, "notified on transfer that is not relevant to this exchange"