Skip to content

Commit

Permalink
Merge pull request #47 from dojoengine/loaf/view-functions
Browse files Browse the repository at this point in the history
Loaf/view functions
  • Loading branch information
ponderingdemocritus authored Aug 31, 2023
2 parents 9a4c340 + 0be4158 commit 9cbeee5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 13 deletions.
47 changes: 47 additions & 0 deletions src/cairo/systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,53 @@ The `execute` function is mandatory in a system and runs when called, taking `Co

You are free to add other functions to your system, but they will not be callable from the world. This is useful for breaking up your logic into smaller chunks.

### Using View Functions

There are times when we need to compute the value of a component dynamically, rather than fetching its static state. For instance, in the context of a VRGDA, if you want to ascertain the current price, merely querying the component state won't suffice. Instead, you'd need to compute the price based on certain parameters and the current state.

This is where view functions come into play.

**What are View Functions?**

View functions are a way to derive or compute values from the existing state of a component. They are invoked by the world and receive the current state of the component as an argument. Subsequently, these functions return a computed value based on this state.

**Example from VRGDA**:

The below snippet, taken from the VRGDA example available on [this link](https://github.com/dojoengine/dojo-examples), illustrates how to implement a view function:

```rust,ignore
#[system]
mod view_price {
//... other code ...
fn execute(ctx: Context, game_id: u64, item_id: u128, amount: u128) -> Fixed {
let mut auction = get!(ctx.world, (game_id, item_id), Auction);
// Convert auction to VRGDA
let VRGDA = auction.to_LogisticVRGDA();
// Calculate time since the auction began
let time_since_start: u128 = get_block_timestamp().into() - auction.start_time.into();
// Compute the current price
VRGDA.get_vrgda_price(
FixedTrait::new(time_since_start, false), // Time elapsed since auction start
FixedTrait::new(auction.sold, false) // Quantity sold
)
}
}
```

In this example, the function computes and returns the current price of the VRGDA based on the ongoing state of the auction.

**How to Invoke View Functions?**

- **Using Dojo Core**: If you are working within the [Dojo Core](../client/npm/core.md), utilize the `call` function.

- **For Rust Users**: The [Starkli](https://book.starkli.rs/) library provides a handy method to invoke view functions in Rust.

I hope this revised version enhances the clarity and flow of the information you want to convey!

### System Authentication

Systems must be given permission to write to components. By default they have no permissions. With `sozo` we can however give them permissions to write to components.
Expand Down
15 changes: 4 additions & 11 deletions src/client/npm/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,21 @@ import { Account, num } from "starknet";
import { GraphQLClient } from 'graphql-request';
import { getSdk } from '../generated/graphql';

export const WORLD_ADDRESS = import.meta.env.VITE_PUBLIC_WORLD_ADDRESS!

export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;

export async function setupNetwork() {

const client = new GraphQLClient('http://localhost:8080');

const graphSdk = getSdk(client);

const contractComponents = defineContractComponents(world);

const provider = new RPCProvider(WORLD_ADDRESS);
const provider = new RPCProvider(import.meta.env.VITE_PUBLIC_WORLD_ADDRESS, import.meta.env.VITE_PUBLIC_NODE_URL);

return {
contractComponents,
contractComponents: defineContractComponents(world),
provider,
execute: async (signer: Account, system: string, call_data: num.BigNumberish[]) => provider.execute(signer, system, call_data),
entity: async (component: string, query: Query) => provider.entity(component, query),
entities: async (component: string, partition: number) => provider.entities(component, partition),
world,
graphSdk
graphSdk: getSdk(new GraphQLClient(import.meta.env.VITE_PUBLIC_TORII)),
call: async (selector: string, call_data: num.BigNumberish[]) => provider.call(selector, call_data),
};
}
```
44 changes: 42 additions & 2 deletions src/deployment/remote.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
## Deployment to remote network
## Deployment to Remote Network

> *IMPORTANT: Dojo is unaudited. Use at your own risk.*
Dojo makes it easy to deploy to remote networks, you just need to have a valid account and network endpoint.

Scarb.toml

```toml
[package]
name = "ohayoo"
version = "0.1.0"
cairo-version = "2.1.1"

[cairo]
sierra-replace-ids = true

[dependencies]
dojo = { git = "https://github.com/dojoengine/dojo.git" }

# # Katana
# rpc_url = "http://localhost:5050"
# account_address = "0x03ee9e18edc71a6df30ac3aca2e0b02a198fbce19b7480a63a0d71cbd76652e0"
# private_key = "0x0300001800000000300000180000000000030000000000003006001800006600"

#Madara
rpc_url = "https://api.cartridge.gg/x/shinai/madara"
account_address = "0x2"
private_key = "0xc1cf1490de1352865301bb8705143f3ef938f97fdf892f1090dcb5ac7bcd1d"
#world_address = "0x5b328933afdbbfd44901fd69a2764a254edbb6e992ae87cf958c70493f2d201"
```

### Remote Katana

Katanas are able to be hosted and run as remote testnets, however this is not recommended for production use.
Expand All @@ -13,7 +41,19 @@ __todo__: add instructions for deploying to remote katana

[Madara](https://github.com/keep-starknet-strange/madara) is a blazinly fast Starknet sequencer. Built on the robust Substrate framework and fast, thanks to Rust 🦀, Madara delivers unmatched performance and scalability to power your Starknet-based Validity Rollup chain.

__todo__: add instructions for deploying to remote Madara
A public Madara testnet is available for deployment:

**Testnet RPC:** https://api.cartridge.gg/x/shinai/madara

You can use the following account to deploy:

```toml
# ...rest of Scarb.toml

rpc_url = "https://api.cartridge.gg/x/shinai/madara"
account_address = "0x2"
private_key = "0xc1cf1490de1352865301bb8705143f3ef938f97fdf892f1090dcb5ac7bcd1d"
```


### Starknet
Expand Down

0 comments on commit 9cbeee5

Please sign in to comment.