Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable alfajores #98

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const isDev = process.env.NODE_ENV !== 'production';
const ENABLE_CSP_HEADER = true;
const CONNECT_SRC_HOSTS = [
'https://*.celo.org',
'https://*.celo-testnet.org',
'https://*.celoscan.io',
'https://*.walletconnect.com',
'wss://*.walletconnect.com',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"homepage": "https://github.com/celo-org/celo-mondo",
"dependencies": {
"@celo/abis": "^11.0.0",
"@celo/abis-12": "npm:@celo/[email protected]",
"@celo/compliance": "^1.0.24",
"@headlessui/react": "^1.7.19",
"@metamask/jazzicon": "https://github.com/jmrossy/jazzicon#7a8df28974b4e81129bfbe3cab76308b889032a6",
Expand Down
9 changes: 4 additions & 5 deletions src/app/staking/api/activate/route.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { electionABI } from '@celo/abis';
import { fornoRpcUrl } from 'src/config/config';
import { Addresses } from 'src/config/contracts';
import {
StakeActivationRequest,
StakeActivationRequestSchema,
} from 'src/features/staking/autoActivation';
import { eqAddress } from 'src/utils/addresses';
import { createCeloPublicClient, createCeloWalletClient } from 'src/utils/client';
import { logger } from 'src/utils/logger';
import { errorToString } from 'src/utils/strings';
import { createPublicClient, createWalletClient, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { celo } from 'viem/chains';

export const dynamic = 'force-dynamic';

Expand Down Expand Up @@ -50,7 +48,7 @@ async function activateStake(request: StakeActivationRequest) {
const group = request.group as HexString;
const transactionHash = request.transactionHash as HexString;

const client = createPublicClient({ chain: celo, transport: http(fornoRpcUrl) });
const client = createCeloPublicClient();

const transaction = await client.getTransaction({ hash: transactionHash });
if (!eqAddress(transaction.from, address))
Expand Down Expand Up @@ -104,5 +102,6 @@ function getWalletClient() {
const privateKey = process.env.STAKE_ACTIVATION_PRIVATE_KEY as HexString;
if (!privateKey) throw new Error('No private key set for staking activation');
const account = privateKeyToAccount(privateKey);
return createWalletClient({ account, chain: celo, transport: http(fornoRpcUrl) });

return createCeloWalletClient(account);
}
10 changes: 6 additions & 4 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ChainId } from 'src/config/chains';
import { links } from 'src/config/links';

interface Config {
debug: boolean;
Expand All @@ -12,6 +11,7 @@ interface Config {
infuraApiKey: string;
upstashKey: string;
watchBlockNumber: boolean;
isAlfajores: boolean;
}

const isDevMode = process?.env?.NODE_ENV === 'development';
Expand All @@ -21,19 +21,21 @@ const fornoApiKey = process?.env?.NEXT_PUBLIC_FORNO_API_KEY || '';
const celoscanApiKey = process?.env?.NEXT_PUBLIC_CELOSCAN_API_KEY || '';
const infuraApiKey = process?.env?.NEXT_PUBLIC_INFURA_API_KEY || '';
const upstashKey = process?.env?.UPSTASH_KEY || '';
const isAlfajores = process?.env?.NEXT_PUBLIC_IS_ALFAJORES === '1' || false;

export const fornoRpcUrl = `${links.forno}?apikey=${fornoApiKey}`;
export const infuraRpcUrl = `${links.infura}/${infuraApiKey}`;
export const fornoRpcUrl = `https://forno.celo.org?apikey=${fornoApiKey}`;
aaronmgdr marked this conversation as resolved.
Show resolved Hide resolved
export const infuraRpcUrl = `https://celo-mainnet.infura.io/v3/${infuraApiKey}`;

export const config: Config = Object.freeze({
debug: isDevMode,
version,
appName: 'Celo Mondo',
chainId: ChainId.Celo,
chainId: isAlfajores ? ChainId.Alfajores : ChainId.Celo,
walletConnectProjectId,
fornoApiKey,
celoscanApiKey,
infuraApiKey,
upstashKey,
watchBlockNumber: false,
isAlfajores,
});
50 changes: 39 additions & 11 deletions src/config/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
export const Addresses = {
import { registryABI } from '@celo/abis-12';
import { config } from 'src/config/config';
import { ZERO_ADDRESS } from 'src/config/consts';
import { createCeloPublicClient } from 'src/utils/client';

const ALFAJORES_ADDRESSES = {
Accounts: '0xed7f51A34B4e71fbE69B3091FcF879cD14bD73A9',
Election: '0x1c3eDf937CFc2F6F51784D20DEB1af1F9a8655fA',
EpochManager: '0x106d8A1192fB0E58079487d093b42332e66F5d8f',
Governance: '0xAA963FC97281d9632d96700aB62A4D1340F9a28a',
LockedGold: '0x6a4CC5693DC5BFA3799C699F3B941bA2Cb00c341',
Validators: '0x9acF2A99914E083aD0d610672E93d14b0736BBCc',
} as const;

const MAINNET_ADDRESSES = {
Accounts: '0x7d21685C17607338b313a7174bAb6620baD0aaB7',
DoubleSigningSlasher: '0x50C100baCDe7E2b546371EB0Be1eACcf0A6772ec',
DowntimeSlasher: '0x71CAc3B31c138F3327C6cA14f9a1c8d752463fDd',
Election: '0x8D6677192144292870907E3Fa8A5527fE55A7ff6',
EpochRewards: '0x07F007d389883622Ef8D4d347b3f78007f28d8b7',
GasPriceMinimum: '0xDfca3a8d7699D8bAfe656823AD60C17cb8270ECC',
GoldToken: '0x471EcE3750Da237f93B8E339c536989b8978a438',
// TODO once the migration is done add the address here
// for now it will be resolved automatically during runtime
EpochManager: ZERO_ADDRESS,
Governance: '0xD533Ca259b330c7A88f74E000a3FaEa2d63B7972',
LockedGold: '0x6cC083Aed9e3ebe302A6336dBC7c921C9f03349E',
Registry: '0x000000000000000000000000000000000000ce10',
Reserve: '0x9380fA34Fd9e4Fd14c06305fd7B6199089eD4eb9',
StableToken: '0x765DE816845861e75A25fCA122bb6898B8B1282a',
StableTokenBRL: '0xe8537a3d056DA446677B9E9d6c5dB704EaAb4787',
StableTokenEUR: '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73',
Validators: '0xaEb865bCa93DdC8F47b8e29F40C5399cE34d0C58',
} as const;

export const Addresses = config.isAlfajores ? ALFAJORES_ADDRESSES : MAINNET_ADDRESSES;

export const REGISTRY_ADDRESS = '0x000000000000000000000000000000000000ce10';

export const resolveAddress = async (name: keyof typeof Addresses) => {
if (Addresses[name] === ZERO_ADDRESS) {
console.log(`Resolving ${name} address from registry`);

const client = createCeloPublicClient();

return await client.readContract({
address: REGISTRY_ADDRESS,
functionName: 'getAddressForStringOrDie',
abi: registryABI,
args: [name],
});
}

return Addresses[name];
};
11 changes: 6 additions & 5 deletions src/config/links.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { config } from 'src/config/config';

export const links = {
home: 'https://mondo.celo.org',
celo: 'https://celo.org',
Expand All @@ -8,13 +10,12 @@ export const links = {
forum: 'https://forum.celo.org',
governance: 'https://github.com/celo-org/governance',
delegate: 'https://github.com/celo-org/celo-mondo/blob/main/src/config/delegates.json',
// RPCs
forno: 'https://forno.celo.org',
infura: 'https://celo-mainnet.infura.io/v3',
// Explorers
blockscout: 'https://explorer.celo.org',
aaronmgdr marked this conversation as resolved.
Show resolved Hide resolved
celoscan: 'https://celoscan.io',
celoscanApi: 'https://api.celoscan.io',
celoscan: config.isAlfajores ? 'https://alfajores.celoscan.io/' : 'https://celoscan.io',
celoscanApi: config.isAlfajores
? 'https://api-alfajores.celoscan.io/api'
: 'https://api.celoscan.io',
// Auto-activation
upstash: 'https://qstash.upstash.io/v2/publish',
};
5 changes: 3 additions & 2 deletions src/config/wagmi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'react-toastify/dist/ReactToastify.css';
import { config, fornoRpcUrl, infuraRpcUrl } from 'src/config/config';
import { Color } from 'src/styles/Color';
import { fallback } from 'viem';
import { celo } from 'viem/chains';
import { celo, celoAlfajores } from 'viem/chains';
import { WagmiProvider, createConfig, http } from 'wagmi';

const connectors = connectorsForWallets(
Expand All @@ -41,10 +41,11 @@ const connectors = connectorsForWallets(
);

export const wagmiConfig = createConfig({
chains: [celo],
chains: [config.isAlfajores ? celoAlfajores : celo],
connectors,
transports: {
[celo.id]: fallback([http(fornoRpcUrl), http(infuraRpcUrl)]),
[celoAlfajores.id]: http(),
},
});

Expand Down
6 changes: 2 additions & 4 deletions src/features/delegation/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { accountsABI } from '@celo/abis';
import { App, Octokit } from 'octokit';
import path from 'path';
import { fornoRpcUrl } from 'src/config/config';
import { Addresses } from 'src/config/contracts';
import {
delegateeRegistrationRequestToMetadata,
metadataToJSONString,
} from 'src/features/delegation/delegateeMetadata';
import { RegisterDelegateRequest } from 'src/features/delegation/types';
import { createPublicClient, http } from 'viem';
import { celo } from 'viem/chains';
import { createCeloPublicClient } from 'src/utils/client';

export async function isAddressAnAccount(address: HexString) {
const client = createPublicClient({ chain: celo, transport: http(fornoRpcUrl) });
const client = createCeloPublicClient();

return await client.readContract({
address: Addresses.Accounts,
Expand Down
8 changes: 4 additions & 4 deletions src/features/validators/useGroupRewardHistory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { electionABI } from '@celo/abis';
import { useQuery } from '@tanstack/react-query';
import { useToastError } from 'src/components/notifications/useToastError';
import { infuraRpcUrl } from 'src/config/config';
import { config, infuraRpcUrl } from 'src/config/config';
import { AVG_BLOCK_TIMES_MS, EPOCH_DURATION_MS } from 'src/config/consts';
import { Addresses } from 'src/config/contracts';
import { queryCeloscanPath } from 'src/features/explorers/celoscan';
import { logger } from 'src/utils/logger';
import { Block, createPublicClient, decodeEventLog, http, parseAbiItem } from 'viem';
import { celo } from 'viem/chains';
import { celo, celoAlfajores } from 'viem/chains';

const REWARD_DISTRIBUTED_ABI_FRAGMENT =
'event EpochRewardsDistributedToVoters(address indexed group, uint256 value)';
Expand Down Expand Up @@ -68,8 +68,8 @@ async function fetchValidatorGroupRewardHistory(
timeout: 20_000,
});
const infuraBatchClient = createPublicClient({
chain: celo,
transport: infuraTransport,
chain: config.isAlfajores ? celoAlfajores : celo,
transport: config.isAlfajores ? http() : infuraTransport,
});

const rewardLogs = await infuraBatchClient.getLogs({
Expand Down
20 changes: 14 additions & 6 deletions src/features/validators/useValidatorGroups.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { accountsABI, electionABI, lockedGoldABI, validatorsABI } from '@celo/abis';
import { epochManagerABI } from '@celo/abis-12';
import { useQuery } from '@tanstack/react-query';
import BigNumber from 'bignumber.js';
import { useToastError } from 'src/components/notifications/useToastError';
import { MAX_NUM_ELECTABLE_VALIDATORS, ZERO_ADDRESS } from 'src/config/consts';
import { Addresses } from 'src/config/contracts';
import { Addresses, resolveAddress } from 'src/config/contracts';
import { isCel2 } from 'src/utils/is-cel2';
import { logger } from 'src/utils/logger';
import { bigIntSum } from 'src/utils/math';
import { PublicClient } from 'viem';
Expand Down Expand Up @@ -122,11 +124,17 @@ async function fetchValidatorAddresses(publicClient: PublicClient) {
abi: validatorsABI,
functionName: 'getRegisteredValidators',
} as const,
{
address: Addresses.Election,
abi: electionABI,
functionName: 'getCurrentValidatorSigners',
} as const,
(await isCel2(publicClient))
aaronmgdr marked this conversation as resolved.
Show resolved Hide resolved
? {
address: await resolveAddress('EpochManager'),
abi: epochManagerABI,
functionName: 'getElectedSigners',
}
: {
address: Addresses.Election,
abi: electionABI,
functionName: 'getCurrentValidatorSigners',
},
],
});
if (validatorAddrsResp.status !== 'success' || !validatorAddrsResp.result?.length) {
Expand Down
16 changes: 16 additions & 0 deletions src/utils/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { config, fornoRpcUrl } from 'src/config/config';
import { Account, createPublicClient, createWalletClient, http } from 'viem';
import { celo, celoAlfajores } from 'viem/chains';

export const createCeloPublicClient = () =>
createPublicClient({
chain: config.isAlfajores ? celoAlfajores : celo,
transport: config.isAlfajores ? http() : http(fornoRpcUrl),
});

export const createCeloWalletClient = (account: Account) =>
createWalletClient({
account,
chain: config.isAlfajores ? celoAlfajores : celo,
transport: config.isAlfajores ? http() : http(fornoRpcUrl),
});
20 changes: 20 additions & 0 deletions src/utils/is-cel2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This util checks if we're in L2 context, it's a port of the technique used in
* https://github.com/celo-org/celo-monorepo/blob/da9b4955c1fdc8631980dc4adf9b05e0524fc228/packages/protocol/contracts-0.8/common/IsL2Check.sol#L17
*/

import { PublicClient } from 'viem';

export const PROXY_ADMIN_ADDRESS = '0x4200000000000000000000000000000000000018';

export const isCel2 = async (publicClient: PublicClient) => {
const code = await publicClient.getCode({
address: PROXY_ADMIN_ADDRESS,
});

if (typeof code === 'string') {
return code != '0x' && code.length > 2;
}

return false;
};
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ __metadata:
resolution: "@celo-mondo/celo-mondo-web@workspace:."
dependencies:
"@celo/abis": "npm:^11.0.0"
"@celo/abis-12": "npm:@celo/[email protected]"
"@celo/compliance": "npm:^1.0.24"
"@headlessui/react": "npm:^1.7.19"
"@metamask/jazzicon": "https://github.com/jmrossy/jazzicon#7a8df28974b4e81129bfbe3cab76308b889032a6"
Expand Down Expand Up @@ -112,6 +113,13 @@ __metadata:
languageName: unknown
linkType: soft

"@celo/abis-12@npm:@celo/[email protected]":
version: 12.0.0-canary.66
resolution: "@celo/abis@npm:12.0.0-canary.66"
checksum: 10/71f85edda338c651e214e77d983a9fb644cdc2f5bb0fe6bc471d73e3a8f87256dc4dc6e966e79d7b239fd5729d4862606c384a8f601e030829111e32b0824279
languageName: node
linkType: hard

"@celo/abis@npm:^11.0.0":
version: 11.0.0
resolution: "@celo/abis@npm:11.0.0"
Expand Down