Skip to content

Commit

Permalink
DX-2821/update_deposits (#1836)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihirota authored May 29, 2024
1 parent 8da642d commit 622dc96
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('Deposit ERC20', () => {
let getSignableDepositMock: jest.Mock;
let encodeAssetMock: jest.Mock;
let getTokenMock: jest.Mock;
let getSignableRegistrationMock: jest.Mock;

const signableDepositRequest = {
tokenAddress: 'kljh5kl3j4biu3b59385',
Expand All @@ -41,7 +40,6 @@ describe('Deposit ERC20', () => {
const getTokenResponse = {
decimals: 18,
};
const getSignableRegistrationResponse = {};

beforeEach(() => {
jest.restoreAllMocks();
Expand All @@ -67,20 +65,13 @@ describe('Deposit ERC20', () => {
getToken: getTokenMock,
});

getSignableRegistrationMock = jest.fn().mockResolvedValue({
data: getSignableRegistrationResponse,
});
(imx.UsersApi as jest.Mock).mockReturnValue({
getSignableRegistration: getSignableRegistrationMock,
});

(Contracts.IERC20.connect as jest.Mock).mockReturnValue({
populateTransaction: {
approve: async () => 'test',
},
});

(Contracts.Core.connect as jest.Mock).mockReturnValue({
(Contracts.CoreV4.connect as jest.Mock).mockReturnValue({
populateTransaction: {
depositERC20: async () => 'test',
registerAndDepositERC20: async () => 'test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import {
import { TransactionResponse } from '@ethersproject/providers';
import { parseUnits } from '@ethersproject/units';
import { BigNumber } from '@ethersproject/bignumber';
import {
getSignableRegistrationOnchain,
isRegisteredOnChain,
} from '../registration';
import { validateChain } from '../helpers';
import { Signers } from '../types';
import { ProviderConfiguration } from '../../config';
Expand All @@ -27,39 +23,6 @@ type DepositERC20Params = {
deposit: ERC20Amount;
config: ProviderConfiguration;
};

async function executeRegisterAndDepositERC20(
ethSigner: EthSigner,
quantizedAmount: BigNumber,
assetType: string,
starkPublicKey: string,
vaultId: number,
config: EthConfiguration,
usersApi: imx.UsersApi,
): Promise<TransactionResponse> {
const etherKey = await ethSigner.getAddress();
const coreContract = Contracts.Core.connect(
config.coreContractAddress,
ethSigner,
);
const signableResult = await getSignableRegistrationOnchain(
etherKey,
starkPublicKey,
usersApi,
);

const populatedTransaction = await coreContract.populateTransaction.registerAndDepositERC20(
etherKey,
starkPublicKey,
signableResult.operator_signature,
assetType,
vaultId,
quantizedAmount,
);

return ethSigner.sendTransaction(populatedTransaction);
}

async function executeDepositERC20(
ethSigner: EthSigner,
quantizedAmount: BigNumber,
Expand All @@ -68,7 +31,7 @@ async function executeDepositERC20(
vaultId: number,
config: EthConfiguration,
): Promise<TransactionResponse> {
const coreContract = Contracts.Core.connect(
const coreContract = Contracts.CoreV4.connect(
config.coreContractAddress,
ethSigner,
);
Expand All @@ -95,7 +58,6 @@ export async function depositERC20({
const tokensApi = new imx.TokensApi(apiConfiguration);
const depositsApi = new imx.DepositsApi(apiConfiguration);
const encodingApi = new imx.EncodingApi(apiConfiguration);
const usersApi = new imx.UsersApi(apiConfiguration);

// Get decimals for this specific ERC20
const token = await tokensApi.getToken({ address: deposit.tokenAddress });
Expand Down Expand Up @@ -152,23 +114,6 @@ export async function depositERC20({
const vaultId = signableDepositResult.data.vault_id;
const quantizedAmount = BigNumber.from(signableDepositResult.data.amount);

const isRegistered = await isRegisteredOnChain(
starkPublicKey,
ethSigner,
config,
);

if (!isRegistered) {
return executeRegisterAndDepositERC20(
ethSigner,
quantizedAmount,
assetType,
starkPublicKey,
vaultId,
ethConfiguration,
usersApi,
);
}
return executeDepositERC20(
ethSigner,
quantizedAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('Deposit ERC721', () => {
describe('depositERC721()', () => {
let getSignableDepositMock: jest.Mock;
let encodeAssetMock: jest.Mock;
let getSignableRegistrationMock: jest.Mock;

const signableDepositRequest = {
tokenAddress: 'kljh5kl3j4biu3b59385',
Expand All @@ -40,8 +39,6 @@ describe('Deposit ERC721', () => {
amount: '1',
};

const getSignableRegistrationResponse = {};

beforeEach(() => {
jest.restoreAllMocks();

Expand All @@ -59,18 +56,11 @@ describe('Deposit ERC721', () => {
encodeAsset: encodeAssetMock,
});

getSignableRegistrationMock = jest.fn().mockResolvedValue({
data: getSignableRegistrationResponse,
});
(imx.UsersApi as jest.Mock).mockReturnValue({
getSignableRegistration: getSignableRegistrationMock,
});

(Contracts.IERC721.connect as jest.Mock).mockReturnValue({
isApprovedForAll: async () => true,
});

(Contracts.Core.connect as jest.Mock).mockReturnValue({
(Contracts.CoreV4.connect as jest.Mock).mockReturnValue({
registerUser: async () => 'test',
populateTransaction: {
approve: async () => 'test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import {
ImmutableXConfiguration,
} from '@imtbl/x-client';
import { TransactionResponse } from '@ethersproject/providers';
import {
getSignableRegistrationOnchain,
isRegisteredOnChain,
} from '../registration';
import { validateChain } from '../helpers';
import { Signers } from '../types';
import { ProviderConfiguration } from '../../config';
Expand All @@ -35,7 +31,7 @@ async function executeDepositERC721(
vaultId: number,
config: ImmutableXConfiguration,
): Promise<TransactionResponse> {
const coreContract = Contracts.Core.connect(
const coreContract = Contracts.CoreV4.connect(
config.ethConfiguration.coreContractAddress,
ethSigner,
);
Expand All @@ -60,7 +56,6 @@ export async function depositERC721({
const { immutableXConfig } = config;
const depositsApi = new imx.DepositsApi(immutableXConfig.apiConfiguration);
const encodingApi = new imx.EncodingApi(immutableXConfig.apiConfiguration);
const usersApi = new imx.UsersApi(immutableXConfig.apiConfiguration);

const data: ERC721TokenData = {
token_address: deposit.tokenAddress,
Expand Down Expand Up @@ -100,12 +95,6 @@ export async function depositERC721({
const starkPublicKey = signableDepositResult.data.stark_key;
const vaultId = signableDepositResult.data.vault_id;

const isRegistered = await isRegisteredOnChain(
starkPublicKey,
ethSigner,
config,
);

// Approve whether an amount of token from an account can be spent by a third-party account
const tokenContract = Contracts.IERC721.connect(
deposit.tokenAddress,
Expand All @@ -117,26 +106,6 @@ export async function depositERC721({
await tokenContract.setApprovalForAll(operator, true);
}

if (!isRegistered) {
const signableResult = await getSignableRegistrationOnchain(
user,
starkPublicKey,
usersApi,
);

const coreContract = Contracts.Core.connect(
immutableXConfig.ethConfiguration.coreContractAddress,
ethSigner,
);
// 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(
ethSigner,
deposit.tokenId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('Deposit ETH', () => {
describe('depositETH()', () => {
let getSignableDepositMock: jest.Mock;
let encodeAssetMock: jest.Mock;
let getSignableRegistrationMock: jest.Mock;

const signableDepositRequest = {
tokenAddress: 'kljh5kl3j4biu3b59385',
Expand All @@ -39,8 +38,6 @@ describe('Deposit ETH', () => {
amount: '1000000000000000000',
};

const getSignableRegistrationResponse = {};

beforeEach(() => {
jest.restoreAllMocks();

Expand All @@ -58,14 +55,7 @@ describe('Deposit ETH', () => {
encodeAsset: encodeAssetMock,
});

getSignableRegistrationMock = jest.fn().mockResolvedValue({
data: getSignableRegistrationResponse,
});
(imx.UsersApi as jest.Mock).mockReturnValue({
getSignableRegistration: getSignableRegistrationMock,
});

(Contracts.Core.connect as jest.Mock).mockReturnValue({
(Contracts.CoreV4.connect as jest.Mock).mockReturnValue({
populateTransaction: {
registerAndDepositEth: async () => 'test',
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import {
import { parseUnits } from '@ethersproject/units';
import { BigNumber } from '@ethersproject/bignumber';
import { TransactionResponse } from '@ethersproject/providers';
import {
getSignableRegistrationOnchain,
isRegisteredOnChain,
} from '../registration';
import { validateChain } from '../helpers';
import { Signers } from '../types';
import { ProviderConfiguration } from '../../config';
Expand All @@ -26,38 +22,6 @@ type DepositEthParams = {
config: ProviderConfiguration;
};

async function executeRegisterAndDepositEth(
ethSigner: EthSigner,
amount: BigNumber,
assetType: string,
starkPublicKey: string,
vaultId: number,
config: ImmutableXConfiguration,
usersApi: imx.UsersApi,
): Promise<TransactionResponse> {
const etherKey = await ethSigner.getAddress();
const coreContract = Contracts.Core.connect(
config.ethConfiguration.coreContractAddress,
ethSigner,
);

const signableResult = await getSignableRegistrationOnchain(
etherKey,
starkPublicKey,
usersApi,
);

const populatedTransaction = await coreContract.populateTransaction.registerAndDepositEth(
etherKey,
starkPublicKey,
signableResult.operator_signature,
assetType,
vaultId,
);

return ethSigner.sendTransaction({ ...populatedTransaction, value: amount });
}

async function executeDepositEth(
ethSigner: EthSigner,
amount: BigNumber,
Expand All @@ -66,7 +30,7 @@ async function executeDepositEth(
vaultId: number,
config: ImmutableXConfiguration,
): Promise<TransactionResponse> {
const coreContract = Contracts.Core.connect(
const coreContract = Contracts.CoreV4.connect(
config.ethConfiguration.coreContractAddress,
ethSigner,
);
Expand All @@ -93,7 +57,6 @@ export async function depositEth({
const imxConfig = config.immutableXConfig;
const depositsApi = new imx.DepositsApi(imxConfig.apiConfiguration);
const encodingApi = new imx.EncodingApi(imxConfig.apiConfiguration);
const usersApi = new imx.UsersApi(imxConfig.apiConfiguration);

const getSignableDepositRequest = {
user,
Expand Down Expand Up @@ -121,23 +84,6 @@ export async function depositEth({
const starkPublicKey = signableDepositResult.data.stark_key;
const vaultId = signableDepositResult.data.vault_id;

const isRegistered = await isRegisteredOnChain(
starkPublicKey,
ethSigner,
config,
);

if (!isRegistered) {
return executeRegisterAndDepositEth(
ethSigner,
amount,
assetType,
starkPublicKey,
vaultId,
imxConfig,
usersApi,
);
}
return executeDepositEth(
ethSigner,
amount,
Expand Down

0 comments on commit 622dc96

Please sign in to comment.