Skip to content

Commit

Permalink
Merge pull request #212 from alephium/fix-display-nft
Browse files Browse the repository at this point in the history
Reduce API calls and fix NFT display issue
  • Loading branch information
polarker authored Aug 21, 2024
2 parents ed93f07 + ce77bab commit d05044d
Show file tree
Hide file tree
Showing 19 changed files with 203 additions and 140 deletions.
17 changes: 7 additions & 10 deletions packages/dapp/.project.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
{
"fullNodeVersion": "v3.3.0",
"fullNodeVersion": "v3.5.0",
"compilerOptionsUsed": {
"ignoreUnusedConstantsWarnings": false,
"ignoreUnusedVariablesWarnings": false,
"ignoreUnusedFieldsWarnings": false,
"ignoreUnusedPrivateFunctionsWarnings": false,
"ignoreUpdateFieldsCheckWarnings": false,
"ignoreCheckExternalCallerWarnings": false
"ignoreCheckExternalCallerWarnings": false,
"ignoreUnusedFunctionReturnWarnings": false
},
"infos": {
"Destroy": {
"sourceFile": "destroy.ral",
"sourceCodeHash": "81d9d935ecd97844aa8bb59687865673567402bdc1b9fa610ce2e435f3f4c87d",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
"codeHashDebug": ""
},
"IFungibleToken": {
"sourceFile": "../../../node_modules/@alephium/web3/std/fungible_token_interface.ral",
"sourceCodeHash": "62910bf11e1eeb6cb2fd468626ff606a9b06306b2b81590c3b10f6deb5966bde",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
"codeHashDebug": ""
},
"ShinyToken": {
"sourceFile": "shiny-token.ral",
"sourceCodeHash": "b6fdc5d87df9a51ee1611196216db85f14ee257cf7487ad2d7676f6518336c09",
"bytecodeDebugPatch": "",
"codeHashDebug": "9bdc139154d4e611dd391a5b262cc081d2519b9a3ccc95df943a98a9e3c67661",
"warnings": []
"codeHashDebug": "9bdc139154d4e611dd391a5b262cc081d2519b9a3ccc95df943a98a9e3c67661"
},
"Transfer": {
"sourceFile": "transfer.ral",
"sourceCodeHash": "2e2589a93623a069b127fd94f27a155d1794a80d1ee5f354457f61814e15bb40",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
"codeHashDebug": ""
}
}
}
5 changes: 1 addition & 4 deletions packages/dapp/artifacts/Destroy.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v3.3.0",
"version": "v3.5.0",
"name": "Destroy",
"bytecodeTemplate": "01010300000005{1}0d0c{0}0105",
"fieldsSig": {
Expand All @@ -19,9 +19,6 @@
"functions": [
{
"name": "main",
"usePreapprovedAssets": true,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand Down
20 changes: 1 addition & 19 deletions packages/dapp/artifacts/ShinyToken.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v3.3.0",
"version": "v3.5.0",
"name": "ShinyToken",
"bytecode": "050609121b40244041404f010000000102ce0002010000000102ce0102010000000102ce0202010000000102ce0302010202020008d36ee15a7b1600b11601ab160013c3038d7ea4c68000a8010201010003d320f98f621600b0",
"codeHash": "9bdc139154d4e611dd391a5b262cc081d2519b9a3ccc95df943a98a9e3c67661",
Expand Down Expand Up @@ -30,9 +30,6 @@
"functions": [
{
"name": "getSymbol",
"usePreapprovedAssets": false,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand All @@ -42,9 +39,6 @@
},
{
"name": "getName",
"usePreapprovedAssets": false,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand All @@ -54,9 +48,6 @@
},
{
"name": "getDecimals",
"usePreapprovedAssets": false,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand All @@ -66,9 +57,6 @@
},
{
"name": "getTotalSupply",
"usePreapprovedAssets": false,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand All @@ -78,9 +66,6 @@
},
{
"name": "transfer",
"usePreapprovedAssets": false,
"useAssetsInContract": true,
"isPublic": true,
"paramNames": [
"to",
"amount"
Expand All @@ -97,9 +82,6 @@
},
{
"name": "destroy",
"usePreapprovedAssets": false,
"useAssetsInContract": true,
"isPublic": true,
"paramNames": [
"to"
],
Expand Down
5 changes: 1 addition & 4 deletions packages/dapp/artifacts/Transfer.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v3.3.0",
"version": "v3.5.0",
"name": "Transfer",
"bytecodeTemplate": "01010300000006{1}{2}0e0c{0}0104",
"fieldsSig": {
Expand All @@ -22,9 +22,6 @@
"functions": [
{
"name": "main",
"usePreapprovedAssets": true,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand Down
53 changes: 27 additions & 26 deletions packages/dapp/artifacts/ts/ShinyToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,34 @@

import {
Address,
CallContractParams,
CallContractResult,
Contract,
ContractState,
TestContractResult,
HexString,
ContractEvent,
ContractFactory,
ContractInstance,
ContractState,
EventSubscribeOptions,
EventSubscription,
CallContractParams,
CallContractResult,
HexString,
SignExecuteContractMethodParams,
SignExecuteScriptTxResult,
TestContractParams,
ContractEvent,
subscribeContractEvent,
subscribeContractEvents,
testMethod,
callMethod,
multicallMethods,
fetchContractState,
ContractInstance,
getContractEventsCurrentCount,
TestContractParamsWithoutMaps,
TestContractResult,
TestContractResultWithoutMaps,
SignExecuteContractMethodParams,
SignExecuteScriptTxResult,
signExecuteMethod,
addStdIdToFields,
callMethod,
encodeContractFields,
fetchContractState,
getContractEventsCurrentCount,
multicallMethods,
signExecuteMethod,
subscribeContractEvent,
subscribeContractEvents,
testMethod,
} from "@alephium/web3";

import { default as ShinyTokenContractJson } from "../ShinyToken.ral.json";
import { getContractByCodeHash } from "./contracts";

Expand Down Expand Up @@ -83,6 +84,10 @@ export namespace ShinyTokenTypes {
? CallMethodTable[MaybeName]["result"]
: undefined;
};
export type MulticallReturnType<Callss extends MultiCallParams[]> =
Callss["length"] extends 1
? MultiCallResults<Callss[0]>
: { [index in keyof Callss]: MultiCallResults<Callss[index]> };

export interface SignExecuteMethodTable {
getSymbol: {
Expand Down Expand Up @@ -128,10 +133,6 @@ class Factory extends ContractFactory<
);
}

getInitialFieldsWithDefaultValues() {
return this.contract.getInitialFieldsWithDefaultValues() as ShinyTokenTypes.Fields;
}

at(address: string): ShinyTokenInstance {
return new ShinyTokenInstance(address);
}
Expand Down Expand Up @@ -310,14 +311,14 @@ export class ShinyTokenInstance extends ContractInstance {
},
};

async multicall<Calls extends ShinyTokenTypes.MultiCallParams>(
calls: Calls
): Promise<ShinyTokenTypes.MultiCallResults<Calls>> {
async multicall<Callss extends ShinyTokenTypes.MultiCallParams[]>(
...callss: Callss
): Promise<ShinyTokenTypes.MulticallReturnType<Callss>> {
return (await multicallMethods(
ShinyToken,
this,
calls,
callss,
getContractByCodeHash
)) as ShinyTokenTypes.MultiCallResults<Calls>;
)) as ShinyTokenTypes.MulticallReturnType<Callss>;
}
}
1 change: 1 addition & 0 deletions packages/dapp/artifacts/ts/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable */

import { Contract, ContractFactory } from "@alephium/web3";

import { ShinyToken } from ".";

let contracts: ContractFactory<any>[] | undefined = undefined;
Expand Down
5 changes: 3 additions & 2 deletions packages/dapp/artifacts/ts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
ExecutableScript,
ExecuteScriptParams,
ExecuteScriptResult,
HexString,
Script,
SignerProvider,
HexString,
} from "@alephium/web3";
import { getContractByCodeHash } from "./contracts";

import { default as DestroyScriptJson } from "../Destroy.ral.json";
import { default as TransferScriptJson } from "../Transfer.ral.json";
import { getContractByCodeHash } from "./contracts";

export const Destroy = new ExecutableScript<{
shinyToken: HexString;
Expand Down
6 changes: 3 additions & 3 deletions packages/dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"lint": "next lint"
},
"dependencies": {
"@alephium/get-extension-wallet": "^1.2.2",
"@alephium/web3": "^1.2.2",
"@alephium/get-extension-wallet": "^1.5.0",
"@alephium/web3": "^1.5.0",
"ethers": "^5.5.1",
"next": "^13.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"@alephium/cli": "^1.2.2",
"@alephium/cli": "^1.5.0",
"@types/node": "18.11.18",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@alephium/get-extension-wallet": "^1.2.2",
"@alephium/get-extension-wallet": "^1.5.0",
"@alephium/ledger-app": "0.5.2",
"@alephium/token-list": "0.0.19",
"@alephium/web3": "^1.2.2",
"@alephium/web3-test": "^1.2.2",
"@alephium/web3-wallet": "^1.2.2",
"@alephium/web3": "^1.5.0",
"@alephium/web3-test": "^1.5.0",
"@alephium/web3-wallet": "^1.5.0",
"@ledgerhq/hw-transport-webusb": "6.29.0",
"@ledgerhq/hw-transport-webhid": "6.29.0",
"@playwright/test": "^1.23.0",
Expand Down
5 changes: 4 additions & 1 deletion packages/extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import browser from "webextension-polyfill"
import { getAccountSelector, withoutHiddenSelector } from "../shared/account/selectors"

import { accountStore, getAccounts } from "../shared/account/store"
import { globalActionQueueStore } from "../shared/actionQueue/store"
Expand Down Expand Up @@ -44,7 +45,9 @@ setTokenListUpdateAlarm()
browser.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === transactionTrackerHistoryAlarm) {
console.info("~> fetching transaction history")
await transactionTracker.loadHistory(await getAccounts())
const selectedAccount = await walletStore.get("selected")
const accountSelector = selectedAccount ? getAccountSelector(selectedAccount) : withoutHiddenSelector
await transactionTracker.loadHistory(await getAccounts(accountSelector))
}

if (alarm.name === transactionTrackerUpdateAlarm) {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/background/networkStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const swrStorage = new KeyValueStorage<Record<SwrCacheKey, any>>(
// see: https://github.com/jperasmus/stale-while-revalidate-cache#configuration
const swr = createStaleWhileRevalidateCache({
storage: swrStorage, // can be any object with getItem and setItem methods
minTimeToStale: 60e3, // 1 minute
minTimeToStale: 2 * 60e3, // 2 minutes
maxTimeToLive: 30 * 60e3, // 30 minutes
})

Expand Down
8 changes: 8 additions & 0 deletions packages/extension/src/shared/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ export const getErrorObject = (error: any, includeStack = true) => {
// ignore parsing error
}
}

const statusCodeRegex = /Status\s+code:\s+(\d{3})/
export const extractStatusCode = (error: any): number | undefined => {
const match = (error instanceof Error) && error.message.match(statusCodeRegex)
if (match) {
return Number(match[1])
}
}
8 changes: 8 additions & 0 deletions packages/extension/src/shared/utils/fetchImmutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export async function fetchImmutable<Data>(key: string, fetcher: Fetcher<Data, s
}
}

export async function getImmutable<Data>(key: string): Promise<Data | undefined> {
return await fetchImmutableStorage.get(key)
}

export async function storeImmutable<Data>(key: string, data: Data): Promise<void> {
return await fetchImmutableStorage.set(key, data)
}

export async function clearCache(key: string): Promise<void> {
await fetchImmutableStorage.delete(key)
}
Loading

0 comments on commit d05044d

Please sign in to comment.