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

Update @api3/promise-utils #419

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from 'cypress';
import { readFileSync, existsSync } from 'fs';

export default defineConfig({
defaultCommandTimeout: 10000,
defaultCommandTimeout: 15000,

retries: {
runMode: 1,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"wagmi": "^0.12.13"
},
"devDependencies": {
"@api3/promise-utils": "^0.1.0",
"@api3/promise-utils": "^0.4.0",
"@nomiclabs/hardhat-ethers": "^2.0.4",
"@percy/cli": "^1.6.0",
"@percy/cypress": "^3.1.2",
Expand Down
2 changes: 1 addition & 1 deletion src/chain-data/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const displayPendingTransaction = async (
{ autoClose: false, closeOnClick: false }
);

const goRes = await go(transaction.wait());
const goRes = await go(() => transaction.wait());
if (infoToastId) notifications.close(infoToastId);

// NOTE: ethers.js adds various additional fields to Error, so it's easier to type as 'any'
Expand Down
4 changes: 2 additions & 2 deletions src/logic/dashboard/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const useLoadDashboardData = () => {
const loadDashboardData = useCallback(async () => {
if (!provider || !api3Pool || !api3Token || !convenience || !userAccount) return null;

const goStakingData = await go(convenience.getUserStakingData(userAccount));
const goStakingData = await go(() => convenience.getUserStakingData(userAccount));
if (!goStakingData.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_CHAIN_DATA,
Expand All @@ -23,7 +23,7 @@ export const useLoadDashboardData = () => {
}
const stakingData = goStakingData.data;

const goAllowance = await go(api3Token.allowance(userAccount, api3Pool.address));
const goAllowance = await go(() => api3Token.allowance(userAccount, api3Pool.address));
if (!goAllowance.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_CHAIN_DATA,
Expand Down
2 changes: 1 addition & 1 deletion src/logic/proposals/encoding/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const goEncodeEvmScript = async (
provider: providers.Provider,
formData: NewProposalFormData,
api3Agent: Api3Agent
): Promise<GoResult<string, EncodedEvmScriptError>> => {
): Promise<GoResult<string>> => {
// Ensure that the form parameters form a valid JSON array
const goJsonParams = goSync(() => {
const json = JSON.parse(formData.parameters);
Expand Down
2 changes: 1 addition & 1 deletion src/logic/proposals/encoding/ens-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const convertToAddressOrThrow = async (provider: providers.Provider, ensN
export const convertToEnsName = async (provider: providers.Provider, ensNameOrAddress: string) => {
if (!utils.isAddress(ensNameOrAddress)) return null;

const goEnsName = await go(provider.lookupAddress(ensNameOrAddress));
const goEnsName = await go(() => provider.lookupAddress(ensNameOrAddress));
if (!goEnsName.success) return null;

return goEnsName.data;
Expand Down
2 changes: 1 addition & 1 deletion src/logic/proposals/hooks/active-proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const useReloadActiveProposals = () => {
}
};

const goResponse = await go(loadProposals());
const goResponse = await go(() => loadProposals());
if (!goResponse.success) {
// TODO: error handling
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion src/logic/proposals/hooks/genesis-epoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const useLoadGenesisEpoch = () => {
const loadIsGenesisEpoch = useCallback(async () => {
if (!api3Pool) return null;

const goResponse = await go(api3Pool.isGenesisEpoch());
const goResponse = await go(() => api3Pool.isGenesisEpoch());
if (!goResponse.success) {
notifications.error({
message: messages.FAILED_TO_LOAD_GENESIS_EPOCH,
Expand Down
2 changes: 1 addition & 1 deletion src/logic/proposals/hooks/history-proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const useReloadHistoryProposals = () => {
);
};

const goResponse = await go(loadProposals());
const goResponse = await go(() => loadProposals());
if (!goResponse.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_PROPOSALS,
Expand Down
10 changes: 6 additions & 4 deletions src/logic/proposals/hooks/load-by-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const useProposalById = (type: ProposalType, id: BigNumber) => {
const votingApp = api3Voting[type];
const startVoteFilter = votingApp.filters.StartVote(id, null, null);

const goStartVoteFilters = await go(votingApp.queryFilter(startVoteFilter));
const goStartVoteFilters = await go(() => votingApp.queryFilter(startVoteFilter));
if (!goStartVoteFilters.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_PROPOSALS,
Expand All @@ -62,7 +62,7 @@ export const useProposalById = (type: ProposalType, id: BigNumber) => {
voteId: ethersArgs.voteId,
};

const goOpenVoteIds = await go(convenience.getOpenVoteIds(VOTING_APP_IDS[type]));
const goOpenVoteIds = await go(() => convenience.getOpenVoteIds(VOTING_APP_IDS[type]));
if (!goOpenVoteIds.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_PROPOSALS,
Expand All @@ -71,7 +71,9 @@ export const useProposalById = (type: ProposalType, id: BigNumber) => {
}
const openVoteIds = goOpenVoteIds.data;

const goLoadProposal = await go(getProposals(provider, convenience, userAccount, [startVote], openVoteIds, type));
const goLoadProposal = await go(() =>
getProposals(provider, convenience, userAccount, [startVote], openVoteIds, type)
);
if (!goLoadProposal.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_PROPOSALS,
Expand All @@ -97,7 +99,7 @@ export const useProposalById = (type: ProposalType, id: BigNumber) => {
const reloadProposalsByIds = useCallback(async () => {
if (!convenience) return;

const goVotingData = await go(convenience.getDynamicVoteData(VOTING_APP_IDS[type], userAccount, [id]));
const goVotingData = await go(() => convenience.getDynamicVoteData(VOTING_APP_IDS[type], userAccount, [id]));
if (!goVotingData.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_PROPOSALS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useTreasuryAndDelegation = () => {
if (!api3Voting || !convenience || !provider) return;

const loadTreasuryAndDelegation = async () => {
const goResponse = await go(convenience.getTreasuryAndUserDelegationData(userAccount));
const goResponse = await go(() => convenience.getTreasuryAndUserDelegationData(userAccount));
assertGoSuccess(goResponse);
const data = goResponse.data;

Expand Down
4 changes: 2 additions & 2 deletions src/logic/vesting/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useLoadVestingData = () => {
const loadDashboardData = useCallback(async () => {
if (!provider || !api3Pool || !timelockManager || !userAccount) return null;

const goVestingData = await go(api3Pool.getUser(userAccount));
const goVestingData = await go(() => api3Pool.getUser(userAccount));
if (!goVestingData.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_VESTING_DATA,
Expand All @@ -22,7 +22,7 @@ export const useLoadVestingData = () => {
}
const vestingData = goVestingData.data;

const goTimelock = await go(timelockManager.getTimelock(userAccount));
const goTimelock = await go(() => timelockManager.getTimelock(userAccount));
if (!goTimelock.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_VESTING_DATA,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/dashboard/forms/token-deposit-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const TokenDepositForm = (props: Props) => {

setError('');

const goResponse = await go(api3Token.connect(signer!).approve(api3Pool.address, MAX_ALLOWANCE));
const goResponse = await go(() => api3Token.connect(signer!).approve(api3Pool.address, MAX_ALLOWANCE));
if (goResponse.success) {
const tx = goResponse.data;
setChainData('Save deposit approval', { transactions: [...transactions, { type: 'approve-deposit', tx }] });
Expand Down Expand Up @@ -66,7 +66,7 @@ const TokenDepositForm = (props: Props) => {
setError('');

const methodName = type === 'deposit-only' ? 'depositRegular' : 'depositAndStake';
const goResponse = await go(api3Pool.connect(signer!)[methodName](parsedInput));
const goResponse = await go(() => api3Pool.connect(signer!)[methodName](parsedInput));
if (goResponse.success) {
const tx = goResponse.data;
setChainData(`Save "${type}" transaction`, { transactions: [...transactions, { type, tx }] });
Expand Down
4 changes: 2 additions & 2 deletions src/pages/proposals/forms/delegate/delegate-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DelegateVotesForm = (props: Props) => {
const onDelegate = async () => {
if (!api3Pool || !provider) return;

const goDelegationTargetResolvedName = await go(convertToAddressOrThrow(provider, delegationAddress));
const goDelegationTargetResolvedName = await go(() => convertToAddressOrThrow(provider, delegationAddress));
const delegationTarget = goDelegationTargetResolvedName.success
? goDelegationTargetResolvedName.data
: delegationAddress;
Expand All @@ -42,7 +42,7 @@ const DelegateVotesForm = (props: Props) => {
return setError(messages.DELEGATE_IS_YOURSELF);
}

const goDelegate = await go(api3Pool.userDelegate(delegationTarget));
const goDelegate = await go(() => api3Pool.userDelegate(delegationTarget));
if (!goDelegate.success) {
return notifications.error({
message: messages.FAILED_TO_LOAD_DELEGATE,
Expand Down
11 changes: 8 additions & 3 deletions src/pages/proposals/forms/new-proposal-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Input from '../../../components/input';
import Textarea from '../../../components/textarea';
import { Tooltip } from '../../../components/tooltip';
import { ModalFooter, ModalHeader } from '../../../components/modal';
import { goEncodeEvmScript, NewProposalFormData } from '../../../logic/proposals/encoding';
import { goEncodeEvmScript, EncodedEvmScriptError, NewProposalFormData } from '../../../logic/proposals/encoding';
import { Api3Agent } from '../../../contracts';
import { filterAlphanumerical, images } from '../../../utils';
import styles from './new-proposal-form.module.scss';
Expand Down Expand Up @@ -77,8 +77,13 @@ const NewProposalForm = (props: Props) => {

const goRes = await goEncodeEvmScript(provider, formData, api3Agent);
if (!goRes.success) {
const { field, value } = goRes.error;
newErrors[field] = value;
if (goRes.error instanceof EncodedEvmScriptError) {
const { field, value } = goRes.error;
newErrors[field] = value;
} else {
// We should always get an EncodedEvmScriptError, but we give the user a message just in case it is not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

newErrors.generic = 'Failed to encode';
}
foundErrors = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/error-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type ErrorWithCode = Error & { code?: number };
export const isUserRejection = (err: ErrorWithCode) => err.code === 4001 || err.code === 4100;

export const handleTransactionError = async <T>(transaction: Promise<T>) => {
const goTransaction = await go(transaction);
const goTransaction = await go(() => transaction);

if (!goTransaction.success) {
if (isUserRejection(goTransaction.error)) {
Expand Down
15 changes: 4 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
"@jridgewell/gen-mapping" "^0.1.0"
"@jridgewell/trace-mapping" "^0.3.9"

"@api3/promise-utils@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@api3/promise-utils/-/promise-utils-0.1.0.tgz#bc0678a5d19b32274192656dc787a131e22d82dc"
integrity sha512-hel2yq9p9d5Yki9nf+9S7XNDzko+FVzYUps5JGwl6XvV5eBaEUDkEeTG5ulyUORzJdplfT+KyE1zxwIMG/ZfhQ==
dependencies:
"@lifeomic/attempt" "^3.0.2"
"@api3/promise-utils@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@api3/promise-utils/-/promise-utils-0.4.0.tgz#d1dcd77d74377b4fdb3071d2cc76d98c9151309c"
integrity sha512-+8fcNjjQeQAuuSXFwu8PMZcYzjwjDiGYcMUfAQ0lpREb1zHonwWZ2N0B9h/g1cvWzg9YhElbeb/SyhCrNm+b/A==

"@apideck/better-ajv-errors@^0.3.1":
version "0.3.6"
Expand Down Expand Up @@ -1931,11 +1929,6 @@
resolved "https://registry.yarnpkg.com/@leichtgewicht/ip-codec/-/ip-codec-2.0.4.tgz#b2ac626d6cb9c8718ab459166d4bb405b8ffa78b"
integrity sha512-Hcv+nVC0kZnQ3tD9GVu5xSMR4VVYOteQIr/hwFPVEvPdlXqgGEuRjiheChHgdM+JyqdgNcmzZOX/tnl0JOiI7A==

"@lifeomic/attempt@^3.0.2":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@lifeomic/attempt/-/attempt-3.0.3.tgz#e742a5b85eb673e2f1746b0f39cb932cbc6145bb"
integrity sha512-GlM2AbzrErd/TmLL3E8hAHmb5Q7VhDJp35vIbyPVA5Rz55LZuRr8pwL3qrwwkVNo05gMX1J44gURKb4MHQZo7w==

"@lit-labs/ssr-dom-shim@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.0.0.tgz#427e19a2765681fd83411cd72c55ba80a01e0523"
Expand Down