📜  aeternity sophia contarct call value (1)

📅  最后修改于: 2023-12-03 15:29:18.450000             🧑  作者: Mango

Aeternity Sophia contract call value - an introduction for programmers

Aeternity Sophia is a powerful smart contract language designed to enable developers to build decentralized applications on the Aeternity blockchain. One important aspect of developing smart contracts is being able to call contract functions and manipulate data within the contract. In this article, we will explore how to call a contract function and return a value using Sophia.

What is a contract call value?

A contract call is simply a request to execute a function within a contract. A contract call value is the data that is returned as a result of calling a function. This value can be of any type defined within the contract, including integers, strings, and tuples.

How to call a contract function

To call a function within a Sophia contract, you first need to have the address of the contract. This can be obtained from the blockchain explorer or by deploying the contract using a Sophia compiler such as Aeproject. Once you have the contract address, you can use the Call function to instantiate the contract and call its functions. Here is an example of how to call a contract function that returns an integer value:

my_contract_address = "ct_2abQqhAmVGVt3d5ypbLQXc88NzRb8gz5tjzKSx6cBwGuapwX5z"
my_contract_instance = Chain.contract(my_contract_address)

result = my_contract_instance.my_function(arg1, arg2)

In the code above, we first define the contract address as a string. We then create a contract instance using this address and the Chain.contract function. Finally, we call the my_function function of the contract instance and pass in the required arguments. The returned value is stored in the result variable.

Returning a contract call value

To return a value from a contract function, you simply need to define the return type of the function and use the return keyword to return the value. Here's an example:

contract my_contract =
  function my_function(arg1: int, arg2: string) : (int) =
    let result = arg1 + string.length(arg2)
    return(result)

In the code above, we define a my_function function that takes in an integer and a string as arguments and returns an integer. We calculate the length of the string argument and add it to the integer argument to get the result. We then use the return keyword to return this result.

Conclusion

In this article, we've covered the basics of calling a function and returning a value in Aeternity Sophia smart contracts. By understanding these concepts, you can build more powerful and sophisticated smart contracts that can interact with the Aeternity blockchain.