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

doc: examples for Near-related host functions #1259

Merged
merged 4 commits into from
Nov 28, 2024

Conversation

denbite
Copy link
Contributor

@denbite denbite commented Nov 18, 2024

Make it straightforward for developers how host functions can be imported and used

The final markdown doc page will appear as follows
image

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.16%. Comparing base (22f3500) to head (90b43a7).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1259   +/-   ##
=======================================
  Coverage   80.16%   80.16%           
=======================================
  Files         102      102           
  Lines       14598    14598           
=======================================
  Hits        11703    11703           
  Misses       2895     2895           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dj8yfo
Copy link
Collaborator

dj8yfo commented Nov 19, 2024

@denbite please fix compilation errors. There's at least one, where NearToken::from_yocto is used instead of NearToken::from_yoctonear

@denbite
Copy link
Contributor Author

denbite commented Nov 19, 2024

@dj8yfo done, please review

@dj8yfo dj8yfo changed the title Add examples for Near-related host functions doc: examples for Near-related host functions Nov 20, 2024
@denbite
Copy link
Contributor Author

denbite commented Nov 21, 2024

@dj8yfo also just added a little text about the purpose of functions like storage_write, storage_read, etc, with a reference to the real-life example
must be helpful for devs to understand that these methods are typically used to upgrade/migrate state of a contract

@dj8yfo
Copy link
Collaborator

dj8yfo commented Nov 25, 2024

@race-of-sloths score 13

@race-of-sloths
Copy link

race-of-sloths commented Nov 25, 2024

@denbite Thank you for your contribution! Your pull request is now a part of the Race of Sloths!
Weekly streak is on the road, smart strategy! Secure your streak with another PR!

Shows inviting banner with latest news.

Shows profile picture for the author of the PR

Current status: executed
Reviewer Score
@dj8yfo 13

Your contribution is much appreciated with a final score of 13!
You have received 155 (130 base + 15 weekly bonus + 10 monthly bonus) Sloth points for this contribution

@dj8yfo received 25 Sloth Points for reviewing and scoring this pull request.

Congratulations @denbite! Your PR was highly scored and you completed another monthly streak! To keep your monthly streak make another pull request next month and get 8+ score for it

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths and receive a reward
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

Copy link
Collaborator

@dj8yfo dj8yfo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@denbite it felt somewhat overwhelming to request changes for all found suggestions
with current pr not introducing anything particularly erroneous,

so i'll extract those to an issue in case someone volunteers to further improve docs by adding links

Comment on lines +185 to +190
/// # Examples
/// ```
/// use near_sdk::env::signer_account_pk;
/// use near_sdk::{PublicKey, CurveType};
///
/// let pk = PublicKey::from_parts(near_sdk::CurveType::ED25519, vec![0; 32]).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest using ```no_run annotation
and using a public key, which looks real,
and not the one tied to MockedBlockchain::default() result,
( equivalent to calling testing_env!(VMContextBuilder::new().build()); )

Comment on lines +223 to +227
/// ```
/// use near_sdk::env::input;
///
/// assert_eq!(input(), Some(Vec::new()));
/// ```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// ```
/// use near_sdk::env::input;
///
/// assert_eq!(input(), Some(Vec::new()));
/// ```
/// ```no_run
/// use near_sdk::env::input;
///
/// // contract had some input
/// assert_eq!(input(), Some(vec![0u8, 31, 54, 38, 22]));
/// ```

/// ```
/// use near_sdk::env::block_index;
///
/// assert_eq!(block_index(), 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest using ```no_run and some realistic value from current testnet/mainnet

/// ```
/// use near_sdk::env::block_height;
///
/// assert_eq!(block_height(), 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest using ```no_run and some realistic value from current testnet/mainnet

/// ```
/// use near_sdk::env::block_timestamp;
///
/// assert_eq!(block_timestamp(), 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest using ```no_run and some realistic value from current testnet/mainnet

@@ -948,6 +1721,19 @@ pub fn storage_read(key: &[u8]) -> Option<Vec<u8>> {
}
/// Removes the value stored under the given key.
/// If key-value existed returns `true`, otherwise `false`.
///
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

///
/// # Use cases
/// Storage functions are typically used to upgrade/migrate a contract state, preventing errors like `Cannot deserialize the contract state` after rolling out the breaking changes to the network.
/// For practical examples, see different implementations in [this repository](https://github.com/near-examples/update-migrate-rust).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this call isn't used in the referenced repo, and also in a pending pr near-examples/update-migrate-rust#2
won't request change to remove this link, hoping that this and some of other suggestions will be adressed in some of subsequent prs

@@ -956,10 +1742,34 @@ pub fn storage_remove(key: &[u8]) -> bool {
}
}
/// Reads the most recent value that was evicted with `storage_write` or `storage_remove` command.
///
/// # Use cases
/// Storage functions are typically used to upgrade/migrate a contract state, preventing errors like `Cannot deserialize the contract state` after rolling out the breaking changes to the network.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this call isn't used in the referenced repo, and also in a pending pr near-examples/update-migrate-rust#2
won't request change to remove this link, hoping that this and some of other suggestions will be adressed in some of subsequent prs

///
/// # Use cases
/// Storage functions are typically used to upgrade/migrate a contract state, preventing errors like `Cannot deserialize the contract state` after rolling out the breaking changes to the network.
/// For practical examples, see different implementations in [this repository](https://github.com/near-examples/update-migrate-rust).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this call isn't used in the referenced repo, and also in a pending pr near-examples/update-migrate-rust#2
won't request change to remove this link, hoping that this and some of other suggestions will be adressed in some of subsequent prs

@@ -996,7 +1806,14 @@ pub fn state_exists() -> bool {

/// Price per 1 byte of storage from mainnet genesis config.
/// TODO: will be using the host function when it will be available.

///
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dj8yfo dj8yfo merged commit e0a9b54 into master Nov 28, 2024
38 checks passed
@dj8yfo dj8yfo deleted the update_documentation_for_near_host_functions branch November 28, 2024 15:00
@dj8yfo dj8yfo restored the update_documentation_for_near_host_functions branch November 28, 2024 15:01
@frol frol mentioned this pull request Nov 28, 2024
@dj8yfo dj8yfo mentioned this pull request Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Shipped 🚀
Development

Successfully merging this pull request may close these issues.

3 participants