Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit 419d184

Browse files
authored
CORE-1932 rename deposit (#385)
* rename deposit * copy deposit v3 to v4
1 parent ad63ec5 commit 419d184

File tree

9 files changed

+443
-1
lines changed

9 files changed

+443
-1
lines changed
File renamed without changes.
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { Signer } from '@ethersproject/abstract-signer';
2+
import { TransactionResponse } from '@ethersproject/providers';
3+
import { DepositsApi, EncodingApi, UsersApi } from '../../api';
4+
import {
5+
Core,
6+
Core__factory,
7+
IERC721__factory,
8+
Registration__factory,
9+
} from '../../contracts';
10+
import {
11+
getSignableRegistrationOnchain,
12+
isRegisteredOnChainWorkflow,
13+
} from '../registration';
14+
import { ERC721Token } from '../../types';
15+
import { ImmutableXConfiguration } from '../../config';
16+
17+
interface ERC721TokenData {
18+
token_id: string;
19+
token_address: string;
20+
}
21+
22+
async function executeDepositERC721(
23+
signer: Signer,
24+
tokenId: string,
25+
assetType: string,
26+
starkPublicKey: string,
27+
vaultId: number,
28+
contract: Core,
29+
): Promise<TransactionResponse> {
30+
const populatedTransaction = await contract.populateTransaction.depositNft(
31+
starkPublicKey,
32+
assetType,
33+
vaultId,
34+
tokenId,
35+
);
36+
37+
return signer.sendTransaction(populatedTransaction);
38+
}
39+
40+
export async function depositERC721Workflow(
41+
signer: Signer,
42+
deposit: ERC721Token,
43+
depositsApi: DepositsApi,
44+
usersApi: UsersApi,
45+
encodingApi: EncodingApi,
46+
config: ImmutableXConfiguration,
47+
): Promise<TransactionResponse> {
48+
const user = await signer.getAddress();
49+
50+
const data: ERC721TokenData = {
51+
token_address: deposit.tokenAddress,
52+
token_id: deposit.tokenId,
53+
};
54+
55+
const amount = '1';
56+
57+
const getSignableDepositRequest = {
58+
user,
59+
token: {
60+
type: deposit.type,
61+
data,
62+
},
63+
amount: amount.toString(),
64+
};
65+
66+
const signableDepositResult = await depositsApi.getSignableDeposit({
67+
getSignableDepositRequest,
68+
});
69+
70+
// Perform encoding on asset details to get an assetType (required for stark contract request)
71+
const encodingResult = await encodingApi.encodeAsset({
72+
assetType: 'asset',
73+
encodeAssetRequest: {
74+
token: {
75+
type: deposit.type,
76+
data: {
77+
token_address: deposit.tokenAddress,
78+
token_id: deposit.tokenId,
79+
},
80+
},
81+
},
82+
});
83+
84+
const assetType = encodingResult.data.asset_type;
85+
const starkPublicKey = signableDepositResult.data.stark_key;
86+
const vaultId = signableDepositResult.data.vault_id;
87+
88+
const coreContract = Core__factory.connect(
89+
config.ethConfiguration.coreContractAddress,
90+
signer,
91+
);
92+
93+
const registrationContract = Registration__factory.connect(
94+
config.ethConfiguration.registrationContractAddress,
95+
signer,
96+
);
97+
98+
const isRegistered = await isRegisteredOnChainWorkflow(
99+
starkPublicKey,
100+
registrationContract,
101+
);
102+
103+
// Approve whether an amount of token from an account can be spent by a third-party account
104+
const tokenContract = IERC721__factory.connect(deposit.tokenAddress, signer);
105+
const operator = config.ethConfiguration.coreContractAddress;
106+
const isApprovedForAll = await tokenContract.isApprovedForAll(user, operator);
107+
if (!isApprovedForAll) {
108+
await tokenContract.setApprovalForAll(operator, true);
109+
}
110+
111+
if (!isRegistered) {
112+
const signableResult = await getSignableRegistrationOnchain(
113+
user,
114+
starkPublicKey,
115+
usersApi,
116+
);
117+
118+
// Note: proxy registration contract registerAndDepositNft method is not used as it currently fails erc721 transfer ownership check
119+
await coreContract.registerUser(
120+
user,
121+
starkPublicKey,
122+
signableResult.operator_signature,
123+
);
124+
}
125+
126+
return executeDepositERC721(
127+
signer,
128+
deposit.tokenId,
129+
assetType,
130+
starkPublicKey,
131+
vaultId,
132+
coreContract,
133+
);
134+
}

0 commit comments

Comments
 (0)