Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): add cosmpy missing docs #131

Merged
merged 13 commits into from
Sep 19, 2023
Merged
4 changes: 3 additions & 1 deletion pages/guides/fetch-network/cosmpy/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
"querying-address-balances": "Querying Balances \uD83D\uDD0D",
"creating-wallet": "Wallets and Private Keys \uD83D\uDCB8\uD83D\uDD10",
"transaction-broadcast": "Sending funds \uD83D\uDCB8",
"staking": "Staking \uD83D\uDCB0"
"staking": "Staking \uD83D\uDCB0",
"smart-contracts": "Smart contracts",
"use-cases": "Use cases"
}
67 changes: 67 additions & 0 deletions pages/guides/fetch-network/cosmpy/smart-contracts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Smart contracts

## Deploy a smart contract

CosmPy offers the possibility to easily deploy **smart contracts** using `LedgerContract` object. For this, you will need the **path** to where the contract is stored (in this case `simple.wasm`), a [`LedgerClient` ↗️](/guides/fetch-network/cosmpy/establishing-node-connection) and a [Wallet ↗️](/guides/fetch-network/cosmpy/creating-wallet):

```py copy
from cosmpy.aerial.contract import LedgerContract

PATH = "contracts/simple/simple.wasm"

contract = LedgerContract(PATH, ledger_client)
contract.deploy({}, wallet)
```

## Interact with smart contracts

We can now start interacting with any smart contract in different ways by executing one of the following operations.

### Retrieve contract address

After deployment, you can obtain the address of the deployed contract on the network using the `address` method in the following way:

```py copy
print(f"Contract deployed at: {contract.address}")
```

### Query contract's state variables

We can also query the values of the contract's state variables using the `query` method and provide a dictionary specifying the query information:

```py copy
result = contract.query({"get": {"owner": wallet}})
print("Initial state:", result)
```

### Set contract's state variables

We can update the contract's state variables by using the `execute` method and by providing a dictionary specifying the update wanted. Use the wait_to_complete() method to wait for the execution to finish.

The following code sets the state variable `value` to `foobar`:

```py copy
contract.execute({"set": {"value": "foobar"}}, wallet).wait_to_complete()
```

Once you set a state variable to its updated value, you can query it again to confirm such changes took place effectively. Considering the example above, we can check if changes were set correctly in the following way:

```py copy
result = contract.query({"get": {"owner": wallet)}})
print("State after set:", result)
```

### Clear state variables

We can clear the contract's state variables using the `execute` method in the following way:

```py copy
contract.execute({"clear": {}}, wallet).wait_to_complete()
```

And check if operations went smoothly by running:

```py copy
result = contract.query({"get": {"owner": wallet}})
print("State after clear:", result)
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

CosmPy simplifies broadcasting transactions. After [creating a wallet ↗️](/guides/fetch-network/cosmpy/creating-wallet.mdx) you can send transactions by following the provided example. In it we first connect to the desired network then provide the desired destination address. The transaction is then broadcast using the `Ledger_client.send_tokens` function:

```py copy
```py copy
from cosmpy.aerial.client import LedgerClient, NetworkConfig

# Establishing connection to the network
Expand Down
8 changes: 8 additions & 0 deletions pages/guides/fetch-network/cosmpy/use-cases/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"stake-auto-compounder": "Stake auto-compounder️",
"stake-optimizer": "Stake optimizer️",
"oracles": "Oracles \uD83D\uDD2E",
"wallet-top-up": "Wallet top-up \uD83D\uDCB5",
"liquidity-pool": "Liquidity pool",
"swap_automation": "Swap automation \uD83D\uDD04"
}
Loading