Skip to content

Commit

Permalink
Merge branch 'main' into balance-label-width-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
0xDaedalus authored Mar 17, 2023
2 parents 346cae4 + d42e4ae commit 2a53385
Show file tree
Hide file tree
Showing 91 changed files with 2,263 additions and 1,035 deletions.
1 change: 0 additions & 1 deletion .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ POSTHOG_PERSONAL_API_KEY="phx_pE7GvpSzv207stzDwDqUOMfg8SzNzojzaFLrSdwKWE9"
USE_ANALYTICS_SOURCE="DEV"
POAP_API_KEY="Djq0jJrWaQCzzvvoTqy0BgFidzy4bugDX1mTY28EZpqcUDSNICLcVQnex2wY7D9t5QB2aQlPQRdw8GiOVcE5kgQywGiDraJDfd3GVxnDASm1PoRBjOBh9fOrgymNsMQc"
SUPPORT_WALLET_CONNECT=false
SUPPORT_ABILITIES=true
SUPPORT_CUSTOM_NETWORKS=false
SUPPORT_CUSTOM_RPCS=false
7 changes: 3 additions & 4 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
# exfiltration and general "roll your own crypto" mistakes. Newer
# contributions to keyring code should be assumed insecure, requiring
# agreement across the team to merge.
/background/services/keyring/* @shadowfiend @mhluongo
/background/services/keyring/* @tahowallet/extension-security-auditors
# Any changes to dependencies deserve extra scrutiny to help prevent supply
# chain attacks
package.json @greg-nagy @0xDaedalus @shadowfiend @mhluongo
yarn.lock @tallyhowallet/extension-dependency-auditors
yarn.lock @tahowallet/extension-dependency-auditors
# Any changes to code owners deserve extra scrutiny
.github/CODEOWNERS @shadowfiend @mhluongo
.github/CODEOWNERS @tahowallet/extension-security-auditors
4 changes: 4 additions & 0 deletions .github/ISSUE_TEMPLATE/BUG.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ body:
label: Version
description: What version of the extension are you running?
options:
- v0.28.0
- v0.27.2
- v0.27.1
- v0.27.0
- v0.26.2
- v0.26.1
- v0.26.0
Expand Down
15 changes: 9 additions & 6 deletions background/constants/base-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ARBITRUM_ONE_ETH: NetworkBaseAsset = {
...ETH,
chainID: "42161",
metadata: {
coinGeckoID: "arbitrum-one",
coinGeckoID: "ethereum",
tokenLists: [],
},
}
Expand All @@ -24,7 +24,7 @@ const ARBITRUM_NOVA_ETH: NetworkBaseAsset = {
...ETH,
chainID: "42170",
metadata: {
coinGeckoID: "arbitrum-nova",
coinGeckoID: "ethereum",
tokenLists: [],
},
}
Expand All @@ -33,7 +33,7 @@ const OPTIMISTIC_ETH: NetworkBaseAsset = {
...ETH,
chainID: "10",
metadata: {
coinGeckoID: "optimistic-ethereum",
coinGeckoID: "ethereum",
tokenLists: [],
},
}
Expand Down Expand Up @@ -64,8 +64,9 @@ const MATIC: NetworkBaseAsset = {
symbol: "MATIC",
decimals: 18,
metadata: {
coinGeckoID: "polygon-pos",
coinGeckoID: "matic-network",
tokenLists: [],
websiteURL: "https://polygon.technology/",
},
}

Expand All @@ -75,8 +76,9 @@ const AVAX: NetworkBaseAsset = {
symbol: "AVAX",
decimals: 18,
metadata: {
coinGeckoID: "avalanche",
coinGeckoID: "avalanche-2",
tokenLists: [],
websiteURL: "https://avax.network/",
},
}

Expand All @@ -86,8 +88,9 @@ const BNB: NetworkBaseAsset = {
symbol: "BNB",
decimals: 18,
metadata: {
coinGeckoID: "binance-smart-chain",
coinGeckoID: "binancecoin",
tokenLists: [],
websiteURL: "https://bnbchain.org",
},
}

Expand Down
10 changes: 9 additions & 1 deletion background/constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export function isBuiltInNetwork(network: EVMNetwork): boolean {
)
}

export const DEFAULT_NETWORKS_BY_CHAIN_ID = new Set(
DEFAULT_NETWORKS.map((network) => network.chainID)
)

export const FORK: EVMNetwork = {
name: "Ethereum",
baseAsset: ETH,
Expand Down Expand Up @@ -168,7 +172,11 @@ export const CHAIN_ID_TO_RPC_URLS: {
[chainId: string]: Array<string> | undefined
} = {
[ROOTSTOCK.chainID]: ["https://public-node.rsk.co"],
[POLYGON.chainID]: ["https://polygon-rpc.com", "https://1rpc.io/matic"],
[POLYGON.chainID]: [
"https://1rpc.io/matic",
// This one sometimes returns 0 for eth_getBalance
"https://polygon-rpc.com",
],
[OPTIMISM.chainID]: [
"https://rpc.ankr.com/optimism",
"https://1rpc.io/op",
Expand Down
1 change: 0 additions & 1 deletion background/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const RuntimeFlag = {
SUPPORT_NFT_SEND: process.env.SUPPORT_NFT_SEND === "true",
SUPPORT_WALLET_CONNECT: process.env.SUPPORT_WALLET_CONNECT === "true",
SUPPORT_SWAP_QUOTE_REFRESH: process.env.SUPPORT_SWAP_QUOTE_REFRESH === "true",
SUPPORT_ABILITIES: process.env.SUPPORT_ABILITIES === "true",
SUPPORT_CUSTOM_NETWORKS: process.env.SUPPORT_CUSTOM_NETWORKS === "true",
SUPPORT_CUSTOM_RPCS: process.env.SUPPORT_CUSTOM_RPCS === "true",
} as const
Expand Down
3 changes: 3 additions & 0 deletions background/lib/daylight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type DaylightAbility = {
openAt: string | null
closeAt: string | null
isClosed: boolean | null
walletCompleted: boolean | null
createdAt: string
chain: string
sourceId: string
Expand Down Expand Up @@ -95,6 +96,8 @@ export const getDaylightAbilities = async (
// The limit needs to be set. It is set to the highest value.
requestURL.searchParams.set("limit", "1000")
requestURL.searchParams.set("deadline", "all")
requestURL.searchParams.set("showCompleted", "true")
requestURL.searchParams.set("markAsShown", "true")

try {
const response: AbilitiesResponse = await fetchJson({
Expand Down
30 changes: 18 additions & 12 deletions background/lib/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
MULTICALL_ABI,
MULTICALL_CONTRACT_ADDRESS,
} from "./multicall"
import logger from "./logger"

export const ERC20_FUNCTIONS = {
allowance: FunctionFragment.from(
Expand Down Expand Up @@ -93,19 +94,24 @@ export async function getMetadata(
provider
)

const [symbol, name, decimals] = await Promise.all(
[
ERC20_FUNCTIONS.symbol,
ERC20_FUNCTIONS.name,
ERC20_FUNCTIONS.decimals,
].map(({ name: functionName }) => token.callStatic[functionName]())
)
try {
const [symbol, name, decimals] = await Promise.all(
[
ERC20_FUNCTIONS.symbol,
ERC20_FUNCTIONS.name,
ERC20_FUNCTIONS.decimals,
].map(({ name: functionName }) => token.callStatic[functionName]())
)

return {
...tokenSmartContract,
symbol,
name,
decimals,
return {
...tokenSmartContract,
symbol,
name,
decimals,
}
} catch (error) {
logger.warn("Invalid metadata for token", tokenSmartContract)
throw new Error("Could not retrieve erc20 token metadata")
}
}

Expand Down
12 changes: 10 additions & 2 deletions background/lib/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ export async function getPrices(
const url = `${COINGECKO_API_ROOT}/simple/price?ids=${coinIds}&include_last_updated_at=true&vs_currencies=${currencySymbols}`

try {
const json = await fetchJson(url)
const json = await fetchJson({
url,
// Prevent throttling
throttleCallback: async () => false,
})
// TODO fix loss of precision from json
// TODO: TESTME

Expand Down Expand Up @@ -118,7 +122,11 @@ export async function getTokenPrices(
const url = `${COINGECKO_API_ROOT}/simple/token_price/${network.coingeckoPlatformID}?vs_currencies=${fiatSymbol}&include_last_updated_at=true&contract_addresses=${addys}`

try {
const json = await fetchJson(url)
const json = await fetchJson({
url,
// Prevent throttling
throttleCallback: async () => false,
})

// TODO Improve typing with Ajv validation.
Object.entries(
Expand Down
55 changes: 42 additions & 13 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ import { getActivityDetails } from "./redux-slices/utils/activities-utils"
import { getRelevantTransactionAddresses } from "./services/enrichment/utils"
import { AccountSignerWithId } from "./signing"
import { AnalyticsPreferences } from "./services/preferences/types"
import { isSmartContractFungibleAsset } from "./assets"
import { isSmartContractFungibleAsset, SmartContractAsset } from "./assets"
import { FeatureFlags, isEnabled } from "./features"
import { NFTCollection } from "./nfts"
import {
Expand Down Expand Up @@ -626,10 +626,7 @@ export default class Main extends BaseService<never> {
await this.nftsService.removeNFTsForAddress(address)
}
// remove abilities
if (
isEnabled(FeatureFlags.SUPPORT_ABILITIES) &&
signer.type !== AccountType.ReadOnly
) {
if (signer.type !== AccountType.ReadOnly) {
await this.abilitiesService.deleteAbilitiesForAccount(address)
}
// remove dApp premissions
Expand Down Expand Up @@ -657,6 +654,8 @@ export default class Main extends BaseService<never> {
network,
}
await this.chainService.addAccountToTrack(addressNetwork)
this.abilitiesService.getNewAccountAbilities(address)

this.store.dispatch(loadAccount(addressNetwork))
})
)
Expand Down Expand Up @@ -933,6 +932,7 @@ export default class Main extends BaseService<never> {
})

uiSliceEmitter.on("userActivityEncountered", (addressOnNetwork) => {
this.abilitiesService.refreshAbilities()
this.chainService.markAccountActivity(addressOnNetwork)
})
}
Expand Down Expand Up @@ -975,7 +975,33 @@ export default class Main extends BaseService<never> {

const filteredBalancesToDispatch: AccountBalance[] = []

balances.forEach((balance) => {
const sortedBalances: AccountBalance[] = []

balances
.filter((balance) => {
const isSmartContract =
"contractAddress" in balance.assetAmount.asset

if (!isSmartContract) {
sortedBalances.push(balance)
}

return isSmartContract
})
// Sort trusted last to prevent shadowing assets from token lists
// FIXME: Balances should not be indexed by symbol in redux
.sort((balance, otherBalance) => {
const asset = balance.assetAmount.asset as SmartContractAsset
const other = otherBalance.assetAmount.asset as SmartContractAsset

return (
(other.metadata?.tokenLists?.length ?? 0) -
(asset.metadata?.tokenLists?.length ?? 0)
)
})
.forEach((balance) => sortedBalances.unshift(balance))

sortedBalances.forEach((balance) => {
// TODO support multi-network assets
const balanceHasAnAlreadyTrackedAsset = assetsToTrack.some(
(tracked) =>
Expand Down Expand Up @@ -1083,6 +1109,7 @@ export default class Main extends BaseService<never> {
address,
network,
})
this.abilitiesService.getNewAccountAbilities(address)
})
})

Expand All @@ -1098,10 +1125,6 @@ export default class Main extends BaseService<never> {
await this.keyringService.unlock(password, true)
})

keyringSliceEmitter.on("unlockKeyrings", async (password) => {
await this.keyringService.unlock(password)
})

keyringSliceEmitter.on("lockKeyrings", async () => {
await this.keyringService.lock()
})
Expand Down Expand Up @@ -1567,15 +1590,17 @@ export default class Main extends BaseService<never> {
this.store.dispatch(updateAbility(ability))
})
this.abilitiesService.emitter.on("newAccount", (address) => {
if (isEnabled(FeatureFlags.SUPPORT_ABILITIES)) {
this.store.dispatch(addAccountFilter(address))
}
this.store.dispatch(addAccountFilter(address))
})
this.abilitiesService.emitter.on("deleteAccount", (address) => {
this.store.dispatch(deleteAccountFilter(address))
})
}

async unlockKeyrings(password: string): Promise<boolean> {
return this.keyringService.unlock(password)
}

async getActivityDetails(txHash: string): Promise<ActivityDetail[]> {
const addressNetwork = this.store.getState().ui.selectedAccount
const transaction = await this.chainService.getTransaction(
Expand Down Expand Up @@ -1678,6 +1703,10 @@ export default class Main extends BaseService<never> {
)
}

async removeEVMNetwork(chainID: string): Promise<void> {
return this.chainService.removeCustomChain(chainID)
}

private connectPopupMonitor() {
runtime.onConnect.addListener((port) => {
if (port.name !== popupMonitorPortName) return
Expand Down
1 change: 0 additions & 1 deletion background/redux-slices/abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ const abilitiesSlice = createSlice({
immerState.abilities[address][ability.abilityId]
immerState.abilities[address][ability.abilityId] = {
...ability,
completed: existingAbility.completed,
removedFromUi: existingAbility.removedFromUi,
}
} else {
Expand Down
4 changes: 1 addition & 3 deletions background/redux-slices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ const mainReducer = combineReducers({
earn: earnReducer,
ledger: ledgerReducer,
nfts: nftsReducer,
...(isEnabled(FeatureFlags.SUPPORT_ABILITIES)
? { abilities: abilitiesReducer }
: {}),
abilities: abilitiesReducer,
...(isEnabled(FeatureFlags.SUPPORT_NFT_TAB)
? { nftsUpdate: nftsUpdateReducer }
: {}),
Expand Down
5 changes: 2 additions & 3 deletions background/redux-slices/keyrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const initialState: KeyringsState = {

export type Events = {
createPassword: string
unlockKeyrings: string
lockKeyrings: never
generateNewKeyring: string | undefined
deriveAddress: string
Expand Down Expand Up @@ -148,8 +147,8 @@ export const deriveAddress = createBackgroundAsyncThunk(

export const unlockKeyrings = createBackgroundAsyncThunk(
"keyrings/unlockKeyrings",
async (password: string) => {
await emitter.emit("unlockKeyrings", password)
async (password: string, { extra: { main } }) => {
return { success: await main.unlockKeyrings(password) }
}
)

Expand Down
Loading

0 comments on commit 2a53385

Please sign in to comment.