Add override switch to allow sending token funds to a sol wallet with zero balances (#84)

* Add override switch to allow sending token funds to a sol wallet with zero balances

* Only show when address fails validation
This commit is contained in:
Nathaniel Parke 2021-02-17 10:59:26 +08:00 committed by GitHub
parent 3fc7be09f1
commit cf8647f57e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -33,6 +33,7 @@ import {
WRAPPED_SOL_MINT,
} from '../utils/tokens/instructions';
import { parseTokenAccountData } from '../utils/tokens/data';
import { Switch } from "@material-ui/core";
const WUSDC_MINT = new PublicKey(
'BXXkv6z8ykpG1yuvUDPgh732wzVHB69RnB9YgSYh3itW',
@ -147,6 +148,8 @@ function SendSplDialog({ onClose, publicKey, balanceInfo, onSubmitRef }) {
defaultAddressHelperText,
);
const [passValidation, setPassValidation] = useState();
const [overrideDestinationCheck, setOverrideDestinationCheck] = useState();
const [shouldShowOverride, setShouldShowOverride] = useState();
const {
fields,
destinationAddress,
@ -161,12 +164,14 @@ function SendSplDialog({ onClose, publicKey, balanceInfo, onSubmitRef }) {
if (!destinationAddress) {
setAddressHelperText(defaultAddressHelperText);
setPassValidation(undefined);
setShouldShowOverride(undefined);
return;
}
try {
const destinationAccountInfo = await wallet.connection.getAccountInfo(
new PublicKey(destinationAddress),
);
setShouldShowOverride(false);
if (destinationAccountInfo.owner.equals(TOKEN_PROGRAM_ID)) {
const accountInfo = parseTokenAccountData(
@ -186,6 +191,7 @@ function SendSplDialog({ onClose, publicKey, balanceInfo, onSubmitRef }) {
} catch (e) {
console.log(`Received error validating address ${e}`);
setAddressHelperText(defaultAddressHelperText);
setShouldShowOverride(true);
setPassValidation(undefined);
}
})();
@ -202,6 +208,8 @@ function SendSplDialog({ onClose, publicKey, balanceInfo, onSubmitRef }) {
new PublicKey(destinationAddress),
amount,
balanceInfo.mint,
null,
overrideDestinationCheck
);
}
@ -213,6 +221,16 @@ function SendSplDialog({ onClose, publicKey, balanceInfo, onSubmitRef }) {
<>
<DialogContent>{fields}</DialogContent>
<DialogActions>
{ shouldShowOverride && (
<div style={{'align-items': 'center', 'display': 'flex', 'text-align': 'left'}}>
<b>This address has no funds. Are you sure it's correct?</b>
<Switch
checked={overrideDestinationCheck}
onChange={e => setOverrideDestinationCheck(e.target.checked)}
color="primary"
/>
</div>
)}
<Button onClick={onClose}>Cancel</Button>
<Button
type="submit"
@ -432,7 +450,7 @@ function SendSwapProgress({ publicKey, signature, onClose, blockchain }) {
);
}
function useForm(balanceInfo, addressHelperText, passAddressValidation) {
function useForm(balanceInfo, addressHelperText, passAddressValidation, overrideValidation) {
const [destinationAddress, setDestinationAddress] = useState('');
const [transferAmountString, setTransferAmountString] = useState('');
const { amount: balanceAmount, decimals, tokenSymbol } = balanceInfo;

View File

@ -204,6 +204,7 @@ export async function transferTokens({
amount,
memo,
mint,
overrideDestinationCheck,
}) {
const destinationAccountInfo = await connection.getAccountInfo(
destinationPublicKey,
@ -221,7 +222,8 @@ export async function transferTokens({
memo,
});
}
if (!destinationAccountInfo || destinationAccountInfo.lamports === 0) {
if ((!destinationAccountInfo || destinationAccountInfo.lamports === 0) && !overrideDestinationCheck) {
throw new Error('Cannot send to address with zero SOL balances');
}
const destinationSplTokenAccount = (

View File

@ -84,7 +84,7 @@ export class Wallet {
);
};
transferToken = async (source, destination, amount, mint, memo = null) => {
transferToken = async (source, destination, amount, mint, memo = null, overrideDestinationCheck = false) => {
if (source.equals(this.publicKey)) {
if (memo) {
throw new Error('Memo not implemented');
@ -99,6 +99,7 @@ export class Wallet {
amount,
memo,
mint,
overrideDestinationCheck,
});
};