Skip to content

Commit

Permalink
Merge pull request #2331 from DistributedCollective/development
Browse files Browse the repository at this point in the history
Release 2022-07-13
  • Loading branch information
soulBit authored Jul 13, 2022
2 parents 3bd2b3d + 2d03c38 commit 0284a79
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 18 deletions.
26 changes: 26 additions & 0 deletions src/app/hooks/useGsnSendTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'store/global/transactions-store/selectors';
import {
Transaction,
TxFailReason,
TxStatus,
TxType,
} from 'store/global/transactions-store/types';
Expand All @@ -24,6 +25,26 @@ import { BridgeNetworkDictionary } from '../pages/BridgeDepositPage/dictionaries
import { getContract } from '../../utils/blockchain/contract-helpers';
import { ContractName } from '../../utils/types/contracts';

// example of the error message can be seen here: https://pastebin.com/2kkCyWfk
const parseGsnErrorMessage = (message: string): TxFailReason => {
const codeSubstring = '"code":';
const codeIndex = message.indexOf(codeSubstring);
const errorCodeBeginningIndex = codeIndex + codeSubstring.length;
const errorCode = message.substring(
errorCodeBeginningIndex,
errorCodeBeginningIndex + 1,
);

// The error codes should be 1-10 according to this doc https://docs.openzeppelin.com/contracts/3.x/api/gsn
// but I haven't been able to find all of the values
switch (errorCode) {
case '3':
return TxFailReason.INSUFFICIENT_USER_FUNDS;
default:
return TxFailReason.UNKNOWN;
}
};

/**
* Call contracts using the Gas Station Network, allowing you to pay transactions fees with another token!
* Falls back to bridgeNetwork, when useGSN = false is passed to the hook.
Expand All @@ -46,6 +67,9 @@ export const useGsnSendTx = (
const account = useAccount();
const [txId, setTxId] = useState<string | TxStatus>(TxStatus.NONE);
const [tx, setTx] = useState<Nullable<Transaction>>(null);
const [txFailReason, setTxFailReason] = useState<TxFailReason | undefined>(
undefined,
);

const chainId = useMemo(() => BridgeNetworkDictionary.get(chain)?.chainId, [
chain,
Expand Down Expand Up @@ -138,6 +162,7 @@ export const useGsnSendTx = (
.catch(e => {
console.error(e.message);
setTxId(TxStatus.FAILED);
setTxFailReason(parseGsnErrorMessage(e.message));
dispatch(actions.setTransactionRequestDialogError(e.message));
});
},
Expand Down Expand Up @@ -172,5 +197,6 @@ export const useGsnSendTx = (
txHash: tx?.transactionHash || '',
status: tx ? tx.status : txId,
loading: loading,
failReason: txFailReason,
};
};
2 changes: 2 additions & 0 deletions src/app/hooks/useSendContractTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'store/global/transactions-store/selectors';
import {
Transaction,
TxFailReason,
TxStatus,
TxType,
} from 'store/global/transactions-store/types';
Expand All @@ -32,6 +33,7 @@ export interface SendTxResponse {
txData: Nullable<Transaction>;
status: TxStatus | any;
loading: boolean;
failReason?: TxFailReason;
}

export interface ResetTxResponseInterface extends SendTxResponse {
Expand Down
50 changes: 41 additions & 9 deletions src/app/pages/FastBtcPage/containers/DepositContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,36 @@ import { Signature } from '../contexts/deposit-context';
import { SignatureValidation } from '../components/Deposit/SignatureValidation';
import { isMainnet } from 'utils/classifiers';
import { Chain, ChainId } from 'types';
import { contractReader } from 'utils/sovryn/contract-reader';
import { debug } from 'utils/debug';

export const DepositContainer: React.FC<NetworkAwareComponentProps> = ({
network,
}) => {
const log = debug('FastBTCDeposit');
const [state, setState] = useState<DepositContextStateType>(defaultValue);
const { step } = state;

const dispatch = useDispatch();
const walletContext = useWalletContext();
const [requiredSigners, setRequiredSigners] = useState<number | undefined>();

useEffect(() => {
const getRequiredSigners = async () => {
try {
const required: number = await contractReader.call(
'fastBtcMultisig',
'required',
[],
);
setRequiredSigners(required);
} catch (e) {
console.error(e);
}
};

getRequiredSigners();
}, []);

useEffect(() => {
if (network === Chain.BSC) {
Expand Down Expand Up @@ -85,16 +106,27 @@ export const DepositContainer: React.FC<NetworkAwareComponentProps> = ({

const handleAddressRequest = useCallback(
(address: string) => {
setState(prevState => ({ ...prevState, addressLoading: true }));
setState(prevState => ({
...prevState,
addressLoading: true,
}));
getDepositAddress(address)
.then(response => {
setState(prevState => ({
...prevState,
addressLoading: false,
address: response.btcadr,
step: DepositStep.VALIDATION,
signatures: response.signatures as Signature[],
}));
log.log(`checking for ${requiredSigners}: ${response.signatures}`);
if (
requiredSigners !== undefined &&
response.signatures.length >= requiredSigners
) {
setState(prevState => ({
...prevState,
addressLoading: false,
address: response.btcadr,
step: DepositStep.VALIDATION,
signatures: response.signatures as Signature[],
}));
} else {
handleAddressRequest(address);
}
})
.catch(error => {
console.error(error);
Expand All @@ -106,7 +138,7 @@ export const DepositContainer: React.FC<NetworkAwareComponentProps> = ({
}));
});
},
[getDepositAddress],
[getDepositAddress, requiredSigners, log],
);

const handleValidation = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const getBridgeUrl = () => {
return 'https://bridge.staging.sovryn.app';
}

return 'https://bridge.test.sovryn.app';
// TODO: Change it back to https://bridge.test.sovryn.app once we resolve the DNS issue
return 'https://dev--legacy-bridge.netlify.app';
};

export const AccountBalanceForm: React.FC<AccountBalanceFormProps> = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
import { useWalletContext } from '@sovryn/react-wallet';
import classNames from 'classnames';
import { usePerpetual_transaction } from '../../../hooks/usePerpetual_transaction';
import { TxStatus } from '../../../../../../store/global/transactions-store/types';
import {
TxFailReason,
TxStatus,
} from '../../../../../../store/global/transactions-store/types';
import { useSelector } from 'react-redux';
import { selectTransactions } from '../../../../../../store/global/transactions-store/selectors';
import { TransitionAnimation } from '../../../../../containers/TransitionContainer';
Expand All @@ -39,7 +42,13 @@ export const ConfirmationStep: TransitionStep<TradeDialogStep> = ({

const { useMetaTransactions } = useSelector(selectPerpetualPage);
const { wallet } = useWalletContext();
const { execute, txHash, status, reset } = usePerpetual_transaction(
const {
execute,
txHash,
status,
reset,
failReason,
} = usePerpetual_transaction(
currentTransaction ? transactions[currentTransaction.index] : undefined,
useMetaTransactions,
);
Expand All @@ -60,13 +69,15 @@ export const ConfirmationStep: TransitionStep<TradeDialogStep> = ({
rejected || transactionStatus === TxStatus.FAILED,
(currentTransaction?.index || 0) + 1,
transactions.length,
failReason,
),
[
t,
rejected,
transactionStatus,
transactions.length,
currentTransaction?.index,
transactions.length,
failReason,
],
);

Expand Down Expand Up @@ -181,11 +192,20 @@ export const ConfirmationStep: TransitionStep<TradeDialogStep> = ({
);
};

const failReasonsMapping = {
[TxFailReason.INSUFFICIENT_USER_FUNDS]:
translations.perpetualPage.processTrade.texts.failReasons
.insufficientUserFunds,
[TxFailReason.UNKNOWN]:
translations.perpetualPage.processTrade.texts.failReasons.unknown,
};

const getTranslations = (
t,
rejected: boolean,
current: number,
count: number,
failReason?: TxFailReason,
) => {
const wallet = detectWeb3Wallet();

Expand All @@ -202,9 +222,15 @@ const getTranslations = (
),
text: (
<p className="tw-text-warning">
{t(translations.perpetualPage.processTrade.texts.rejected)}
<br />
{t(translations.perpetualPage.processTrade.texts.cancelOrRetry)}
{failReason && failReason !== TxFailReason.UNKNOWN ? (
t(failReasonsMapping[failReason])
) : (
<>
{t(translations.perpetualPage.processTrade.texts.rejected)}
<br />
{t(translations.perpetualPage.processTrade.texts.cancelOrRetry)}
</>
)}
</p>
),
};
Expand Down
6 changes: 5 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2423,7 +2423,11 @@
"confirmMulit": "Please confirm transaction {{current}} of {{count}} in your {{wallet}} wallet",
"error": "Your transaction has failed due to <0>{{error}}</0> Your position has not been opened",
"rejected": "The transaction request was rejected",
"cancelOrRetry": "Please cancel or retry your transaction"
"cancelOrRetry": "Please cancel or retry your transaction",
"failReasons": {
"insufficientUserFunds": "You do not have enough balance to pay the gas for this transaction",
"unknown": "An unknown error has occurred"
}
},
"labels": {
"approvalTx": "Approval Tx ID:",
Expand Down
2 changes: 1 addition & 1 deletion src/locales/pt_br/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@
"originsLaunchpad": "Vendas realizadas na plataforma Origens",
"originsClaim": "Reivindicar recompensas FISH",
"myntToken": "Protocolo Sovryn de moedas estáveis apoiado por BTC",
"perpetuals": "Em breve",
"perpetuals": "Negocie contratos futuros perpétuos",
"zero": "Empréstimos sem juros garantidos por seus bitcoins",
"voting": "Participe das decisões de governança",
"forum": "Junte-se à comunidade"
Expand Down
5 changes: 5 additions & 0 deletions src/store/global/transactions-store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export enum TxStatus {
FAILED = 'failed',
}

export enum TxFailReason {
INSUFFICIENT_USER_FUNDS = 'insufficient_user_funds',
UNKNOWN = 'unknown',
}

export interface Transactions {
[transactionHash: string]: Transaction;
}
Expand Down

0 comments on commit 0284a79

Please sign in to comment.