|
| 1 | +import { ContractTransaction, Signer, providers, BigNumberish, BigNumber } from 'ethers'; |
| 2 | + |
| 3 | +import { ClientTypes, ExtensionTypes, PaymentTypes } from '@requestnetwork/types'; |
| 4 | +import { getPaymentNetworkExtension } from '@requestnetwork/payment-detection'; |
| 5 | + |
| 6 | +import { getNetworkProvider, getProvider, validateRequest, MAX_ALLOWANCE } from './utils'; |
| 7 | +import Token from '@superfluid-finance/sdk-core/dist/module/Token'; |
| 8 | +import { getSuperFluidFramework } from './erc777-stream'; |
| 9 | +import Operation from '@superfluid-finance/sdk-core/dist/module/Operation'; |
| 10 | +import { checkErc20Allowance, encodeApproveAnyErc20, getAnyErc20Balance } from './erc20'; |
| 11 | +import { IPreparedTransaction } from './prepared-transaction'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Gets the underlying token address of an ERC777 currency based request |
| 15 | + * @param request the request that contains currency information |
| 16 | + * @param provider the web3 provider. Defaults to Etherscan |
| 17 | + */ |
| 18 | +export async function getRequestUnderlyingToken( |
| 19 | + request: ClientTypes.IRequestData, |
| 20 | + provider: providers.Provider = getNetworkProvider(request), |
| 21 | +): Promise<Token> { |
| 22 | + const id = getPaymentNetworkExtension(request)?.id; |
| 23 | + if (id !== ExtensionTypes.ID.PAYMENT_NETWORK_ERC777_STREAM) { |
| 24 | + throw new Error('Not a supported ERC777 payment network request'); |
| 25 | + } |
| 26 | + validateRequest(request, PaymentTypes.PAYMENT_NETWORK_ID.ERC777_STREAM); |
| 27 | + const sf = await getSuperFluidFramework(request, provider); |
| 28 | + const superToken = await sf.loadSuperToken(request.currencyInfo.value); |
| 29 | + return superToken.underlyingToken; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Gets the underlying token address of an ERC777 currency based request |
| 34 | + * @param request the request that contains currency information |
| 35 | + * @param provider the web3 provider. Defaults to Etherscan |
| 36 | + */ |
| 37 | +export async function getUnderlyingTokenBalanceOf( |
| 38 | + request: ClientTypes.IRequestData, |
| 39 | + address: string, |
| 40 | + provider: providers.Provider = getNetworkProvider(request), |
| 41 | +): Promise<BigNumberish> { |
| 42 | + const underlyingToken = await getRequestUnderlyingToken(request, provider); |
| 43 | + return getAnyErc20Balance(underlyingToken.address, address, provider); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Check if the user has the specified amount of underlying token |
| 48 | + * @param request the request that contains currency information |
| 49 | + * @param address token owner |
| 50 | + * @param provider the web3 provider. Defaults to Etherscan |
| 51 | + * @param amount the required amount |
| 52 | + */ |
| 53 | +export async function hasEnoughUnderlyingToken( |
| 54 | + request: ClientTypes.IRequestData, |
| 55 | + address: string, |
| 56 | + provider: providers.Provider = getNetworkProvider(request), |
| 57 | + amount: BigNumber, |
| 58 | +): Promise<boolean> { |
| 59 | + const balance = await getUnderlyingTokenBalanceOf(request, address, provider); |
| 60 | + return amount.lte(balance); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Determine whether or not the supertoken has enough allowance |
| 65 | + * @param request the request that contains currency information |
| 66 | + * @param address token owner |
| 67 | + * @param provider the web3 provider. Defaults to Etherscan |
| 68 | + * @param amount of token required |
| 69 | + */ |
| 70 | +export async function checkSuperTokenUnderlyingAllowance( |
| 71 | + request: ClientTypes.IRequestData, |
| 72 | + address: string, |
| 73 | + provider: providers.Provider = getNetworkProvider(request), |
| 74 | + amount: BigNumber = MAX_ALLOWANCE, |
| 75 | +): Promise<boolean> { |
| 76 | + const underlyingToken = await getRequestUnderlyingToken(request, provider); |
| 77 | + return checkErc20Allowance( |
| 78 | + address, |
| 79 | + request.currencyInfo.value, |
| 80 | + provider, |
| 81 | + underlyingToken.address, |
| 82 | + amount, |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Get the SF operation to approve the supertoken to spend underlying tokens |
| 88 | + * @param request the request that contains currency information |
| 89 | + * @param provider the web3 provider. Defaults to Etherscan |
| 90 | + * @param amount to allow, defalts to max allowance |
| 91 | + */ |
| 92 | +export async function prepareApproveUnderlyingToken( |
| 93 | + request: ClientTypes.IRequestData, |
| 94 | + provider: providers.Provider = getNetworkProvider(request), |
| 95 | + amount: BigNumber = MAX_ALLOWANCE, |
| 96 | +): Promise<IPreparedTransaction> { |
| 97 | + const underlyingToken = await getRequestUnderlyingToken(request, provider); |
| 98 | + return { |
| 99 | + data: encodeApproveAnyErc20( |
| 100 | + underlyingToken.address, |
| 101 | + request.currencyInfo.value, |
| 102 | + provider, |
| 103 | + amount, |
| 104 | + ), |
| 105 | + to: underlyingToken.address, |
| 106 | + value: 0, |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Get the SF operation to Wrap the underlying asset into supertoken |
| 112 | + * @param request the request that contains currency information |
| 113 | + * @param address the user address |
| 114 | + * @param provider the web3 provider |
| 115 | + * @param amount to allow, defalts to max allowance |
| 116 | + */ |
| 117 | +export async function getWrapUnderlyingTokenOp( |
| 118 | + request: ClientTypes.IRequestData, |
| 119 | + provider: providers.Provider = getNetworkProvider(request), |
| 120 | + amount: BigNumber, |
| 121 | +): Promise<Operation> { |
| 122 | + const sf = await getSuperFluidFramework(request, provider); |
| 123 | + const superToken = await sf.loadSuperToken(request.currencyInfo.value); |
| 124 | + return superToken.upgrade({ |
| 125 | + amount: amount.toString(), |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Approve the supertoken to spend the speicified amount of underlying token |
| 131 | + * @param request the request that contains currency information |
| 132 | + * @param signer the web3 signer |
| 133 | + * @param amount to allow, defaults to max allowance |
| 134 | + * @returns |
| 135 | + */ |
| 136 | +export async function approveUnderlyingToken( |
| 137 | + request: ClientTypes.IRequestData, |
| 138 | + signer: Signer, |
| 139 | + amount: BigNumber = MAX_ALLOWANCE, |
| 140 | +): Promise<ContractTransaction> { |
| 141 | + if ( |
| 142 | + !(await hasEnoughUnderlyingToken( |
| 143 | + request, |
| 144 | + await signer.getAddress(), |
| 145 | + signer.provider ?? getProvider(), |
| 146 | + amount, |
| 147 | + )) |
| 148 | + ) { |
| 149 | + throw new Error('Sender does not have enough underlying token'); |
| 150 | + } |
| 151 | + const preparedTx = await prepareApproveUnderlyingToken( |
| 152 | + request, |
| 153 | + signer.provider ?? getProvider(), |
| 154 | + amount, |
| 155 | + ); |
| 156 | + return signer.sendTransaction(preparedTx); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Wrap the speicified amount of underlying token into supertokens |
| 161 | + * @param request the request that contains currency information |
| 162 | + * @param signer the web3 signer |
| 163 | + * @param amount to allow, defaults to max allowance |
| 164 | + * @returns |
| 165 | + */ |
| 166 | +export async function wrapUnderlyingToken( |
| 167 | + request: ClientTypes.IRequestData, |
| 168 | + signer: Signer, |
| 169 | + amount: BigNumber = MAX_ALLOWANCE, |
| 170 | +): Promise<ContractTransaction> { |
| 171 | + const senderAddress = await signer.getAddress(); |
| 172 | + const provider = signer.provider ?? getProvider(); |
| 173 | + if (!(await checkSuperTokenUnderlyingAllowance(request, senderAddress, provider, amount))) { |
| 174 | + throw new Error('Supertoken not allowed to wrap this amount of underlying'); |
| 175 | + } |
| 176 | + if (!(await hasEnoughUnderlyingToken(request, senderAddress, provider, amount))) { |
| 177 | + throw new Error('Sender does not have enough underlying token'); |
| 178 | + } |
| 179 | + const wrapOp = await getWrapUnderlyingTokenOp(request, signer.provider ?? getProvider(), amount); |
| 180 | + return wrapOp.exec(signer); |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * Unwrap the supertoken (ERC777) into underlying asset (ERC20) |
| 185 | + * @param request the request that contains currency information |
| 186 | + * @param signer the web3 signer |
| 187 | + * @param amount to unwrap |
| 188 | + */ |
| 189 | +export async function unwrapSuperToken( |
| 190 | + request: ClientTypes.IRequestData, |
| 191 | + signer: Signer, |
| 192 | + amount: BigNumber, |
| 193 | +): Promise<ContractTransaction> { |
| 194 | + const sf = await getSuperFluidFramework(request, signer.provider ?? getProvider()); |
| 195 | + const superToken = await sf.loadSuperToken(request.currencyInfo.value); |
| 196 | + const underlyingToken = await getRequestUnderlyingToken( |
| 197 | + request, |
| 198 | + signer.provider ?? getProvider(), |
| 199 | + ); |
| 200 | + |
| 201 | + if (underlyingToken.address === superToken.address) { |
| 202 | + throw new Error('This is a native super token'); |
| 203 | + } |
| 204 | + |
| 205 | + const userAddress = await signer.getAddress(); |
| 206 | + const userBalance = await superToken.balanceOf({ |
| 207 | + account: userAddress, |
| 208 | + providerOrSigner: signer.provider ?? getProvider(), |
| 209 | + }); |
| 210 | + if (amount.gt(userBalance)) { |
| 211 | + throw new Error('Sender does not have enough supertoken'); |
| 212 | + } |
| 213 | + const downgradeOp = superToken.downgrade({ |
| 214 | + amount: amount.toString(), |
| 215 | + }); |
| 216 | + return downgradeOp.exec(signer); |
| 217 | +} |
0 commit comments