Skip to content

Commit

Permalink
Reworked contract detection + added hash
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Feb 21, 2025
1 parent 0d81ff4 commit d3cf6b4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/button/copy.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
async function copyToClipboard() {
try {
await navigator.clipboard.writeText(props.data);
if (context.settings.data.debugMode) console.log(props.data, 'copied to clipboard');
if (context.settings.data.debugMode) console.info(props.data, 'copied to clipboard');
hint = true;
setTimeout(() => (hint = false), 300);
} catch (err) {
Expand Down
11 changes: 7 additions & 4 deletions src/lib/state/client/account.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import type {
} from '$lib/types/account';

import { calculateValue, isSameToken } from '$lib/utils';
import { defaultAccountDataSources, defaultVoteInfo } from '$lib/state/defaults/account';
import {
defaultAccountDataSources,
defaultVoteInfo,
nullContractHash
} from '$lib/state/defaults/account';
import * as SystemContract from '$lib/wharf/contracts/system';

export class AccountState {
Expand All @@ -35,9 +39,7 @@ export class AccountState {

public name: Name = $state(Name.from(''));
public last_update: Date = $state(new Date());
public contract: boolean = $derived(
Number(new Date(`${this.sources?.get_account?.last_code_update}z`)) > 0
);
public contract: boolean = $derived(!this.sources.contract_hash.equals(nullContractHash));
public loaded: boolean = $state(false);

public balance = $derived.by(() =>
Expand Down Expand Up @@ -105,6 +107,7 @@ export class AccountState {
this.last_update = new Date();
this.sources = {
get_account: data.get_account,
contract_hash: Checksum256.from(data.contract_hash),
balance: data.balance,
giftedram: data.giftedram,
light_api: data.light_api,
Expand Down
7 changes: 6 additions & 1 deletion src/lib/state/defaults/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { API, Asset, Float64, Int64, Name } from '@wharfkit/antelope';
import { API, Asset, Checksum256, Float64, Int64, Name } from '@wharfkit/antelope';

import { gifted_ram, type AccountDataSources, type VoterInfo } from '$lib/types/account';

Expand Down Expand Up @@ -75,8 +75,13 @@ export const defaultRexFund = SystemContract.Types.rex_fund.from({
balance: '0 '
});

export const nullContractHash = Checksum256.from(
'0000000000000000000000000000000000000000000000000000000000000000'
);

export const defaultAccountDataSources: AccountDataSources = {
get_account: defaultGetAccount,
contract_hash: nullContractHash,
balance: defaultAsset,
light_api: [],
delegated: [],
Expand Down
14 changes: 1 addition & 13 deletions src/lib/state/defaults/network.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { API, Asset } from '@wharfkit/antelope';

import { gifted_ram, type AccountDataSources } from '$lib/types/account';
import { gifted_ram } from '$lib/types/account';

import * as SystemContract from '$lib/wharf/contracts/system';

Expand Down Expand Up @@ -75,17 +75,5 @@ export const defaultRexFund = SystemContract.Types.rex_fund.from({
balance: '0 '
});

export const defaultAccountDataSources: AccountDataSources = {
get_account: defaultGetAccount,
balance: defaultAsset,
light_api: [],
delegated: [],
giftedram: defaultGiftedRam,
proposals: [],
refund_request: defaultRefundRequest,
rexbal: defaultRexBalance,
rexfund: defaultRexFund
};

export const defaultPriceSymbol = Asset.Symbol.from('4,USD');
export const defaultPrice = Asset.fromUnits(0, defaultPriceSymbol);
13 changes: 12 additions & 1 deletion src/lib/types/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Struct, API, Int64, Name, Asset, Float64 } from '@wharfkit/antelope';
import {
Struct,
API,
Int64,
Name,
Asset,
Float64,
type Checksum256Type,
Checksum256
} from '@wharfkit/antelope';

import { Types as MsigTypes } from '$lib/wharf/contracts/msig';
import { Types as SystemTypes } from '$lib/wharf/contracts/system';
Expand Down Expand Up @@ -29,6 +38,8 @@ export interface VoterInfo {
export interface AccountDataSources {
// Native get_account endpoint (deprecated?)
get_account: API.v1.AccountObject;
// Hash of the contract on the account
contract_hash: Checksum256;
// Light API balances call
light_api: LightAPIBalanceRow[];
// Table rows from eosio.token::accounts
Expand Down
10 changes: 8 additions & 2 deletions src/routes/[network]/api/account/[name]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { LightAPIBalanceResponse, LightAPIBalanceRow } from '$lib/types/lig
import type { RequestEvent, RequestHandler } from './$types';

import { Types as SystemTypes } from '$lib/wharf/contracts/system';
import { nullContractHash } from '$lib/state/defaults/account';

export const GET: RequestHandler = async ({ locals: { network }, params }: RequestEvent) => {
const headers = getCacheHeaders(5);
Expand Down Expand Up @@ -58,10 +59,11 @@ async function loadBalances(
async function getAccount(network: NetworkState, account: NameType): Promise<AccountDataSources> {
const { system: systemContract, msig: msigContract } = network.contracts;

const [get_account, delegated, proposals] = await Promise.all([
const [get_account, delegated, proposals, hash] = await Promise.all([
network.client.v1.chain.get_account(account),
systemContract.table('delband').all({ scope: account }),
msigContract.table('proposal', account).all()
msigContract.table('proposal', account).all(),
systemContract.table('abihash').get(account)
]);

let rex;
Expand Down Expand Up @@ -108,8 +110,11 @@ async function getAccount(network: NetworkState, account: NameType): Promise<Acc
rexfund = SystemTypes.rex_fund.from(rex);
}

const contract_hash = hash?.hash || nullContractHash;

return {
get_account,
contract_hash,
balance: get_account.core_liquid_balance || defaultBalance,
light_api: balances,
delegated,
Expand Down Expand Up @@ -146,6 +151,7 @@ async function getAccount2(network: NetworkState, account: NameType): Promise<Ac

return {
get_account,
contract_hash: getaccount.contracthash,
balance: getaccount.balance,
light_api: balances,
delegated: getaccount.delegations,
Expand Down

0 comments on commit d3cf6b4

Please sign in to comment.