Example #1
0
File: code.py Project: anklee/pyeos
def apply(code, action):
    #    print(eoslib.n2s(code),eoslib.n2s(action))
    if code == test:
        eoslib.require_auth(test)
        if action == N(b'transfer'):
            msg = eoslib.read_message()
            result = struct.unpack('QQQ', msg)
            print(result)
            from_ = result[0]
            to_ = result[1]
            amount = result[2]
        elif action == N(b'testrwdb'):
            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()
        elif action == N(b'testexec'):
            test_exec_code()
        elif action == N(b'testimport'):
            test_import()
        elif action == N(b'testloadstr'):
            test_load_str()
        elif action == N(b'testrecursive'):
            test_recursive()
Example #2
0
def _create(msg):
    eoslib.require_auth(_code)
    issuer, amount, symbol = struct.unpack('QQ8s', msg)

    cs = currency_stats(symbol)
    if cs.issuer:
        raise Exception('currency has already been created')
    cs.issuer = issuer
    cs.max_supply.amount = amount
    cs.store(_code)
Example #3
0
def _transfer(msg):
    _from, to, amount, symbol = struct.unpack('QQQ8s', msg[:32])
    #    print('transfer:', _from, to, amount, symbol)
    eoslib.require_auth(_from)
    eoslib.require_recipient(_from)
    eoslib.require_recipient(to)
    #        memo = eoslib.unpack_bytes(msg[32:])
    a1 = Balance(_from, symbol)
    a2 = Balance(to, symbol)
    a1.sub(amount)
    a2.add(amount, _from)
Example #4
0
def apply_exchange_sell():
    order = SellOrder()
    ask = order;
    require_auth( 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" );
Example #5
0
def apply_exchange_buy():
    order = BuyOrder()
    bid = order
    print(eoslib.n2s(bid.buyer.name))
    eoslib.require_auth( 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" );
Example #6
0
def _issue(msg):
    _to, amount, symbol = struct.unpack('QQ8s', msg[:24])
    #        memo = eoslib.unpack_bytes(msg[24:])
    cs = currency_stats(symbol)
    assert cs.issuer, 'currency does not exists'
    eoslib.require_auth(cs.issuer)
    cs.supply.amount += amount
    assert cs.supply.amount < cs.max_supply.amount
    cs.store(cs.issuer)

    acc = Balance(_to, symbol)
    acc.add(amount, cs.issuer)
Example #7
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.require_auth(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"
Example #8
0
def apply_exchange_cancel_sell():
    msg = read_message()
    order = OrderID()
    require_auth( 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" )
Example #9
0
def train():
    eoslib.require_auth(eoslib.N(b'mnist'))
    data = eoslib.read_message()
    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...')
Example #10
0
def dotest():
    msg = read_message()
    
    name = uint64(msg[:8])
    print(name)
    print(eoslib.n2s(name))
    
    id = uint64(msg[8:])
    require_auth(name)
    
    ask = Ask.front_by_price()
    print(ask)

    bid = Bid.front_by_price()
    print(bid)
Example #11
0
def apply_currency_transfer():
    transfer = Transfer()
    print('apply_currency_transfer', transfer.from_, transfer.to_)
    if transfer.to_ == exchange:
        account = Account(transfer.from_)
        account.currency_balance += transfer.amount
        account.save()
    elif transfer.from_ == exchange:
        require_auth(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" 
Example #12
0
def apply_exchange_cancel_buy():
    msg = read_message()
    order = OrderID(msg)
    require_auth( order.name ); 
    
    bid_to_cancel = Bid.load_from_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;
    buyer_account.open_orders-=1;
    
    bid_to_cancel.remove();
    buyer_account.save();
    print( "bid removed\n" );
Example #13
0
def apply(receiver, code, action):

    if action == N('sayhello'):
        eoslib.require_auth(N('call'))
        msg = eoslib.read_action()
        print(msg.decode('utf8'))
    elif action == N('call'):
        msg = eoslib.read_action()
        _from, to, amount = struct.unpack('QQQ', msg)
        symbol = bytearray(8)
        symbol[0] = 4
        symbol[1] = ord('E')
        symbol[2] = ord('O')
        symbol[3] = ord('S')
        memo = eoslib.pack_bytes(b'hello')
        args = struct.pack('QQQ8s%ds' % (len(memo), ), _from, to, amount,
                           symbol, memo)

        print('++++call')
        eoslib.send_inline(N('eosio.token'), N('transfer'), args,
                           {'call': 'active'})
Example #14
0
def apply(name, type):
    #    print('hello from python apply',name,type)
    print(eoslib.n2s(name), eoslib.n2s(type))
    if type == eoslib.N(b'transfer'):
        msg = eoslib.read_message()
        result = struct.unpack('QQQ', msg)
        #         print(result)
        from_ = result[0]
        to_ = result[1]
        amount = result[2]

        eoslib.require_auth(from_)
        eoslib.require_notice(from_)
        eoslib.require_notice(to_)

        from_ = Account(from_)
        to_ = Account(to_)
        if from_.balance >= amount:
            from_.balance -= amount
            to_.balance += amount
            from_.store()
            to_.store()