Skip to content

Commit

Permalink
feat(docs): add cosmpy missing docs (#131)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Croft <[email protected]>
Co-authored-by: josh <[email protected]>
  • Loading branch information
3 people authored Sep 19, 2023
1 parent 4569a62 commit 4b917a7
Show file tree
Hide file tree
Showing 13 changed files with 2,611 additions and 2 deletions.
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

0 comments on commit 4b917a7

Please sign in to comment.