示例#1
0
async def conclude_contract(request):
    start = time()
    post = await web_base.content_type_json_check(request)
    try:
        c_address = post['c_address']
        start_hash = unhexlify(post['start_hash'].encode())
        start_tx = tx_builder.get_tx(txhash=start_hash)
        if start_tx is None:
            return web_base.error_res('Not found start_tx {}'.format(
                post['start_hash']))
        send_pairs = post.get('send_pairs', None)
        c_storage = post.get('storage', None)
        tx = create_conclude_tx(c_address=c_address,
                                start_tx=start_tx,
                                send_pairs=send_pairs,
                                c_storage=c_storage)
        if not send_newtx(new_tx=tx):
            raise Exception('Failed to send new tx.')
        return web_base.json_res({
            'hash': hexlify(tx.hash).decode(),
            'gas_amount': tx.gas_amount,
            'gas_price': tx.gas_price,
            'fee': tx.gas_amount * tx.gas_price,
            'time': round(time() - start, 3)
        })
    except Exception:
        return web_base.error_res()
示例#2
0
async def contract_transfer(request):
    start = time()
    post = await utils.content_type_json_check(request)
    try:
        c_address = post['c_address']
        c_method = post['c_method']
        c_args = post['c_args']
        send_pairs = post.get('send_pairs', None)
        sender_name = post.get('from', C.account2name[C.ANT_UNKNOWN])
        with create_db(V.DB_ACCOUNT_PATH) as db:
            cur = db.cursor()
            sender = read_name2userid(sender_name, cur)
            tx = create_contract_transfer_tx(c_address=c_address,
                                             cur=cur,
                                             c_method=c_method,
                                             c_args=c_args,
                                             send_pairs=send_pairs,
                                             sender=sender)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise Exception('Failed to send new tx')
            db.commit()
        return utils.json_res({
            'hash': tx.hash.hex(),
            'gas_amount': tx.gas_amount,
            'gas_price': tx.gas_price,
            'fee': tx.gas_amount * tx.gas_price,
            'time': round(time() - start, 3)
        })
    except Exception:
        return utils.error_res()
示例#3
0
async def change_mint_tx(request):
    start = time()
    post = await utils.content_type_json_check(request)
    with create_db(V.DB_ACCOUNT_PATH, f_strict=True) as db:
        cur = db.cursor()
        try:
            user_name = post.get('from', C.account2name[C.ANT_UNKNOWN])
            sender = read_name2userid(user_name, cur)
            tx = change_mintcoin(mint_id=post['mint_id'],
                                 cur=cur,
                                 amount=post.get('amount'),
                                 description=post.get('description'),
                                 image=post.get('image'),
                                 setting=post.get('setting'),
                                 new_address=post.get('new_address'),
                                 sender=sender)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise BlockChainError('Failed to send new tx')
            db.commit()
            return utils.json_res({
                'hash': tx.hash.hex(),
                'gas_amount': tx.gas_amount,
                'gas_price': tx.gas_price,
                'fee': tx.gas_amount * tx.gas_price,
                'time': round(time() - start, 3)
            })
        except Exception:
            return utils.error_res()
示例#4
0
async def issue_mint_tx(request):
    start = time()
    post = await web_base.content_type_json_check(request)
    with closing(create_db(V.DB_ACCOUNT_PATH)) as db:
        cur = db.cursor()
        try:
            user_name = post.get('from', C.ANT_NAME_UNKNOWN)
            sender = read_name2user(user_name, cur)
            mint_id, tx = issue_mintcoin(
                name=post['name'],
                unit=post['unit'],
                digit=post.get('digit', 8),
                amount=post['amount'],
                cur=cur,
                description=post.get('description', None),
                image=post.get('image', None),
                additional_issue=post.get('additional_issue', True),
                sender=sender)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise BaseException('Failed to send new tx.')
            db.commit()
            return web_base.json_res({
                'hash': hexlify(tx.hash).decode(),
                'gas_amount': tx.gas_amount,
                'gas_price': tx.gas_price,
                'fee': tx.gas_amount * tx.gas_price,
                'time': round(time() - start, 3),
                'mint_id': mint_id
            })
        except BaseException:
            return web_base.error_res()
示例#5
0
async def contract_create(request):
    post = await web_base.content_type_json_check(request)
    with closing(create_db(V.DB_ACCOUNT_PATH, f_on_memory=True)) as db:
        cur = db.cursor()
        try:
            # バイナリをピックルしオブジェクトに戻す
            c_bin = unhexlify(post['hex'].encode())
            c_cs = {
                k.encode(errors='ignore'): v.encode(errors='ignore')
                for k, v in post.get('c_cs', dict()).items()
            }
            binary2contract(c_bin)  # can compile?
            sender_name = post.get('account', C.ANT_NAME_UNKNOWN)
            sender_id = read_name2user(sender_name, cur)
            c_address, c_tx = create_contract_tx(c_bin, cur, sender_id, c_cs)
            if not send_newtx(new_tx=c_tx, outer_cur=cur):
                raise BaseException('Failed to send new tx.')
            db.commit()
            data = {
                'txhash': hexlify(c_tx.hash).decode(),
                'c_address': c_address,
                'time': c_tx.time,
                'fee': {
                    'gas_price': c_tx.gas_price,
                    'gas_amount': c_tx.gas_amount,
                    'total': c_tx.gas_price * c_tx.gas_amount
                }
            }
            return web_base.json_res(data)
        except BaseException:
            return web_base.error_res()
示例#6
0
async def issue_mint_tx(request):
    start = time()
    post = await utils.content_type_json_check(request)
    with create_db(V.DB_ACCOUNT_PATH, f_strict=True) as db:
        cur = db.cursor()
        try:
            user_name = post.get('from', C.account2name[C.ANT_UNKNOWN])
            sender = read_name2userid(user_name, cur)
            mint_id, tx = issue_mintcoin(
                name=post['name'],
                unit=post['unit'],
                digit=post.get('digit', 8),
                amount=post['amount'],
                cur=cur,
                description=post.get('description', None),
                image=post.get('image', None),
                additional_issue=post.get('additional_issue', True),
                sender=sender)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise BlockChainError('Failed to send new tx')
            db.commit()
            return utils.json_res({
                'hash': tx.hash.hex(),
                'gas_amount': tx.gas_amount,
                'gas_price': tx.gas_price,
                'fee': tx.gas_amount * tx.gas_price,
                'time': round(time() - start, 3),
                'mint_id': mint_id
            })
        except Exception:
            return utils.error_res()
示例#7
0
async def change_mint_tx(request):
    start = time()
    post = await web_base.content_type_json_check(request)
    with closing(create_db(V.DB_ACCOUNT_PATH)) as db:
        cur = db.cursor()
        try:
            user_name = post.get('from', C.ANT_NAME_UNKNOWN)
            sender = read_name2user(user_name, cur)
            tx = change_mintcoin(mint_id=post['mint_id'],
                                 cur=cur,
                                 amount=post.get('amount'),
                                 description=post.get('description'),
                                 image=post.get('image'),
                                 setting=post.get('setting'),
                                 new_address=post.get('new_address'),
                                 sender=sender)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise BaseException('Failed to send new tx.')
            db.commit()
            return web_base.json_res({
                'hash': hexlify(tx.hash).decode(),
                'gas_amount': tx.gas_amount,
                'gas_price': tx.gas_price,
                'fee': tx.gas_amount * tx.gas_price,
                'time': round(time() - start, 3)
            })
        except BaseException:
            return web_base.error_res()
示例#8
0
async def validator_edit(request):
    start = time()
    post = await utils.content_type_json_check(request)
    v_address = post.get('v_address', None)
    new_address = post.get('new_address', None)
    flag = int(post.get('flag', F_NOP))
    sig_diff = int(post.get('sig_diff', 0))
    try:
        with create_db(V.DB_ACCOUNT_PATH) as db:
            cur = db.cursor()
            if v_address is None:
                v_address = generate_new_address_by_userid(
                    user=C.ANT_VALIDATOR, cur=cur)
            tx = create_validator_edit_tx(v_address=v_address,
                                          cur=cur,
                                          new_address=new_address,
                                          flag=flag,
                                          sig_diff=sig_diff)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise Exception('Failed to send new tx')
            db.commit()
            return utils.json_res({
                'hash': tx.hash.hex(),
                'gas_amount': tx.gas_amount,
                'gas_price': tx.gas_price,
                'fee': tx.gas_amount * tx.gas_price,
                'time': round(time() - start, 3)
            })
    except Exception:
        return utils.error_res()
示例#9
0
async def validate_unconfirmed(request):
    start = time()
    post = await utils.content_type_json_check(request)
    try:
        txhash = a2b_hex(post['hash'])
        tx = tx_builder.get_tx(txhash=txhash)
        if tx is None or tx.height is not None:
            return utils.error_res('You cannot validate tx. {}'.format(tx))
        with create_db(V.DB_ACCOUNT_PATH) as db:
            cur = db.cursor()
            new_tx = create_signed_tx_as_validator(tx=tx)
            assert tx is not new_tx, 'tx={}, new_tx={}'.format(
                id(tx), id(new_tx))
            if not send_newtx(new_tx=new_tx, outer_cur=cur):
                raise Exception('Failed to send new tx')
            db.commit()
            return utils.json_res({
                'hash': new_tx.hash.hex(),
                'gas_amount': new_tx.gas_amount,
                'gas_price': new_tx.gas_price,
                'fee': new_tx.gas_amount * new_tx.gas_price,
                'time': round(time() - start, 3)
            })
    except Exception:
        return utils.error_res()
示例#10
0
async def conclude_contract(request):
    start = time()
    post = await utils.content_type_json_check(request)
    try:
        start_hash = a2b_hex(post['start_hash'])
        start_tx = tx_builder.get_tx(txhash=start_hash)
        if start_tx is None:
            return utils.error_res('Not found start_tx {}'.format(
                post['start_hash']))
        c_address, c_method, redeem_address, c_args = start_tx.encoded_message(
        )
        send_pairs = post.get('send_pairs', None)
        c_storage = post.get('storage', None)
        tx = create_conclude_tx(c_address=c_address,
                                start_tx=start_tx,
                                redeem_address=redeem_address,
                                send_pairs=send_pairs,
                                c_storage=c_storage)
        if not send_newtx(new_tx=tx):
            raise Exception('Failed to send new tx')
        return utils.json_res({
            'hash': tx.hash.hex(),
            'gas_amount': tx.gas_amount,
            'gas_price': tx.gas_price,
            'fee': tx.gas_amount * tx.gas_price,
            'time': round(time() - start, 3)
        })
    except Exception:
        return utils.error_res()
示例#11
0
async def validator_edit(request):
    start = time()
    post = await web_base.content_type_json_check(request)
    c_address = post.get('c_address', None)
    new_address = post.get('new_address', None)
    flag = int(post.get('flag', F_NOP))
    sig_diff = int(post.get('sig_diff', 0))
    try:
        with closing(create_db(V.DB_ACCOUNT_PATH)) as db:
            cur = db.cursor()
            if c_address is None:
                c_address = create_new_user_keypair(name=C.ANT_NAME_CONTRACT,
                                                    cur=cur)
            tx = create_validator_edit_tx(c_address=c_address,
                                          new_address=new_address,
                                          flag=flag,
                                          sig_diff=sig_diff)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise Exception('Failed to send new tx.')
            db.commit()
            return web_base.json_res({
                'hash': hexlify(tx.hash).decode(),
                'gas_amount': tx.gas_amount,
                'gas_price': tx.gas_price,
                'fee': tx.gas_amount * tx.gas_price,
                'time': round(time() - start, 3)
            })
    except Exception:
        return web_base.error_res()
示例#12
0
async def contract_init(request):
    start = time()
    post = await web_base.content_type_json_check(request)
    try:
        c_address = post['c_address']
        c_bin = unhexlify(post['hex'].encode())
        c_extra_imports = post.get('extra_imports', None)
        c_settings = post.get('settings', None)
        send_pairs = post.get('send_pairs', None)
        binary2contract(c_bin=c_bin,
                        extra_imports=c_extra_imports)  # can compile?
        sender_name = post.get('from', C.ANT_NAME_UNKNOWN)
        with closing(create_db(V.DB_ACCOUNT_PATH)) as db:
            cur = db.cursor()
            sender = read_name2user(sender_name, cur)
            tx = create_contract_init_tx(c_address=c_address,
                                         c_bin=c_bin,
                                         cur=cur,
                                         c_extra_imports=c_extra_imports,
                                         c_settings=c_settings,
                                         send_pairs=send_pairs,
                                         sender=sender)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise Exception('Failed to send new tx.')
            db.commit()
            return web_base.json_res({
                'hash': hexlify(tx.hash).decode(),
                'gas_amount': tx.gas_amount,
                'gas_price': tx.gas_price,
                'fee': tx.gas_amount * tx.gas_price,
                'time': round(time() - start, 3)
            })
    except Exception:
        return web_base.error_res()
示例#13
0
async def send_many_user(request):
    start = time.time()
    if P.F_NOW_BOOTING:
        return web.Response(text='Now booting...', status=403)
    post = await web_base.content_type_json_check(request)
    with closing(create_db(V.DB_ACCOUNT_PATH)) as db:
        cur = db.cursor()
        try:
            user_name = post.get('from', C.ANT_NAME_UNKNOWN)
            user_id = read_name2user(user_name, cur)
            send_pairs = list()
            for address, coin_id, amount in post['pairs']:
                send_pairs.append((address, int(coin_id), int(amount)))
            message = post.get('message', None)
            if message:
                msg_type = C.MSG_PLAIN
                msg_body = message.encode()
            else:
                msg_type = C.MSG_NONE
                msg_body = b''
            new_tx = send_many(user_id,
                               send_pairs,
                               cur,
                               msg_type=msg_type,
                               msg_body=msg_body)
            if not send_newtx(new_tx=new_tx, outer_cur=cur):
                raise BaseException('Failed to send new tx.')
            db.commit()
            return web_base.json_res({
                'txhash': hexlify(new_tx.hash).decode(),
                'time': round(time.time() - start, 3)
            })
        except Exception as e:
            db.rollback()
            return web_base.error_res()
示例#14
0
async def change_mint_tx(request):
    post = await web_base.content_type_json_check(request)
    with closing(create_db(V.DB_ACCOUNT_PATH)) as db:
        cur = db.cursor()
        try:
            user_name = post.get('account', C.ANT_NAME_UNKNOWN)
            sender = read_name2user(user_name, cur)
            mint, mintcoin_tx = change_mintcoin(
                mint_id=int(post['mint_id']),
                cur=cur,
                amount=int(post.get('amount', 0)),
                message=post.get('message', None),
                image=post.get('image', None),
                additional_issue=bool(post['additional_issue'])
                if 'additional_issue' in post else None,
                sender=sender)
            if not send_newtx(new_tx=mintcoin_tx, outer_cur=cur):
                raise BaseException('Failed to send new tx.')
            db.commit()
            data = mintcoin_tx.getinfo()
            return web_base.json_res({
                'txhash': data['hash'],
                'mintcoin': mint.getinfo()
            })
        except BaseException:
            return web_base.error_res()
示例#15
0
文件: account.py 项目: volbil/bc4py
async def sendtoaddress(*args, **kwargs):
    """
    Send an amount to a given address.

    Arguments:
        1. "address"            (string, required) The bitcoin address to send to.
        2. "amount"             (numeric or string, required) The amount in BTC to send. eg 0.1
        3. "comment"            (string, optional) A comment used to store what the transaction is for.
                                     This is not part of the transaction, just kept in your wallet.
        4. "comment_to"         (string, optional) A comment to store the name of the person or organization
                                     to which you're sending the transaction. This is not part of the
                                     transaction, just kept in your wallet.
        5. subtractfeefromamount  (boolean, optional, default=false) The fee will be deducted from the amount being sent.
                                     The recipient will receive less bitcoins than you enter in the amount field.

    Result:
        "txid"                  (string) The transaction id.
    """
    if len(args) < 2:
        raise ValueError('too few arguments num={}'.format(len(args)))
    address, amount, *options = args
    if not is_address(address, V.BECH32_HRP, 0):
        raise ValueError('address is invalid')
    amount = int(amount * pow(10, V.COIN_DIGIT))
    _comment = str(
        options[0]) if 0 < len(options) else None  # do not use by Yiimp
    _comment_to = str(
        options[1]) if 1 < len(options) else None  # do not use by Yiimp
    subtract_fee_amount = bool(options[2]) if 2 < len(options) else False

    # execute send
    error = None
    from_id = C.ANT_UNKNOWN
    coin_id = 0
    coins = Balance(coin_id, amount)
    with create_db(V.DB_ACCOUNT_PATH) as db:
        cur = db.cursor()
        try:
            new_tx = send_from(from_id,
                               address,
                               coins,
                               cur,
                               subtract_fee_amount=subtract_fee_amount)
            if send_newtx(new_tx=new_tx, outer_cur=cur):
                db.commit()
            else:
                error = 'Failed to send new tx'
                db.rollback()
        except Exception as e:
            error = str(e)
            log.debug("sendtoaddress", exc_info=True)
            db.rollback()

    # submit result
    if error:
        raise ValueError(error)
    return new_tx.hash.hex()
示例#16
0
文件: account.py 项目: volbil/bc4py
async def sendmany(*args, **kwargs):
    """
    Send multiple times. Amounts are double-precision floating point numbers.
    Requires wallet passphrase to be set with walletpassphrase call.

    Arguments:
        1. "fromaccount"         (string, required) DEPRECATED. The account to send the funds from. Should be "" for the default account
        2. "amounts"             (string, required) A json object with addresses and amounts
            {
              "address":amount   (numeric or string) The monacoin address is the key, the numeric amount (can be string) in MONA is the value
              ,...
            }
        3. minconf                 (numeric, optional, default=1) Only use the balance confirmed at least this many times.
        4. "comment"             (string, optional) A comment

    Result:
        "txid"                   (string) The transaction id for the send. Only 1 transaction is created regardless of
                                            the number of addresses.
    """
    if len(args) < 2:
        raise ValueError('too few arguments num={}'.format(len(args)))
    from_account, pairs, *options = args
    _minconf = options[0] if 0 < len(options) else 1  # ignore
    _comment = options[1] if 1 < len(options) else None  # ignore

    # replace account "" to "@Unknown"
    from_account = C.account2name[
        C.ANT_UNKNOWN] if from_account == '' else from_account

    error = None
    with create_db(V.DB_ACCOUNT_PATH) as db:
        cur = db.cursor()
        try:
            user_id = read_name2userid(from_account, cur)
            send_pairs = list()
            multiple = pow(10, V.COIN_DIGIT)
            for address, amount in pairs.items():
                send_pairs.append((address, 0, int(amount * multiple)))
            new_tx = send_many(user_id, send_pairs, cur)
            if send_newtx(new_tx=new_tx, outer_cur=cur):
                db.commit()
            else:
                error = 'Failed to send new tx'
                db.rollback()
        except Exception as e:
            error = str(e)
            log.debug("sendmany", exc_info=True)
            db.rollback()

    # submit result
    if error:
        raise ValueError(error)
    return new_tx.hash.hex()
示例#17
0
async def broadcast_tx(request):
    post = await web_base.content_type_json_check(request)
    try:
        binary = unhexlify(post['hex'].encode())
        new_tx = TX(binary=binary)
        new_tx.signature = [(pk, unhexlify(sign.encode()))
                            for pk, sign in post['signature']]
        if not send_newtx(new_tx=new_tx):
            raise BaseException('Failed to send new tx.')
        return web_base.json_res({'txhash': hexlify(new_tx.hash).decode()})
    except BaseException:
        return web_base.error_res()
示例#18
0
async def send_from_user(request):
    start = time()
    if P.F_NOW_BOOTING:
        return web.Response(text='Now booting', status=403)
    post = await utils.content_type_json_check(request)
    with create_db(V.DB_ACCOUNT_PATH, f_strict=True) as db:
        cur = db.cursor()
        try:
            from_name = post.get('from', C.account2name[C.ANT_UNKNOWN])
            from_id = read_name2userid(from_name, cur)
            to_address = post['address']
            coin_id = int(post.get('coin_id', 0))
            amount = int(post['amount'])
            coins = Balance(coin_id, amount)
            if 'hex' in post:
                msg_type = C.MSG_BYTE
                msg_body = a2b_hex(post['hex'])
            elif 'message' in post:
                msg_type = post.get('message_type', C.MSG_PLAIN)
                msg_body = type2message(msg_type, post['message'])
            else:
                msg_type = C.MSG_NONE
                msg_body = b''
            new_tx = send_from(from_id,
                               to_address,
                               coins,
                               cur,
                               msg_type=msg_type,
                               msg_body=msg_body)
            if 'R' in post:
                new_tx.R = a2b_hex(post['R'])
            if not send_newtx(new_tx=new_tx, outer_cur=cur):
                raise BlockChainError('Failed to send new tx')
            db.commit()
            return utils.json_res({
                'hash': new_tx.hash.hex(),
                'gas_amount': new_tx.gas_amount,
                'gas_price': new_tx.gas_price,
                'fee': new_tx.gas_amount * new_tx.gas_price,
                'time': round(time() - start, 3)
            })
        except Exception as e:
            db.rollback()
            return utils.error_res()
示例#19
0
async def broadcast_tx(request):
    start = time()
    post = await web_base.content_type_json_check(request)
    try:
        binary = unhexlify(post['hex'].encode())
        new_tx = TX(binary=binary)
        new_tx.signature = [(pk, unhexlify(_sign.encode()))
                            for pk, _sign in post['signature']]
        if not send_newtx(new_tx=new_tx):
            raise BaseException('Failed to send new tx.')
        return web_base.json_res({
            'hash': hexlify(new_tx.hash).decode(),
            'gas_amount': new_tx.gas_amount,
            'gas_price': new_tx.gas_price,
            'fee': new_tx.gas_amount * new_tx.gas_price,
            'time': round(time() - start, 3)
        })
    except BaseException:
        return web_base.error_res()
示例#20
0
async def broadcast_tx(request):
    start = time()
    post = await utils.content_type_json_check(request)
    try:
        binary = a2b_hex(post['hex'])
        new_tx = TX.from_binary(binary=binary)
        new_tx.signature = [(a2b_hex(pk), a2b_hex(sig))
                            for pk, sig in post['signature']]
        if 'R' in post:
            new_tx.R = a2b_hex(post['R'])
        if not send_newtx(new_tx=new_tx):
            raise BlockChainError('Failed to send new tx')
        return utils.json_res({
            'hash': new_tx.hash.hex(),
            'gas_amount': new_tx.gas_amount,
            'gas_price': new_tx.gas_price,
            'fee': new_tx.gas_amount * new_tx.gas_price,
            'time': round(time() - start, 3)
        })
    except Exception:
        return utils.error_res()
示例#21
0
async def send_from_user(request):
    start = time()
    if P.F_NOW_BOOTING:
        return web.Response(text='Now booting...', status=403)
    post = await web_base.content_type_json_check(request)
    with closing(create_db(V.DB_ACCOUNT_PATH)) as db:
        cur = db.cursor()
        try:
            from_name = post.get('from', C.ANT_NAME_UNKNOWN)
            from_id = read_name2user(from_name, cur)
            to_address = post['address']
            coin_id = int(post.get('coin_id', 0))
            amount = int(post['amount'])
            coins = CoinObject(coin_id, amount)
            message = post.get('message', None)
            if message:
                msg_type = C.MSG_PLAIN
                msg_body = message.encode()
            else:
                msg_type = C.MSG_NONE
                msg_body = b''
            new_tx = send_from(from_id,
                               to_address,
                               coins,
                               cur,
                               msg_type=msg_type,
                               msg_body=msg_body)
            if not send_newtx(new_tx=new_tx, outer_cur=cur):
                raise BaseException('Failed to send new tx.')
            db.commit()
            return web_base.json_res({
                'hash': hexlify(new_tx.hash).decode(),
                'gas_amount': new_tx.gas_amount,
                'gas_price': new_tx.gas_price,
                'fee': new_tx.gas_amount * new_tx.gas_price,
                'time': round(time() - start, 3)
            })
        except Exception as e:
            db.rollback()
            return web_base.error_res()
示例#22
0
async def contract_update(request):
    start = time()
    post = await utils.content_type_json_check(request)
    try:
        c_address = post['c_address']
        c_extra_imports = post.get('extra_imports', None)
        if 'hex' in post:
            c_bin = a2b_hex(post['hex'])
            args = ("start_tx", "c_address", "c_storage", "redeem_address"
                    )  # dummy data
            binary2contract(b=c_bin, extra_imports=c_extra_imports,
                            args=args)  # can compile?
        else:
            c_bin = None
        c_settings = post.get('settings', None)
        send_pairs = post.get('send_pairs', None)
        sender_name = post.get('from', C.account2name[C.ANT_UNKNOWN])
        with create_db(V.DB_ACCOUNT_PATH) as db:
            cur = db.cursor()
            sender = read_name2userid(sender_name, cur)
            tx = create_contract_update_tx(c_address=c_address,
                                           cur=cur,
                                           c_bin=c_bin,
                                           c_extra_imports=c_extra_imports,
                                           c_settings=c_settings,
                                           send_pairs=send_pairs,
                                           sender=sender)
            if not send_newtx(new_tx=tx, outer_cur=cur):
                raise Exception('Failed to send new tx')
            db.commit()
        return utils.json_res({
            'hash': tx.hash.hex(),
            'gas_amount': tx.gas_amount,
            'gas_price': tx.gas_price,
            'fee': tx.gas_amount * tx.gas_price,
            'time': round(time() - start, 3)
        })
    except Exception:
        return utils.error_res()
示例#23
0
async def contract_start(request):
    post = await web_base.content_type_json_check(request)
    with closing(create_db(V.DB_ACCOUNT_PATH, f_on_memory=True)) as db:
        cur = db.cursor()
        try:
            c_address = post['address']
            c_method = post['method']
            c_args = post.get('args', None)
            outputs = post.get('outputs', list())
            account = post.get('account', C.ANT_NAME_UNKNOWN)
            user_id = read_name2user(account, cur)
            # TX作成
            outputs = [(address, coin_id, amount)
                       for address, coin_id, amount in outputs]
            start_tx = start_contract_tx(c_address, c_method, cur, c_args,
                                         outputs, user_id)
            # 送信
            if not send_newtx(new_tx=start_tx, outer_cur=cur):
                raise BaseException('Failed to send new tx.')
            db.commit()
            data = {
                'txhash': hexlify(start_tx.hash).decode(),
                'time': start_tx.time,
                'c_address': c_address,
                'fee': {
                    'gas_price': start_tx.gas_price,
                    'gas_amount': start_tx.gas_amount,
                    'total': start_tx.gas_price * start_tx.gas_amount
                },
                'params': {
                    'method': c_method,
                    'args': c_args
                }
            }
            return web_base.json_res(data)
        except BaseException:
            return web_base.error_res()