Skip to content

Commit

Permalink
[Update][619] convert call data account info from horo to lcd (#3619)
Browse files Browse the repository at this point in the history
* [fix][598] fix set token info

* Update: convert call data account info from horo to lcd

* update function recursion to get value
  • Loading branch information
TranTrungTien authored Jul 11, 2024
1 parent cbe437f commit 03301a8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/app/core/services/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { isEvmAddress } from '../utils/common/validation';

@Injectable()
export class AccountService extends CommonService {
lcd = this.environmentService.chainConfig.chain_info.rest;

constructor(
private http: HttpClient,
private environmentService: EnvironmentService,
Expand All @@ -21,6 +23,10 @@ export class AccountService extends CommonService {
super(http, environmentService);
}

getAccountInfo(address: string) {
return this.http.get(`${this.lcd}/cosmos/auth/v1beta1/accounts/${address}`);
}

getAccountDetail(account_id: string | number): Observable<any> {
return this.apiService.getAccountByAddress(account_id as string);
}
Expand Down
25 changes: 25 additions & 0 deletions src/app/core/utils/ethers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,29 @@ export const hex2a = (hex: string) => {
for (let i = 0; i < data.length; i += 2)
str += String.fromCharCode(parseInt(data.substr(i, 2), 16));
return str;
}

export const getValueOfKeyInObject = (obj: object, key: string) => {
let result = null;
if(obj instanceof Array) {
for(const element of obj) {
result = getValueOfKeyInObject(element, key);
if (result) {
break;
}
}
} else {
for(let prop in obj) {
if(prop === key) {
return obj[key] || {};
}
if(obj[prop] instanceof Object || obj[prop] instanceof Array) {
result = getValueOfKeyInObject(obj[prop], key);
if (result) {
break;
}
}
}
}
return result;
}
19 changes: 8 additions & 11 deletions src/app/pages/account/account-detail/account-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { AccountService } from '../../../core/services/account.service';
import { CommonService } from '../../../core/services/common.service';
import { CHART_OPTION, ChartOptions, chartCustomOptions } from './chart-options';
import { isAddress, isEvmAddress } from '../../../core/utils/common/validation';
import { getValueOfKeyInObject } from 'src/app/core/utils/ethers/utils';

@Component({
selector: 'app-account-detail',
Expand Down Expand Up @@ -139,32 +140,28 @@ export class AccountDetailComponent implements OnInit, OnDestroy {
this.getAccountDetail();
this.checkWatchList();

const payload = {
address: accountAddress,
};

if(accountAddress === BASE_ACCOUNT_ADDRESS.cosmos && accountEvmAddress === BASE_ACCOUNT_ADDRESS.evm) {
this.accountType = 'evm';
this.tooltipCosmosText = COSMOS_WARNING_MESSAGE;
return;
}

this.userService.getAccountInfoOfAddress(payload).subscribe({
next: (data: { account?: {type?: any; sequence?: number; pubkey?: object}[] } = {}) => {
this.accountService.getAccountInfo(accountAddress).subscribe({
next: (data: { account?: {'@type'?: string } } = {}) => {
const { account } = data;
const { "@type": type } = account || {};

if(!account?.length) return;

const { type, sequence, pubkey = {} } = account[0] || {};

if (type === EVM_ACCOUNT_MESSAGE_TYPE) {
this.accountType = 'evm';
this.tooltipCosmosText = COSMOS_WARNING_MESSAGE;
return;
}

if (type === COSMOS_ACCOUNT_MESSAGE_TYPE) {
if (!sequence) return;
const sequence = getValueOfKeyInObject(account, 'sequence');
const pubkey = getValueOfKeyInObject(account, 'pub_key');

if (Number(sequence) <= 0) return;

if(!pubkey || !Object.keys(pubkey)?.length) {
this.accountType = 'evm';
Expand Down

0 comments on commit 03301a8

Please sign in to comment.