We are trying to modify the Polkadot-JS/apps UI to add an app of our own. We have cloned the Contracts page to make our own page so that we can easily call the contract message functions. Contract, like accounts, must be loaded into the browser of the user. We can do this with the Add component. But we want to automate it so that the user will not have to do it themselves. We have narrowed it down to this one file: Add.tsx (code below). Is it possible to alter this file to add just one particular contract whose public key and JSON file are known? We are OK with having a button that says "load the app", but would like to auto populate all the elements in that form. Something even more automated would be even better but we will take what we can get!
// Copyright 2017-2023 @polkadot/app-contracts authors & contributors
// SPDX-License-Identifier: Apache-2.0
//import { Abi } from '@polkadot/api-contract';
import type { ActionStatus } from '@polkadot/react-components/Status/types';
import JSONlifeAndWork from './claim_registration_v20230314.json';
import React, { useCallback, useState } from 'react';
import { AddressRow, Button, Input, Modal } from '@polkadot/react-components';
import { useApi, useNonEmptyString } from '@polkadot/react-hooks';
import { keyring } from '@polkadot/ui-keyring';
import { ABI, InputName } from '../shared';
import { useTranslation } from '../translate';
import useAbi from '../useAbi';
import ValidateAddr from './ValidateAddr';
interface Props {
defaultAddress: string;
onClose: () => void;
}
function Add ({ defaultAddress, onClose }: Props): React.ReactElement {
const { t } = useTranslation();
//const JSONlifeAndWork: string | null ='./claim_registration_v20230314.json';
//const _JSONParse = JSON.parse(JSONlifeAndWork.toString());
const JSONabi: string | null = JSON.stringify(JSONlifeAndWork);
const _contractName: string = 'Life & Work';
const { api } = useApi();
const [address, setAddress] = useState<string | null>(defaultAddress);
const [isAddressValid, setIsAddressValid] = useState(false);
const [name, isNameValid, setName] = useNonEmptyString(_contractName);
const { abi, contractAbi, errorText, isAbiError, isAbiSupplied, isAbiValid, onChangeAbi, onRemoveAbi } = useAbi([null, null], null, true);
const isTestInfo: boolean = false;
const _onAdd = useCallback(
(): void => {
const status: Partial<ActionStatus> = { action: 'create' };
if (!address || !abi || !name) {
return;
}
//const contract = new ContractPromise(api, JSONlifeAndWork, address);
try {
let json = {
contract: {
abi,
genesisHash: api.genesisHash.toHex()
},
name,
tags: []
};
keyring.saveContract(address, json);
status.account = address;
status.status = address ? 'success' : 'error';
status.message = 'contract added';
onClose();
} catch (error) {
console.error(error);
status.status = 'error';
status.message = (error as Error).message;
}
},
[abi, address, api, name, onClose]
);
const isValid = isAddressValid && isNameValid && isAbiValid;
return (
<Modal
header={t('Load the Life & Work Contract to your local browser: (Click Save)')}
onClose={onClose}
>
<Modal.Content>
{isTestInfo && (
<div>
<strong>{'STEPS: '}</strong> <br />
{'(1) Is Address Correct?: '}{(address && address.length===48)? 'YES' : 'NO'}<br />
{'(2) Is the Contract Name Correct: '}{(name && name==='Life & Work')? 'YES' : 'NO'}<br />
{'(3) Is JSON Loaded?: '}{(JSONabi.length>0)? 'YES' : 'NO'}<br />
{'(4) Is ABI Loaded?: '}{(abi)? 'YES! - Click Save' : 'NO'}<br /><br />
{'abi Length: '}{abi?.length}<br />
{'json Length: '}{JSONabi.length}<br />
</div>
)}
<AddressRow
defaultName={name}
isValid
value={address || null}
>
<Input
autoFocus
isError={!isAddressValid}
label={t<string>('contract address')}
onChange={setAddress}
value={address || ''}
/>
<ValidateAddr
address={address}
onChange={setIsAddressValid}
/>
<InputName
isContract
isError={!isNameValid}
onChange={setName}
value={name || undefined}
/>
<ABI
contractAbi={contractAbi}
errorText={errorText}
isError={isAbiError || !isAbiValid}
isSupplied={isAbiSupplied}
isValid={isAbiValid}
onChange={onChangeAbi}
onRemove={onRemoveAbi}
/>
</AddressRow>
</Modal.Content>
<Modal.Actions>
<Button
icon='save'
isDisabled={!isValid}
label={t<string>('Save')}
onClick={_onAdd}
/>
</Modal.Actions>
</Modal>
);
}
export default React.memo(Add);