-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update ether formatting and url sharing
- Loading branch information
Showing
6 changed files
with
136 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useSearchParams } from "react-router-dom"; | ||
import { Proposal } from "../schemas/proposal"; | ||
|
||
export const useLoadProposalFromQuery = () => { | ||
const [proposal, setProposal] = useState<undefined | Proposal>(); | ||
const [params] = useSearchParams(); | ||
|
||
useEffect(() => { | ||
const targets = params.get("targets")?.split("|"); | ||
const calldatas = params.get("calldatas")?.split("|"); | ||
const values = params.get("values")?.split("|"); | ||
if (targets && calldatas) { | ||
// ensure the 3 lengths are the same. check if values also has the same length if its not empty | ||
// check the inverse of the above, if inverse is true, return: | ||
if ( | ||
targets.length !== calldatas.length || | ||
(values?.length && values?.length !== targets.length) | ||
) { | ||
console.log("invalid lengths"); | ||
return; | ||
} | ||
|
||
const actions = targets.map((target, index) => ({ | ||
to: target, | ||
data: calldatas[index]!, | ||
value: (values && values[index]) || "0", | ||
})); | ||
setProposal({ actions }); | ||
} | ||
}, [params, setProposal]); | ||
|
||
return proposal; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useCallback } from "react"; | ||
import { useSearchParams } from "react-router-dom"; | ||
import { Proposal } from "../schemas/proposal"; | ||
|
||
export const useSetParamsFromQuery = () => { | ||
const [_, setParams] = useSearchParams(); | ||
|
||
return useCallback((proposal: Proposal) => { | ||
if (!proposal.actions?.length) { | ||
return; | ||
} | ||
console.log('setting params', proposal.actions); | ||
setParams({ | ||
targets: proposal.actions!.map((action) => action.to).join('|'), | ||
data: proposal.actions!.map((action) => action.data).join('|'), | ||
value: proposal.actions!.map((action) => action.value).join('|'), | ||
}) | ||
}, [setParams]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { InferType, array, number, object, string } from "yup"; | ||
import { yupAddress } from "../utils/validators"; | ||
|
||
export const proposalSchema = object({ | ||
nonce: number().nullable(), | ||
actions: array( | ||
object({ | ||
to: yupAddress, | ||
value: string() | ||
.default("0") | ||
.matches( | ||
/^[0-9]+(\.[0-9]+)?$/, | ||
"Needs to be a ETH price (0, 1, or 0.23)" | ||
) | ||
.required(), | ||
data: string() | ||
.default("0x") | ||
.matches( | ||
/^0x(?:[0-9A-Za-z][0-9A-Za-z])*$/, | ||
"Data is required to match hex format" | ||
) | ||
.required(), | ||
}) | ||
), | ||
}); | ||
|
||
export interface Proposal extends InferType<typeof proposalSchema> { | ||
// using interface instead of type generally gives nicer editor feedback | ||
} | ||
|
||
export const DEFAULT_ACTION_ITEM = { | ||
to: "0x", | ||
value: "0", | ||
data: "0x", | ||
}; | ||
|
||
export const DEFAULT_PROPOSAL = { | ||
nonce: null, | ||
actions: [DEFAULT_ACTION_ITEM], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { formatEther, parseEther } from "viem"; | ||
import { Proposal } from "../schemas/proposal"; | ||
|
||
export function transformValuesToWei(proposal: Proposal): Proposal { | ||
return { | ||
...proposal, | ||
actions: proposal.actions?.map((action) => ({ | ||
...action, | ||
value: parseEther(action.value).toString(), | ||
})), | ||
}; | ||
} | ||
|
||
export function transformValuesFromWei(proposal: Proposal): Proposal { | ||
return { | ||
...proposal, | ||
actions: proposal.actions?.map((action) => ({ | ||
...action, | ||
value: formatEther(BigInt(action.value)), | ||
})), | ||
}; | ||
} |