-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(collection): add collection offer #452
Conversation
|
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 }; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, removing it.
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 }; |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
fe64329
to
8a12e01
Compare
Description
Add collection offer in sdk core and react.
What type of PR is this? (check all applicable)
feat:
)Added tests?
Added to documentation?