Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function getContractAbi (address: string | null): Abi | null {
if (!address) {
return null;
}
let abi: Abi | undefined;
const meta = getAddressMeta(address, 'contract');
try {
const data = meta.contract && JSON.parse(meta.contract.abi);
abi = new Abi(registry, data);
} catch (error) {
// invalid address, maybe
}
return abi || null;
}
private addCode (json: CodeJson): void {
try {
const abi = json.abi ? JSON.parse(json.abi) : null;
this.allCode[json.codeHash] = {
json,
contractAbi: abi
? new Abi(registry, abi)
: undefined
};
this.emit('new-code');
} catch (error) {
console.error(error);
}
}
private onChange = (u8a: Uint8Array): void => {
const { onChange, t } = this.props;
const json = u8aToString(u8a);
try {
const abi = JSON.parse(json);
if (abi.deploy || abi.messages) {
throw new Error(t('You are using an ABI with an outdated format. Please generate a new one.'));
}
const contractAbi = new Abi(registry, abi);
this.setState({
contractAbi,
isAbiValid: true,
isEmpty: false,
isError: false
}, (): void => onChange(json, contractAbi));
} catch (error) {
console.error(error);
this.setState({
isAbiValid: false,
isEmpty: false,
isError: true,
errorText: error
}, (): void => onChange(null, null));
export function getContractForAddress (api: ApiPromise, address: StringOrNull): Contract | null {
if (!address) {
return null;
} else {
const abi = getContractAbi(address);
return abi
? new Contract(api, abi, address)
: null;
}
}