コード例 #1
0
    def fulfill(self, contract: ContractInstance, args: dict,
                res: int) -> None:
        keypair = Keypair.create_from_private_key(
            private_key=self.credentials["private_key"],
            public_key=self.credentials["public_key"],
        )

        # Estimate gas fee
        predicted_gas = contract.read(
            keypair,
            "simple_callback",
            args={
                "request_id": args["request_id"],
                "callback_addr": args["from"],
                "result": {
                    "Numeric": res
                },
            },
        )

        logger.debug(
            f"[[bold]{self.name}[/]] Estimating gas fee: {predicted_gas.gas_consumed}"
        )

        # Execute call
        receipt = contract.exec(
            keypair,
            "simple_callback",
            args={
                "request_id": args["request_id"],
                "callback_addr": args["from"],
                "result": {
                    "Numeric": res
                },
            },
            gas_limit=predicted_gas.gas_consumed * 2,
        )

        logger.info(
            f"[[bold]{self.name}[/]] Callback substrate receipt: {receipt}")

        if receipt.is_succes:
            # Log the callback completed events
            for contract_event in receipt.contract_events:
                logger.info(
                    f"[[bold]{self.name}[/]] Contract callback events {contract_event.name} {contract_event.docs}:  {contract_event.value}"
                )
        else:
            raise Exception(
                f"[[bold]{self.name}[/]] Contract fulfill request {args} unsuccessful {receipt.error_message}"
            )

        # Read back the result
        result = contract.read(keypair,
                               "oracle_results",
                               args={"request_id": args["request_id"]})
        logger.info(
            f"[[bold]{self.name}[/]] Reading the value from contract after the callback {result}",
        )
コード例 #2
0
    def create_contract_from_address(
            self,
            address: str,
            substrate: SubstrateInterface = None) -> ContractInstance:
        """Given the `address` return a ContractInstance of the contract using the
        `self.metadata_file` metadata.

        Args:
            address (str): contract address to create contract from

        Returns:
            ContractInstance: instance of the contract on the address.
        """
        if not substrate:
            substrate = self.get_connection()

        return ContractInstance.create_from_address(
            contract_address=address,
            metadata_file=self.metadata_file,
            substrate=substrate,
        )
コード例 #3
0
    )
    exit()

keypair = Keypair.create_from_uri('//Alice')

# Check if contract is on chain
contract_info = substrate.query(
    "Contracts",
    "ContractInfoOf",
    params=['5DS85d9YE5KHqoffuYpLHwL3XNjJPQKc7ftrxdqS7S282gkK'])

if contract_info:
    # Create contract instance from deterministic address
    contract = ContractInstance.create_from_address(
        contract_address="5DS85d9YE5KHqoffuYpLHwL3XNjJPQKc7ftrxdqS7S282gkK",
        metadata_file=os.path.join(os.path.dirname(__file__), 'assets',
                                   'erc20.json'),
        substrate=substrate)

else:
    # Upload WASM code
    code = ContractCode.create_from_contract_files(
        metadata_file=os.path.join(os.path.dirname(__file__), 'assets',
                                   'erc20.json'),
        wasm_file=os.path.join(os.path.dirname(__file__), 'assets',
                               'erc20.wasm'),
        substrate=substrate)

    receipt = code.upload_wasm(keypair)

    if receipt.is_succes:
コード例 #4
0
    keypair = Keypair.create_from_uri('//Alice')
    contract_address = "5FbuqfZwkNjadtaCfhAwMb5ZQ4Bi2iF5m4AnibhAR987Jra5"

    # Check if contract is on chain
    contract_info = substrate.query("Contracts", "ContractInfoOf",
                                    [contract_address])

    if contract_info.value:

        print(f'Found contract on chain: {contract_info.value}')

        # Create contract instance from deterministic address
        contract = ContractInstance.create_from_address(
            contract_address=contract_address,
            metadata_file=os.path.join(os.path.dirname(__file__), 'assets',
                                       'flipper.json'),
            substrate=substrate)
    else:

        # Upload WASM code
        code = ContractCode.create_from_contract_files(
            metadata_file=os.path.join(os.path.dirname(__file__), 'assets',
                                       'flipper.json'),
            wasm_file=os.path.join(os.path.dirname(__file__), 'assets',
                                   'flipper.wasm'),
            substrate=substrate)

        # Deploy contract
        print('Deploy contract...')
        contract = code.deploy(keypair=keypair,