Skip to content

deprecated-near-examples/escrow-js

Repository files navigation

Escrow Contract Example for NEAR in JavaScript

This is an example of an escrow contract written in JavaScript for the NEAR blockchain.

Requirements

Running the Example

  1. Install dependencies
npm install
  1. Build the contract
npm run build
  1. Run the tests
npm test

What this Example Covers

  1. How to write a smart contract in JavaScript

    a. How to perform cross-contract calls and pass data among them in JavaScript

  2. How to write integration tests for a smart contract in JavaScript

Flows Covered in the Example

  1. User A purchases a product in-escrow from User B
  2. User A purchases a product in-escrow from User B and cancels the purchase
  3. User A purchases a product in-escrow from User B while User B owns no products
  4. User A purchases a product in-escrow from User B and approves the purchase
  5. User A purchases a product in-escrow from User B and a day passes without approval
  6. User A purchases a product in-escrow from User B and attempts to transfer the product to User C

Detailed Guide in Running the Example


Required Accounts

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

  1. Login to your NEAR account
near login
  1. 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>
  1. 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>
  1. 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>
  1. 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>
  1. 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
  1. 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>
  1. Approve the Purchase
near call <your-escrow-testnet-account-id> approve_purchase '{}' --accountId <your-buyer-account-id>

About

A simple example of a NEAR escrow contract in JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •