def approveTokenTransfer(state):
    web3 = web3_client(state.get('network'))
    bountiesContract = getBountiesContract(state.get('network'))
    token = getTokenContract(state.get('network'),
                             to_checksum_address(state.get('token_address')))

    tx = token.functions.approve(to_checksum_address(
        bountiesContract.address), state.get('amount')).buildTransaction({
            'gasPrice':
            web3.toWei(state.get('gas_price'), 'gwei'),
            'gas':
            state.get('gas_limit'),
            'nonce':
            web3.eth.getTransactionCount(
                to_checksum_address(state.get('wallet').get('address'))),
        })

    signed = web3.eth.account.signTransaction(
        tx, private_key=state.get('wallet').get('private_key'))

    # send transaction and wait for receipt
    print('Approving token usage... ', end='', flush=True)
    receipt = web3.eth.waitForTransactionReceipt(
        web3.eth.sendRawTransaction(signed.rawTransaction))
    puts(colored.green(web3.toHex(receipt.transactionHash)))
Пример #2
0
def tokenBalance(state):
    web3 = web3_client(state.get('network'))
    address = to_checksum_address(state.get('wallet').get('address'))
    token_balance = 0

    # if the user is funding the bounty with a token, ensure they have enough tokens
    if (state.get('token_address') !=
            '0x0000000000000000000000000000000000000000'):
        token = getTokenContract(
            state.get('network'),
            to_checksum_address(state.get('token_address')))
        token_balance = token.functions.balanceOf(address).call()

    return token_balance
def getTokenInfo(state):
    # get token address and decimal places from symbol
    info = name_to_token(state.get('token'))

    # user wants to use custom token if address != 0x0
    if (state.get('token_address') !=
            '0x0000000000000000000000000000000000000000'):

        try:
            token = getTokenContract(
                state.get('network'),
                to_checksum_address(state.get('token_address')))
        except BadFunctionCallOutput:
            raise UsageError(
                'there doesn\'t seem to be a token at that address...')

        print('Getting token info... ', end='', flush=True)

        info.update({
            #'name' : t.functions.name().call()),
            'addr': state.get('token_address'),
            'name': token.functions.symbol().call(),
            'decimals': token.functions.decimals().call()
        })

        puts(colored.green('done.'))

    # fail if no token info was provided
    if (not info):
        print(f'Error {data.get("tokenName")} is not supported.')
        exit(1)

    return {
        'token': info.get('name'),
        'token_address': info.get('addr'),
        'token_decimals': int(info.get('decimals')),

        # set amount to correct unit by multiplying the human-readable unit by
        # the number of decimals.
        'amount':
        int(state.get('amount') * pow(10, int(info.get('decimals'))))
    }
Пример #4
0
def issueAndActivateBounty(state, ipfsHash):
    web3 = web3_client(state.get('network'))
    bountiesContract = getBountiesContract(state.get('network'))

    platform_fees = int(
        state.get('platform').get('fees_factor') * state.get('amount'))
    if state.get(
            'token_address') == '0x0000000000000000000000000000000000000000':
        # build transaction to pay the platform fees
        platform_fees_tx = {
            'from':
            state.get('wallet').get('address'),
            'to':
            state.get('platform').get('address'),
            'gas':
            state.get('gas_limit'),
            'gasPrice':
            web3.toWei(state.get('gas_price'), 'gwei'),
            'value':
            platform_fees,
            'nonce':
            web3.eth.getTransactionCount(state.get('wallet').get('address'))
        }

        signed_platform_fees_tx = web3.eth.account.signTransaction(
            platform_fees_tx,
            private_key=state.get('wallet').get('private_key'))

        # send platform fees transaction and wait for receipt
        print('Sending platform fees to gitcoin... ', end='', flush=True)
        platform_fees_receipt = web3.eth.waitForTransactionReceipt(
            web3.eth.sendRawTransaction(
                signed_platform_fees_tx.rawTransaction))
        puts(colored.green(web3.toHex(platform_fees_receipt.transactionHash)))

    else:
        tokenContract = getTokenContract(state.get('network'),
                                         state.get('token_address'))
        platform_fees_tx = tokenContract.functions.transfer(
            to_checksum_address(state.get('platform').get('address')),
            platform_fees).buildTransaction({
                'from':
                state.get('wallet').get('address'),
                'gasPrice':
                web3.toWei(state.get('gas_price'), 'gwei'),
                'gas':
                state.get('gas_limit'),
                'nonce':
                web3.eth.getTransactionCount(
                    to_checksum_address(state.get('wallet').get('address')))
            })
        signed_platform_fees_tx = web3.eth.account.signTransaction(
            platform_fees_tx,
            private_key=state.get('wallet').get('private_key'))

        # send platform fees transaction and wait for receipt
        print('Sending platform fees to gitcoin... ', end='', flush=True)
        platform_fees_receipt = web3.eth.waitForTransactionReceipt(
            web3.eth.sendRawTransaction(
                signed_platform_fees_tx.rawTransaction))
        puts(colored.green(web3.toHex(platform_fees_receipt.transactionHash)))

    # build transaction
    tx = bountiesContract.functions.issueAndActivateBounty(
        state.get('wallet').get('address'),
        9999999999,  # 11/20/2286, https://github.com/Bounties-Network/StandardBounties/issues/25
        ipfsHash,
        state.get('amount'),
        '0x0000000000000000000000000000000000000000',
        state.get('token_address') !=
        '0x0000000000000000000000000000000000000000',
        to_checksum_address(state.get('token_address')),
        state.get('amount')).buildTransaction({
            'from':
            state.get('wallet').get('address'),
            'value':
            state.get('amount') if state.get('token_address')
            == '0x0000000000000000000000000000000000000000' else 0,
            'gasPrice':
            web3.toWei(state.get('gas_price'), 'gwei'),
            'gas':
            state.get('gas_limit'),
            'nonce':
            web3.eth.getTransactionCount(state.get('wallet').get('address'))
        })

    signed = web3.eth.account.signTransaction(
        tx, private_key=state.get('wallet').get('private_key'))

    old_id = bountiesContract.functions.getNumBounties().call()

    # send transaction and wait for receipt
    print('Funding bounty... ', end='', flush=True)
    receipt = web3.eth.waitForTransactionReceipt(
        web3.eth.sendRawTransaction(signed.rawTransaction))
    new_id = bountiesContract.functions.getNumBounties().call()
    puts(colored.green(web3.toHex(receipt.transactionHash)))

    return old_id < new_id, old_id