Skip to content

Commit

Permalink
refactor: configurable DAIMO_CHAIN
Browse files Browse the repository at this point in the history
  • Loading branch information
dcposch committed Oct 18, 2023
1 parent 92c5c91 commit 831be46
Show file tree
Hide file tree
Showing 67 changed files with 2,988 additions and 1,640 deletions.
16 changes: 8 additions & 8 deletions apps/daimo-mobile/eas.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"simulator": true
},
"env": {
"NEXT_PUBLIC_DOMAIN": "daimo.xyz"
"DAIMO_CHAIN": "baseGoerli",
"DAIMO_DOMAIN": "daimo.xyz"
}
},
"dev": {
Expand All @@ -25,21 +26,20 @@
"buildConfiguration": "Debug"
},
"env": {
"NEXT_PUBLIC_DOMAIN": "daimo.xyz"
"DAIMO_CHAIN": "baseGoerli",
"DAIMO_DOMAIN": "daimo.xyz"
}
},
"stage": {
"prod": {
"resourceClass": "large",
"autoIncrement": true,
"node": "20.3.0",
"env": {
"DAIMO_APP_API_URL": "https://daimo-api-prod.onrender.com",
"NEXT_PUBLIC_DOMAIN": "daimo.xyz"
"DAIMO_BUNDLER_RPC": "https://api.pimlico.io/v1/base/rpc?apikey=70ecef54-a28e-4e96-b2d3-3ad67fbc1b07",
"DAIMO_CHAIN": "base",
"DAIMO_DOMAIN": "daimo.xyz"
}
},
"prod": {
"autoIncrement": true,
"resourceClass": "large"
}
},
"submit": {
Expand Down
2 changes: 1 addition & 1 deletion apps/daimo-mobile/src/action/useExistingAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useEffect } from "react";

import { useActStatus } from "./actStatus";
import { useLoadOrCreateEnclaveKey } from "./key";
import { useTime } from "../logic/time";
import { rpcFunc } from "../logic/trpc";
import { useTime } from "../logic/useTime";
import { defaultEnclaveKeyName, useAccount } from "../model/account";

export function useExistingAccount() {
Expand Down
2 changes: 1 addition & 1 deletion apps/daimo-mobile/src/action/useSendAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function loadOpSender(
};
};

return await DaimoOpSender.init(address, signer);
return await DaimoOpSender.initFromEnv(address, signer);
})();
accountCache.set([address, keySlot], promise);

Expand Down
7 changes: 0 additions & 7 deletions apps/daimo-mobile/src/logic/chainConfig.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { guessTimestampFromNum } from "@daimo/common";
import { chainConfig } from "@daimo/contract";
import { useEffect, useState } from "react";

/** Returns the current time in Unix seconds. Ticks every `secs`. */
Expand All @@ -11,3 +13,8 @@ export function useTime(secs: number = 1) {

return Math.floor(time / 1000);
}

/** Returns the timestamp, in Unix seconds, for a given L2 block number. */
export function timestampForBlock(blockNum: number) {
return guessTimestampFromNum(blockNum, chainConfig.chainL2.network);
}
8 changes: 2 additions & 6 deletions apps/daimo-mobile/src/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import {
TransferOpEvent,
amountToDollars,
assert,
guessTimestampFromNum,
} from "@daimo/common";
import { useEffect } from "react";

import { chainConfig } from "../logic/chainConfig";
import { timestampForBlock } from "../logic/time";
import { rpcFunc } from "../logic/trpc";
import { Account, getAccountManager } from "../model/account";

Expand Down Expand Up @@ -239,10 +238,7 @@ function addTransfers(
amount: Number(transfer.amount),
nonceMetadata: transfer.nonceMetadata,

timestamp: guessTimestampFromNum(
transfer.blockNumber!,
chainConfig.l2.network
),
timestamp: timestampForBlock(transfer.blockNumber!),
txHash: transfer.txHash,
blockNumber: transfer.blockNumber,
blockHash: transfer.blockHash,
Expand Down
4 changes: 2 additions & 2 deletions apps/daimo-mobile/src/view/HomeStack.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tokenMetadata } from "@daimo/contract";
import { chainConfig } from "@daimo/contract";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { useEffect, useMemo, useState } from "react";

Expand Down Expand Up @@ -46,7 +46,7 @@ export function HomeStackNav() {
/>
<HomeStack.Screen
name="RequestSend"
options={{ headerTitle: `Request ${tokenMetadata.symbol}` }}
options={{ headerTitle: `Request ${chainConfig.tokenSymbol}` }}
component={SendRequestScreen}
/>
<HomeStack.Screen name="HistoryOp" component={HistoryOpScreen} />
Expand Down
11 changes: 4 additions & 7 deletions apps/daimo-mobile/src/view/screen/DeviceScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { assert, guessTimestampFromNum, timeString } from "@daimo/common";
import { assert, timeString } from "@daimo/common";
import {
DaimoOpSender,
DaimoNonce,
DaimoNonceMetadata,
DaimoNonceType,
DaimoOpSender,
} from "@daimo/userop";
import { NativeStackScreenProps } from "@react-navigation/native-stack";
import { ReactNode, useCallback, useEffect, useMemo } from "react";
import { ActivityIndicator, Alert, View } from "react-native";

import { useSendAsync } from "../../action/useSendAsync";
import { chainConfig } from "../../logic/chainConfig";
import { keySlotToDeviceIdentifier } from "../../logic/device";
import { deleteEnclaveKey } from "../../logic/enclave";
import { timestampForBlock } from "../../logic/time";
import { useAccount } from "../../model/account";
import { getAmountText } from "../shared/Amount";
import { ButtonBig } from "../shared/Button";
Expand Down Expand Up @@ -127,10 +127,7 @@ export function DeviceScreen({ route, navigation }: Props) {
}
})();

const addedAtS = guessTimestampFromNum(
device.addedAt,
chainConfig.l2.network
);
const addedAtS = timestampForBlock(device.addedAt);

return (
<View style={ss.container.fullWidthModal}>
Expand Down
4 changes: 2 additions & 2 deletions apps/daimo-mobile/src/view/screen/HistoryOpScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
timeString,
TransferOpEvent,
} from "@daimo/common";
import { chainConfig } from "@daimo/contract";
import { DaimoNonceMetadata } from "@daimo/userop";
import Octicons from "@expo/vector-icons/Octicons";
import { NativeStackScreenProps } from "@react-navigation/native-stack";
import { useCallback, useEffect } from "react";
import { Linking, StyleSheet, Text, View } from "react-native";

import { chainConfig } from "../../logic/chainConfig";
import { useAccount } from "../../model/account";
import { syncFindSameOp } from "../../sync/sync";
import { TitleAmount } from "../shared/Amount";
Expand Down Expand Up @@ -55,7 +55,7 @@ export function HistoryOpScreen({ route, navigation }: Props) {
}

function LinkToExplorer({ txHash }: { txHash: string }) {
const explorer = chainConfig.l2.blockExplorers.default;
const explorer = chainConfig.chainL2.blockExplorers!.default;
const url = `${explorer.url}/tx/${txHash}`;

const openURL = useCallback(() => Linking.openURL(url), []);
Expand Down
10 changes: 6 additions & 4 deletions apps/daimo-mobile/src/view/screen/OnboardingScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertNotNull, validateName } from "@daimo/common";
import { tokenMetadata } from "@daimo/contract";
import { chainConfig } from "@daimo/contract";
import Octicons from "@expo/vector-icons/Octicons";
import * as Device from "expo-device";
import * as Notifications from "expo-notifications";
Expand Down Expand Up @@ -163,6 +163,8 @@ function IntroPages({
setPageIndex(page);
};

const { tokenSymbol } = chainConfig;

return (
<View style={styles.introPages}>
<PageBubble count={4} index={pageIndex} />
Expand All @@ -180,10 +182,10 @@ function IntroPages({
technology. Use at your own risk.
</TextBody>
</IntroPage>
<IntroPage title={tokenMetadata.symbol}>
<IntroPage title={tokenSymbol}>
<TextBody>
Daimo lets you send and receive money using the{" "}
{tokenMetadata.symbol} stablecoin. 1 {tokenMetadata.symbol} is $1.
Daimo lets you send and receive money using the {tokenSymbol}{" "}
stablecoin. 1 {tokenSymbol} is $1.
</TextBody>
<InfoLink
url="https://www.circle.com/en/usdc"
Expand Down
13 changes: 6 additions & 7 deletions apps/daimo-mobile/src/view/screen/SettingsScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyData, guessTimestampFromNum, timeAgo } from "@daimo/common";
import { tokenMetadata } from "@daimo/contract";
import { KeyData, timeAgo } from "@daimo/common";
import { chainConfig } from "@daimo/contract";
import * as ExpoEnclave from "@daimo/expo-enclave";
import Octicons from "@expo/vector-icons/Octicons";
import * as Notifications from "expo-notifications";
Expand All @@ -16,12 +16,11 @@ import {
} from "react-native";

import { getDebugLog } from "../../debugLog";
import { chainConfig } from "../../logic/chainConfig";
import { keySlotToDeviceIdentifier } from "../../logic/device";
import { deleteEnclaveKey, getHardwareSec } from "../../logic/enclave";
import { env } from "../../logic/env";
import { getPushNotificationManager } from "../../logic/notify";
import { useTime } from "../../logic/useTime";
import { timestampForBlock, useTime } from "../../logic/time";
import { Account, serializeAccount, useAccount } from "../../model/account";
import { ButtonMed, ButtonSmall } from "../shared/Button";
import Spacer from "../shared/Spacer";
Expand Down Expand Up @@ -63,7 +62,7 @@ export function SettingsScreen() {
}
}, []);

const explorer = chainConfig.l2.blockExplorers.default;
const explorer = chainConfig.chainL2.blockExplorers!.default;
const linkToExplorer = useCallback(() => {
if (!account) return;
const url = `${explorer.url}/address/${account.address}`;
Expand All @@ -81,7 +80,7 @@ export function SettingsScreen() {
{account.name}
<TextBody>
{` \u00A0 `}
{tokenMetadata.name} · {chainConfig.l2.name}
{chainConfig.tokenSymbol} · {chainConfig.chainL2.name}
</TextBody>
</TextH2>
</View>
Expand Down Expand Up @@ -157,7 +156,7 @@ function DeviceRow({

const viewDevice = () => nav.navigate("Device", { pubKey: keyData.pubKey });

const addAtS = guessTimestampFromNum(keyData.addedAt, chainConfig.l2.network);
const addAtS = timestampForBlock(keyData.addedAt);

return (
<ButtonSmall onPress={viewDevice}>
Expand Down
4 changes: 2 additions & 2 deletions apps/daimo-mobile/src/view/screen/link/NoteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
dollarsToAmount,
getAccountName,
} from "@daimo/common";
import { ephemeralNotesAddress } from "@daimo/contract";
import { daimoEphemeralNotesAddress } from "@daimo/contract";
import {
DaimoNonce,
DaimoNonceMetadata,
Expand Down Expand Up @@ -105,7 +105,7 @@ function NoteDisplay({
pendingOp: {
type: "transfer",
status: OpStatus.pending,
from: ephemeralNotesAddress,
from: daimoEphemeralNotesAddress,
to: account.address,
amount: Number(dollarsToAmount(noteStatus.dollars)),
timestamp: Date.now() / 1e3,
Expand Down
13 changes: 7 additions & 6 deletions apps/daimo-mobile/src/view/screen/receive/DepositScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { AddrLabel, assert } from "@daimo/common";
import { tokenMetadata } from "@daimo/contract";
import { chainConfig } from "@daimo/contract";
import Octicons from "@expo/vector-icons/Octicons";
import * as Clipboard from "expo-clipboard";
import { useCallback, useEffect, useState } from "react";
import { StyleSheet, Text, TouchableHighlight, View } from "react-native";
import { Address, getAddress } from "viem";

import { chainConfig } from "../../../logic/chainConfig";
import { rpcHook } from "../../../logic/trpc";
import { useAccount } from "../../../model/account";
import { ButtonMed } from "../../shared/Button";
Expand All @@ -18,15 +17,17 @@ export default function DepositScreen() {
const [account] = useAccount();
if (account == null) return null;

const { chainL2, tokenSymbol } = chainConfig;

return (
<View style={styles.vertOuter}>
{chainConfig.testnet && <TestnetFaucet recipient={account.address} />}
{chainConfig.testnet && <Spacer h={32} />}
{chainL2.testnet && <TestnetFaucet recipient={account.address} />}
{chainL2.testnet && <Spacer h={32} />}
<OnrampStub />
<Spacer h={32} />
<TextBody>
<TextBold>
Deposit {tokenMetadata.symbol} on {chainConfig.l2.name} only.
Deposit {tokenSymbol} on {chainL2.name} only.
</TextBold>{" "}
Use the following address.
</TextBody>
Expand Down Expand Up @@ -99,7 +100,7 @@ function TestnetFaucet({ recipient }: { recipient: Address }) {
<TextBody>
<Octicons name="alert" size={16} color="black" />{" "}
<TextBold>Testnet version.</TextBold> This unreleased version of Daimo
runs on {chainConfig.l2.name}.
runs on {chainConfig.chainL2.name}.
</TextBody>
<Spacer h={16} />
<ButtonMed
Expand Down
4 changes: 2 additions & 2 deletions apps/daimo-mobile/src/view/screen/send/SendNoteButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
dollarsToAmount,
formatDaimoLink,
} from "@daimo/common";
import { ephemeralNotesAddress } from "@daimo/contract";
import { daimoEphemeralNotesAddress } from "@daimo/contract";
import {
DaimoNonce,
DaimoNonceMetadata,
Expand Down Expand Up @@ -53,7 +53,7 @@ export function SendNoteButton({ dollars }: { dollars: number }) {
type: "transfer",
status: OpStatus.pending,
from: account.address,
to: ephemeralNotesAddress,
to: daimoEphemeralNotesAddress,
amount: Number(dollarsToAmount(dollars)),
timestamp: Date.now() / 1e3,
nonceMetadata: nonce.metadata.toHex(),
Expand Down
18 changes: 15 additions & 3 deletions apps/daimo-mobile/src/view/shared/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { chainConfig } from "@daimo/contract";
import { useCallback } from "react";
import { StyleSheet, Text, View } from "react-native";
import { StyleSheet, Text, TextStyle, View, ViewStyle } from "react-native";

import { ButtonSmall } from "./Button";
import { useNav } from "./nav";
Expand All @@ -25,9 +26,20 @@ export function Header() {
}

function StyleTBD() {
const { testnet } = chainConfig.chainL2;
const stylePellet = [styles.pellet] as ViewStyle[];
const styleText = [styles.pelletText] as TextStyle[];
let text;
if (testnet) {
stylePellet.push({ backgroundColor: color.primaryBgLight });
styleText.push({ color: color.grayDark });
text = "TESTNET";
} else {
text = "MAINNET";
}
return (
<View style={styles.pellet}>
<Text style={styles.pelletText}>DESIGN SOON</Text>
<View style={stylePellet}>
<Text style={styleText}>{text}</Text>
</View>
);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/daimo-mobile/src/view/shared/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { useNavigation } from "@react-navigation/native";
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
import { addEventListener, getInitialURL } from "expo-linking";
import { useEffect, useRef, useState } from "react";
import { useEffect, useRef } from "react";
import { Hex } from "viem";

import { useAccount } from "../../model/account";
Expand Down
2 changes: 1 addition & 1 deletion apps/daimo-testbed/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function App() {
"P-256 Public Key, deploy corresponding account by calling createAccount on factory:",
derPublicKey
);
const account = await DaimoOpSender.init(accAddress, signer);
const account = await DaimoOpSender.initFromEnv(accAddress, signer);
console.log(
"account, give it some eth + usdc magically:",
account.getAddress()
Expand Down
Loading

0 comments on commit 831be46

Please sign in to comment.