Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thor314/near_cross_n_sim_ex

Examples of making cross contract calls with callbacks and simulation tests on NEAR
https://github.com/thor314/near_cross_n_sim_ex

Last synced: 9 days ago
JSON representation

Examples of making cross contract calls with callbacks and simulation tests on NEAR

Awesome Lists containing this project

README

        

* What this:
A alright reference to NEAR's cross contract calls, callbacks, and simulation tests.

** Build the contracts:
=./build.sh= builds c1 and c2 to wasm blobs.

** Deploy the contracts:
0. =./create_accounts $YOURADDRESS= to create some dummy accounts, c1 and c2 for your account. =$YOURADDRESS=
should be a string without any subaccount or =.testnet= suffix.
1. =near login= with your own address
2. =./deploy.sh $YOURADDRESS=. =$YOURADDRESS= should not include =c{1,2}.= or =.testnet=.

* Interact with stuff with simulation tests (Easy Way)
** Prelude motivations
The main purpose of writing this repo was to document NEAR's simulation testing suite in it's most compelling use
case: creating several contracts that interact with one another in complex ways, requiring cross contract calls and
callback logic. The Simulation test suite gives us the tools we need to test our cross contract methods, and get
information about things like gas usage, without writing guess and check scripts, as we do below (this was how this
repo was originally written)

** Simulation test examples
Take a look at =tests/general.rs=. A normal =cargo test= will demonstrate a simulation test. They take a bit longer
than usual tests to run. To get more detailed information (like profiling) on tests that are passing, instead run:

=cargo test -- --nocapture=

* Interact with stuff with scripts (Hard way)
Note that, if you get error messages about gas usage, always refer to the gas usage details in [[https://explorer.testnet.near.org/transactions/2LZDe35vxQB5LCsTLts7Uuu4eDyJ4noHaE6VuVQECUz8][the explorer]] as more
truthy than the error messages. The error messages in the terminal can undershoot the actual necessary gas.
** Interact with the contracts, local-contract logic
In the dirs =c{1,2}/call_view_scripts=, there are scripts to view and modify the contents of each contract field.
=get_X= scripts take one argument, $YOURADDRESS.
=set_X= scripts take two arguments, $YOURADDRESS, $CHANGE_TO_THIS.

** Interact with the contracts, cross-contract-logic
The dir =c1/cross_contract_scripts= mirrors the scripts available in =c2/call_view_scripts=, but calls via
cross-contract calls.

** Modify the state of Contract 1 based on the state of Contract 2 with callbacks
The dir =c1/callback_scripts= extends the some functionality of =cross_contract_scripts= using callbacks to modify
the state of Contract 1 based on stuff in C2.

Call:
=./build.sh=
=./deploy.sh $YOURADDRESS=
=./c1/callback_scripts/SCRIPT $YOURADDRESS=
to demo callbacks at work.

* Sidequest: Deploy Contract 2 from Contract 1
Because we wanted to have contracts 1 and 2 live at addresses =c1.YOURADDRESS.testnet= and =c2.YOURADDRESS.testnet=
respectively, we haven't had the opportunity to demonstrate deploying contract 2 from contract 1. For this, c1 has
to live at address =YOURADDRESS.testnet=, not =c1.YOURADDRESS.testnet=.

Try deploying Contract 1 to that address, and call script:

=./c1/cross_contract_scripts/deploy_con2_to.sh $YOURADDRESS $SUBADDRESS=.

to give that a go.