Giter VIP home page Giter VIP logo

escrow-js's Introduction

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>

escrow-js's People

Contributors

idea404 avatar ailisp avatar kasodon avatar

Stargazers

MelodeagleDev avatar Vijayendra Gaur avatar  avatar

Watchers

Alexander Skidanov avatar James Cloos avatar  avatar

Forkers

kasodon ailisp

escrow-js's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.