Skip to content

Commit

Permalink
feat(core): simplify core action parameters (ArkProjectNFTs#454)
Browse files Browse the repository at this point in the history
## Description

Simplify core function and react hooks parameters.

Before:

```ts
const orderHash = await createOffer(config, {
  starknetAccount: buyer,
  offer: {
    brokerId: accounts.listingBroker.address,
    tokenAddress: STARKNET_NFT_ADDRESS,
    tokenId,
    startAmount: BigInt(10)
  },
  approveInfo: {
    currencyAddress: config.starknetCurrencyContract,
    amount: BigInt(10)
  }
});
```

After:

```ts
const { orderHash } = await createOffer(config, {
  account: buyer,
  brokerAddress: accounts.listingBroker.address,
  currencyAddress: config.starknetCurrencyContract,
  tokenAddress,
  tokenId,
  amount: BigInt(10)
});
```


## What type of PR is this? (check all applicable)

- [ ] 🍕 Feature (`feat:`)
- [ ] 🐛 Bug Fix (`fix:`)
- [ ] 📝 Documentation Update (`docs:`)
- [ ] 🎨 Style (`style:`)
- [ ] 🧑‍💻 Code Refactor (`refactor:`)
- [ ] 🔥 Performance Improvements (`perf:`)
- [ ] ✅ Test (`test:`)
- [ ] 🤖 Build (`build:`)
- [ ] 🔁 CI (`ci:`)
- [ ] 📦 Chore (`chore:`)
- [ ] ⏩ Revert (`revert:`)
- [ ] 🚀 Breaking Changes (`BREAKING CHANGE:`)

## Related Tickets & Documents

<!--
Please use this format to link related issues: Fixes #<issue_number>
More info:
https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->

## Added tests?

- [ ] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help

## Added to documentation?

- [ ] 📜 README.md
- [ ] 📓 Documentation
- [ ] 🙅 no documentation needed

## [optional] Are there any post-deployment tasks we need to perform?

<!-- Describe any additional tasks, if any, and provide steps. -->

## [optional] What gif best describes this PR or how it makes you feel?

<!-- Share a fun gif related to your PR! -->

### PR Title and Description Guidelines:

- Ensure your PR title follows semantic versioning standards. This helps
automate releases and changelogs.
- Use types like `feat:`, `fix:`, `chore:`, `BREAKING CHANGE:` etc. in
your PR title.
- Your PR title will be used as a commit message when merging. Make sure
it adheres to [Conventional Commits
standards](https://www.conventionalcommits.org/).

## Closing Issues

<!--
Use keywords to close related issues. This ensures that the associated
issues will automatically close when the PR is merged.

- `Fixes ArkProjectNFTs#123` will close issue 123 when the PR is merged.
- `Closes ArkProjectNFTs#123` will also close issue 123 when the PR is merged.
- `Resolves ArkProjectNFTs#123` will also close issue 123 when the PR is merged.

You can also use multiple keywords in one comment:
- `Fixes ArkProjectNFTs#123, Resolves ArkProjectNFTs#456`

More info:
https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->
  • Loading branch information
gershon authored Sep 14, 2024
1 parent 881ffc5 commit 15dc33d
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 497 deletions.
26 changes: 17 additions & 9 deletions packages/core/src/actions/order/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {
} from "starknet";

import { Config } from "../../createConfig.js";
import { CancelInfo, FullCancelInfo } from "../../types/index.js";
import { FullCancelInfo } from "../../types/index.js";

export interface CancelOrderParameters {
starknetAccount: AccountInterface;
cancelInfo: CancelInfo;
account: AccountInterface;
orderHash: bigint;
tokenAddress: string;
tokenId: bigint;
waitForTransaction?: boolean;
}

Expand Down Expand Up @@ -43,20 +45,26 @@ export async function cancelOrder(
config: Config,
parameters: CancelOrderParameters
): Promise<CancelOrderResult> {
const { starknetAccount, cancelInfo, waitForTransaction = true } = parameters;
const {
account,
orderHash,
tokenAddress,
tokenId,
waitForTransaction = true
} = parameters;
const chainId = await config.starknetProvider.getChainId();
const fullCancelInfo: FullCancelInfo = {
orderHash: cancelInfo.orderHash,
canceller: starknetAccount.address,
orderHash,
canceller: account.address,
tokenChainId: chainId,
tokenAddress: cancelInfo.tokenAddress,
tokenAddress: tokenAddress,
tokenId: new CairoOption<Uint256>(
CairoOptionVariant.Some,
cairo.uint256(cancelInfo.tokenId)
cairo.uint256(tokenId)
)
};

const result = await starknetAccount.execute({
const result = await account.execute({
contractAddress: config.starknetExecutorContract,
entrypoint: "cancel_order",
calldata: CallData.compile({
Expand Down
103 changes: 60 additions & 43 deletions packages/core/src/actions/order/createAuction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import {
} from "starknet";

import { Config } from "../../createConfig.js";
import {
ApproveErc721Info,
AuctionV1,
OrderV1,
RouteType
} from "../../types/index.js";
import { OrderV1, RouteType } from "../../types/index.js";
import { getOrderHashFromOrderV1 } from "../../utils/index.js";

export interface CreateAuctionParameters {
starknetAccount: AccountInterface;
order: AuctionV1;
approveInfo: ApproveErc721Info;
account: AccountInterface;
brokerAddress: string;
tokenAddress: string;
tokenId: bigint;
currencyAddress?: string;
startAmount: bigint;
endAmount?: bigint;
startDate?: number;
endDate?: number;
waitForTransaction?: boolean;
}

Expand Down Expand Up @@ -47,72 +48,88 @@ export async function createAuction(
parameters: CreateAuctionParameters
): Promise<CreateAuctionResult> {
const {
starknetAccount,
order: baseOrder,
approveInfo,
account,
brokerAddress,
tokenAddress,
tokenId,
currencyAddress = config.starknetCurrencyContract,
startAmount,
endAmount,
startDate,
endDate,
waitForTransaction = true
} = parameters;
const currentDate = new Date();
currentDate.setDate(currentDate.getDate());
const startDate = baseOrder.startDate || Math.floor(Date.now() / 1000);
const endDate =
baseOrder.endDate ||
Math.floor(currentDate.getTime() / 1000) + 60 * 60 * 24 * 7;
const chainId = await config.starknetProvider.getChainId();
const now = Math.floor(Date.now() / 1000);
const startedAt = startDate || now;
const endedAt = endDate || now + 60 * 60 * 24;
const maxEndedAt = now + 60 * 60 * 24 * 30;

if (startedAt < now) {
throw new Error(
`Invalid start date. Start date (${startDate}) cannot be in the past.`
);
}

if (startDate < Math.floor(Date.now() / 1000)) {
throw new Error("Invalid start date");
if (endedAt < startedAt) {
throw new Error(
`Invalid end date. End date (${endDate}) must be after the start date (${startDate}).`
);
}

if (endDate < startDate) {
throw new Error("Invalid end date");
if (endedAt > maxEndedAt) {
throw new Error(
`End date too far in the future. End date (${endDate}) exceeds the maximum allowed (${maxEndedAt}).`
);
}

if (baseOrder.startAmount === BigInt(0)) {
throw new Error("Invalid start amount");
if (startAmount === BigInt(0)) {
throw new Error(
"Invalid start amount. The start amount must be greater than zero."
);
}

if (baseOrder.endAmount < baseOrder.startAmount) {
throw new Error("Invalid end amount");
if (endAmount && endAmount < startAmount) {
throw new Error(
"Invalid end amount. The end amount must be greater than the start amount."
);
}

const chainId = await config.starknetProvider.getChainId();

const order: OrderV1 = {
route: RouteType.Erc721ToErc20,
currencyAddress:
baseOrder.currencyAddress ?? config.starknetCurrencyContract,
currencyAddress,
currencyChainId: chainId,
salt: 1,
offerer: starknetAccount.address,
offerer: account.address,
tokenChainId: chainId,
tokenAddress: baseOrder.tokenAddress,
tokenAddress,
tokenId: new CairoOption<Uint256>(
CairoOptionVariant.Some,
cairo.uint256(baseOrder.tokenId)
cairo.uint256(tokenId)
),
quantity: cairo.uint256(1),
startAmount: cairo.uint256(baseOrder.startAmount),
endAmount: cairo.uint256(baseOrder.endAmount || 0),
startDate: startDate,
endDate: endDate,
brokerId: baseOrder.brokerId,
startAmount: cairo.uint256(startAmount),
endAmount: cairo.uint256(endAmount || 0),
startDate: startedAt,
endDate: endedAt,
brokerId: brokerAddress,
additionalData: []
};

const result = await starknetAccount.execute([
const result = await account.execute([
{
contractAddress: approveInfo.tokenAddress as string,
contractAddress: tokenAddress,
entrypoint: "approve",
calldata: CallData.compile({
to: config.starknetExecutorContract,
token_id: cairo.uint256(approveInfo.tokenId)
token_id: cairo.uint256(tokenId)
})
},
{
contractAddress: config.starknetExecutorContract,
entrypoint: "create_order",
calldata: CallData.compile({
order: order
})
calldata: CallData.compile({ order })
}
]);

Expand Down
92 changes: 58 additions & 34 deletions packages/core/src/actions/order/createListing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import {
} from "starknet";

import { Config } from "../../createConfig.js";
import {
ApproveErc721Info,
ListingV1,
OrderV1,
RouteType
} from "../../types/index.js";
import { OrderV1, RouteType } from "../../types/index.js";
import { getOrderHashFromOrderV1 } from "../../utils/index.js";

export interface CreateListingParameters {
starknetAccount: AccountInterface;
order: ListingV1;
approveInfo: ApproveErc721Info;
account: AccountInterface;
brokerAddress: string;
currencyAddress?: string;
tokenAddress: string;
tokenId: bigint;
amount: bigint;
startDate?: number;
endDate?: number;
waitForTransaction?: boolean;
}

Expand Down Expand Up @@ -47,56 +47,80 @@ export async function createListing(
parameters: CreateListingParameters
): Promise<CreateListingResult> {
const {
starknetAccount,
order: baseOrder,
approveInfo,
account,
brokerAddress,
currencyAddress = config.starknetCurrencyContract,
tokenAddress,
tokenId,
amount,
startDate,
endDate,
waitForTransaction = true
} = parameters;
const currentDate = new Date();
currentDate.setDate(currentDate.getDate());
const startDate = baseOrder.startDate || Math.floor(Date.now() / 1000);
const endDate =
baseOrder.endDate ||
Math.floor(currentDate.getTime() / 1000) + 60 * 60 * 24 * 7;
const chainId = await config.starknetProvider.getChainId();
const now = Math.floor(Date.now() / 1000);
const startedAt = startDate || now;
const endedAt = endDate || now + 60 * 60 * 24;
const maxEndedAt = now + 60 * 60 * 24 * 30;

if (startedAt < now) {
throw new Error(
`Invalid start date. Start date (${startDate}) cannot be in the past.`
);
}

if (endedAt < startedAt) {
throw new Error(
`Invalid end date. End date (${endDate}) must be after the start date (${startDate}).`
);
}

if (endedAt > maxEndedAt) {
throw new Error(
`End date too far in the future. End date (${endDate}) exceeds the maximum allowed (${maxEndedAt}).`
);
}

if (amount === BigInt(0)) {
throw new Error(
"Invalid start amount. The start amount must be greater than zero."
);
}

const chainId = await config.starknetProvider.getChainId();
const order: OrderV1 = {
route: RouteType.Erc721ToErc20,
currencyAddress:
baseOrder.currencyAddress ?? config.starknetCurrencyContract,
currencyAddress,
currencyChainId: chainId,
salt: 1,
offerer: starknetAccount.address,
offerer: account.address,
tokenChainId: chainId,
tokenAddress: baseOrder.tokenAddress,
tokenAddress,
tokenId: new CairoOption<Uint256>(
CairoOptionVariant.Some,
cairo.uint256(baseOrder.tokenId)
cairo.uint256(tokenId)
),
quantity: cairo.uint256(1),
startAmount: cairo.uint256(baseOrder.startAmount),
startAmount: cairo.uint256(amount),
endAmount: cairo.uint256(0),
startDate: startDate,
endDate: endDate,
brokerId: baseOrder.brokerId,
startDate: startedAt,
endDate: endedAt,
brokerId: brokerAddress,
additionalData: []
};

const result = await starknetAccount.execute([
const result = await account.execute([
{
contractAddress: approveInfo.tokenAddress as string,
contractAddress: tokenAddress,
entrypoint: "approve",
calldata: CallData.compile({
to: config.starknetExecutorContract,
token_id: cairo.uint256(approveInfo.tokenId)
token_id: cairo.uint256(tokenId)
})
},
{
contractAddress: config.starknetExecutorContract,
entrypoint: "create_order",
calldata: CallData.compile({
order: order
})
calldata: CallData.compile({ order })
}
]);

Expand Down
Loading

0 comments on commit 15dc33d

Please sign in to comment.