forked from forbole/big-dipper-2.0-cosmos
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate smart contracts execution tab
- Loading branch information
Showing
13 changed files
with
1,387 additions
and
181 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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
NEXT_PUBLIC_GRAPHQL_URL=http://localhost:8080/v1/graphql | ||
NEXT_PUBLIC_GRAPHQL_WS=ws://localhost:8080/v1/graphql | ||
NEXT_PUBLIC_GRAPHQL_URL=http://34.122.182.3:8080/v1/graphql | ||
NEXT_PUBLIC_GRAPHQL_WS=ws://34.122.182.3:8080/v1/graphql | ||
NODE_ENV=development | ||
PORT=3000 | ||
NEXT_PUBLIC_URL=http://localhost:3000 | ||
NEXT_PUBLIC_WS_CHAIN_URL=ws://localhost:26657/websocket | ||
NEXT_PUBLIC_WS_CHAIN_URL=ws://34.123.153.6:26657/websocket | ||
NEXT_PUBLIC_CHAIN_STATUS=testnet | ||
NEXT_PUBLIC_CONTRACTS_URL=http://localhost:3333 | ||
NEXT_PUBLIC_RPC_URL=http://34.123.153.6:26657 | ||
# TODO: This can be fetched direclty from hasura | ||
NEXT_PUBLIC_CHAIN_ID=cudos-testnet-private-3 | ||
NEXT_PUBLIC_GAS_PRICE=5000000000000acudos |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
version: '3.6' | ||
services: | ||
big-dipper-2: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile-dev | ||
args: | ||
NEXT_PUBLIC_GRAPHQL_URL: ${NEXT_PUBLIC_GRAPHQL_URL} | ||
NEXT_PUBLIC_GRAPHQL_WS: ${NEXT_PUBLIC_GRAPHQL_WS} | ||
NEXT_PUBLIC_URL: ${NEXT_PUBLIC_URL} | ||
NEXT_PUBLIC_WS_CHAIN_URL: ${NEXT_PUBLIC_WS_CHAIN_URL} | ||
NEXT_PUBLIC_CHAIN_STATUS: ${NEXT_PUBLIC_CHAIN_STATUS} | ||
NODE_ENV: ${NODE_ENV} | ||
PORT: ${PORT} | ||
restart: always | ||
ports: | ||
- "3000:3000" |
Large diffs are not rendered by default.
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
190 changes: 190 additions & 0 deletions
190
src/screens/account_details/components/smart_contract/index.tsx
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 |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import { | ||
useEffect, useState, | ||
} from 'react'; | ||
import Form from 'react-jsonschema-form'; | ||
import { | ||
connectWasmQueryClient, connectWasmSigningClient, | ||
getAccount, getQueryFunc, getExecuteFunc, | ||
} from '@utils/smart_contract_interaction'; | ||
import { | ||
SigningCosmWasmClient, CosmWasmClient, | ||
} from 'cudosjs'; | ||
|
||
interface ISmartContractInteractionProps { | ||
rpcUrl: string; | ||
chainID: string, | ||
gasPrice:string, | ||
address: string, | ||
querySchema?: string, | ||
executeSchema?: string, | ||
} | ||
|
||
type SmartContractInteractionDetails = { | ||
account: any; | ||
querySchemaObj: any; | ||
executeSchemaObj: any; | ||
queryClient: CosmWasmClient; | ||
executeClient: SigningCosmWasmClient; | ||
} | ||
|
||
const initialState: SmartContractInteractionDetails = { | ||
account: '', | ||
querySchemaObj: null, | ||
executeSchemaObj: null, | ||
queryClient: null, | ||
executeClient: null, | ||
}; | ||
|
||
const SmartContractInteraction = (props: ISmartContractInteractionProps) => { | ||
const [state, setState] = useState<SmartContractInteractionDetails>(initialState); | ||
|
||
useEffect(() => { | ||
const newState: any = {}; | ||
|
||
if (props.querySchema) { | ||
const querySchemaObj = JSON.parse(props.querySchema); | ||
|
||
AddMissingTitles(querySchemaObj, ''); | ||
|
||
newState.querySchemaObj = querySchemaObj; | ||
|
||
connectWasmQueryClient(props.rpcUrl).then((client) => { | ||
setState((prevState) => ({ | ||
...prevState, | ||
queryClient: client, | ||
})); | ||
}).catch((err) => { | ||
console.error(`connecting query client failed ${err}`); | ||
}); | ||
} | ||
|
||
if (props.executeSchema) { | ||
const executeSchemaObj = JSON.parse(props.executeSchema); | ||
|
||
AddMissingTitles(executeSchemaObj, ''); | ||
|
||
newState.executeSchemaObj = executeSchemaObj; | ||
|
||
getAccount(props.chainID).then((acc) => { | ||
setState((prevState) => ({ | ||
...prevState, | ||
account: acc, | ||
})); | ||
}).catch((err) => { | ||
console.error(`getting account failed ${err}`); | ||
}); | ||
|
||
connectWasmSigningClient(props.chainID, props.rpcUrl, props.gasPrice).then((client) => { | ||
setState((prevState) => ({ | ||
...prevState, | ||
executeClient: client, | ||
})); | ||
}).catch((err) => { | ||
console.error(`connecting execute client failed ${err}`); | ||
}); | ||
} | ||
|
||
setState((prevState) => ({ | ||
...prevState, | ||
...newState, | ||
})); | ||
}, []); | ||
|
||
const onSubmitQuery = (event: any) => { | ||
const formData = getFormData(event, 0); | ||
const queryFunc = getQueryFunc(state.queryClient, props.address); | ||
queryFunc(formData).then((res) => { | ||
console.log(res); | ||
}); | ||
}; | ||
|
||
const onSubmitExecute = async (event: any) => { | ||
const formData = getFormData(event, 1); | ||
const executeFunc = getExecuteFunc(state.executeClient, props.address); | ||
executeFunc(state.account.address, formData).then((res) => { | ||
console.log(res); | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
{ | ||
state.querySchemaObj | ||
&& <h2>Query:</h2> | ||
&& <Form schema={state.querySchemaObj} onSubmit={onSubmitQuery} noValidate /> | ||
} | ||
{ | ||
state.executeSchemaObj | ||
&& <h2>Execute:</h2> | ||
&& <Form schema={state.executeSchemaObj} onSubmit={onSubmitExecute} noValidate /> | ||
} | ||
</> | ||
); | ||
}; | ||
|
||
SmartContractInteraction.defaultProps = { | ||
querySchema: '', | ||
executeSchema: '', | ||
}; | ||
|
||
const getFormData = (event: any, menuIndex: number): string => { | ||
const menu = document.querySelectorAll('select[id=\'root_anyof_select\']')[menuIndex] as any; | ||
const selectedOption = menu.selectedOptions[0].text as string; | ||
|
||
if (!(selectedOption in event.formData)) { | ||
throw new Error('Invalid form data'); | ||
} | ||
|
||
// Not using event.formData directly becasue it may contain unnecessary properties | ||
|
||
return JSON.stringify({ [selectedOption]: event.formData[selectedOption] }); | ||
}; | ||
|
||
const AddMissingTitles = (obj: any, parentProp: string) => { | ||
if (typeof obj !== 'object') { | ||
return; | ||
} | ||
|
||
const keys = Object.keys(obj); | ||
|
||
for (const key of keys) { | ||
if (!obj.hasOwnProperty(key)) { | ||
continue; | ||
} | ||
|
||
let nextKey = key; | ||
|
||
if (Number.isInteger(key)) { | ||
nextKey = parentProp; | ||
} | ||
|
||
AddMissingTitles(obj[key], nextKey); | ||
|
||
if (['oneOf', 'anyOf'].includes(parentProp)) { | ||
AddMissingTitle(obj[nextKey]); | ||
} | ||
} | ||
}; | ||
|
||
const AddMissingTitle = (obj: any) => { | ||
if ('title' in obj) { | ||
return; | ||
} | ||
|
||
if ('required' in obj && obj.required.length == 1) { | ||
obj.title = obj.required[0]; | ||
return; | ||
} | ||
|
||
if ('type' in obj && obj.type === 'null') { | ||
obj.title = 'None'; | ||
return; | ||
} | ||
|
||
if ('$ref' in obj) { | ||
const parts = obj.$ref.split('/'); | ||
obj.title = parts[parts.length - 1]; | ||
} | ||
}; | ||
|
||
export default SmartContractInteraction; |
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.