-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from vechain/update-x-app-template
- Loading branch information
Showing
57 changed files
with
1,365 additions
and
780 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ A blazing-fast React application powered by Vite: | |
An Express server crafted with TypeScript for robust API development: | ||
|
||
- **Vechain SDK:** Seamlessly fetch and perform transactions with the VechainThor blockchain. [Learn more](https://docs.vechain.org/developer-resources/sdks-and-providers/sdk) | ||
- **OpenAI GPT-Vision-Preview:** Integrate image analysis capabilities. [Explore here](https://platform.openai.com/docs/guides/vision) | ||
- **OpenAI GPT-4o:** Integrate image analysis capabilities. [Explore here](https://platform.openai.com/docs/guides/vision) | ||
|
||
### Contracts (apps/contracts) 📜 | ||
|
||
|
@@ -69,18 +69,11 @@ Shared configurations and utility functions to unify and simplify your developme | |
|
||
Configure your environment variables for seamless integration: | ||
|
||
### Frontend | ||
|
||
Place your `.env` files in `apps/frontend`: | ||
|
||
- **VITE_RECAPTCHA_V3_SITE_KEY:** [Request your RecaptchaV3 site keys](https://developers.google.com/recaptcha/docs/v3) | ||
|
||
### Backend | ||
|
||
Store your environment-specific `.env` files in `apps/backend`. `.env.development.local` & `.env.production.local` allow for custom environment variables based on the environment: | ||
|
||
- **OPENAI_API_KEY:** [Get your GPT-4 OpenAI key](https://platform.openai.com/api-keys) (Enable GPT-4 [here](https://help.openai.com/en/articles/7102672-how-can-i-access-gpt-4-gpt-4-turbo-and-gpt-4o)) | ||
- **RECAPTCHA_SECRET_KEY:** [Request your RecaptchaV3 site keys](https://developers.google.com/recaptcha/docs/v3) | ||
|
||
### Contracts | ||
|
||
|
@@ -96,41 +89,62 @@ Clone the repository and install dependencies with ease: | |
yarn install # Run this at the root level of the project | ||
``` | ||
|
||
To distribute rewards this contract necesitates of a valid APP_ID provided by VeBetterDAO when joining the ecosystem. | ||
In testnet you can generate the APP_ID by using the [VeBetterDAO sandbox](https://dev.testnet.governance.vebetterdao.org/). | ||
This contract can be initially deployed without this information and DEFAULT_ADMIN_ROLE can update it later through {EcoEarn-setAppId}. | ||
### Run locally | ||
|
||
This contract must me set as a `rewardDistributor` inside the X2EarnApps contract to be able to send rewards to users and withdraw. | ||
#### Deploy contracts | ||
|
||
## Deploying Contracts 🚀 | ||
To start deploy contract locally you must run a local instance of the Vechain Thor blockchain. You can do this by running the following command: | ||
|
||
Deploy your contracts effortlessly on either the Testnet or Solo networks. | ||
If you are deploying on the Testnet, ensure you have the correct addresses in the `config-contracts` package (generated on the [VeBetterDAO sandbox](https://dev.testnet.governance.vebetterdao.org/)). | ||
When deploying on the SOLO network the script will deploy for you the mocked VeBetterDAO contracts and generate an APP_ID. | ||
```bash | ||
yarn contracts:solo-up | ||
``` | ||
|
||
This command will also start a local instance of the tools Insight (http://localhost:8080/) and Inspector (http://localhost:8081/). | ||
|
||
### Deploying on Solo Network | ||
At this point you can deploy the contracts to the local network by running: | ||
|
||
```bash | ||
yarn contracts:deploy:solo | ||
``` | ||
|
||
### Deploying on Testnet | ||
Once the deployment is completed successfully you can go ahead and start the frontend and backend: | ||
|
||
> ⚠️ **Warning:** | ||
> Remeber to set the OPENAI_API_KEY env variable in the backend .env.development.local file. Refer to the [Environment Variables](#environment-variables) section for more information. | ||
```bash | ||
yarn contracts:deploy:testnet | ||
yarn dev | ||
``` | ||
|
||
## Running on Solo Network 🔧 | ||
At this point you can access the frontend at [http://localhost:8082/](http://localhost:8082/). | ||
|
||
Start uploading a receipt! | ||
|
||
## Deploying on Testnet 🚀 | ||
|
||
Run the Frontend and Backend, connected to the Solo network and pointing to your deployed contracts. Ensure all environment variables are properly configured: | ||
To distribute rewards this contract needs of a valid APP_ID provided by VeBetterDAO when joining the ecosystem. | ||
In testnet you can generate the APP_ID by using the [VeBetterDAO sandbox](https://dev.testnet.governance.vebetterdao.org/). | ||
This contract can be initially deployed without this information and DEFAULT_ADMIN_ROLE can update it later through {EcoEarn-setAppId}. | ||
|
||
This contract must be set as a `rewardDistributor` inside the X2EarnApps contract to be able to send rewards to users and withdraw. | ||
|
||
For deploying on Testnet you should check that you have the correct addresses in the `config-contracts` package (generated on the [VeBetterDAO sandbox](https://dev.testnet.governance.vebetterdao.org/)). | ||
|
||
When deploying on the SOLO network the script will deploy for you the mocked VeBetterDAO contracts and generate an APP_ID. | ||
|
||
Once everything is set up you can deploy the contracts to the testnet by running: | ||
|
||
```bash | ||
yarn dev | ||
yarn contracts:deploy:testnet | ||
``` | ||
|
||
### Setting up rewards | ||
## Triggering Cycles and Setting the Rewards | ||
|
||
The deployment scripts will configure the 1st cycle for you with a reward of 1000 tokens. | ||
|
||
To start a new cycle and set the rewards you can follow the steps below: | ||
|
||
## Testnet | ||
### Testnet | ||
|
||
Read the [VeBetterDAO documentation](https://docs.vebetterdao.org/developer-guides/test-environmnet) to learn how to set up rewards for your users and use the Testnet environment. | ||
|
||
|
@@ -153,22 +167,18 @@ Now you just need to trigger cycles and set amount of rewards per cycle on your | |
4. Trigger a cycle: | ||
![image](https://i.ibb.co/47V2Zjb/SCR-20240723-kkxx.png) | ||
|
||
## Solo Network | ||
### Solo Network | ||
|
||
Since the Solo network is a local network with mocked VeBetterDAO contracts you can use the following steps to set up available rewards to distribute to users: | ||
|
||
0. Ensure you are using a wallet with imported pre-funded accounts mnemonic into your wallet. Mnemoninc: | ||
0. Ensure you are using a wallet with imported pre-funded accounts mnemonic into your wallet. Mnemonic: | ||
|
||
``` | ||
denial kitchen pet squirrel other broom bar gas better priority spoil cross | ||
``` | ||
|
||
1. Copy the `APP_ID` generated by the `contracts:deploy:solo` script and logged in the console. | ||
2. Run `devpal`, a frontend tool to interact with your contracts: | ||
|
||
```bash | ||
npx [email protected] http://localhost:8669 | ||
``` | ||
2. Open the [inspector app](http://localhost:8081/#/contracts), running on localhost, that you can use to interact with your contracts. | ||
|
||
3. Open the `Inspector` tab and perform the following actions: | ||
4. Add the B3TR_Mock contract (get the address from the console logs and ABI from the `apps/contracts/artifacts/contracts/mock/B3TR_Mock.sol/B3TR_Mock.json` file) | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,28 @@ | ||
import { ADMIN_ADDRESS, ADMIN_PRIVATE_KEY, REWARD_AMOUNT } from '@/config'; | ||
import { HttpException } from '@/exceptions/HttpException'; | ||
import { Submission } from '@/interfaces/submission.interface'; | ||
import { thor } from '@/utils/thor'; | ||
import { ecoEarnContract } from '@/utils/thor'; | ||
import { Service } from 'typedi'; | ||
import { EcoEarnABI } from '@/utils/const'; | ||
import { ethers } from 'ethers'; | ||
import { config } from '@repo/config-contract'; | ||
import { TransactionHandler, clauseBuilder, coder } from '@vechain/sdk-core'; | ||
import * as console from 'node:console'; | ||
import { unitsUtils } from '@vechain/sdk-core'; | ||
import { REWARD_AMOUNT } from '@config'; | ||
@Service() | ||
export class ContractsService { | ||
public async registerSubmission(submission: Submission): Promise<void> { | ||
const clause = clauseBuilder.functionInteraction( | ||
config.CONTRACT_ADDRESS, | ||
coder.createInterface(EcoEarnABI).getFunction('registerValidSubmission'), | ||
[submission.address, `0x${ethers.parseEther(REWARD_AMOUNT).toString(16)}`], | ||
); | ||
public async registerSubmission(submission: Submission): Promise<boolean> { | ||
let isSuccess = false; | ||
try { | ||
const result = await ( | ||
await ecoEarnContract.transact.registerValidSubmission(submission.address, unitsUtils.parseUnits(REWARD_AMOUNT, 'ether')) | ||
).wait(); | ||
isSuccess = !result.reverted; | ||
} catch (error) { | ||
console.log('Error', error); | ||
} | ||
|
||
const gasResult = await thor.gas.estimateGas([clause], ADMIN_ADDRESS); | ||
|
||
if (gasResult.reverted === true) throw new HttpException(500, `EcoEarn: Internal server error: ${gasResult.revertReasons}`); | ||
|
||
const txBody = await thor.transactions.buildTransactionBody([clause], gasResult.totalGas); | ||
|
||
const signedTx = TransactionHandler.sign(txBody, Buffer.from(ADMIN_PRIVATE_KEY)); | ||
|
||
await thor.transactions.sendTransaction(signedTx); | ||
return isSuccess; | ||
} | ||
|
||
public async validateSubmission(submission: Submission): Promise<void> { | ||
const isMaxSubmissionsReached = await thor.contracts.executeCall( | ||
config.CONTRACT_ADDRESS, | ||
coder.createInterface(EcoEarnABI).getFunction('isUserMaxSubmissionsReached'), | ||
[submission.address], | ||
); | ||
|
||
if (Boolean(isMaxSubmissionsReached[0]) === true) throw new HttpException(409, `EcoEarn: Max submissions reached for this cycle`); | ||
const isMaxSubmissionsReached = (await ecoEarnContract.read.isUserMaxSubmissionsReached(submission.address))[0]; | ||
if (Boolean(isMaxSubmissionsReached) === true) throw new HttpException(409, `EcoEarn: Max submissions reached for this cycle`); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './captcha'; | ||
export * from './openai'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -595,4 +595,4 @@ export const EcoEarnABI = [ | |
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
]; | ||
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import { NETWORK_URL } from '../config'; | ||
import { HttpClient, ThorClient } from '@vechain/sdk-network'; | ||
import { ADMIN_PRIVATE_KEY, NETWORK_URL } from '../config'; | ||
import { HttpClient, ThorClient, VeChainPrivateKeySigner, VeChainProvider } from '@vechain/sdk-network'; | ||
import { EcoEarnABI } from '@utils/const'; | ||
import { ECO_SOL_ABI, config } from '@repo/config-contract'; | ||
|
||
export const thor = new ThorClient(new HttpClient(NETWORK_URL), { | ||
isPollingEnabled: false, | ||
}); | ||
|
||
export const ecoEarnContract = thor.contracts.load( | ||
config.CONTRACT_ADDRESS, | ||
ECO_SOL_ABI, | ||
new VeChainPrivateKeySigner(Buffer.from(ADMIN_PRIVATE_KEY), new VeChainProvider(thor)), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.