From 9b1622997beb1b23107142b7b5208df382b57c33 Mon Sep 17 00:00:00 2001 From: David Disu Date: Mon, 28 Feb 2022 07:36:32 +0100 Subject: [PATCH] Load compiled contracts for opened files --- .../remix-ui/run-tab/src/lib/actions/index.ts | 15 ++++++---- .../run-tab/src/lib/actions/payload.ts | 3 +- .../src/lib/components/contractDropdownUI.tsx | 18 ++++++------ .../run-tab/src/lib/reducers/runTab.ts | 28 +++++++++++++------ libs/remix-ui/run-tab/src/lib/types/index.ts | 10 +++---- 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/libs/remix-ui/run-tab/src/lib/actions/index.ts b/libs/remix-ui/run-tab/src/lib/actions/index.ts index 464a68eed0..0bc3107e40 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/index.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/index.ts @@ -8,6 +8,7 @@ import { RunTab } from '../types/run-tab' import { CompilerAbstract } from '@remix-project/remix-solidity' import * as remixLib from '@remix-project/remix-lib' import { ContractData, FuncABI, MainnetPrompt } from '../types' +import { CompilerAbstract as CompilerAbstractType } from '@remix-project/remix-solidity-ts' const txFormat = remixLib.execution.txFormat declare global { @@ -95,6 +96,7 @@ const setupEvents = () => { } else { dispatch(setLoadType('other')) } + dispatch(setCurrentFile(currentFile)) }) plugin.recorder.event.register('recorderCountChange', (count) => { @@ -309,10 +311,10 @@ const broadcastCompilationResult = (file, source, languageVersion, data, input?) plugin.compilersArtefacts.__last = compiler const contracts = getCompiledContracts(compiler).map((contract) => { - return { name: languageVersion, alias: contract.name, file: contract.file } + return { name: languageVersion, alias: contract.name, file: contract.file, compiler } }) - dispatch(fetchContractListSuccess(contracts)) + dispatch(fetchContractListSuccess({ [file]: contracts })) dispatch(setCurrentFile(file)) } @@ -343,9 +345,9 @@ const getCompiledContracts = (compiler) => { return contracts } -export const getSelectedContract = (contractName: string, compilerAtributeName: string) => { +export const getSelectedContract = (contractName: string, compiler: CompilerAbstractType) => { if (!contractName) return null - const compiler = plugin.compilersArtefacts[compilerAtributeName] + // const compiler = plugin.compilersArtefacts[compilerAtributeName] if (!compiler) return null @@ -571,8 +573,9 @@ export const loadAddress = (contract: ContractData, address: string) => { return addInstance({ abi, address, name: '' }) } else if (loadType === 'instance') { if (!contract) return dispatch(displayPopUp('No compiled contracts found.')) - const compiler = plugin.REACT_API.contracts.contractList.find(item => item.alias === contract.name) - const contractData = getSelectedContract(contract.name, compiler.name) + const currentFile = plugin.REACT_API.contracts.currentFile + const compiler = plugin.REACT_API.contracts.contractList[currentFile].find(item => item.alias === contract.name) + const contractData = getSelectedContract(contract.name, compiler.compiler) return addInstance({ contractData, address, name: contract.name }) } } diff --git a/libs/remix-ui/run-tab/src/lib/actions/payload.ts b/libs/remix-ui/run-tab/src/lib/actions/payload.ts index 812b51711a..63cfa72e13 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/payload.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/payload.ts @@ -1,3 +1,4 @@ +import { ContractList } from '../reducers/runTab' import { ContractData } from '../types' export const fetchAccountsListRequest = () => { @@ -137,7 +138,7 @@ export const fetchContractListRequest = () => { } } -export const fetchContractListSuccess = (contracts: { name: string, alias: string, file: string }[]) => { +export const fetchContractListSuccess = (contracts: ContractList) => { return { type: 'FETCH_CONTRACT_LIST_SUCCESS', payload: contracts diff --git a/libs/remix-ui/run-tab/src/lib/components/contractDropdownUI.tsx b/libs/remix-ui/run-tab/src/lib/components/contractDropdownUI.tsx index 35791681e1..d367038e8a 100644 --- a/libs/remix-ui/run-tab/src/lib/components/contractDropdownUI.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/contractDropdownUI.tsx @@ -75,11 +75,11 @@ export function ContractDropdownUI (props: ContractDropdownProps) { }, [loadType, currentFile, compilationCount]) useEffect(() => { - if (selectedContract) { - const contract = contractList.find(contract => contract.alias === selectedContract) + if (selectedContract && contractList[currentFile]) { + const contract = contractList[currentFile].find(contract => contract.alias === selectedContract) if (contract) { - const loadedContractData = props.getSelectedContract(selectedContract, contract.name) + const loadedContractData = props.getSelectedContract(selectedContract, contract.compiler) if (loadedContractData) { setLoadedContractData(loadedContractData) @@ -95,10 +95,10 @@ export function ContractDropdownUI (props: ContractDropdownProps) { }, [contractList]) const initSelectedContract = () => { - if (contractList.length > 0) { - const contract = contractList.find(contract => contract.alias === selectedContract) + if (contractList[currentFile] && contractList[currentFile].length > 0) { + const contract = contractList[currentFile].find(contract => contract.alias === selectedContract) - if (!selectedContract || !contract) setSelectedContract(contractList[0].alias) + if (!selectedContract || !contract) setSelectedContract(contractList[currentFile][0].alias) } } @@ -202,16 +202,16 @@ export function ContractDropdownUI (props: ContractDropdownProps) {
- { (contractList.length <= 0) && } + { (contractList[currentFile] && contractList[currentFile].length <= 0) && } { abiLabel.content }
- { contractList.length <= 0 ? 'No compiled contracts' + { contractList[currentFile] && contractList[currentFile].length <= 0 ? 'No compiled contracts' : loadedContractData ?
diff --git a/libs/remix-ui/run-tab/src/lib/reducers/runTab.ts b/libs/remix-ui/run-tab/src/lib/reducers/runTab.ts index 2aaf3f4c1a..1e8a916a0b 100644 --- a/libs/remix-ui/run-tab/src/lib/reducers/runTab.ts +++ b/libs/remix-ui/run-tab/src/lib/reducers/runTab.ts @@ -1,10 +1,19 @@ +import { CompilerAbstract } from '@remix-project/remix-solidity-ts' import { ContractData } from '../types' - interface Action { type: string payload: any } +export interface Contract { + name: string, + alias: string, + file: string, + compiler: CompilerAbstract +} +export interface ContractList { + [file: string]: Contract[] +} export interface RunTabState { accounts: { loadedAccounts: Record, @@ -46,10 +55,13 @@ export interface RunTabState { matchPassphrase: string, contracts: { contractList: { - name: string, - alias: string, - file: string - }[], + [file: string]: { + name: string, + alias: string, + file: string, + compiler: CompilerAbstract + }[] + }, loadType: 'abi' | 'sol' | 'other' currentFile: string, compilationCount: number, @@ -141,7 +153,7 @@ export const runTabInitialState: RunTabState = { passphrase: '', matchPassphrase: '', contracts: { - contractList: [], + contractList: {}, loadType: 'other', currentFile: '', compilationCount: 0, @@ -438,13 +450,13 @@ export const runTabReducer = (state: RunTabState = runTabInitialState, action: A } case 'FETCH_CONTRACT_LIST_SUCCESS': { - const payload: { name: string, alias: string, file: string }[] = action.payload + const payload: ContractList = action.payload return { ...state, contracts: { ...state.contracts, - contractList: payload, + contractList: { ...state.contracts.contractList, ...payload }, isSuccessful: true, isRequesting: false, error: null diff --git a/libs/remix-ui/run-tab/src/lib/types/index.ts b/libs/remix-ui/run-tab/src/lib/types/index.ts index c4b4fd908e..ac08c94086 100644 --- a/libs/remix-ui/run-tab/src/lib/types/index.ts +++ b/libs/remix-ui/run-tab/src/lib/types/index.ts @@ -1,3 +1,5 @@ +import { CompilerAbstract } from '@remix-project/remix-solidity-ts' +import { ContractList } from '../reducers/runTab' import { RunTab } from './run-tab' export interface RunTabProps { plugin: RunTab @@ -147,11 +149,7 @@ export type MainnetPrompt = ( export interface ContractDropdownProps { exEnvironment: string, contracts: { - contractList: { - name: string, - alias: string, - file: string - }[], + contractList: ContractList, loadType: 'abi' | 'sol' | 'other', currentFile: string, compilationCount: number, @@ -159,7 +157,7 @@ export interface ContractDropdownProps { isSuccessful: boolean, error: string }, - getSelectedContract: (contractName: string, compilerAtributeName: string) => ContractData, + getSelectedContract: (contractName: string, compiler: CompilerAbstract) => ContractData, modal: (title: string, message: string | JSX.Element, okLabel: string, okFn: () => void, cancelLabel?: string, cancelFn?: () => void) => void, passphrase: string, setPassphrase: (passphrase: string) => void,