def contract_call(ctx, deploy_descriptor, function, params, return_type):
    try:
        with open(deploy_descriptor) as fp:
            contract = json.load(fp)
            source = contract.get('source')
            bytecode = contract.get('bytecode')
            address = contract.get('address')

            kp, _ = _keypair()
            contract = Contract(source,
                                bytecode=bytecode,
                                address=address,
                                client=_epoch_cli())
            result = contract.tx_call(kp, function, params, gas=40000000)
            _pp([
                ('Contract address', contract.address),
                ('Gas price', result.gas_price),
                ('Gas used', result.gas_used),
                ('Return value (encoded)', result.return_value),
            ])
            if result.return_type == 'ok':
                value, remote_type = contract.decode_data(
                    result.return_value, return_type)
                _pp([
                    ('Return value', value),
                    ('Return remote type', remote_type),
                ])

            pass
    except Exception as e:
        print(e)
def contract_compile(contract_file):
    try:
        with open(contract_file) as fp:
            code = fp.read()
            contract = Contract(Contract.SOPHIA, client=_epoch_cli())
            result = contract.compile(code)
            _pp([("bytecode", result)])
    except Exception as e:
        print(e)
def test_sophia_contract_tx_call():
    contract = Contract(aer_identity_contract, Contract.SOPHIA)
    address, tx = contract.tx_create_wait(KEYPAIR, gas=1000)
    print("contract: ", address)
    print("tx contract: ", tx)

    result = contract.tx_call(address, KEYPAIR, 'main', '42')
    assert result is not None
    assert result.return_type == 'ok'
    assert result.return_value.lower() == f'0x{hex(42)[2:].zfill(64).lower()}'
Beispiel #4
0
def contract_compile(contract_file):
    try:
        with open(contract_file) as fp:
            c = fp.read()
            print(c)
            contract = Contract(fp.read(),
                                Contract.SOPHIA,
                                client=_epoch_cli())
            result = contract.compile('')
            _pp([("contract", result)])
    except Exception as e:
        print(e)
Beispiel #5
0
def contract_deploy(contract_file, gas):
    try:
        with open(contract_file) as fp:
            contract = Contract(fp.read(),
                                Contract.SOPHIA,
                                client=_epoch_cli())
            kp, _ = _keypair()
            address, tx = contract.tx_create(kp, gas=gas)
            _pp([
                ("Contract address", address),
                ("Transaction hash", tx.tx_hash),
            ])
    except Exception as e:
        print(e)
    def __init__(self, **kwargs):
        """
        Initialize a ContractNative object

        :param client: an instance of NodeClient
        :param source: the source code of the contract
        :param compiler: an instance of the CompilerClient
        :param address: address of the currently deployed contract (optional)
        :param gas: Gas to be used for all contract interactions (optional)
        :param fee: fee to be used for all contract interactions (optional)
        :param gas_price: Gas price to be used for all contract interactions (optional)
        :param account: Account to be used for contract deploy and contract calls (optional)
        :param use_dry_run: use dry run for all method calls except for payable and stateful methods (default: True)
        """
        if 'client' in kwargs:
            self.contract = Contract(kwargs.get('client'))
        else:
            raise ValueError("client is not provided")
        self.compiler = kwargs.get('compiler', None)
        if self.compiler is None:
            raise ValueError("Compiler is not provided")
        else:
            if isinstance(self.compiler, str):
                self.compiler = compiler.CompilerClient(self.compiler)
        self.source = kwargs.get('source', None)
        if self.source is not None:
            self.bytecode = self.compiler.compile(self.source).bytecode
            self.aci = self.compiler.aci(self.source)
        else:
            raise ValueError("contract source not provided")

        self.contract_name = self.aci.encoded_aci.contract.name
        self.gas = kwargs.get('gas', defaults.CONTRACT_GAS)
        self.gas_price = kwargs.get('gas_price', defaults.CONTRACT_GAS_PRICE)
        self.fee = kwargs.get('fee', defaults.FEE)
        self.contract_amount = kwargs.get('amount', defaults.CONTRACT_AMOUNT)
        self.use_dry_run = kwargs.get('use_dry_run', True)

        address = kwargs.get('address', None)
        if address:
            self.at(address)

        self.account = kwargs.get('account', None)
        if self.account and type(self.account) is not signing.Account:
            raise TypeError(
                "Invalid account type. Use `class Account` for creating an account"
            )
        self.sophia_transformer = SophiaTransformation()
        self.__generate_methods()
def contract_deploy(contract_file, gas):
    """
    Deploy a contract to the chain and create a deploy descriptor
    with the contract informations that can be use to invoke the contract
    later on.

    The generated descriptor will be created in the same folde of the contract
    source file. Multiple deploy of the same contract file will generate different
    deploy descriptor
    """
    try:
        with open(contract_file) as fp:
            code = fp.read()
            contract = Contract(code, client=_epoch_cli())
            kp, _ = _keypair()
            tx = contract.tx_create(kp, gas=gas)

            # save the contract data
            contract_data = {
                'source': contract.source_code,
                'bytecode': contract.bytecode,
                'address': contract.address,
                'transaction': tx.tx_hash,
                'owner': kp.get_address(),
                'created_at': datetime.now().isoformat('T')
            }
            # write the contract data to a file
            deploy_descriptor = f"{contract_file}.deploy.{contract.address[3:]}.json"
            with open(deploy_descriptor, 'w') as fw:
                json.dump(contract_data, fw, indent=2)
            _pp([
                ("Contract address", contract.address),
                ("Transaction hash", tx.tx_hash),
                ("Deploy descriptor", deploy_descriptor),
            ])
    except Exception as e:
        print(e)
def test_evm_broken_contract_call():
    contract = Contract(broken_contract, Contract.EVM)
    with raises(AException):
        result = contract.call('IdentityBroken.main', '1')
        print(result)
def test_evm_broken_contract_compile():
    contract = Contract(broken_contract, Contract.EVM)
    with raises(AException):
        result = contract.compile('')
def test_evm_encode_calldata():
    contract = Contract(aer_identity_contract, Contract.EVM)
    result = contract.encode_calldata('main', '1')
    assert result is not None
    assert result == 'main1'
def test_evm_contract_call():
    contract = Contract(aer_identity_contract, Contract.EVM)
    result = contract.call('main', '1')
    assert result is not None
    assert result.get('out')
def test_evm_contract_compile():
    contract = Contract(aer_identity_contract, Contract.EVM)
    result = contract.compile()
    assert result is not None
    assert result.startswith('0x')
def test_sophia_contract_compile():
    contract = Contract(aer_identity_contract, Contract.SOPHIA)
    result = contract.compile('')
    assert result is not None
    assert result.startswith('0x')
def test_sophia_broken_encode_calldata():
    contract = Contract(broken_contract, Contract.SOPHIA)
    with raises(ContractError):
        result = contract.encode_calldata('IdentityBroken.main', '1')
        print(result)
def test_sophia_broken_contract_compile():
    contract = Contract(broken_contract, Contract.SOPHIA)
    with raises(ContractError):
        result = contract.compile('')
        print(result)
def test_sophia_encode_calldata():
    contract = Contract(aer_identity_contract, Contract.SOPHIA)
    result = contract.encode_calldata('main', '1')
    assert result is not None
    assert result == 'main1'
def test_sophia_contract_call():
    contract = Contract(aer_identity_contract, Contract.SOPHIA)
    result = contract.call('main', '1')
    assert result is not None
    assert result.out
def test_evm_broken_encode_calldata():
    contract = Contract(broken_contract, Contract.EVM)
    #with raises(AException):
    result = contract.encode_calldata('IdentityBroken.main', '1')
def test_ring_broken_contract_compile():
    contract = Contract(broken_contract, Contract.RING)
    with raises(AException):
        result = contract.compile('')
def test_evm_broken_contract_compile():
    contract = Contract(broken_contract, Contract.EVM)
    with raises(ContractError):
        result = contract.compile('')
        print(result)
def test_ring_broken_contract_call():
    contract = Contract(broken_contract, Contract.RING)
    with raises(AException):
        result = contract.call('IdentityBroken.main', '1')
def test_ring_contract_compile():
    contract = Contract(aer_identity_contract, Contract.RING)
    result = contract.compile('')
    assert result is not None
    assert result.startswith('0x')
def test_sophia_contract_tx_create():
    contract = Contract(aer_identity_contract, Contract.SOPHIA)
    address, tx = contract.tx_create(KEYPAIR, gas=1000)
    assert address is not None
    assert len(address) > 0