( signer: Signer, sdk: ContractSDK, amount: BigNumber, spender: string, logger: Logger, operationName: string, prompt?: Prompt )
| 8 | import {checkTransactionSuccess} from './constants'; |
| 9 | |
| 10 | export async function checkAndIncreaseAllowance( |
| 11 | signer: Signer, |
| 12 | sdk: ContractSDK, |
| 13 | amount: BigNumber, |
| 14 | spender: string, |
| 15 | logger: Logger, |
| 16 | operationName: string, |
| 17 | prompt?: Prompt |
| 18 | ): Promise<void> { |
| 19 | const userAddress = await signer.getAddress(); |
| 20 | |
| 21 | // Check current allowance |
| 22 | const allowance = await sdk.sqToken.allowance(userAddress, spender); |
| 23 | logger.info(`Current allowance: ${formatEther(allowance)} SQT`); |
| 24 | logger.info(`Amount needed: ${formatEther(amount)} SQT`); |
| 25 | |
| 26 | if (allowance.lt(amount)) { |
| 27 | const needed = amount.sub(allowance); |
| 28 | logger.warn(`Insufficient allowance. Need to approve ${formatEther(needed)} SQT additional allowance.`); |
| 29 | |
| 30 | if (prompt) { |
| 31 | const confirm = await prompt({ |
| 32 | type: 'boolean', |
| 33 | message: `Approve additional ${formatEther(needed)} SQT allowance to ${operationName}?`, |
| 34 | }); |
| 35 | if (!confirm) { |
| 36 | throw new Error(`Allowance approval was cancelled. Cannot proceed with ${operationName}.`); |
| 37 | } |
| 38 | } else { |
| 39 | logger.info('Auto-approving allowance in non-interactive mode'); |
| 40 | } |
| 41 | |
| 42 | logger.info(`Increasing allowance of SQT to ${operationName}...`); |
| 43 | try { |
| 44 | const allowanceTx = await sdk.sqToken.increaseAllowance(spender, needed); |
| 45 | logger.info(`Allowance tx hash: ${allowanceTx.hash}`); |
| 46 | await checkTransactionSuccess(allowanceTx); |
| 47 | } catch (e) { |
| 48 | console.error('Error increasing allowance', e); |
| 49 | throw e; |
| 50 | } |
| 51 | logger.info(`✅ Successfully increased allowance of SQT to ${operationName}`); |
| 52 | } else { |
| 53 | logger.info('✅ Sufficient allowance already exists'); |
| 54 | } |
| 55 | } |
no test coverage detected