Skip to content

Commit

Permalink
Merge pull request #2450 from decentdao/fix/bigint-serialization
Browse files Browse the repository at this point in the history
BigInt serialization fix
  • Loading branch information
mudrila authored Nov 6, 2024
2 parents 29a9349 + be3f8dd commit a9f325d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/hooks/DAO/loaders/useFractalGovernance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useFractal } from '../../../providers/App/AppProvider';
import { FractalGovernanceAction } from '../../../providers/App/governance/action';
import useIPFSClient from '../../../providers/App/hooks/useIPFSClient';
import { useNetworkConfig } from '../../../providers/NetworkConfig/NetworkConfigProvider';
import { GovernanceType } from '../../../types';
import { GovernanceType, ProposalTemplate } from '../../../types';
import { useERC20LinearStrategy } from './governance/useERC20LinearStrategy';
import { useERC20LinearToken } from './governance/useERC20LinearToken';
import { useERC721LinearStrategy } from './governance/useERC721LinearStrategy';
Expand Down Expand Up @@ -44,27 +44,43 @@ export const useFractalGovernance = () => {
const { daos } = data;
const dao = daos[0];

if (dao) {
const { proposalTemplatesHash } = dao;
if (proposalTemplatesHash) {
const proposalTemplates = await ipfsClient.cat(proposalTemplatesHash);
const { proposalTemplatesHash } = dao;

action.dispatch({
type: FractalGovernanceAction.SET_PROPOSAL_TEMPLATES,
payload: proposalTemplates || [],
});
} else {
action.dispatch({
type: FractalGovernanceAction.SET_PROPOSAL_TEMPLATES,
payload: [],
});
}
} else {
if (!proposalTemplatesHash) {
action.dispatch({
type: FractalGovernanceAction.SET_PROPOSAL_TEMPLATES,
payload: [],
});
return;
}

const proposalTemplates: ProposalTemplate[] | undefined =
await ipfsClient.cat(proposalTemplatesHash);

if (!proposalTemplates) {
action.dispatch({
type: FractalGovernanceAction.SET_PROPOSAL_TEMPLATES,
payload: [],
});
return;
}

const mappedProposalTemplates = proposalTemplates.map(proposalTemplate => ({
...proposalTemplate,
transactions: proposalTemplate.transactions.map(transaction => ({
...transaction,
ethValue: {
// bigintValue was serialized as a string, so we need to convert it back to a bigint
bigintValue: BigInt(transaction.ethValue.bigintValue || 0n),
value: transaction.ethValue.value ?? '0',
},
})),
}));

action.dispatch({
type: FractalGovernanceAction.SET_PROPOSAL_TEMPLATES,
payload: mappedProposalTemplates,
});
},
context: {
subgraphSpace: subgraph.space,
Expand Down
1 change: 1 addition & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import './insights';
import { useNetworkConfig } from './providers/NetworkConfig/NetworkConfigProvider';
import Providers from './providers/Providers';
import { router } from './router';
import './utils/polyfills';

function DecentRouterProvider() {
const { addressPrefix } = useNetworkConfig();
Expand Down
8 changes: 8 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare global {
interface BigInt {
/** Convert BigInt to string form in JSON.stringify */
toJSON: () => string;
}
}

export {};
3 changes: 3 additions & 0 deletions src/utils/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BigInt.prototype.toJSON = function () {
return this.toString();
};

0 comments on commit a9f325d

Please sign in to comment.