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

Commit

Permalink
TD1237 Accept primary sale workflow (#418)
Browse files Browse the repository at this point in the history
init

Signed-off-by: Frank Li <[email protected]>
  • Loading branch information
frankisawesome authored Feb 12, 2024
1 parent 495c2be commit ae8de4d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/ImmutableX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
51 changes: 50 additions & 1 deletion src/workflows/primarySales.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {
AcceptPrimarySaleBadRequestBody,
AcceptPrimarySaleForbiddenBody,
AcceptPrimarySaleNotFoundBody,
AcceptPrimarySaleOKBody,
AcceptPrimarySaleUnauthorizedBody,
CreatePrimarySaleBadRequestBody,
CreatePrimarySaleCreatedBody,
CreatePrimarySaleForbiddenBody,
Expand All @@ -8,21 +13,34 @@ import {
PrimarySalesApiCreatePrimarySaleRequest,
PrimarySalesApiSignableCreatePrimarySaleRequest,
} from '../api';
import { WalletConnection } from '../types';
import { EthSigner, WalletConnection } from '../types';
import { signRaw } from '../utils';

type CreatePrimarySaleWorkflowParams = WalletConnection & {
request: PrimarySalesApiSignableCreatePrimarySaleRequest;
primarySalesApi: PrimarySalesApi;
};

type AcceptPrimarySaleWorkflowParams = {
ethSigner: EthSigner;
primarySaleId: number;
primarySalesApi: PrimarySalesApi;
};

type CreatePrimarySaleResponse =
| CreatePrimarySaleBadRequestBody
| CreatePrimarySaleCreatedBody
| CreatePrimarySaleForbiddenBody
| CreatePrimarySaleUnauthorizedBody
| CreatePrimarySaleNotFoundBody;

type AcceptPrimarySaleResponse =
| AcceptPrimarySaleOKBody
| AcceptPrimarySaleBadRequestBody
| AcceptPrimarySaleForbiddenBody
| AcceptPrimarySaleNotFoundBody
| AcceptPrimarySaleUnauthorizedBody;

export async function CreatePrimarySaleWorkflow({
ethSigner,
starkSigner,
Expand Down Expand Up @@ -77,3 +95,34 @@ export async function CreatePrimarySaleWorkflow({
...createPrimarySaleResp.data,
};
}

export async function AcceptPrimarySalesWorkflow({
ethSigner,
primarySalesApi,
primarySaleId,
}: AcceptPrimarySaleWorkflowParams): Promise<AcceptPrimarySaleResponse> {
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,
};
}
13 changes: 12 additions & 1 deletion src/workflows/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -527,4 +530,12 @@ export class Workflows {
primarySalesApi: this.primarySalesApi,
});
}

public async acceptPrimarySale(ethSigner: EthSigner, primarySaleId: number) {
return AcceptPrimarySalesWorkflow({
ethSigner,
primarySaleId,
primarySalesApi: this.primarySalesApi,
});
}
}

0 comments on commit ae8de4d

Please sign in to comment.