This is an example of an escrow contract written in JavaScript for the NEAR blockchain.
- Node.js ~= 16.16.0
- Install dependencies
npm install
- Build the contract
npm run build
- Run the tests
npm test
-
How to write a smart contract in JavaScript
a. How to perform cross-contract calls and pass data among them in JavaScript
-
How to write integration tests for a smart contract in JavaScript
- User A purchases a product in-escrow from User B
- User A purchases a product in-escrow from User B and cancels the purchase
- User A purchases a product in-escrow from User B while User B owns no products
- User A purchases a product in-escrow from User B and approves the purchase
- User A purchases a product in-escrow from User B and a day passes without approval
- User A purchases a product in-escrow from User B and attempts to transfer the product to User C
your-testnet-account-id # <-- Your usual testnet account
your-escrow-testnet-account-id # <-- The testnet account holding this escrow contract/program
your-assets-testnet-account-id # <-- The testnet account holding the assets contract/program
your-asset-owner-account-id # <-- The testnet account that owns the assets seeking $NEAR for them
your-buyer-account-id # <-- The testnet account that owns $NEAR seeking to buy assets
- Login to your NEAR account
near login
- Create sub accounts for deploying the contracts
near create-account --accountId <your-escrow-testnet-account-id> --masterAccount <your-testnet-account-id> --initialBalance <your-escrow-testnet-account-balance>
near create-account --accountId <your-assets-testnet-account-id> --masterAccount <your-testnet-account-id> --initialBalance <your-assets-testnet-account-balance>
- Create sub accounts to simulate users executing a transaction
near create-account --accountId <your-asset-owner-account-id> --masterAccount <your-testnet-account-id> --initialBalance <your-asset-owner-account-balance>
near create-account --accountId <your-buyer-account-id> --masterAccount <your-testnet-account-id> --initialBalance <your-buyer-account-balance>
- Deploy the Contracts
near deploy --wasmFile build/escrow.wasm --accountId <your-escrow-testnet-account-id>
near deploy --wasmFile build/assets.wasm --accountId <your-assets-testnet-account-id>
- Initialize the Assets Contract
near call <your-assets-testnet-account-id> init '{"owner_id": "<your-asset-owner-account-id>", "total_supply": "1000", "escrow_contract_id": "<your-escrow-testnet-account-id>", "asset_price": "100000000000000000000000"}' --accountId <your-assets-testnet-account-id>
- Perform a Purchase on Escrow
near call <your-escrow-testnet-account-id> purchase_in_escrow '{"seller_account_id": "<your-asset-owner-account-id>", "asset_contract_id ": "<your-assets-testnet-account-id>"}' --accountId <your-buyer-account-id> --amount 0.11 --gas=300000000000000
- Check the Balance of the Buyer Account
near view <your-assets-testnet-account-id> get_account_assets '{"account_id": "<your-buyer-account-id>"}'
near state <your-asset-owner-account-id>
- Approve the Purchase
near call <your-escrow-testnet-account-id> approve_purchase '{}' --accountId <your-buyer-account-id>