Skip to content

feat(collection): add collection offer - #452

Merged
gershon merged 12 commits into
feat/contract-v2from
feat/add-collection-offer
Sep 17, 2024
Merged

gershon merged 12 commits into
feat/contract-v2from
feat/add-collection-offer

Conversation

@gershon

@gershon gershon commented Sep 13, 2024

Copy link
Copy Markdown
Contributor

Description

Add collection offer in sdk core and react.

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

  • 🍕 Feature (feat:)

Added tests?

  • 👍 yes

Added to documentation?

  • 📜 README.md
  • 📓 Documentation
  • 🙅 no documentation needed

@changeset-bot

changeset-bot Bot commented Sep 13, 2024

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 8a12e01

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@gershon
gershon requested a review from kwiss September 13, 2024 10:24
Comment on lines +1 to +90
import {
AccountInterface,
cairo,
CairoOption,
CairoOptionVariant,
CallData,
Uint256
} from "starknet";

import { Config } from "../../createConfig.js";
import {
ApproveErc721Info,
BaseFulfillInfo,
FulfillInfo
} from "../../types/index.js";

interface FulfillCollectionOfferParameters {
starknetAccount: AccountInterface;
fulfillOfferInfo: BaseFulfillInfo;
approveInfo: ApproveErc721Info;
waitForTransaction?: boolean;
}

interface FulfillCollectionOfferResult {
transactionHash: string;
}

/**
* Fulfill a collection offer on the Arkchain.
*
* @param {Config} config - The core SDK configuration.
* @param {FulfillOfferParameters} parameters - Parameters for fulfilling the offer.
*
* @returns {Promise<void>} A promise that resolves when the transaction is completed.
*/
async function fulfillCollectionOffer(
config: Config,
parameters: FulfillCollectionOfferParameters
): Promise<FulfillCollectionOfferResult> {
const {
starknetAccount,
fulfillOfferInfo,
approveInfo,
waitForTransaction = true
} = parameters;
const chainId = await config.starknetProvider.getChainId();

const fulfillInfo: FulfillInfo = {
orderHash: fulfillOfferInfo.orderHash,
relatedOrderHash: new CairoOption<bigint>(CairoOptionVariant.None),
fulfiller: starknetAccount.address,
tokenChainId: chainId,
tokenAddress: fulfillOfferInfo.tokenAddress,
tokenId: new CairoOption<Uint256>(
CairoOptionVariant.Some,
cairo.uint256(fulfillOfferInfo.tokenId)
),
fulfillBrokerAddress: fulfillOfferInfo.brokerId
};

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

if (!waitForTransaction) {
await config.starknetProvider.waitForTransaction(result.transaction_hash, {
retryInterval: 1000
});
}

return {
transactionHash: result.transaction_hash
};
}

export { fulfillCollectionOffer };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a fulfillOffer function since fulfilling an offer or a collection offer is the same

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, removing it.

Comment on lines +1 to +145
import {
AccountInterface,
cairo,
CairoOption,
CairoOptionVariant,
CallData,
Uint256
} from "starknet";

import { Config } from "../../createConfig.js";
import {
ApproveErc20Info,
CollectionOfferV1,
OrderV1,
RouteType
} from "../../types/index.js";
import { getOrderHashFromOrderV1 } from "../../utils/index.js";
import { getAllowance } from "../read/getAllowance.js";

interface CreateCollectionOfferParameters {
starknetAccount: AccountInterface;
offer: CollectionOfferV1;
approveInfo: ApproveErc20Info;
waitForTransaction?: boolean;
}

export interface CreateCollectionOfferResult {
orderHash: bigint;
transactionHash: string;
}

/**
* Creates a collection offer on the ArkProject.
*
* This function takes a configuration object and listing parameters, builds a complete OrderV1 object
* with default values for unspecified fields, compiles the order data, signs it, and then executes
* the transaction to create a listing on the Arkchain using the specified Starknet and Arkchain accounts.
*
* @param {Config} config - The core SDK config, including network and contract information.
* @param {CreateCollectionOfferParameters} parameters - The parameters for the listing, including Starknet account,
* Arkchain account, base order details, and an optional owner address.
*
* @returns {Promise<CreateCollectionOfferResult>} A promise that resolves with the hash of the created order.
*
*/
async function createCollectionOffer(
config: Config,
parameters: CreateCollectionOfferParameters
): Promise<CreateCollectionOfferResult> {
const {
starknetAccount,
offer: baseOrder,
approveInfo,
waitForTransaction = true
} = parameters;
const now = Math.floor(Date.now() / 1000);
const startDate = baseOrder.startDate || now;
const endDate = baseOrder.endDate || now + 30;
const maxEndDate = now + 60 * 60 * 24 * 30;
const chainId = await config.starknetProvider.getChainId();
const currencyAddress =
baseOrder.currencyAddress || config.starknetCurrencyContract;

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

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

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

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

if (currencyAddress !== approveInfo.currencyAddress) {
throw new Error("Invalid currency address. Offer and approveInfo mismatch");
}

const currentAllowance = await getAllowance(
config,
approveInfo.currencyAddress,
starknetAccount.address
);
const allowance = currentAllowance + approveInfo.amount;

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

const result = await starknetAccount.execute([
{
contractAddress: approveInfo.currencyAddress,
entrypoint: "approve",
calldata: CallData.compile({
spender: config.starknetExecutorContract,
amount: cairo.uint256(allowance)
})
},
{
contractAddress: config.starknetExecutorContract,
entrypoint: "create_order",
calldata: CallData.compile({
order
})
}
]);

if (waitForTransaction) {
await config.starknetProvider.waitForTransaction(result.transaction_hash, {
retryInterval: 1000
});
}

const orderHash = getOrderHashFromOrderV1(order);

return {
orderHash,
transactionHash: result.transaction_hash
};
}

export { createCollectionOffer };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same do we need that since the only difference with a classic offer is the token ID, we should use the same function with a boolean no ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep 2 separates functions as create collection offer might accept quantity parameter in near future, and it will be more error prone with optional token id.

@gershon
gershon force-pushed the feat/add-collection-offer branch from fe64329 to 8a12e01 Compare September 16, 2024 21:18
@gershon
gershon merged commit 2210e6a into feat/contract-v2 Sep 17, 2024
@gershon
gershon deleted the feat/add-collection-offer branch September 17, 2024 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants