This tutorial showcases how to interact with the SimpleExchange dApp in a local environment, as well as how to deploy the contract in the Agoric Devnet.
The tutorial page is structured as follows:
- Pre-requisites
- Contract Testing
- Unit tests
- Integration tests
- Smoke tests
- Swingset tests
- Contract Deployment
- Launch UI
- Follow the installing the Agoric SDK guide to install the Agoric Software Development Kit (SDK);
- Important: instead of using the community-dev branch, you need to check out the following revision:
92b6cd72484079b0349d8ccfa4510aeb820e8d67, which is the one used on Devnet at the moment that this component was developed.
- Important: instead of using the community-dev branch, you need to check out the following revision:
go version # go version go1.20.6 darwin/arm64
node --version # v18.18.0
npm --version # 9.8.1
yarn --version # 1.22.5
# inside agoric-sdk folder
`git checkout 92b6cd72484079b0349d8ccfa4510aeb820e8d67`
yarn install && yarn build
agoric --version # 0.21.2-u11.0- Clone the SimpleExchange repository and install the Agoric dependencies by running the following commands:
cd simple-exchange/contract
agoric installWe covered the most important cases in unit tests, making sure that the exchange contract works as expected. The testing framework used is Ava, and we build a set of tools that make the testing environment cleaner and easier to scale.
We include 2 versions of unit tests, one for the basic and the other for the upgradable version of the contract. Both of them contain the exact same test cases, but each of them reflects the difference when deploying, configuring, and communicating with the durable and non-durable contract versions :
./contract/test/unitTests/test-simpleExchange.js./contract/test/unitTests/test-upgradableSimpleExchange.js
Unit tests include the following test cases:
- make sell offer - check that the contract properly handles incoming sell offer
- make buy offer - check that the contract properly handles incoming buy offer
- make trade - check that the contract properly executes the exchange when sell and buy offers can satisfy each other
- make offer with wrong issuers - check that offer with the wrong issuer fails
- make offer with offerProposal missing attribute - check that offer with wrong offerProposal attributes fails
- make offer without offerProposal - check that offer without offerProposal fails
- offers with null or invalid shapes on the proposals - check with different invalid shapes set that each of them fails
- make offer with NFT - check that NFTs can be traded on the exchange properly
- make offer with misplaced issuers - check if the contract refuses an order with a mismatch in the keyword-issuer pair
- make trade with surplus assets in Alice's payout - check if the contract returns the expected surplus when an exchange is done
To run unit tests, just run the following command:
cd simple-exchange/contract
yarn unit-test-basic
yarn unit-test-upgradableThis will run all test cases in each file. You will see a lot of debug console logs from Agoric SDK, including some errors - these errors are triggered by unit tests to assert the failing branches. In total 16 unit tests should pass, 8 tests for basic contract and 8 tests for durable contract.
The integration test is setting up a testing environment with 2 different smart wallets and checks that simpleExchange contract can execute a successful exchange. In order to run unit tests, follow these instructions and then run this command:
cd simple-exchange/contract
yarn integration-testThe swingset tests are meant to check that simpleExchange contract upgradeability works as expected. In this case, it means that the orderBook state and the subscriber object survive a contract upgrade.
The swingset test includes 3 test cases:
- null-upgrade - check that the contract null-upgrade is executed successfully
- null-upgrade-orderBook - check that the upgrade is executed successfully and the order book is preserved
- null-upgrade-exchange - check that the contract can execute a match an order made before the upgrade with one made after it.
To run Swingset tests, just run the following command:
cd simple-exchange/contract
yarn swingset-testThe smoke tests intend to build the core-eval and deploy the simpleExchange contract in a local chain, and interact with it using shell scripts.
Open a new terminal and run these commands:
agd keys add gov1 --keyring-backend=test
agd keys add gov2 --keyring-backend=testNote: save the mnemonic phrase printed in the terminal in a safe place. They will be useful if you wish import this account to the Keplr wallet.
In the same terminal as above run these commands:
cd agoric-sdk/packages/inter-protocol/scripts
./start-local-chain.shIf you get the following error message FATAL ERROR: listen EADDRINUSE: address already in use :::9464, stop the process and execute the following commands:
lsof -i :9464
kill <PID>In a second terminal run these commands:
cd agoric-sdk/packages/cosmic-swingset
make SOLO_COINS='13000000ubld,12345000000000uist,1122000000ibc/toyusdc' scenario2-run-clientFor this section, the first step is to make sure that the SDK_ROOT path, in the Makefile is pointing to your agoric-sdk.
If so, open a third terminal and run these commands:
cd simple-exchange/contract
make bundle-contractNote: your terminal will print a message similar to the one below, make sure to copy the bundle IDs (b1-265...e54.json and b1-60f...509.json) and update the Makefile variables CONTRACT_REF_BUNDLE_ID and MANIFEST_REF_BUNDLE_ID respectively.
...
Remember to install bundles before submitting the proposal:
agd tx swingset install-bundle @/Users/jorgelopes/Documents/GitHub/Agoric/bytepitch-bounties/simple-exchange/contract/cache/b1-180be6a3be174c957d853e862801c3643a2897ef3e96993507fe6a02ad57d33806754bf2bc73bb2664f8536861e42d46139971f9cc85724abed9408ba1b57e8a.json
agd tx swingset install-bundle @/Users/jorgelopes/Documents/GitHub/Agoric/bytepitch-bounties/simple-exchange/contract/cache/b1-b2bb6209e466d1d7298523776513ce11c57c2eef47e52e050b5f40c2e056a03ae350c2328ad05b748ae8ee503e94c4b9ebf974aea3202643d0c7377bbe582aea.json
After making the adjustments mentioned above, run the following command in the same terminal:
make submit-core-evalNow you should see the success message in the chain logs, and also, be able to query the contract instance in the agoricNames.
Note that if you plan to execute this command more than once, you need to overwrite the variable VOTE_PROPOSAL accordingly.
Before executing the offer scripts, it is necessary to update the assets and reference lists with the respective board IDs, for that run the following commands:
cd simple-exchange/contract/test/smokeTests/
agoric deploy updateAssetList
agoric deploy updateReferenceListAfter completing the process above, you can now make sell and buy orders by running each of the following scripts:
cd simple-exchange/contract/test/smokeTests/
./sellOffer.sh
./buyOffer.shThere are 2 methods to easily query the contract state of the order book:
- Agoric Wallet REPL
- get instance from agoricNames
- get publicFacet
- get subscriber
- get updated state
- Storage Viewer
- load published children's keys
- load simpleExchange data
This chapter contains code and instructions for submitting a swingset.CoreEval proposal to add the simpleExchange dApp to the Agoric Devnet.
The build the core-eval it was required to first prepare the simpleExchange-proposal and proposalBuilder-script. A Makefile was also built to facilitate the process of deploying the contract.
Open a new terminal and run the following commands:
cd simple-exchange/contract
make bundle-contractThis target will generate the startSimpleExchange and startSimpleExchange-permit files in the project root, as well as the contract and manifest bundles.
Note: your terminal will print a message similar to the one below, make sure to copy the bundle IDs (b1-265...e54.json and b1-60f...509.json) and update the Makefile variables CONTRACT_REF_BUNDLE_ID and MANIFEST_REF_BUNDLE_ID respectively.
See Smoke tests for more details
One wallet is needed for submitting the proposal, for that you can run the following command on the terminal:
agd keys add govSet up a smart-wallet and request funds at the devnet faucet, using the address from the previous step.
To install the bundles follow these instructions in the same terminal as above
make install-bundles-devVerify the bundle deployment by running the following command:
agd query vstorage data bundles --node='https://devnet.rpc.agoric.net:443' --chain-id='agoricdev-23'The next step is to submit the proposal, to do that run the following command:
make submit-proposal-devVerify that the proposal was submitted by running the following command:
agd query gov proposals --node='https://devnet.rpc.agoric.net:443' --chain-id='agoricdev-23' --output json | jq -c '.proposals[] | [.proposal_id,.voting_end_time,.status]';You can also follow the state of your proposal on the Agoric devnet explorer.
To deploy a contract on devnet, the proposal needs to pass the voting stage. So it is advised to inform the community on the devnet channel on Agoric discord in advance, that you plan to submit a proposal, so they can vote on it.
To launch the simpleExhanghe UI the first step is to update the RPC address and chain ID according to where the contract was deployed.
At the store.js file, line 5, update the rpcAddr and chainId passed to the makeAgoricChainStorageWatcher.
If you wish to run the application on:
- local chain -
('http://localhost:26657', 'agoriclocal') - devnet -
('https://devnet.rpc.agoric.net:443', 'agoricdev-23')
Then, open a new terminal and run the following commands:
cd simple-exchange/ui
yarn
yarn run devNow, you can open your browser and go to http://localhost:5173/#trade
In this guide, we'll demonstrate how to test multi-user functionality using a wallet extension like Keplr. We'll use two different users, Bob and Emma, to show how transactions and orders appear for separate accounts.
- Keplr wallet extension installed in your browser.
- Access to a local server (
http://localhost:5173/#trade). - Two sets of government keys for wallet setup.
- Open a new browser or a new user profile.
- Open the Keplr wallet extension.
- Click on 'Import existing wallet'.
- Choose 'Use Recovery Phrase or Private Key'.
- Select the '24 words' option and insert the first government key (gov1).
- Navigate to
http://localhost:5173/#tradeand connect to the wallet.
- Place a sell order, e.g., sell
0.002 BLDfor0.001 IST. - If the transaction is successful, check the 'Order Book' and 'Your Orders' tab to see your order.
- Open a new browser or user profile different from Bob's.
- Follow the same steps to set up the wallet, but use the second government key (gov2).
- Connect to
http://localhost:5173/#tradewith the new wallet.
- As Emma, you will see Bob's sell order in the 'Order Book'.
- However, it won't appear in Emma's 'Your Orders' tab as it belongs to Bob.
- Place a sell order, like
0.001 BLDfor0.03 IST. - Upon successful transaction, this order will appear in both the 'Order Book' and Emma's 'Your Orders' tab.
- As Bob, create a buy order matching Emma's sell order, for instance,
0.03 ISTfor0.001 BLD. - If the orders match, they will disappear from the 'Order Book'.
- The matched order will no longer be visible in either user's 'Your Orders' tab.
Through these steps, you can simulate a trading scenario with two different users. This demonstrates how orders are specific to each user's wallet and how matched orders are processed and removed from the system.
