Skip to content

Commit

Permalink
Merge pull request #18 from Build-Squad/api
Browse files Browse the repository at this point in the history
Adding API routes & services
  • Loading branch information
AmarildoGrembi authored Jun 11, 2023
2 parents 0155bee + 70b89b2 commit f796c09
Show file tree
Hide file tree
Showing 16 changed files with 2,831 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ We also hope to help you understand the main concepts and best practices of Cade

`AssetHandover` is a dapp where account `holders` can grant a `recipient` the ability to `withdraw` specific `tokens` that they own (both `FungibleToken` and `NonFungibleToken`), at a future release date. Each account can only declare one recipient, as this removes the complexity of handling race conditions upon withdrawals. However, an account can be the recipient of multiple handovers. The account holder can specify which fungible tokens will be handed over and a maximum amount for each token. It is also possible to specify which non-fungible tokens will be handed over and a specific list of NFT IDs from each `NFT` Collection. The above tokens are not locked for the account holder, meaning that they can still be utilized/transferred. The recipient (or any other account) can attempt to withdraw them, at any given time, however, this will only be successful after the release date has passed, and only for the authorized recipient. One real-world scenario would be to create a digital `"will"` for one's account, or to simply add another account as a backup, in case the account holder loses access to his/her account or is no longer able to interact with it.

## Project Overview

Below is a description of the Dapp's components.

### 1. Web Server | [asset-handover/api](https://github.com/Build-Squad/asset-handover/tree/master/api)

The API demonstrates how to send transactions and queries to the Flow blockchain. By using the API you would be able to interact with the 3 main smart contracts of [Asset-Handover](https://github.com/Build-Squad/asset-handover/blob/master/cadence/contracts/AssetHandover.cdc), [BlpToken](https://github.com/Build-Squad/asset-handover/blob/master/cadence/contracts/tokens/BlpToken.cdc) and [Domains](https://github.com/Build-Squad/asset-handover/blob/master/cadence/contracts/nfts/Domains.cdc).

### 2. Cadence Code | [asset-handover/cadence](https://github.com/onflow/asset-handover/tree/master/cadence)

[Cadence](https://docs.onflow.org/cadence) This folder contains all of the blockchain logic for the application, meaning, smart contracts, smart contract implementations, as well as the scripts and transactions that interact with them.

## ✨ Getting Started

### 1. Install Dependencies
Expand Down
12 changes: 12 additions & 0 deletions api/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

PORT = 3000
FLOW_ACCESS_API_URL = 'https://rest-testnet.onflow.org'
ASSET_HANDOVER_ADDRESS = '0xff4cc652369f3857'
BLP_TOKEN_ADDRESS = '0xff4cc652369f3857'
DOMAINS_TOKEN_ADDRESS = '0xff4cc652369f3857'
NETWORK= 'testnet'
MINTER_ADDRESS= '0xee174d2e2ed89369'
MINTER_PRIVATE_KEY_HEX= '5998d5276ca1f204f55ab31fe970ef7654772ac6325bc4ce1ca226f3bd2cc93a'
MINTER_ACCOUNT_KEY_INDEX = 0
FUNGIBLE_TOKEN_ADDRESS = '0x9a0766d93b6608b7'
NON_FUNGIBLE_TOKEN_ADDRESS = '0x631e88ae7f1d7c20'
53 changes: 53 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Asset-handover API
The Asset-handover API is a RESTful API built with [express](https://expressjs.com/) that sends transactions to Flow using the [Flow JS SDK](https://github.com/onflow/fcl-js/tree/master/packages/sdk). It contains endpoints for the [Asset-handover](src/services/assetHandover.js), the [BLP token](src/services/blpToken.js) and [Domains token](src/services/domainsToken.js) services to read & write data to the Flow blockchain.

## Services
The Asset-handover API contains four main services. These services synchronize data between the blockchain, and also provide API endpoints that could be consumed by the asset-handover client-side application.

### [Asset-Handover Service](src/services/assetHandover.js)
This service contains functions that utilize the Flow Service to send Cadence transactions and scripts that fetch and update Asset-handover data.

You can also run the [transactions and scripts](../cadence) manually using the [Flow CLI](https://docs.onflow.org/flow-cli/).

[Exposed Endpoints](src/routes/assetHandover.js):
- **POST `/v1/asset-handover/lockUp/updateCreationFees`**: Allows the admin (deployer) of the Asset-Handover smart contract to update the fees charged for creating a lock-up. Uses [updateCreationFees.cdc](/cadence/transactions/lockUps/updateCreationFees.cdc)

- **POST `/v1/asset-handover/lockUp/updateWithdrawFees`**: Allows the admin (deployer) of the Asset-Handover smart contract to update the fees charged for withdrawing from a lock-up. Uses [updateWithdrawFees.cdc](/cadence/transactions/lockUps/updateWithdrawFees.cdc)

- **GET `/v1/asset-handover/accountLockUp/:address`**: Fetches the lock-up by holder account. Calls [getAccountLockUp.cdc](/cadence/scripts/lockUps/getAccountLockUp.cdc)

- **GET `/v1/asset-handover/LockUpsByRecipient/:address`**: Fetches the lock-up by recipient account. Calls [getLockUpsByRecipient.cdc](/cadence/scripts/lockUps/getLockUpsByRecipient.cdc)

- **GET `/v1/asset-handover/fungibleTokenInfoMapping`**: Fetches the fungible tokens of the Asset-handover registry. Calls [getFungibleTokenInfoMapping.cdc](/cadence/scripts/lockUps/getFungibleTokenInfoMapping.cdc)

- **GET `/v1/asset-handover/nonFungibleTokenInfoMapping`**: Fetches the non fungible tokens of the Asset-handover registry. Calls [getNonFungibleTokenInfoMapping.cdc](/cadence/scripts/lockUps/getNonFungibleTokenInfoMapping.cdc)

### [BLP Token Service](src/services/blpToken.js)
This service contains functions that utilize the Flow Service to send Cadence transactions and scripts that fetch and update BLP token data.

You can also run the [transactions and scripts](../cadence) manually using the [Flow CLI](https://docs.onflow.org/flow-cli/).

[Exposed Endpoints](src/routes/blpToken.js):
- **POST `/v1/asset-handover/blp/mint`**: Mint BLP tokens for the minter account. Calls [mintTokens.cdc](/cadence/transactions/blp/mintTokens.cdc)

- **GET `/v1/asset-handover/blp/:address`**: Fetches the balance of BLP tokens for an account. Calls [getAccountBalance.cdc](/cadence/scripts/blp/getAccountBalance.cdc)

### [Domains Token Service](src/services/domainsToken.js)
This service contains functions that utilize the Flow Service to send Cadence transactions and scripts that fetch and update Domains token data.

You can also run the [transactions and scripts](../cadence) manually using the [Flow CLI](https://docs.onflow.org/flow-cli/).

[Exposed Endpoints](src/routes/domainsToken.js):
- **GET `/v1/asset-handover/domains/accountCollection/:address`**: Fetches all the listings created by the account. Returns an array of `listing_resource_ids`. Calls [getAccountCollection.cdc](/cadence/scripts/domains/getAccountCollection.cdc)

- **POST `/v1/asset-handover/domains/mint`**: Mint Domains token for an account. Calls [registerDomain.cdc](/cadence/transactions/domains/registerDomain.cdc)

### [Flow Service](src/services/flow.js)
This service contains functions that interact with the Flow blockchain using the [FCL JS](https://docs.onflow.org/fcl/) library. While it has no exposed endpoints, its methods used to read, write and authorize data on the chain are used extensively by its sister services.

Notable functions:
- `sendTx()`: Takes a Cadence transaction as an arguement and sends it to write data into the Flow blockchain.
- `executeScript()`: Takes a Cadence script as an arguement and executes it to read data from the Flow blockchain.
- `authorizeMinter()`: Returns an asynchronous method to authorize an account to mint tokens.

*We reuse this service from another active sample-dapp on Flow, [Kitty-items](https://github.com/onflow/kitty-items/tree/master/api#flow-service).*
Loading

0 comments on commit f796c09

Please sign in to comment.