Skip to content

Commit

Permalink
[FeatFix][582] - validate bool type for read/write contract (#3484)
Browse files Browse the repository at this point in the history
[FeatFix][611] - fix switch chain
  • Loading branch information
tamvb2391 committed Jun 7, 2024
1 parent 64204e2 commit 3ccc377
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 40 deletions.
57 changes: 25 additions & 32 deletions src/app/core/services/wallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ export class WalletService implements OnDestroy {
this.connectToChain();
}, 1000);
};
(window as any).ethereum.on('accountsChanged', reconnect);
(window as any).ethereum.on('chainChanged', reconnect);
(window as any).ethereum?.on('accountsChanged', reconnect);
(window as any).ethereum?.on('chainChanged', reconnect);
}

private async _getSigningCosmWasmClientAuto() {
Expand Down Expand Up @@ -422,13 +422,9 @@ export class WalletService implements OnDestroy {
}

async connectEvmWallet() {
const network = await checkNetwork(this.env.evmChainInfo.chainId);

if (!network) {
const connected = await this.connectToChain();
if (!connected) {
return;
}
const connected = await this.connectToChain();
if (!connected) {
return;
}

getSigner(this.env.etherJsonRpc).then((signer) => {
Expand All @@ -448,31 +444,28 @@ export class WalletService implements OnDestroy {
}

async connectToChain() {
const network = await checkNetwork(this.env.evmChainInfo.chainId);

if (!network) {
const metamask = getMetamask();
const chainId = '0x' + this.env.evmChainInfo.chainId.toString(16);
try {
await metamask.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: chainId }],
});
} catch (switchError: any) {
switch (switchError.code) {
case 4902:
// This error code indicates that the chain has not been added to MetaMask.
await addNetwork(this.env.evmChainInfo);
break;
case 4001:
// This error code : "User rejected the request."
return false;
case -32002:
// This error code : "Request of type 'wallet_switchEthereumChain' already pending"
return false;
}
const metamask = getMetamask();
const chainId = '0x' + this.env.evmChainInfo.chainId.toString(16);
try {
await metamask.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: chainId }],
});
} catch (switchError: any) {
switch (switchError.code) {
case 4902:
// This error code indicates that the chain has not been added to MetaMask.
await addNetwork(this.env.evmChainInfo);
break;
case 4001:
// This error code : "User rejected the request."
return false;
case -32002:
// This error code : "Request of type 'wallet_switchEthereumChain' already pending"
return false;
}
}

return true;
}

Expand Down
9 changes: 7 additions & 2 deletions src/app/core/utils/ethers/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ export function validateAndParsingInput(type: JsonFragmentType, value: any) {
switch (type.type) {
case 'uint256':
case 'bytes4':
return value;
return { value };
case 'bool':
if (value?.toLowerCase() !== 'true' && value?.toLowerCase() !== 'false') {
return { value, error: `"${value}" is an invalid parameter, please use true/false.` };
}
return { value: value?.toLowerCase() === 'true' ? true : false };
default:
return value;
return { value };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ export class EvmReadComponent implements OnChanges {
return validateAndParsingInput(i, value); // TODO
});

const errorParams = params.map((i) => i.error).filter((f) => f);
if (errorParams.length > 0) {
jsonFragment.isLoading = false;
jsonFragment.error = {
code: 'INVALID_ARGUMENT',
message: errorParams.join(' '),
};
return;
}

const connected = await this.walletService.connectToChain();
if (!connected) {
jsonFragment.isLoading = false;
Expand All @@ -153,7 +163,9 @@ export class EvmReadComponent implements OnChanges {
jsonFragment.isLoading = true;
jsonFragment.result = undefined;
jsonFragment.error = undefined;
contract[name]?.(...params)
const paramsData = params.map((i) => i.value);

contract[name]?.(...paramsData)
.then((res) => {
jsonFragment.result = res;
jsonFragment.isLoading = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ export class EvmWriteComponent implements OnChanges {
return validateAndParsingInput(i, value); // TODO
});

const errorParams = params.map((i) => i.error).filter((f) => f);
if (errorParams.length > 0) {
jsonFragment.isLoading = false;
jsonFragment.error = {
code: 'INVALID_ARGUMENT',
message: errorParams.join(' '),
};
return;
}

const fundAmount = formControls['fund']?.value || '0';

const connected = await this.walletService.connectToChain();
Expand All @@ -172,9 +182,10 @@ export class EvmWriteComponent implements OnChanges {
jsonFragment.result = undefined;
jsonFragment.error = undefined;

const paramsData = params.map((i) => i.value);
const nameContract = `${name}(${paramsDes.join(',')})`;
const x = await contract[nameContract]?.estimateGas(...params).catch((e) => e);
contract[nameContract]?.(...params, {
const x = await contract[nameContract]?.estimateGas(...paramsData).catch((e) => e);
contract[nameContract]?.(...paramsData, {
gasLimit: Number(x) || 250_000,
gasPrice: 1_000_0000,
value: parseEther(fundAmount),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ <h1 class="fw-bold sub-text text--white mb-0 mr-1 break-word">
<h2 class="mb-0 h5-mob text--white card__ntf-detail-item" id="popupCopy">Details</h2>
<div class="card__ntf-detail-item body">
<div class="label-detail text--gray-5">Owner:</div>
<div class="d-flex align-items-center position-relative" *ngIf="nftDetail?.owner && !nftDetail?.burned">
<div class="d-flex align-items-center position-relative" *ngIf="nftDetail?.owner">
<app-name-tag
[value]="nftDetail?.owner | beautyAddress"
[fullText]="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class EvmNFTDetailComponent implements OnInit {
templates: Array<TableTemplate> = [
{ matColumnDef: 'tx_hash', headerCellDef: 'Txn Hash' },
{ matColumnDef: 'type', headerCellDef: 'Method' },
{ matColumnDef: 'status', headerCellDef: 'Result' },
// { matColumnDef: 'status', headerCellDef: 'Result' },
{ matColumnDef: 'timestamp', headerCellDef: 'Time' },
{ matColumnDef: 'from_address', headerCellDef: 'From' },
{ matColumnDef: 'to_address', headerCellDef: 'To' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class NFTDetailComponent implements OnInit {
templates: Array<TableTemplate> = [
{ matColumnDef: 'tx_hash', headerCellDef: 'Txn Hash' },
{ matColumnDef: 'type', headerCellDef: 'Method' },
{ matColumnDef: 'status', headerCellDef: 'Result' },
// { matColumnDef: 'status', headerCellDef: 'Result' },
{ matColumnDef: 'timestamp', headerCellDef: 'Time' },
{ matColumnDef: 'from_address', headerCellDef: 'From' },
{ matColumnDef: 'to_address', headerCellDef: 'To' },
Expand Down
3 changes: 3 additions & 0 deletions src/app/pages/validators/validators.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ export class ValidatorsComponent implements OnInit, OnDestroy {
try {
const connected = await this.walletService.connectToChain();
if (!connected) {
this.isLoading = false;
this.isHandleStake = false;
this.toastr.error(`Please switch to ${this.environmentService.evmChainInfo.chain} chain.`);
return null;
}

Expand Down

0 comments on commit 3ccc377

Please sign in to comment.