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

Remove unnecessary read only methods #15

Open
radicleart opened this issue Jun 5, 2024 · 3 comments
Open

Remove unnecessary read only methods #15

radicleart opened this issue Jun 5, 2024 · 3 comments

Comments

@radicleart
Copy link
Owner

According to Clarity WG we should favour map-get / var-get api calls over read-only functions which return the same information as the contract footprints are lighter.

@whoabuddy
Copy link

I agree with this in general but do see the value in creating aggregated read-only functions that expose a set of variables.

This serves as an external lookup for UIs and other contracts that cannot read these values directly.

For example, if you have:

;; tracking counts for each map
(define-data-var userCount uint u0)
(define-data-var resourceCount uint u0)
(define-data-var invoiceCount uint u0)
;; tracking overall contract revenue
(define-data-var totalRevenue uint u0)

Then instead of a one-off read only function exposing the variable like this:

;; returns total registered users
(define-read-only (get-total-users)
(var-get userCount)
)

We could return a single tuple instead:

(define-read-only (get-contract-stats)
  {
    totalUsers: (var-get userCount),
    totalResources: (var-get resourceCount),
    totalInvoices: (var-get invoiceCount),
    totalRevenue: (var-get totalRevenue),
  }
)

Before I'm fully convinced though I'd like to do a cost comparison.

  • while this makes the contract length shorter, how much are we saving by doing so? Is it a significant amount?
  • if using the read-only function from another contract, how much more expensive is it to read the tuple and get the needed values versus calling the individual read-only functions when needed?

Also, looking at the docs for the API, it looks like we can query a map inside a contract but can you read a data-var?

@radicleart
Copy link
Owner Author

Agree with all these points.

You can get a data var through API but it hasn't been documented just raised this here

/v2/data_var/${contractAddress}/${contractName}/${dataVarName}

Contracts can also directly read data-vars from other contracts (according to chatgpt - never tried this myself);

(contract-call? .SP2JNWX37AJFEMZ8JD4X9C7JK3YSQ6QP7K05F7Z8.get-my-var))

Agree comparison of the costs is important.

@whoabuddy
Copy link

I suspect that won't work but we should test, would be another reason to fine tune a model more on stacks/clarity!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants