Skip to content

Commit

Permalink
import custom deployment (#245)
Browse files Browse the repository at this point in the history
* Fix contract_name input

* Create custom contract fn

* Merging the loop solution in home

* Refactoring

* Fixing name typo error

* Adding toast notification

* Fixing newDeploy on Step3

* Parsin Record to json string

* fix layout

* Fixing custom Token img on contracts table

* Adding useForm

* Fixing deploy psp contracts

* Fix typo on Routes

---------

Co-authored-by: Agustín Longoni <[email protected]>
  • Loading branch information
henrypalacios and alongoni authored Oct 26, 2023
1 parent df28515 commit c69e76a
Show file tree
Hide file tree
Showing 49 changed files with 1,151 additions and 585 deletions.
4 changes: 4 additions & 0 deletions src/constants/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const backendRouterApi = {
updateDeployment: {
pathName: 'deployments',
method: 'PATCH'
},
version: {
pathName: 'version',
method: 'GET'
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export const ROUTES = {
WIZARD: '/wizard',
DOCS: '/docs',
CONTRACTS: '/contracts',
CUSTOM: '/custom-contract'
CUSTOM: '/custom-contract',
CONTRACTDETAIL: '/custom-contract/contract-detail'
} as const
2 changes: 1 addition & 1 deletion src/constants/titleTokenType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const TITLE_MAP_TOKEN: Record<ContractType, TitleMapProps> = {
custom: {
title: 'CUSTOM',
subtitle: 'Standard smart contract for a custom Token',
imgPath: TOKEN_PATHS.psp37,
imgPath: TOKEN_PATHS.custom,
imgProps: { width: 45, height: 39 }
}
}
15 changes: 15 additions & 0 deletions src/context/ApiVersionContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createContext, useContext } from 'react'

interface ApiVersionContextProps {
version: string | null
}

const ApiVersionContext = createContext<ApiVersionContextProps>({
version: null
})

export const useApiVersion = () => {
return useContext(ApiVersionContext)
}

export default ApiVersionContext
24 changes: 21 additions & 3 deletions src/context/LocalDbContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import {
ApiDeploymentRepository,
IApiDeploymentRepository
} from '@/services/backendApi/ApiDeploymentRepository'
import React, { createContext, PropsWithChildren, useContext } from 'react'
import React, {
createContext,
PropsWithChildren,
useContext,
useEffect,
useState
} from 'react'
import { BACKEND_API } from '../constants'
import { MyDatabase } from '@/services/localDB'
import { UserContractsRepository } from '@/services/localDB/UserContractsRepository'
Expand All @@ -13,12 +19,14 @@ import {
ApiCompileContractRepository,
IApiCompileContractRepository
} from '@/services/backendApi/ApiCompileContractRepository'
import { apiVersionService } from '@/services/backendApi/ApiVersionService'

interface DbContext {
networkRepository: INetworkRepository
deploymentsRepository: IApiDeploymentRepository
userContractsRepository: IUserContractsRepository
compileContractRepository: IApiCompileContractRepository
backendApiVersion: string
}

const networkRepository = new LocalStorageNetworkRepository()
Expand All @@ -30,17 +38,27 @@ const DbContext = createContext<DbContext>({
networkRepository,
deploymentsRepository,
userContractsRepository,
compileContractRepository
compileContractRepository,
backendApiVersion: ''
})

export const LocalDbProvider: React.FC<PropsWithChildren> = ({ children }) => {
const [backendApiVersion, setBackendApiVersion] = useState('')

useEffect(() => {
void apiVersionService
.getApiVersion()
.then(version => setBackendApiVersion(version))
}, [])

return (
<DbContext.Provider
value={{
networkRepository,
deploymentsRepository,
userContractsRepository,
compileContractRepository
compileContractRepository,
backendApiVersion
}}
>
{children}
Expand Down
22 changes: 10 additions & 12 deletions src/domain/UserContractDetails.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { TokenType } from '@/domain/TokenType'
import { ContractCompiledRaw } from '@/services'
import { ContractType } from '@/domain/repositories/DeploymentRepository'
import { ChainId } from '@/services/useink/chains'

export type ContractMetadata = ContractCompiledRaw

export interface UserContractDetails {
userAddress: string
blockchain: ChainId
address: string
txHash: string | undefined
codeHash: string
type: ContractType
uuid: string
name: string
address: string
network: ChainId
codeId: string
userAddress: string
txHash?: string
date: string
type: ContractType
abi?: Record<string, unknown>
external: boolean // Contracts not deployed by PCW are custom and external
hidden: boolean
}

export type ContractCompiled = Pick<
export type UserContractDetailsDraft = Omit<
UserContractDetails,
'userAddress' | 'codeHash' | 'name' | 'name'
> & {
type: TokenType
}
'uuid' | 'external'
>
5 changes: 5 additions & 0 deletions src/domain/common/OnCallbacks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface OnCallbacks<I> {
onStartCallback?: () => void
onSuccessCallback?: (result: I) => void
onErrorCallback?: (e: unknown) => void
}
20 changes: 4 additions & 16 deletions src/domain/repositories/DeploymentRepository.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import { ChainId } from '@/services/useink/chains'
import { TokenType } from '../TokenType'
import { UserContractDetailsDraft } from '../UserContractDetails'

export type ContractType = TokenType | 'custom'
export type UpdateDeployment = Partial<DeploymentItem>

export interface DeploymentItem {
contractName: string
contractAddress: string
network: ChainId
codeId: string
userAddress: string
txHash?: string
date: string
contractType: ContractType
externalAbi?: Record<string, unknown>
hidden: boolean
}
export type UpdateDeployment = Partial<UserContractDetailsDraft>

export interface IDeploymentsRepository<A, B> {
add: (deployment: DeploymentItem) => Promise<A>
findBy: (userAddress: string, networkId?: ChainId) => Promise<B>
add: (deployment: UserContractDetailsDraft) => Promise<A>
findBy: (userAddress: string, network?: ChainId) => Promise<B>
updateBy: (deployment: UpdateDeployment) => Promise<A>
}
11 changes: 7 additions & 4 deletions src/domain/repositories/IUserContractsRepository.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { ChainId } from '@/services/useink/chains'
import { UserContractDetails } from '../UserContractDetails'
import { DeploymentItem, UpdateDeployment } from './DeploymentRepository'
import {
UserContractDetails,
UserContractDetailsDraft
} from '../UserContractDetails'
import { UpdateDeployment } from './DeploymentRepository'
import { FilterType } from '@/services/localDB/UserContractsRepository'

export interface IUserContractsRepository {
add: (deployment: UserContractDetails) => Promise<string>
list: (userAddress: string) => Promise<UserContractDetails[]>
searchBy: (
userAddress: string,
networkId: ChainId,
network: ChainId,
filterBy?: FilterType
) => Promise<UserContractDetails[]>
bulkAddByUser(
userAddress: string,
deployments: DeploymentItem[]
deployments: UserContractDetailsDraft[]
): Promise<UserContractDetails[]>

updateBy(deployment: UpdateDeployment): Promise<number>
Expand Down
41 changes: 0 additions & 41 deletions src/hooks/deployments/useCreateContractsDeployments.ts

This file was deleted.

61 changes: 61 additions & 0 deletions src/hooks/deployments/useCreateDeployments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useCallback, useState } from 'react'

import { useLocalDbContext } from '@/context/LocalDbContext'
import { useAddUserContracts } from '@/hooks/userContracts/useAddUserContracts'
import { deploymentItemToUserContractDetails } from '@/services/transformers/toUserContractDetails'
import { UserContractDetailsDraft } from '@/domain'
import { OnCallbacks } from '@/domain/common/OnCallbacks'

interface NewDeploymentProps extends OnCallbacks<string> {
userContract: UserContractDetailsDraft
}

interface UseServiceAddDeployment {
newDeployment: (props: NewDeploymentProps) => Promise<string | undefined>
isLoading: boolean
error?: string
}

export function useCreateDeployments(): UseServiceAddDeployment {
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<string | undefined>()
const { deploymentsRepository } = useLocalDbContext()
const { addUserContract } = useAddUserContracts()

const newDeployment = useCallback(
async ({
userContract,
onSuccessCallback,
onErrorCallback
}: NewDeploymentProps): Promise<string | undefined> => {
setError(undefined)
setIsLoading(true)

try {
const response = await deploymentsRepository.add(userContract)
if (response.error) {
throw Error(response.error.message)
}

const newDeployId = response['data']
const userContractWithId = deploymentItemToUserContractDetails(
userContract,
newDeployId
)
addUserContract(userContractWithId)

onSuccessCallback?.(newDeployId)
return newDeployId
} catch (error) {
const _errorMsg = `An error occurred when trying to upload the deployed contract on the server`
setError(_errorMsg)
onErrorCallback?.(error)
} finally {
setIsLoading(false)
}
},
[addUserContract, deploymentsRepository]
)

return { newDeployment, isLoading, error }
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { useCallback, useState } from 'react'

import { DeploymentItem } from '@/domain/repositories/DeploymentRepository'
import { useLocalDbContext } from '@/context/LocalDbContext'
import { getErrorMessage } from '@/utils/error'
import { ChainId } from '@/services/useink/chains'
import { UserContractDetails } from '@/domain'

interface UseAddDeployment {
userContractsFromApi: (
userAddress: string,
networkId?: ChainId
) => Promise<DeploymentItem[] | undefined>
) => Promise<UserContractDetails[] | undefined>
isLoading: boolean
error?: string
}

export function useListContractDeployments(): UseAddDeployment {
export function useListDeployments(): UseAddDeployment {
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<string | undefined>()
const { deploymentsRepository } = useLocalDbContext()

const userContractsFromApi = useCallback(
async (userAddress: string, networkId?: ChainId) => {
async (userAddress: string, network?: ChainId) => {
setIsLoading(true)
setError(undefined)

try {
const deployments = await deploymentsRepository.findBy(
userAddress,
networkId
network
)
return deployments
} catch (e) {
Expand Down
8 changes: 1 addition & 7 deletions src/hooks/deployments/useUpdateContractsDeployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ export function useUpdateContractsDeployments(): UseAddDeployment {
setIsLoading(true)
setError(undefined)
try {
const deployments = await deploymentsRepository.updateBy({
contractAddress: deployed.contractAddress,
userAddress: deployed.userAddress,
network: deployed.network,
contractName: deployed.contractName,
hidden: deployed.hidden
})
const deployments = await deploymentsRepository.updateBy(deployed)
return deployments
} catch (e) {
setError(getErrorMessage(e))
Expand Down
Loading

0 comments on commit c69e76a

Please sign in to comment.