diff --git a/src/workflows/depositV3/depositERC20.ts b/src/workflows/deposit/depositERC20.ts similarity index 100% rename from src/workflows/depositV3/depositERC20.ts rename to src/workflows/deposit/depositERC20.ts diff --git a/src/workflows/depositV3/depositERC721.ts b/src/workflows/deposit/depositERC721.ts similarity index 100% rename from src/workflows/depositV3/depositERC721.ts rename to src/workflows/deposit/depositERC721.ts diff --git a/src/workflows/depositV3/depositEth.ts b/src/workflows/deposit/depositEth.ts similarity index 100% rename from src/workflows/depositV3/depositEth.ts rename to src/workflows/deposit/depositEth.ts diff --git a/src/workflows/depositV3/index.ts b/src/workflows/deposit/index.ts similarity index 100% rename from src/workflows/depositV3/index.ts rename to src/workflows/deposit/index.ts diff --git a/src/workflows/depositV4/depositERC20.ts b/src/workflows/depositV4/depositERC20.ts deleted file mode 100644 index 1a452fa1..00000000 --- a/src/workflows/depositV4/depositERC20.ts +++ /dev/null @@ -1,168 +0,0 @@ -import { Signer } from '@ethersproject/abstract-signer'; -import { TransactionResponse } from '@ethersproject/providers'; -import { DepositsApi, EncodingApi, TokensApi, UsersApi } from '../../api'; -import { parseUnits } from '@ethersproject/units'; -import { - Core, - Core__factory, - IERC20__factory, - Registration__factory, -} from '../../contracts'; -import { - getSignableRegistrationOnchain, - isRegisteredOnChainWorkflow, -} from '../registration'; -import { ERC20Amount } from '../../types'; -import { BigNumber } from '@ethersproject/bignumber'; -import { ImmutableXConfiguration } from '../../config'; - -interface ERC20TokenData { - decimals: number; - token_address: string; -} - -async function executeDepositERC20( - signer: Signer, - quantizedAmount: BigNumber, - assetType: string, - starkPublicKey: string, - vaultId: number, - contract: Core, -): Promise { - const populatedTransaction = await contract.populateTransaction.depositERC20( - starkPublicKey, - assetType, - vaultId, - quantizedAmount, - ); - - return signer.sendTransaction(populatedTransaction); -} - -async function executeRegisterAndDepositERC20( - signer: Signer, - quantizedAmount: BigNumber, - assetType: string, - starkPublicKey: string, - vaultId: number, - contract: Core, - usersApi: UsersApi, -): Promise { - const etherKey = await signer.getAddress(); - - const signableResult = await getSignableRegistrationOnchain( - etherKey, - starkPublicKey, - usersApi, - ); - - const populatedTransaction = - await contract.populateTransaction.registerAndDepositERC20( - etherKey, - starkPublicKey, - signableResult.operator_signature, - assetType, - vaultId, - quantizedAmount, - ); - - return signer.sendTransaction(populatedTransaction); -} - -export async function depositERC20Workflow( - signer: Signer, - deposit: ERC20Amount, - depositsApi: DepositsApi, - usersApi: UsersApi, - tokensApi: TokensApi, - encodingApi: EncodingApi, - config: ImmutableXConfiguration, -): Promise { - const user = await signer.getAddress(); - - // Get decimals for this specific ERC20 - const token = await tokensApi.getToken({ address: deposit.tokenAddress }); - const decimals = parseInt(token.data.decimals); - - const data: ERC20TokenData = { - decimals, - token_address: deposit.tokenAddress, - }; - - const amount = parseUnits(deposit.amount, 0); // 0 to always use undecimalized value - - // Approve whether an amount of token from an account can be spent by a third-party account - const tokenContract = IERC20__factory.connect(deposit.tokenAddress, signer); - const approveTransaction = await tokenContract.populateTransaction.approve( - config.ethConfiguration.coreContractAddress, - amount, - ); - await signer.sendTransaction(approveTransaction); - - const getSignableDepositRequest = { - user, - token: { - type: deposit.type, - data, - }, - amount: amount.toString(), - }; - - const signableDepositResult = await depositsApi.getSignableDeposit({ - getSignableDepositRequest, - }); - - // Perform encoding on asset details to get an assetType (required for stark contract request) - const encodingResult = await encodingApi.encodeAsset({ - assetType: 'asset', - encodeAssetRequest: { - token: { - type: deposit.type, - data: { - token_address: deposit.tokenAddress, - }, - }, - }, - }); - - const assetType = encodingResult.data.asset_type; - const starkPublicKey = signableDepositResult.data.stark_key; - const vaultId = signableDepositResult.data.vault_id; - const quantizedAmount = BigNumber.from(signableDepositResult.data.amount); - - const coreContract = Core__factory.connect( - config.ethConfiguration.coreContractAddress, - signer, - ); - - const registrationContract = Registration__factory.connect( - config.ethConfiguration.registrationContractAddress, - signer, - ); - - const isRegistered = await isRegisteredOnChainWorkflow( - starkPublicKey, - registrationContract, - ); - - if (!isRegistered) { - return executeRegisterAndDepositERC20( - signer, - quantizedAmount, - assetType, - starkPublicKey, - vaultId, - coreContract, - usersApi, - ); - } else { - return executeDepositERC20( - signer, - quantizedAmount, - assetType, - starkPublicKey, - vaultId, - coreContract, - ); - } -} diff --git a/src/workflows/depositV4/depositERC721.ts b/src/workflows/depositV4/depositERC721.ts deleted file mode 100644 index 64e8209f..00000000 --- a/src/workflows/depositV4/depositERC721.ts +++ /dev/null @@ -1,134 +0,0 @@ -import { Signer } from '@ethersproject/abstract-signer'; -import { TransactionResponse } from '@ethersproject/providers'; -import { DepositsApi, EncodingApi, UsersApi } from '../../api'; -import { - Core, - Core__factory, - IERC721__factory, - Registration__factory, -} from '../../contracts'; -import { - getSignableRegistrationOnchain, - isRegisteredOnChainWorkflow, -} from '../registration'; -import { ERC721Token } from '../../types'; -import { ImmutableXConfiguration } from '../../config'; - -interface ERC721TokenData { - token_id: string; - token_address: string; -} - -async function executeDepositERC721( - signer: Signer, - tokenId: string, - assetType: string, - starkPublicKey: string, - vaultId: number, - contract: Core, -): Promise { - const populatedTransaction = await contract.populateTransaction.depositNft( - starkPublicKey, - assetType, - vaultId, - tokenId, - ); - - return signer.sendTransaction(populatedTransaction); -} - -export async function depositERC721Workflow( - signer: Signer, - deposit: ERC721Token, - depositsApi: DepositsApi, - usersApi: UsersApi, - encodingApi: EncodingApi, - config: ImmutableXConfiguration, -): Promise { - const user = await signer.getAddress(); - - const data: ERC721TokenData = { - token_address: deposit.tokenAddress, - token_id: deposit.tokenId, - }; - - const amount = '1'; - - const getSignableDepositRequest = { - user, - token: { - type: deposit.type, - data, - }, - amount: amount.toString(), - }; - - const signableDepositResult = await depositsApi.getSignableDeposit({ - getSignableDepositRequest, - }); - - // Perform encoding on asset details to get an assetType (required for stark contract request) - const encodingResult = await encodingApi.encodeAsset({ - assetType: 'asset', - encodeAssetRequest: { - token: { - type: deposit.type, - data: { - token_address: deposit.tokenAddress, - token_id: deposit.tokenId, - }, - }, - }, - }); - - const assetType = encodingResult.data.asset_type; - const starkPublicKey = signableDepositResult.data.stark_key; - const vaultId = signableDepositResult.data.vault_id; - - const coreContract = Core__factory.connect( - config.ethConfiguration.coreContractAddress, - signer, - ); - - const registrationContract = Registration__factory.connect( - config.ethConfiguration.registrationContractAddress, - signer, - ); - - const isRegistered = await isRegisteredOnChainWorkflow( - starkPublicKey, - registrationContract, - ); - - // Approve whether an amount of token from an account can be spent by a third-party account - const tokenContract = IERC721__factory.connect(deposit.tokenAddress, signer); - const operator = config.ethConfiguration.coreContractAddress; - const isApprovedForAll = await tokenContract.isApprovedForAll(user, operator); - if (!isApprovedForAll) { - await tokenContract.setApprovalForAll(operator, true); - } - - if (!isRegistered) { - const signableResult = await getSignableRegistrationOnchain( - user, - starkPublicKey, - usersApi, - ); - - // Note: proxy registration contract registerAndDepositNft method is not used as it currently fails erc721 transfer ownership check - await coreContract.registerUser( - user, - starkPublicKey, - signableResult.operator_signature, - ); - } - - return executeDepositERC721( - signer, - deposit.tokenId, - assetType, - starkPublicKey, - vaultId, - coreContract, - ); -} diff --git a/src/workflows/depositV4/depositEth.ts b/src/workflows/depositV4/depositEth.ts deleted file mode 100644 index b04502c6..00000000 --- a/src/workflows/depositV4/depositEth.ts +++ /dev/null @@ -1,137 +0,0 @@ -import { Signer } from '@ethersproject/abstract-signer'; -import { TransactionResponse } from '@ethersproject/providers'; -import { DepositsApi, EncodingApi, UsersApi } from '../../api'; -import { parseUnits } from '@ethersproject/units'; -import { Core, Core__factory, Registration__factory } from '../../contracts'; -import { - getSignableRegistrationOnchain, - isRegisteredOnChainWorkflow, -} from '../registration'; -import { ETHAmount } from '../../types'; -import { BigNumber } from '@ethersproject/bignumber'; -import { ImmutableXConfiguration } from '../../config'; - -interface ETHTokenData { - decimals: number; -} - -async function executeRegisterAndDepositEth( - signer: Signer, - amount: BigNumber, - assetType: string, - starkPublicKey: string, - vaultId: number, - contract: Core, - usersApi: UsersApi, -): Promise { - const etherKey = await signer.getAddress(); - - const signableResult = await getSignableRegistrationOnchain( - etherKey, - starkPublicKey, - usersApi, - ); - - const populatedTransaction = - await contract.populateTransaction.registerAndDepositEth( - etherKey, - starkPublicKey, - signableResult.operator_signature, - assetType, - vaultId, - ); - - return signer.sendTransaction({ ...populatedTransaction, value: amount }); -} - -async function executeDepositEth( - signer: Signer, - amount: BigNumber, - assetType: string, - starkPublicKey: string, - vaultId: number, - contract: Core, -): Promise { - const populatedTransaction = await contract.populateTransaction[ - 'deposit(uint256,uint256,uint256)' - ](starkPublicKey, assetType, vaultId); - - return signer.sendTransaction({ ...populatedTransaction, value: amount }); -} - -export async function depositEthWorkflow( - signer: Signer, - deposit: ETHAmount, - depositsApi: DepositsApi, - usersApi: UsersApi, - encodingApi: EncodingApi, - config: ImmutableXConfiguration, -): Promise { - const user = await signer.getAddress(); - const data: ETHTokenData = { - decimals: 18, - }; - const amount = parseUnits(deposit.amount, 'wei'); - - const getSignableDepositRequest = { - user, - token: { - type: deposit.type, - data, - }, - amount: amount.toString(), - }; - - const signableDepositResult = await depositsApi.getSignableDeposit({ - getSignableDepositRequest, - }); - - const encodingResult = await encodingApi.encodeAsset({ - assetType: 'asset', - encodeAssetRequest: { - token: { - type: deposit.type, - }, - }, - }); - - const assetType = encodingResult.data.asset_type; - const starkPublicKey = signableDepositResult.data.stark_key; - const vaultId = signableDepositResult.data.vault_id; - - const coreContract = Core__factory.connect( - config.ethConfiguration.coreContractAddress, - signer, - ); - - const registrationContract = Registration__factory.connect( - config.ethConfiguration.registrationContractAddress, - signer, - ); - - const isRegistered = await isRegisteredOnChainWorkflow( - starkPublicKey, - registrationContract, - ); - - if (!isRegistered) { - return executeRegisterAndDepositEth( - signer, - amount, - assetType, - starkPublicKey, - vaultId, - coreContract, - usersApi, - ); - } else { - return executeDepositEth( - signer, - amount, - assetType, - starkPublicKey, - vaultId, - coreContract, - ); - } -} diff --git a/src/workflows/depositV4/index.ts b/src/workflows/depositV4/index.ts deleted file mode 100644 index 34c42236..00000000 --- a/src/workflows/depositV4/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './depositEth'; -export * from './depositERC20'; -export * from './depositERC721'; diff --git a/src/workflows/workflows.ts b/src/workflows/workflows.ts index 71f3e780..a7d2ebac 100644 --- a/src/workflows/workflows.ts +++ b/src/workflows/workflows.ts @@ -50,7 +50,7 @@ import { depositERC20Workflow, depositERC721Workflow, depositEthWorkflow, -} from './depositV3'; +} from './deposit'; import { completeERC20WithdrawalWorkflow, completeERC721WithdrawalWorkflow,