|
| 1 | +import { Signer } from '@ethersproject/abstract-signer'; |
| 2 | +import { TransactionResponse } from '@ethersproject/providers'; |
| 3 | +import { DepositsApi, EncodingApi, TokensApi, UsersApi } from '../../api'; |
| 4 | +import { parseUnits } from '@ethersproject/units'; |
| 5 | +import { |
| 6 | + Core, |
| 7 | + Core__factory, |
| 8 | + IERC20__factory, |
| 9 | + Registration__factory, |
| 10 | +} from '../../contracts'; |
| 11 | +import { |
| 12 | + getSignableRegistrationOnchain, |
| 13 | + isRegisteredOnChainWorkflow, |
| 14 | +} from '../registration'; |
| 15 | +import { ERC20Amount } from '../../types'; |
| 16 | +import { BigNumber } from '@ethersproject/bignumber'; |
| 17 | +import { ImmutableXConfiguration } from '../../config'; |
| 18 | + |
| 19 | +interface ERC20TokenData { |
| 20 | + decimals: number; |
| 21 | + token_address: string; |
| 22 | +} |
| 23 | + |
| 24 | +async function executeDepositERC20( |
| 25 | + signer: Signer, |
| 26 | + quantizedAmount: BigNumber, |
| 27 | + assetType: string, |
| 28 | + starkPublicKey: string, |
| 29 | + vaultId: number, |
| 30 | + contract: Core, |
| 31 | +): Promise<TransactionResponse> { |
| 32 | + const populatedTransaction = await contract.populateTransaction.depositERC20( |
| 33 | + starkPublicKey, |
| 34 | + assetType, |
| 35 | + vaultId, |
| 36 | + quantizedAmount, |
| 37 | + ); |
| 38 | + |
| 39 | + return signer.sendTransaction(populatedTransaction); |
| 40 | +} |
| 41 | + |
| 42 | +async function executeRegisterAndDepositERC20( |
| 43 | + signer: Signer, |
| 44 | + quantizedAmount: BigNumber, |
| 45 | + assetType: string, |
| 46 | + starkPublicKey: string, |
| 47 | + vaultId: number, |
| 48 | + contract: Core, |
| 49 | + usersApi: UsersApi, |
| 50 | +): Promise<TransactionResponse> { |
| 51 | + const etherKey = await signer.getAddress(); |
| 52 | + |
| 53 | + const signableResult = await getSignableRegistrationOnchain( |
| 54 | + etherKey, |
| 55 | + starkPublicKey, |
| 56 | + usersApi, |
| 57 | + ); |
| 58 | + |
| 59 | + const populatedTransaction = |
| 60 | + await contract.populateTransaction.registerAndDepositERC20( |
| 61 | + etherKey, |
| 62 | + starkPublicKey, |
| 63 | + signableResult.operator_signature, |
| 64 | + assetType, |
| 65 | + vaultId, |
| 66 | + quantizedAmount, |
| 67 | + ); |
| 68 | + |
| 69 | + return signer.sendTransaction(populatedTransaction); |
| 70 | +} |
| 71 | + |
| 72 | +export async function depositERC20Workflow( |
| 73 | + signer: Signer, |
| 74 | + deposit: ERC20Amount, |
| 75 | + depositsApi: DepositsApi, |
| 76 | + usersApi: UsersApi, |
| 77 | + tokensApi: TokensApi, |
| 78 | + encodingApi: EncodingApi, |
| 79 | + config: ImmutableXConfiguration, |
| 80 | +): Promise<TransactionResponse> { |
| 81 | + const user = await signer.getAddress(); |
| 82 | + |
| 83 | + // Get decimals for this specific ERC20 |
| 84 | + const token = await tokensApi.getToken({ address: deposit.tokenAddress }); |
| 85 | + const decimals = parseInt(token.data.decimals); |
| 86 | + |
| 87 | + const data: ERC20TokenData = { |
| 88 | + decimals, |
| 89 | + token_address: deposit.tokenAddress, |
| 90 | + }; |
| 91 | + |
| 92 | + const amount = parseUnits(deposit.amount, 0); // 0 to always use undecimalized value |
| 93 | + |
| 94 | + // Approve whether an amount of token from an account can be spent by a third-party account |
| 95 | + const tokenContract = IERC20__factory.connect(deposit.tokenAddress, signer); |
| 96 | + const approveTransaction = await tokenContract.populateTransaction.approve( |
| 97 | + config.ethConfiguration.coreContractAddress, |
| 98 | + amount, |
| 99 | + ); |
| 100 | + await signer.sendTransaction(approveTransaction); |
| 101 | + |
| 102 | + const getSignableDepositRequest = { |
| 103 | + user, |
| 104 | + token: { |
| 105 | + type: deposit.type, |
| 106 | + data, |
| 107 | + }, |
| 108 | + amount: amount.toString(), |
| 109 | + }; |
| 110 | + |
| 111 | + const signableDepositResult = await depositsApi.getSignableDeposit({ |
| 112 | + getSignableDepositRequest, |
| 113 | + }); |
| 114 | + |
| 115 | + // Perform encoding on asset details to get an assetType (required for stark contract request) |
| 116 | + const encodingResult = await encodingApi.encodeAsset({ |
| 117 | + assetType: 'asset', |
| 118 | + encodeAssetRequest: { |
| 119 | + token: { |
| 120 | + type: deposit.type, |
| 121 | + data: { |
| 122 | + token_address: deposit.tokenAddress, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + const assetType = encodingResult.data.asset_type; |
| 129 | + const starkPublicKey = signableDepositResult.data.stark_key; |
| 130 | + const vaultId = signableDepositResult.data.vault_id; |
| 131 | + const quantizedAmount = BigNumber.from(signableDepositResult.data.amount); |
| 132 | + |
| 133 | + const coreContract = Core__factory.connect( |
| 134 | + config.ethConfiguration.coreContractAddress, |
| 135 | + signer, |
| 136 | + ); |
| 137 | + |
| 138 | + const registrationContract = Registration__factory.connect( |
| 139 | + config.ethConfiguration.registrationContractAddress, |
| 140 | + signer, |
| 141 | + ); |
| 142 | + |
| 143 | + const isRegistered = await isRegisteredOnChainWorkflow( |
| 144 | + starkPublicKey, |
| 145 | + registrationContract, |
| 146 | + ); |
| 147 | + |
| 148 | + if (!isRegistered) { |
| 149 | + return executeRegisterAndDepositERC20( |
| 150 | + signer, |
| 151 | + quantizedAmount, |
| 152 | + assetType, |
| 153 | + starkPublicKey, |
| 154 | + vaultId, |
| 155 | + coreContract, |
| 156 | + usersApi, |
| 157 | + ); |
| 158 | + } else { |
| 159 | + return executeDepositERC20( |
| 160 | + signer, |
| 161 | + quantizedAmount, |
| 162 | + assetType, |
| 163 | + starkPublicKey, |
| 164 | + vaultId, |
| 165 | + coreContract, |
| 166 | + ); |
| 167 | + } |
| 168 | +} |
0 commit comments