README.md
conf
    clickhouse
        clickhouse-server
            config.d
                docker_related_config.xml
                prometheus.xml
            config.xml
            users.d
                operator.xml
            users.xml
        docker-entrypoint-initdb.d
            init.sql
    fluent-bit
        schema.json
    grafana
        dashboard.json
    prometheus
        prometheus.yml
contracts
    nft
        Cargo.toml
        README.md
        neardev
            dev-account.env
        src
            lib.rs
    vault
        Cargo.toml
        README.md
        neardev.bak2
            dev-account.env
        neardev
            dev-account.env
        src
            external
                mod.rs
                nft.rs
            internal
                claimable.rs
                mod.rs
                nft_token_callback_message.rs
                on_transfer_message.rs
            lib.rs
docs
    assets
        arweave_processor
            DEMO.md
frontend
    vault
        README.md
        docs
            ClaimableNFT.md
        package.json
        public
            index.html
            manifest.json
            robots.txt
        src
            App.css
            Components
                Layout
                    Vault.module.css
                Media
                    Media.module.css
            Libraries
                Near
                    connection.ts
                    nft.ts
                    vault.ts
            Providers
                Wallet.ts
            Routes
                ClaimNFT.module.css
                NotFound.module.css
                ViewNFT.module.css
            index.css
            logo.svg
            react-app-env.d.ts
            reportWebVitals.ts
            setupTests.ts
        tsconfig.json
jobs
    arweave_processor
        .mocharc.json
        README.md
        bin
            callback_server.ts
            storage_queue_clear.ts
            storage_queue_dispatch.ts
            storage_queue_peek.ts
        package.json
        src
            config.ts
            core
                arweave.ts
                event.ts
                processor.ts
            index.ts
            lib
                logger.ts
                util.ts
            queue
                azure_storage_queue.ts
                common.ts
        tests
            common.spec.ts
        tsconfig.build.json
        tsconfig.json
    near_processor
        .mocharc.json
        README.md
        bin
            callback_server.ts
            claim.ts
            claim_challenge_generate.ts
            storage_queue_clear.ts
            storage_queue_dispatch.ts
            storage_queue_list.ts
            storage_queue_peek.ts
        package.json
        src
            config.ts
            core
                event.ts
                near.ts
                processor.ts
            index.ts
            lib
                logger.ts
                util.ts
            queue
                azure_storage_queue.ts
                common.ts
            tsconfig.json
        tests
            common.spec.ts
        tsconfig.build.json
        tsconfig.json
CONFIGURATION

# Arweave Processor

Service responsible for uploading media to the permaweb.

## Environment Configuration
| Key | Description |
| :-- | :---------- |
| `AZURE_ACCOUNT_NAME` | **REQUIRED**. azure account name |
| `AZURE_ACCOUNT_KEY` | **REQUIRED**. azure secret key |
| `TOPIC` | **REQUIRED**. name of the topic |
| `WALLET_JSON` | **REQUIRED**. wallet to use, jwk in json string |
| `MIN_CONFIRMATIONS` | default number of confirmations before considering the arweave transaction to be successful |
| `DEFAULT_CALLBACK_URL` | **REQUIRED**. default callback url that will receive the emitted events |
| `MAX_JOBS` | maximum number of jobs to be processed at the same time, setting to 0 will have no limit |
| `LOG_LEVEL` | [winston log level](https://www.npmjs.com/package/winston#logging-levels)|

## Job Spec

The service will listen for messages on a pubsub queue, the message should be a valid json data that conforms with the format below.

Example Message from Queue
```json
{
    "JobId": "ff975cbd-32f4-4f09-9b9a-01964dd6eb90",
    "MediaURL":
    "https://nftdesignworks.blob.core.windows.net/mintedimages/429538da-ecfa-454f-9fe
d-b13b8b1de043.png",
    "Metadata": {
        "AthleteId": "128073",
        "FirstName": "Artūrs",
        "LastName": "Liepa",
        "Country": "LAT",
        ... REDACTED
    },
    "MinConfirmations": 15
}
```

| Field | Type | Description |
| :---- | :--- | :---------- |
| `JobId` | `string` | **Required**.  A unique string generated by the publisher of the message |
| `MediaURL` | `string` | **Required**. Url of the media to be uploaded to the permaweb |
| `Metadata` | `object` | **Required**. nft metadata, should be deserialiazable into a json string |
| `MinConfirmations` | `number` | **Optional**. the number of confirmations from the network to consider the transaction as successful, defaults to **15** |

## Events

the processor will emit events and send it to the configured `DEFAULT_CALLBACK_URL` if `CallbackURL` is not specified on jobs. 

### Started Event
Emmited to notify the callback url that the processor has received the job and is currently processing it.
```json
{
    "JobId": "ff975cbd-32f4-4f09-9b9a-01964dd6eb90",
    "Event": "started",
    "Time": 1659965535,
    "Message": "Job Started"
}
```
### Failure Event
Emmited to notify the callback url that an error has occured.
```json
{
    "JobId": "ff975cbd-32f4-4f09-9b9a-01964dd6eb90",
    "Event": "failure",
    "Time": 1659965535,
    "Message": "Example Error Message"
}

```
### Success Event
Emmited to notify the callback url that the job has successfully finished.
```json
{
    "JobId": "ff975cbd-32f4-4f09-9b9a-01964dd6eb90",
    "Event": "success",
    "Time": 1659965535,
    "Message": "Media has been successfully uploaded to the permaweb",
    "Details": {
        "TransactionID": "<ARWEAVE_TX_ID>",
        "Confirmations": 15
    }
}
```
## Process Flow
![](../../docs/assets/arweave_processor/arweave-flow.png)
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
# NFT Design Works Smart Contract
# Near Processor

Service responsible for minting the nft

## Environment Configuration
| Key | Description |
| :-- | :---------- |
| `AZURE_ACCOUNT_NAME` | **REQUIRED**. azure account name |
| `AZURE_ACCOUNT_KEY` | **REQUIRED**. azure secret key |
| `TOPIC` | **REQUIRED**. name of the topic |
| `NEAR_ACCOUNT_NAME` | **REQUIRED**. near account that will sign the transactions |
| `NEAR_ACCOUNT_PRIVATE_KEY` | **REQUIRED**. NEAR_ACCOUNT_NAME private key |
| `NEAR_ACCOUNT_CONTRACT_NAME` | name of the minter contract, defaults to `NEAR_ACCOUNT_NAME` if not provided |
| `NEAR_DEPOSIT` | **REQUIRED**. amount of near to deposit in each transaction call, excess tokens are refunded automatically |
| `VAULT_CONTRACT_ADDRESS` | **REQUIRED**. address of the vault contract |
| `VAULT_BASE_URL` | **REQUIRED**. base url of the vault frontend |
| `DEFAULT_CALLBACK_URL` | **REQUIRED**. default callback url that will receive the emitted events |
| `MAX_JOBS` | maximum number of jobs to be processed at the same time, setting to 0 will have no limit |
| `LOG_LEVEL` | [winston log level](https://www.npmjs.com/package/winston#logging-levels)|

## Job Spec
The service will listen for messages on a pubsub queue, the message should be a valid json data that conforms with the format below.

```json
{
    "JobId": "ff975cbd-32f4-4f09-9b9a-01964dd6eb90",
    "ArweaveTxnId": "ZkUxju5Y5Goy-OXw2O-mj8T_T4JSUHb7sDUhUMLNlgg",
    "OwnerAddress": "nft.nftdw-001.testnet",
    "Title": "Samuel Dickinson - 2022 World Triathlon Sprint & Relay Championships Montreal",
    "Description": "<TOKEN DESCRIPTION>",
    "Copies": 1,
    "IssuedAt": "1660818999614",
    "ExpiresAt": null,
    "StartsAt": "1660818999614",
    "UpdatedAt": "1660818999614"
}
```

| Field | Type | Description |
| :---- | :--- | :---------- |
| `JobId` | string | **Required**.  A unique string generated by the publisher of the message |
| `ArweaveTxnId` | string | **Required**. Arweave transaction id, the media should be accessible from https://arweave.net/<TX_ID> |
| `OwnerAddress` | string | **Optional**. The address of the owner of the nft to be minted, defaults to the minter if not provided |
| `Title` | string | **Optional**. NFT title, see [TokenMetadata.title](https://nomicon.io/Standards/Tokens/NonFungibleToken/Metadata#interface) |
| `Description` | string | **Optional**. NFT Description, see [TokenMetadata.description](https://nomicon.io/Standards/Tokens/NonFungibleToken/Metadata#interface) |
| `Copies` | number | **Optional**. NFT copies, see [TokenMetadata.copies](https://nomicon.io/Standards/Tokens/NonFungibleToken/Metadata#interface) |
| `IssuedAt` | string | **Optional**. NFT issuedAt, see [TokenMetadata.issued_at](https://nomicon.io/Standards/Tokens/NonFungibleToken/Metadata#interface) |
| `ExpiresAt` | string | **Optional**. NFT expiresAt, see [TokenMetadata.expires_at](https://nomicon.io/Standards/Tokens/NonFungibleToken/Metadata#interface) |
| `StartsAt` | string | **Optional**. NFT startsAt, see [TokenMetadata.starts_at](https://nomicon.io/Standards/Tokens/NonFungibleToken/Metadata#interface) |
| `UpdatedAt` | string | **Optional**. NFT updatedAt, see [TokenMetadata.updated_at](https://nomicon.io/Standards/Tokens/NonFungibleToken/Metadata#interface) |
## Events

the processor will emit events and send it to the configured `DEFAULT_CALLBACK_URL` if `CallbackURL` is not specified on jobs. 

### Started Event
Emmited to notify the callback url that the processor has received the job and is currently processing it.
```json
{
    "JobId": "ff975cbd-32f4-4f09-9b9a-01964dd6eb90",
    "Event": "started",
    "Time": 1659965535,
    "Message": "Job Started"
}
```
### Failure Event
Emmited to notify the callback url that an error has occured.
```json
{
    "JobId": "ff975cbd-32f4-4f09-9b9a-01964dd6eb90",
    "Event": "failure",
    "Time": 1659965535,
    "Message": "Example Error Message"
}

```
### Success Event
Emmited to notify the callback url that the job has successfully finished.
```json
{
    "JobId": "ff975cbd-32f4-4f09-9b9a-01964dd6eb90",
    "Event": "success",
    "Message": "Job ff975cbd-32f4-4f09-9b9a-01964dd6eb90 has been successfully processed",
    "Details": {
        "ClaimURL": "https://vault.nftdw-001.com/claim/nft.nftdw-001.testnet/2d7c23e9-134c-445e-8882-d5be78c0ce56?token=eyJORlRDb250cmFjdCI6Im5mdC5uZnRkdy0wMDEudGVzdG5ldCIsIlRva2VuSWQiOiIyZDdjMjNlOS0xMzRjLTQ0NWUtODg4Mi1kNWJlNzhjMGNlNTYiLCJQcml2YXRlS2V5IjoiZWQyNTUxOTozZHdpamhHcGNnRXYxN3VWZW14aEtEZkNHS3pLSDk4aERWNjVzVEg3ZVA1aXgza3htc3JLSzU3dE1NTXF2OWlybU1KTEoyM3JWcFYxZFROQ1c0aENkNzlmIiwiVmF1bHRDb250cmFjdCI6InZhdWx0Lm5mdGR3LTAwMS50ZXN0bmV0In0%3D",
        "ExplorerURL": "https://explorer.testnet.near.org/transactions/E9237ZHVQ8ANEtRfzEAkuVkSjxyCZYc9cxeTCfsREoQ9",
        "TransactionId": "E9237ZHVQ8ANEtRfzEAkuVkSjxyCZYc9cxeTCfsREoQ9"
    },
    "Time": 1661128110153
}
```
| Field | Description |
| :---- | :---------- |
| `Details.ClaimURL` | a url that claims the minted nft if OwnerAddress is not set on job payload, this should be sent securely to the supposed owner of the nft as anyone that has access to this data has the ability to claim the nft |
| `Details.ExplorerURL` | a url that redirects to the transaction |
| `TransactionId` | id of the transaction |

## Process Flow
# NFT Design Works Smart Contract
# Near NFT Processor

Near NFT Minter using Arweave and Near Protocol

## Architecture
![](./docs/assets/nft-minter-infra.png)

## Jobs

- [Arweave Processor](./jobs/arweave_processor/README.md) - Service responsible for uploading media to the permaweb. Check [arweave processor local demo](./docs/assets/arweave_processor/DEMO.md) for instructions on how to simulate the service on local machine.
- [Near Processor](./jobs/near_processor/README.md) - Service responsible for minting the nfts and sending them to their respective owners.

## Contracts
- [NFT Contract](./contracts/nft/README.md)

## Production
### Requirements
- [docker/docker-compose](https://docs.docker.com/get-docker/)

### Configuration
Before running, a dotenv file is required to configure each service.
Kindly check the documentation for available keys to configure.

- [arweave_processor.env](./jobs/arweave_processor/README.md#environment-configuration)
- [near_processor.env](./jobs/near_processor/README.md#environment-configuration)

### Build the images

Build the production images with docker-compose
```bash
    docker-compose -f docker-compose-production.yaml build 
```

### Run
Once arweave_processor.env and near_processor.env is configured on the working directory, run the service using docker-compose.
```bash
    docker-compose -f docker-compose-production.yaml up -d
    # OR run each service with additional replica
    docker-compose -f docker-compose-production.yaml up -d --scale arweave_processor=<REPLICA_COUNT> --scale near_processor=<REPLICA_COUNT>
```
