From ae8de4db1024819d6ca987596a396ad87998b7f6 Mon Sep 17 00:00:00 2001 From: Frank Li Date: Mon, 12 Feb 2024 16:40:08 +1100 Subject: [PATCH] TD1237 Accept primary sale workflow (#418) init Signed-off-by: Frank Li --- CHANGELOG.md | 7 +++++ src/ImmutableX.ts | 15 +++++++++++ src/workflows/primarySales.ts | 51 ++++++++++++++++++++++++++++++++++- src/workflows/workflows.ts | 13 ++++++++- 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8231e5e..a9ee4f27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [2.6.0] - 2024-02-14 + +### Added + +- [Experimental] AcceptPrimarySale interface + ## [2.5.1] - 2024-02-07 ### Added diff --git a/src/ImmutableX.ts b/src/ImmutableX.ts index fb3e891a..bfc2e218 100644 --- a/src/ImmutableX.ts +++ b/src/ImmutableX.ts @@ -970,4 +970,19 @@ export class ImmutableX { throw formatError(err); }); } + + /** + * Accept a PrimarySale + * @param ethSigner - eth signer matching the 'studio_ether_key' of the primary sale + * @param primarySaleId - id of the primary sale accepting + * @returns a promise that resolves with the created Trade + * @throws {@link index.IMXError} + */ + public acceptPrimarySale(ethSigner: EthSigner, primarySaleId: number) { + return this.workflows + .acceptPrimarySale(ethSigner, primarySaleId) + .catch(err => { + throw formatError(err); + }); + } } diff --git a/src/workflows/primarySales.ts b/src/workflows/primarySales.ts index c16505bd..f835050f 100644 --- a/src/workflows/primarySales.ts +++ b/src/workflows/primarySales.ts @@ -1,4 +1,9 @@ import { + AcceptPrimarySaleBadRequestBody, + AcceptPrimarySaleForbiddenBody, + AcceptPrimarySaleNotFoundBody, + AcceptPrimarySaleOKBody, + AcceptPrimarySaleUnauthorizedBody, CreatePrimarySaleBadRequestBody, CreatePrimarySaleCreatedBody, CreatePrimarySaleForbiddenBody, @@ -8,7 +13,7 @@ import { PrimarySalesApiCreatePrimarySaleRequest, PrimarySalesApiSignableCreatePrimarySaleRequest, } from '../api'; -import { WalletConnection } from '../types'; +import { EthSigner, WalletConnection } from '../types'; import { signRaw } from '../utils'; type CreatePrimarySaleWorkflowParams = WalletConnection & { @@ -16,6 +21,12 @@ type CreatePrimarySaleWorkflowParams = WalletConnection & { primarySalesApi: PrimarySalesApi; }; +type AcceptPrimarySaleWorkflowParams = { + ethSigner: EthSigner; + primarySaleId: number; + primarySalesApi: PrimarySalesApi; +}; + type CreatePrimarySaleResponse = | CreatePrimarySaleBadRequestBody | CreatePrimarySaleCreatedBody @@ -23,6 +34,13 @@ type CreatePrimarySaleResponse = | CreatePrimarySaleUnauthorizedBody | CreatePrimarySaleNotFoundBody; +type AcceptPrimarySaleResponse = + | AcceptPrimarySaleOKBody + | AcceptPrimarySaleBadRequestBody + | AcceptPrimarySaleForbiddenBody + | AcceptPrimarySaleNotFoundBody + | AcceptPrimarySaleUnauthorizedBody; + export async function CreatePrimarySaleWorkflow({ ethSigner, starkSigner, @@ -77,3 +95,34 @@ export async function CreatePrimarySaleWorkflow({ ...createPrimarySaleResp.data, }; } + +export async function AcceptPrimarySalesWorkflow({ + ethSigner, + primarySalesApi, + primarySaleId, +}: AcceptPrimarySaleWorkflowParams): Promise { + const signableAcceptPrimarySaleResult = + await primarySalesApi.signableAcceptPrimarySale({ + id: primarySaleId, + }); + + const signableMessage = signableAcceptPrimarySaleResult.data.signable_message; + + const signature = await signRaw(signableMessage, ethSigner); + + const acceptPrimarySaleResp = await primarySalesApi.acceptPrimarySale( + { + id: primarySaleId, + }, + { + headers: { + 'x-imx-eth-address': await ethSigner.getAddress(), + 'x-imx-eth-signature': signature, + }, + }, + ); + + return { + ...acceptPrimarySaleResp.data, + }; +} diff --git a/src/workflows/workflows.ts b/src/workflows/workflows.ts index 355fbaf3..f6970337 100644 --- a/src/workflows/workflows.ts +++ b/src/workflows/workflows.ts @@ -64,7 +64,10 @@ import { generateIMXAuthorisationHeaders } from '../utils'; import { ImmutableXConfiguration } from '../config'; import { exchangeTransfersWorkflow } from './exchangeTransfers'; import axios, { AxiosResponse } from 'axios'; -import { CreatePrimarySaleWorkflow } from './primarySales'; +import { + CreatePrimarySaleWorkflow, + AcceptPrimarySalesWorkflow, +} from './primarySales'; export class Workflows { private readonly depositsApi: DepositsApi; @@ -527,4 +530,12 @@ export class Workflows { primarySalesApi: this.primarySalesApi, }); } + + public async acceptPrimarySale(ethSigner: EthSigner, primarySaleId: number) { + return AcceptPrimarySalesWorkflow({ + ethSigner, + primarySaleId, + primarySalesApi: this.primarySalesApi, + }); + } }