Exemple #1
0
def pref_td(w3, accts):
    deploy = create_contract(w3, 'contracts/preferences_basic_timedelta.vy')
    tx_hash = deploy.constructor(accts(0)).transact()
    tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
    contract = w3.eth.contract(address=tx_receipt.contractAddress,
                               abi=deploy.abi)
    return ImplicitContract(contract)
Exemple #2
0
def gov(w3, accts, pref_uint, pref_td):
    deploy = create_contract(w3, 'contracts/governance_basic.vy')
    tx_hash = deploy.constructor(accts(0), pref_uint.address,
                                 pref_td.address).transact()
    tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
    contract = w3.eth.contract(address=tx_receipt.contractAddress,
                               abi=deploy.abi)
    return ImplicitContract(contract)
Exemple #3
0
 def __init__(self, _web3: Web3, address, interface):
     self.web3 = _web3
     self.__address = address
     self.__instance = ImplicitContract(self.web3.eth.contract(self.__address, **interface))
     # Register new filter to watch for logs from this instance's address
     self.__filter = self.web3.eth.filter({
         # Include events from the deployment stage
         'fromBlock': self.web3.eth.blockNumber - 1,
         'address': self.__address
     })
     self.__event_signatures = self.get_event_signatures(interface['abi'])
     self.__event_processors = self.get_event_processors(interface['abi'])
Exemple #4
0
 def __init__(self, _web3: Web3, address: Address, interface):
     self.web3 = _web3
     self.address = address
     self.interface = ImplicitContract(
         classic_contract=self.web3.eth.contract(self.address, **interface))
     # Register new filter to watch for logs from this instance"s address
     self.__filter = self.web3.eth.filter({
         # Include events from the deployment stage
         "fromBlock":
         self.web3.eth.blockNumber - 1,
         "address":
         self.address
     })
     self.event_signatures = self.get_event_signatures(interface["abi"])
     self.event_processors = self.get_event_processors(interface["abi"])
Exemple #5
0
class Contract:
    def __init__(self):
        self._ccontract = web3.eth.contract(address=web3.toChecksumAddress(
            settings.CONTRACT_ADDRESS),
                                            abi=abi)
        self._icontract = ImplicitContract(classic_contract=self._ccontract)

    @classmethod
    def _wait_for_event(cls, event_filter, tx_hash, timeout=120):
        """
        Wait for event by transaction hash
        :param event_filter: event filter from contract
        :param tx_hash: transaction hash
        :param timeout: wait timeout
        :return: event

        Example usage:
            job_filter = self._ccontract.events.JobIssued.createFilter(
                fromBlock='latest',
                argument_filters={'issuer': web3.eth.defaultAccount}
            )
            self._wait_for_event(job_filter, tx_hash)
        """
        spent_time = 0
        while spent_time < timeout:
            for e in event_filter.get_new_entries():
                if e.transactionHash == tx_hash:
                    return e

            time.sleep(1)
            spent_time += 1

    @classmethod
    def _wait_for_transaction_mined(cls, tx_hash: bytearray, timeout=120):
        """
        Wait for transaction has been mined
        :param tx_hash: transaction hash
        :param timeout: timeout seconds
        :return: receipt
        """
        spent_time = 0
        while spent_time < timeout:
            receipt = web3.eth.waitForTransactionReceipt(tx_hash)
            if len(receipt.logs) and receipt.logs[0].type == 'mined':
                return receipt
            time.sleep(1)
            spent_time += 1
        raise TimeoutError("Transaction timed out {}".format(receipt))

    @classmethod
    def _asset_id_2_job_id(cls, asset_id: str):
        return hashlib.sha256(asset_id.encode()).digest()

    def issue_job(self, task_declaration_id: str, value: int):
        """
        Issue Job
        :param task_declaration_id: task declaration asset id
        :param value: deposit amount
        :return: job id
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        tx_hash = self._icontract.issueJob(_id, transact={'value': value})
        self._wait_for_transaction_mined(tx_hash=tx_hash)
        return _id

    def deposit(self, task_declaration_id: str, value: int):
        """
        Deposit Job
        :param task_declaration_id: task declaration id
        :param value: amount to deposit
        :return: receipt
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        tx_hash = self._icontract.deposit(_id, transact={'value': value})
        return self._wait_for_transaction_mined(tx_hash=tx_hash)

    def get_job_balance(self, task_declaration_id: str):
        """
        Get Job
        :param task_declaration_id: task declaration id
        :return: balance
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        return self._icontract.getJobBalance(_id)

    def distribute(self, task_declaration_id: str, workers: list,
                   amounts: list):
        """
        Payout workers
        :role: validator
        :param task_declaration_id: task declaration id
        :param workers: workers address list
        :param amounts: amounts list for each worker
        :return: receipt
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        tx_hash = self._icontract.distribute(_id, workers, amounts)
        return self._wait_for_transaction_mined(tx_hash=tx_hash)

    def distribute_async(self, task_declaration_id: str, workers: list,
                         amounts: list):
        """
        Payout workers
        :role: validator
        :param task_declaration_id: task declaration id
        :param workers: workers address list
        :param amounts: amounts list for each worker
        :return: tx_hash
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        return self._icontract.distribute(_id, workers, amounts)

    def wait_for_transaction_mined(self, tx_hash):
        return self._wait_for_transaction_mined(tx_hash=tx_hash)

    @classmethod
    def is_transaction_mined(cls, tx_hash):
        receipt = web3.eth.waitForTransactionReceipt(tx_hash)
        if len(receipt.logs) and receipt.logs[0].type == 'mined':
            return True
        return False

    def finish_job(self, task_declaration_id: str):
        """
        Finish Job
        :role: validator
        :param task_declaration_id: task declaration id
        :return: receipt
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        tx_hash = self._icontract.finishJob(_id)
        return self._wait_for_transaction_mined(tx_hash=tx_hash)

    def does_job_exist(self, task_declaration_id: str):
        """
        Finish Job
        :param task_declaration_id: task declaration id
        :return: True if job was issued otherwise False
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        return self._icontract.doesJobExist(_id)

    def does_job_finished(self, task_declaration_id: str):
        """
        Get Job
        :param task_declaration_id: task declaration id
        :return: True if job finished
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        return self._icontract.doesJobFinished(_id)

    def add_validator(self, task_declaration_id: str, validator_address: str):
        """
        Add validator to JOB
        :param task_declaration_id: task declaration id
        :return: True if job was issued otherwise False
        """
        _id = self._asset_id_2_job_id(task_declaration_id)
        return self._icontract.add(_id)
Exemple #6
0
 def __init__(self):
     self._ccontract = web3.eth.contract(address=web3.toChecksumAddress(
         settings.CONTRACT_ADDRESS),
                                         abi=abi)
     self._icontract = ImplicitContract(classic_contract=self._ccontract)
Exemple #7
0
def cancel_escrow(inst, escrow_id, tx_p=None):
    tx_hash = inst.cancelEscrow(Web3.toBytes(escrow_id), transact=tx_p)
    print(f'waiting for tx {binascii.hexlify(tx_hash)}')
    receipt = web3.eth.waitForTransactionReceipt(tx_hash)
    return receipt


if __name__ == '__main__':
    contract, web3 = init_contract()
    print(web3.eth.syncing)

    generated_price = web3.eth.generateGasPrice()

    c_contract = ConciseContract(contract)
    t_contract = ImplicitContract(contract)

    print('Network gas price:', web3.fromWei(web3.eth.gasPrice, 'Gwei'))
    print('Computed gas price:', web3.fromWei(generated_price, 'Gwei'))
    # print(web3.eth.accounts)
    # print(web3.txpool.content)
    params = {'from': OWNER, 'gasPrice': generated_price}

    # mint(t_contract, web3.eth.accounts[0], web3.toWei(1, 'Ether'), tx_p=params)
    # mint(t_contract, '0x8862cE71FDCDC386D5a9b6BB5640a8FefD6DDAd0', web3.toWei(100000, 'Ether'), tx_p=params)
    # mint(t_contract, '0xfF989e7D397e6fF1026429A87d7A5eF7c6B09c27', web3.toWei(100000, 'Ether'), tx_p=params)

    print('OWNER', c_contract.balanceOf(OWNER))
    print('RECIPIENT', c_contract.balanceOf(RECIPIENT))
    print('FEE_RECIPIENT', c_contract.balanceOf(FEE_RECIPIENT))