Skip to content

Commit

Permalink
payment added but awaiting clearance on fow after payment
Browse files Browse the repository at this point in the history
  • Loading branch information
codypharm committed Nov 9, 2023
1 parent e0aebeb commit a974d0f
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 53 deletions.
62 changes: 31 additions & 31 deletions hardhat/contracts/sample/Payment.sol
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.22;
pragma solidity ^0.8.9;

import "./MultiSigWallet.sol";
import './MultiSigWallet.sol';

/// @title A Multisig wallet
/// @author DecenterAI
/// @notice Serves as deposit contract

contract Payment {
MultiSigWallet public wallet;
mapping(address => uint) public depositRecord;

event Deposit(address indexed payer, uint _amount, bool status);
event Erase(address indexed user);

/**
*@notice Deposits eth into the treasury
*@param _amount The amount of eth deposited
*/
function deposit(uint _amount) external payable {
require(msg.value == _amount, "Invalid deposit amount");
//deposit to treasury
(bool success, ) = address(wallet).call{value: msg.value}("");
//record deposit
depositRecord[msg.sender] = msg.value;

emit Deposit(msg.sender, _amount, success);
}

/**
*@notice Removes deposit record for a user
*@param _user Address of the depositor
*/
function zeroDepositRecord(address _user) external {
//remove record
depositRecord[_user] = 0;
emit Erase(_user);
}
MultiSigWallet public account;
mapping(address => uint) public depositRecord;

event Deposit(address indexed payer, uint _amount, bool status);
event Erase(address indexed user);

/**
*@notice Deposits eth into the treasury
*@param _amount The amount of eth deposited
*/
function deposit(uint _amount, MultiSigWallet _wallet) external payable {
require(msg.value == _amount, 'Invalid deposit amount');
//deposit to treasury
(bool success, ) = address(_wallet).call{ value: msg.value }('');
//record deposit
depositRecord[msg.sender] = msg.value;

emit Deposit(msg.sender, _amount, success);
}

/**
*@notice Removes deposit record for a user
*@param _user Address of the depositor
*/
function zeroDepositRecord(address _user) external {
//remove record
depositRecord[_user] = 0;
emit Erase(_user);
}
}
103 changes: 103 additions & 0 deletions src/abi/Payment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "payer",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bool",
"name": "status",
"type": "bool"
}
],
"name": "Deposit",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "user",
"type": "address"
}
],
"name": "Erase",
"type": "event"
},
{
"inputs": [],
"name": "account",
"outputs": [
{
"internalType": "contract MultiSigWallet",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
},
{
"internalType": "contract MultiSigWallet",
"name": "_wallet",
"type": "address"
}
],
"name": "deposit",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "depositRecord",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_user",
"type": "address"
}
],
"name": "zeroDepositRecord",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
13 changes: 7 additions & 6 deletions src/app/dashboard/train/components/NewModel.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use client'
import { retrieve } from '@utils/fileCoinIpfs'
import { extractFile } from '@utils/upload'
import React, { useEffect, useRef, useState } from 'react'
import { CiTrash } from 'react-icons/ci'
import { FiUploadCloud } from 'react-icons/fi'
import { MdKeyboardArrowLeft } from 'react-icons/md'
import { extractFile } from '@utils/upload'
import { retrieve } from '@utils/fileCoinIpfs'

interface IProps {
setPage: (page: number | null) => void
setModal: (modal: number | null) => void
setTrain: (train: boolean) => void
train: boolean
modal: number | null
startUpload: (dataSet: File | null, trainScript: string, modelName: string) => void
getData: (dataSet: File | null, trainScript: string, modelName: string) => void
}

interface IFile {
Expand All @@ -22,7 +22,7 @@ interface IFile {
path: string
}

const NewModel = ({ setPage, setModal, setTrain, train, modal, startUpload }: IProps) => {
const NewModel = ({ setPage, setModal, setTrain, train, modal, getData }: IProps) => {
const trainScriptInput = useRef<HTMLInputElement | null>(null)
// const requirementInput = useRef<HTMLInputElement | null>(null)
const dataSetInput = useRef<HTMLInputElement | null>(null)
Expand Down Expand Up @@ -165,8 +165,9 @@ const NewModel = ({ setPage, setModal, setTrain, train, modal, startUpload }: IP
const startProcess = () => {
//show authorization modal
if (modelName && trainScript && dataSet) {
startUpload(dataSet, trainScript, modelName)
setPage(2)
getData(dataSet, trainScript, modelName)
setModal(0)
//setPage(2)
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/app/dashboard/train/modals/TransactionAuthorization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import { BsChevronDown } from 'react-icons/bs'

interface IProps {
setModal: (page: number | null) => void
setTrain: (train: boolean) => void
processPayment: () => void
}

const TransactionAuthorization = ({ setModal, setTrain }: IProps) => {
const startTrain = () => {
setModal(1)
setTrain(true)
}
const TransactionAuthorization = ({ setModal, processPayment }: IProps) => {

return (
<div className="fixed inset-0 bg-primary_13 bg-opacity-80 flex items-center justify-center">
<div className="w-[25%] h-[50%] bg-primary_12 rounded-xl p-4">
Expand Down Expand Up @@ -61,7 +58,7 @@ const TransactionAuthorization = ({ setModal, setTrain }: IProps) => {
Cancel
</button>
<button
onClick={() => startTrain()}
onClick={() => processPayment()}
className="bg-primary_11 text-primary_1 font-semibold font-primaryArchivo text-sm h-full w-[30%] cursor-pointer rounded-full"
>
Proceed
Expand Down
27 changes: 22 additions & 5 deletions src/app/dashboard/train/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client'
import React, { useState } from 'react'
import React, { use, useState } from 'react'
import { DashLayout } from '../dashLayout'
import TransactionAuthorization from './modals/TransactionAuthorization'
import TransactionProcessing from './modals/TransactionProcessing'
Expand All @@ -12,6 +12,7 @@ import ModelDownload from './components/ModelDownload'
import { uploadFile } from '@utils/upload'
import axios from 'axios'
import { decodeCid, retrieve } from '@utils/fileCoinIpfs'
import { makePayment } from '@utils/Payment'

export default function Page() {
const [page, setPage] = useState<number | null>(null)
Expand All @@ -20,6 +21,7 @@ export default function Page() {
const [decodedCid, setDecodedCid] = useState<string>('')
const [trainScript, setTrainScript] = useState<string>('')
const [modelName, setModelName] = useState<string>('')
const [dataSet, setDataSet] = useState<File>(null)

const startTrain = async (cid: string) => {
//call backend
Expand All @@ -35,10 +37,25 @@ export default function Page() {
setPage(3)
}

const startUpload = (dataSet: File | null, trainScript: string, modelName: string) => {
const processPayment = async () => {
setModal(1)
//collect money
const result = await makePayment()
//proceed to upload
if (result) {
// setModal(2)
//go to model training
setPage(2)
setModal(null)
uploadFile(dataSet, startTrain)
}
}

const getData = (dataSet: File | null, trainScript: string, modelName: string) => {
setTrainScript(trainScript)
setModelName(modelName)
uploadFile(dataSet, startTrain)
setDataSet(dataSet)

}

//download trained model
Expand All @@ -58,7 +75,7 @@ export default function Page() {
{page == 1 && (
<NewModel
setPage={setPage}
startUpload={startUpload}
getData={getData}
setModal={setModal}
modal={modal}
train={train}
Expand All @@ -71,7 +88,7 @@ export default function Page() {
)}

{modal === 0 && (
<TransactionAuthorization setModal={setModal} setTrain={setTrain} />
<TransactionAuthorization setModal={setModal} processPayment={processPayment} />
)}
{modal === 1 && <TransactionProcessing />}
{modal === 2 && <TransactionCompleted setModal={setModal} />}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/particle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const particleProvider = new ParticleProvider(particle.auth)

//if you use ethers.js
import { ethers } from 'ethers'
const ethersProvider = new ethers.providers.Web3Provider(particleProvider, 'any')
const ethersSigner = ethersProvider.getSigner()
export const ethersProvider = new ethers.providers.Web3Provider(particleProvider, 'any')
export const ethersSigner = ethersProvider.getSigner()

export default particle
9 changes: 7 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@/*": ["./src/*"],
"@app/*": ["./src/app/*"],
"@api/*": ["./src/app/api/*"],
"@abi/*": ["./src/abi/*"],
"@lib/*": ["./src/lib/*"],
"@data/*": ["./src/app/data/*"],
"@state/*": ["./src/state/*"],
Expand Down Expand Up @@ -59,12 +60,15 @@
},
"files": [
"src/abi/deal_client.js",
"src/abi/Payment.json",
"src/app/demo/page.tsx",
"config/app.ts",
//"src/app/test/page.jsx",
"test/jest-e2e.ts",
"tsconfig.json",
"tsconfig.node.json"
"tsconfig.node.json",
"utils/fileCoinIpfs",
"utils/upload"
],

"include": [
Expand All @@ -76,7 +80,8 @@
"prisma/generated/**/*.ts",
"enums/**/*.ts",
"config/**/*.ts",
"public/**/*.ts"
"public/**/*.ts",
"utils/*.ts"
],
"exclude": ["node_modules", "./jest.config.ts", "./prisma/seed.ts", "hardhat/*.ts"]
}
26 changes: 26 additions & 0 deletions utils/Payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ethers } from 'ethers'
import PaymentAbi from '@abi/Payment.json'
import { ethersProvider, ethersSigner } from '@/lib/particle'
const PaymentAddr = process.env.NEXT_PUBLIC_PAYMENT_CONTRACT_ADDRESS
const WalletAddress = process.env.NEXT_PUBLIC_WALLET_CONTRACT_ADDRESS
export const makePayment = async () => {
//@ts-ignore
// const provider = new ethers.providers.Web3Provider(window.ethereum)
// const signer = provider.getSigner()
const PaymentContract = new ethers.Contract(PaymentAddr, PaymentAbi, ethersSigner)
const amountInWei = ethers.utils.parseEther('0.000000000000001')
const options = { value: amountInWei }
// const reciept = await contract.buyPunk(1001, options)
try {
const reciept = await PaymentContract.functions.deposit(
amountInWei,
WalletAddress,
options,
)
console.log(reciept)
return true
} catch (error) {
console.log(error)
return false
}
}

0 comments on commit a974d0f

Please sign in to comment.