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

Commit

Permalink
CORE-1942 Add prepare withdrawal v2 workflow (#402)
Browse files Browse the repository at this point in the history
* update OpenAPI generated API client code

* add prepare withdrawal v2 workflow

* fix typo

* update error message

* rename vars

* update

* update

* fix bad refs
  • Loading branch information
kaihirota authored Feb 26, 2024
1 parent 03333d6 commit 4fd4f5d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"create-nft-withdrawal": "ts-node -r dotenv/config createNftWithdrawal.ts",
"complete-nft-withdrawal": "ts-node -r dotenv/config completeNftWithdrawal.ts",
"complete-erc20-withdrawal": "ts-node -r dotenv/config completeErc20Withdrawal.ts",
"create-erc20-withdrawal": "ts-node -r dotenv/config completeErc20Withdrawal.ts",
"create-erc20-withdrawal": "ts-node -r dotenv/config createErc20Withdrawal.ts",
"list-collections": "ts-node -r dotenv/config listCollections.ts",
"list-collection-filters": "ts-node -r dotenv/config listCollectionFilters.ts",
"update-collection": "ts-node -r dotenv/config updateCollection.ts"
Expand Down
16 changes: 8 additions & 8 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -14003,7 +14003,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "../../../../common/openapi/errors/api_errors_v2.yaml#/definitions/APIError400"
"$ref": "#/definitions/APIError"
}
}
},
Expand All @@ -14013,7 +14013,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "../../../../common/openapi/errors/api_errors_v2.yaml#/definitions/APIError403"
"$ref": "#/definitions/APIError"
}
}
},
Expand All @@ -14023,7 +14023,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "../../../../common/openapi/errors/api_errors_v2.yaml#/definitions/APIError500"
"$ref": "#/definitions/APIError"
}
}
},
Expand All @@ -14033,7 +14033,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "../../../../common/openapi/errors/api_errors_v2.yaml#/definitions/APIError404"
"$ref": "#/definitions/APIError"
}
}
},
Expand All @@ -14043,7 +14043,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "../../../../common/openapi/errors/api_errors_v2.yaml#/definitions/APIError501"
"$ref": "#/definitions/APIError"
}
}
},
Expand All @@ -14053,7 +14053,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "../../../../common/openapi/errors/api_errors_v2.yaml#/definitions/APIError429"
"$ref": "#/definitions/APIError"
}
}
},
Expand All @@ -14063,7 +14063,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "../../../../common/openapi/errors/api_errors_v2.yaml#/definitions/APIError401"
"$ref": "#/definitions/APIError"
}
}
},
Expand All @@ -14073,7 +14073,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "../../../../common/openapi/errors/api_errors_v2.yaml#/definitions/APIError422"
"$ref": "#/definitions/APIError"
}
}
},
Expand Down
54 changes: 54 additions & 0 deletions src/workflows/withdrawal/prepareWithdrawal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,57 @@ export async function prepareWithdrawalWorkflow(

return prepareWithdrawalResponse.data;
}

export async function prepareWithdrawalV2Workflow(
params: PrepareWithdrawalWorkflowParams,
): Promise<CreateWithdrawalResponse> {
const { ethSigner, starkSigner, withdrawalsApi } = params;
const withdrawalAmount = params.type === 'ERC721' ? '1' : params.amount;
const signableWithdrawalResult = await withdrawalsApi.getSignableWithdrawalV2(
{
getSignableWithdrawalRequest: {
user: await ethSigner.getAddress(),
token: convertToSignableToken(params),
amount: withdrawalAmount,
},
},
);

const { signable_message: signableMessage, payload_hash: payloadHash } =
signableWithdrawalResult.data;

const starkSignature = await starkSigner.signMessage(payloadHash);

const { ethAddress, ethSignature } = await signMessage(
signableMessage,
ethSigner,
);

const prepareWithdrawalResponse = await withdrawalsApi.createWithdrawalV2({
createWithdrawalRequestV2: {
sender_stark_key: assertIsDefined(
signableWithdrawalResult.data.sender_stark_key,
),
sender_vault_id: assertIsDefined(
signableWithdrawalResult.data.sender_vault_id,
),
receiver_stark_key: assertIsDefined(
signableWithdrawalResult.data.receiver_stark_key,
),
receiver_vault_id: assertIsDefined(
signableWithdrawalResult.data.receiver_vault_id,
),
amount: withdrawalAmount,
asset_id: assertIsDefined(signableWithdrawalResult.data.asset_id),
expiration_timestamp: assertIsDefined(
signableWithdrawalResult.data.expiration_timestamp,
),
nonce: assertIsDefined(signableWithdrawalResult.data.nonce),
stark_signature: starkSignature,
},
xImxEthAddress: ethAddress,
xImxEthSignature: ethSignature,
});

return prepareWithdrawalResponse.data;
}
36 changes: 31 additions & 5 deletions src/workflows/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
completeERC20WithdrawalWorkflow,
completeERC721WithdrawalWorkflow,
completeEthWithdrawalWorkflow,
prepareWithdrawalV2Workflow,
prepareWithdrawalWorkflow,
} from './withdrawal';
import { cancelOrderWorkflow, createOrderWorkflow } from './orders';
Expand Down Expand Up @@ -254,11 +255,36 @@ export class Workflows {
) {
await this.validateChain(walletConnection.ethSigner);

return prepareWithdrawalWorkflow({
...walletConnection,
...request,
withdrawalsApi: this.withdrawalsApi,
});
const starkExContractInfo = await this.getStarkExContractVersion();
const majorContractVersion = await this.parseMajorContractVersion(
starkExContractInfo.data.version,
);

if (majorContractVersion === 3) {
return prepareWithdrawalWorkflow({
...walletConnection,
...request,
withdrawalsApi: this.withdrawalsApi,
});
}

if (majorContractVersion >= 4) {
return prepareWithdrawalV2Workflow({
...walletConnection,
...request,
withdrawalsApi: this.withdrawalsApi,
});
}

throw new Error(
`Invalid StarkEx contract version (${majorContractVersion}). Please try again later.`,
);
}

private async parseMajorContractVersion(
contractVersion: string,
): Promise<number> {
return parseInt(contractVersion.charAt(0));
}

public completeWithdrawal(
Expand Down

0 comments on commit 4fd4f5d

Please sign in to comment.