3.1.0
-> 4.0.0
near-sdk-rs
Migration guide
#829
austinabell
started this conversation in
General
Replies: 1 comment
-
#[handle_result] is making my contracts so much cleaner! Great work on all the updates! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This document will note the primary breaking changes and additions for this most recent stable release. These sections will be ordered roughly in order of what will be most to least breaking.
This will not cover everything, as most additions/changes/optimizations do not affect migrating from an older version. The changelog can be used if a change is not listed here, or feel free to comment on what was not documented so that it can be added.
Validated
AccountId
by defaultPreviously was
AccountId
, which was an alias toString
, andValidAccountId
was a wrapper around this for validation. Now there is just one type. This type will have the sameborsh
andJSON
serialization format, except that it is validated on deserialization.If you are coming from a
String
, you have two options:Validation of the string will still happen in
debug
to avoid bugs in testing, but will not be validated (until used in the runtime) when compiling in release mode.If you have a
&str
, you can use:For more information, see the Rustdocs for
AccountId
PublicKey
is now its own type no longer an alias ofVec<u8>
Similar to the above changes to
AccountId
, this change is to avoid requiring using theBase58PublicKey
type to deserialize it correctly as JSON. This type will have the sameborsh
serialization format as both the oldPublicKey
andBase58PublicKey
types. The JSON format of this type will matchBase58PublicKey
.For more information on this type, see the docs.
Updated cross-contract call API
NEP264 allows for utilizing unspent gas for future async cross-contract calls. This enables gas to be used more efficiently but also enables a much cleaner API design.
The details of the change are defined in the API proposal issue.
The main things to know about this updated API is that for a trait annotated with
#[ext_contract]
:What was previously called with:
Which requires one to know the ordering of each parameter and how they combined, can now be:
Where each
with_*
method on this will modify the context the method is called with, but are all optional.The default behavior, if none of the optional parameters are included, is splitting unused gas from the current function execution among all scheduled function calls.
If you only want a static amount of gas to be attached (which is the same as the previous behavior), you can just use
with_unused_gas_weight(0).with_static_gas(Gas(<amount of gas>))
. Otherwise, you can adjust the unused gas weight for each function call if you want it to use more of the unused gas by increasing the weight, which defaults at1
.This API allows you to only specify what you want to configure, which also has a reasonable default that will just work for simple cases where you don't need to optimize where gas is allocated with multiple cross-contract calls.
Another addition is that by default this API will be attached to the contract struct annotated with
#[near_bindgen]
which avoids the requirement to re-define the interface with#[ext_contract]
. The method that will be attached to the struct is the same asext_contract
asext(..)
:And can be re-used within or external to the current crate so that the function signatures are connected at compile-time and remove the need to redefine interfaces twice.
More ergonomic log and panic API
Previously, the parameter for
near_sdk::env::log
andnear_sdk::env::panic
was bytes (&[u8]
) despite the requirement through the NEAR runtime to be valid utf-8. Both of these are now soft deprecated in favor ofnear_sdk::env::log_str
andnear_sdk::env::panic_str
, which both take&str
.This comes at no cost from the previous API, and there is no requirement to change if the deprecation warnings can be ignored.
The common difference is that instead of
env::log(b"some log")
you will now writeenv::log_str("some log")
and same for panic.Added fixed-length bytes alternatives to
near_sdk::env
For functions that always returned a fixed length of bytes, improved APIs that do not allocate a
Vec<u8>
were added. The following were added as alternatives (none have been deprecated yet):keccak256
:keccak256_array
keccak512
:keccak512_array
random_seed
:random_seed_array
sha256
:sha256_array
Function call error handling
An optional addition to the API is handling errors from
#[near_bindgen]
functions, which will panic with astr
throughnear_sdk::env::panic_str
.This is only a breaking change if you were previously serializing a
Result<_, _>
to JSON as a successful serialized return. If you would like to continue this pattern, you can follow the pattern below, but have a nestedResult
as theOk
type (Result<Result<_, _>, _>
).Avoid unwrapping and panic patterns in favor of cleaner error handling:
This desugars to calling
env::panic_str
on error or serializing and returningOk
value.#[handle_result]
annotation will only be temporarily required for version4.0
to avoid silently breaking the case mentioned above. The annotation will not be needed in5.0
when that comes.Callback result error handling
This addition avoids having to manually check the success of promises by using
Result
types with#[callback_result]
annotation:The error type must be
PromiseError
from the SDK, which indicates the possible error cases of the callback.With this change,
#[callback_unwrap]
was added, which is the exact same functionality as#[callback]
but was added to be more clear about what it's doing and so that#[callback]
can be removed/changed in a future version (there is no way to deprecate the annotation).Deprecated
near_sdk::setup_alloc!()
It is now initialized by default. Disable the
wee_alloc
feature on the SDK to remove.Now, this macro is a no-op.
Cleaner low-level syscall interface
Removed need to go through
BLOCKCHAIN_INTERFACE
:is now:
This type is no longer exported, as there is no need to interact with
BLOCKCHAIN_INTERFACE
directly.By extension of this, there is no need to set this interface with
env::set_blockchain_interface(Box::new(near_blockchain::NearBlockchain {}));
if writing a contract without#[near_bindgen]
as this is only used for testing (nonwasm32-unknown-unknown
targets) and already is defaulted.Updated testing APIs
Because the mocked interface is set as default for testing environments, as mentioned in the section above, there is no need to set it with some context with
testing_env!
orenv::set_blockchain_interface
unless you need to modify a specific context for testing.Various types were modified/refactored to limit the connection with inner
nearcore
crates types. Most changes should not require any migration, but some APIs have been changed to be more correct or streamlined. If any do require migration, scan the changelog for what has specifically changed.Another small change is that
MockedBlockchain
is no longer required to be imported for testing, which should not have been previously required. If you need to interact with or modify theMockedBlockchain
, consider usingnear_sdk::mock::with_mocked_blockchain
.Removed unused proc macro attributes from
near-sdk
Since annotations are handled manually within
#[near_bindgen]
there was no need to import thecallback
,callback_vec
,result_serializer
,init
no-op macros. #770For this, the only thing that needs to change is to remove the import of these functions if for some reason they are imported. Do not remove any of the annotations from the code, they function just as they did previously.
Collections updates
unstable
feature flag on the SDKnear-sdk = { version = "4.0.0", features = ["unstable"] }
legacy
feature flagCore differences from
near_sdk::collections
std::collections
UnorderedMap
,TreeMap
,UnorderedSet
and non-state breaking optimizations for othersAvoids manual patterns such as manually moving values, modifying, and re-inserting back into data structures:
Can now be replaced with:
Known downsides
near-contract-standards
are still currently usingnear_sdk::collections
UnorderedMap
,TreeMap
,UnorderedSet
)Beta Was this translation helpful? Give feedback.
All reactions