Skip to content

Commit

Permalink
feat: add dydx chain (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleem1314 authored Oct 31, 2023
1 parent 25cb57c commit ef463f0
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 53 deletions.
71 changes: 71 additions & 0 deletions frontend/chains/dydx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"enable_modules": {
"authz": true,
"feegrant": true,
"group": false
},
"amino_config": {
"authz": false,
"feegrant": false,
"group": false
},
"show_airdrop": false,
"logos": {
"menu": "https://raw.githubusercontent.com/cosmos/chain-registry/master/dydx/images/dydx.svg",
"toolbar": "https://raw.githubusercontent.com/cosmos/chain-registry/master/dydx/images/dydx.svg"
},
"keplr_experimental": false,
"leap_experimental": true,
"is_testnet": false,
"explorer_tx_hash_endpoint": "https://mintscan.io/dydx/txs/",
"config": {
"chain_id": "dydx-mainnet-1",
"chain_name": "DYDX",
"rest": "https://dydx-api.lavenderfive.com:443",
"rpc": "https://dydx-rpc.lavenderfive.com:443",
"currencies": [
{
"coin_denom": "DYDX",
"coin_minimal_denom": "adydx",
"coin_decimals": 18
}
],
"bip44": {
"coin_type": 118
},
"bech32_config": {
"bech32_prefix_acc_addr": "dydx",
"bech32_prefix_acc_pub": "dydxpub",
"bech32_prefix_val_addr": "dydxvaloper",
"bech32_prefix_val_pub": "dydxvaloperpub",
"bech32_prefix_cons_addr": "dydxvalcons",
"bech32_prefix_cons_pub": "dydxvalconspub"
},
"wallet_url_for_staking": "https://resolute.vitwit.com/dydx/staking",
"fee_currencies": [
{
"coin_denom": "DYDX",
"coin_minimal_denom": "adydx",
"coin_decimals": 18,
"coin_gecko_id": "dydx",
"gas_price_step": {
"low": 0.01,
"average": 0.02,
"high": 0.05
}
}
],

"stake_currency": {
"coin_denom": "DYDX",
"coin_minimal_denom": "adydx",
"coin_decimals": 18,
"coin_gecko_id": "dydx"
},
"image": "https://raw.githubusercontent.com/leapwallet/assets/2289486990e1eaf9395270fffd1c41ba344ef602/images/logo.svg",
"theme": {
"primaryColor": "#fff",
"gradient": "linear-gradient(180deg, rgba(255,255,255,0.32) 0%, rgba(255,255,255,0) 100%)"
}
}
}
9 changes: 6 additions & 3 deletions frontend/src/components/ActiveValidators.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export function ActiveValidators(props) {
(state) => state.staking.chains[chainID].delegations.delegatedTo
);

const wallet = useSelector(state => state.wallet);
const coinDecimals = wallet?.networks[chainID]?.network?.config?.currencies[0]?.coinDecimals || 6;

const [activeVals, setActiveVals] = useState(validators.activeSorted);

useEffect(() => {
Expand All @@ -36,7 +39,7 @@ export function ActiveValidators(props) {
<StyledTableRow>
<StyledTableCell>Rank</StyledTableCell>
<StyledTableCell align="left">Validator</StyledTableCell>
<StyledTableCell align="center">Voting Power</StyledTableCell>
<StyledTableCell align="left">Voting Power</StyledTableCell>
<StyledTableCell align="center">Status</StyledTableCell>
<StyledTableCell align="center">Commission</StyledTableCell>
<StyledTableCell align="center">Actions</StyledTableCell>
Expand All @@ -55,8 +58,8 @@ export function ActiveValidators(props) {
<StyledTableCell align="left">
{validators.active[keyName]?.description.moniker}
</StyledTableCell>
<StyledTableCell align="center">
{formatVotingPower(validators.active[keyName]?.tokens, 6)}
<StyledTableCell align="left">
{formatVotingPower(validators.active[keyName]?.tokens, coinDecimals)}
</StyledTableCell>
<StyledTableCell>
{validators.active[keyName]?.jailed
Expand Down
10 changes: 7 additions & 3 deletions frontend/src/components/FilteredValidators.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export function FilteredValidators(props) {
(state) => state.staking.chains[chainID].delegations.delegatedTo
);

const wallet = useSelector(state => state.wallet);
const coinDecimals = wallet?.networks[chainID]?.network?.config?.currencies[0]?.coinDecimals || 6;


return (
<>
<TableContainer component={Paper} elevation={0}>
Expand All @@ -24,7 +28,7 @@ export function FilteredValidators(props) {
<StyledTableRow>
<StyledTableCell>Rank</StyledTableCell>
<StyledTableCell align="left">Validator</StyledTableCell>
<StyledTableCell align="center">Voting Power</StyledTableCell>
<StyledTableCell align="left">Voting Power</StyledTableCell>
<StyledTableCell align="center">Status</StyledTableCell>
<StyledTableCell align="center">Commission</StyledTableCell>
<StyledTableCell align="center">Actions</StyledTableCell>
Expand All @@ -42,8 +46,8 @@ export function FilteredValidators(props) {
<StyledTableCell align="left">
{validators.active[keyName]?.description.moniker}
</StyledTableCell>
<StyledTableCell align="center">
{formatVotingPower(validators.active[keyName]?.tokens, 6)}
<StyledTableCell align="left">
{formatVotingPower(validators.active[keyName]?.tokens, coinDecimals)}
</StyledTableCell>
<StyledTableCell>
{validators.active[keyName]?.jailed
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/components/InActiveValidators.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export function InActiveValidators(props) {

const [inactiveVals, setInactiveVals] = useState(validators.inactiveSorted);

const wallet = useSelector(state => state.wallet);
const coinDecimals = wallet?.networks[chainID]?.network?.config?.currencies[0]?.coinDecimals || 6;

useEffect(() => {
setInactiveVals(validators.inactiveSorted);
}, [validators]);
Expand All @@ -36,7 +39,7 @@ export function InActiveValidators(props) {
<StyledTableRow>
<StyledTableCell>Rank</StyledTableCell>
<StyledTableCell align="left">Validator</StyledTableCell>
<StyledTableCell align="center">Voting Power</StyledTableCell>
<StyledTableCell align="left">Voting Power</StyledTableCell>
<StyledTableCell align="center">Commission</StyledTableCell>
<StyledTableCell align="center">Status</StyledTableCell>
<StyledTableCell align="center">Action</StyledTableCell>
Expand All @@ -55,8 +58,8 @@ export function InActiveValidators(props) {
<StyledTableCell align="left">
{validators.inactive[keyName]?.description.moniker}
</StyledTableCell>
<StyledTableCell align="center">
{formatVotingPower(validators.inactive[keyName]?.tokens, 6)}
<StyledTableCell align="left">
{formatVotingPower(validators.inactive[keyName]?.tokens, coinDecimals)}
</StyledTableCell>
<StyledTableCell align="center">
{(
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/components/WitvalValidator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import { formatVotingPower } from "../utils/denom";
import Typography from "@mui/material/Typography";
import { formatValidatorStatus } from "../utils/util";
import { useTheme } from "@emotion/react";
import { useSelector } from "react-redux";

export function WitvalValidator(props) {
const { validator, onMenuAction } = props;
const { validator, onMenuAction, chainID } = props;

const wallet = useSelector(state => state.wallet);
const coinDecimals = wallet?.networks[chainID]?.network?.config?.currencies[0]?.coinDecimals || 6;

const theme = useTheme();
return (
Expand Down Expand Up @@ -51,7 +55,7 @@ export function WitvalValidator(props) {
<TableHead>
<StyledTableRow>
<StyledTableCell align="center">Validator</StyledTableCell>
<StyledTableCell align="center">Voting Power</StyledTableCell>
<StyledTableCell align="left">Voting Power</StyledTableCell>
<StyledTableCell align="center">Commission</StyledTableCell>
<StyledTableCell align="center">Actions</StyledTableCell>
</StyledTableRow>
Expand All @@ -68,8 +72,8 @@ export function WitvalValidator(props) {
? formatValidatorStatus(true, null)
: formatValidatorStatus(false, validator?.status)}
</StyledTableCell>
<StyledTableCell align="center">
{formatVotingPower(validator.tokens, 6)}
<StyledTableCell align="left">
{formatVotingPower(validator.tokens, coinDecimals)}
</StyledTableCell>
<StyledTableCell align="center">
{(validator.commission.commission_rates.rate * 100).toFixed(
Expand Down
1 change: 0 additions & 1 deletion frontend/src/features/bank/bankSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SendMsg } from "../../txns/bank";
import bankService from "./service";
import { setError, setTxHash } from "../common/commonSlice";
import { signAndBroadcast } from "../../utils/signing";
import { parseBalance } from "../../utils/denom";

const initialState = {
balances: {},
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/features/distribution/distributionSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ export const distSlice = createSlice({
reducers: {
resetTx: (state, action) => {
let chainID = action.payload.chainID;
state.chains[chainID].tx = initialState.defaultState.tx;
if (state.chains[chainID].tx) {
state.chains[chainID].tx = initialState.defaultState.tx;
}
},
resetDefaultState: (state, action) => {
let chainsMap = {};
Expand Down
83 changes: 45 additions & 38 deletions frontend/src/features/wallet/walletSlice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { toBase64 } from "@cosmjs/encoding";
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { getWalletAmino, isWalletInstalled } from "../../txns/execute";
import {
getSignerAndAccountAmino,
getWalletAmino,
isWalletInstalled,
} from "../../txns/execute";
import { setConnected } from "../../utils/localStorage";
import { setError } from "../common/commonSlice";

Expand All @@ -11,13 +15,13 @@ const initialState = {
pubKey: "",
networks: {},
nameToChainIDs: {},
errorMessage: "",
};

export const connectWalletV1 = createAsyncThunk(
"wallet/connectv1",
async (data, { rejectWithValue, fulfillWithValue, dispatch }) => {
const mainnets = data.mainnets;
const testnets = data.testnets;

if (!isWalletInstalled()) {
dispatch(
Expand All @@ -39,58 +43,61 @@ export const connectWalletV1 = createAsyncThunk(
let isNanoLedger = false;
const chainInfos = {};
const nameToChainIDs = {};
const chainIDs = [];
const chainNames = [];
const supportedChains = [];
for (let i = 0; i < mainnets.length; i++) {
try {
if ((data.walletName === "keplr" || data.walletName === "cosmostation" ) && mainnets[i].keplrExperimental) {
if (
(data.walletName === "keplr" ||
data.walletName === "cosmostation") &&
mainnets[i].keplrExperimental
) {
await window.wallet.experimentalSuggestChain(mainnets[i].config);
}
if (data.walletName === "leap" && mainnets[i].leapExperimental) {
await window.wallet.experimentalSuggestChain(mainnets[i].config);
}
let chainId = mainnets[i].config.chainId;
const chainName = mainnets[i].config.chainName;
await getWalletAmino(chainId);
let walletInfo = await window.wallet.getKey(chainId);
walletInfo.pubKey = Buffer.from(walletInfo?.pubKey).toString('base64');
delete walletInfo?.address;

walletName = walletInfo?.name;
isNanoLedger = walletInfo?.isNanoLedger || false;


chainInfos[chainId] = {
walletInfo: walletInfo,
network: mainnets[i],
};
nameToChainIDs[chainName?.toLowerCase().split(" ").join("")] = chainId;
} catch (error) {
console.log(`unable to connect to network ${mainnets[i].config.chainName}: `, error);
console.log(
`unable to connect to network ${mainnets[i].config.chainName}: `,
error
);
continue;
}
chainIDs.push(mainnets[i].config.chainId);
chainNames.push(mainnets[i].config.chainName);
supportedChains.push(mainnets[i]);
}

try {
await window.wallet.enable(chainIDs);
} catch (error) {
return rejectWithValue("Wallet connection request rejected");
}

for (let i = 0; i < testnets.length; i++) {
for (let i = 0; i < chainIDs.length; i++) {
try {
if (testnets[i].experimental) {
await window.wallet.experimentalSuggestChain(testnets[i].config);
}
const chainId = testnets[i].config.chainId;
const chainName = testnets[i].config.chainName;
await getWalletAmino(chainId);
const chainId = chainIDs[i];
const chainName = chainNames[i];
await getSignerAndAccountAmino(chainId);
const walletInfo = await window.wallet.getKey(chainId);
walletInfo.pubKey = Buffer.from(walletInfo?.pubKey).toString('base64');
walletInfo.pubKey = Buffer.from(walletInfo?.pubKey).toString(
"base64"
);
delete walletInfo?.address;

walletName = walletInfo?.name;
isNanoLedger = walletInfo?.isNanoLedger || false;

chainInfos[chainId] = {
walletInfo: walletInfo,
network: testnets[i],
network: supportedChains[i],
};

nameToChainIDs[chainName?.toLowerCase()] = chainId;
} catch (error) {
console.log(`unable to connect to network ${mainnets[i].config.chainName}: `, error);
console.log(`unable to connect to network ${chainNames[i]}: `, error);
}
}

Expand All @@ -108,13 +115,12 @@ export const connectWalletV1 = createAsyncThunk(
chainInfos,
nameToChainIDs,
walletName,
isNanoLedger
isNanoLedger,
});
}

}
}
)
);

export const connectKeplrWallet = createAsyncThunk(
"wallet/connect",
Expand Down Expand Up @@ -193,7 +199,7 @@ export const walletSlice = createSlice({
},
extraReducers: (builder) => {
builder
.addCase(connectKeplrWallet.pending, () => { })
.addCase(connectKeplrWallet.pending, () => {})
.addCase(connectKeplrWallet.fulfilled, (state, action) => {
const result = action.payload;
state.name = result.walletInfo?.name;
Expand All @@ -203,10 +209,9 @@ export const walletSlice = createSlice({
state.connected = true;
state.isNanoLedger = action.payload?.walletInfo?.isNanoLedger || false;
})
.addCase(connectKeplrWallet.rejected, () => { })

.addCase(connectKeplrWallet.rejected, () => {})

.addCase(connectWalletV1.pending, () => { })
.addCase(connectWalletV1.pending, () => {})
.addCase(connectWalletV1.fulfilled, (state, action) => {
const networks = action.payload.chainInfos;
const nameToChainIDs = action.payload.nameToChainIDs;
Expand All @@ -216,7 +221,9 @@ export const walletSlice = createSlice({
state.isNanoLedger = action.payload.isNanoLedger;
state.name = action.payload.walletName;
})
.addCase(connectWalletV1.rejected, () => { });
.addCase(connectWalletV1.rejected, (state, action) => {
state.errorMessage = action.payload || "Something went wrong";
});
},
});

Expand Down
1 change: 1 addition & 0 deletions frontend/src/pages/staking/chain/Validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ export default function Validators(props) {
<WitvalValidator
validator={validators.witvalValidator}
onMenuAction={onMenuAction}
chainID={chainID}
/>
<br />
</>
Expand Down
Loading

0 comments on commit ef463f0

Please sign in to comment.