Skip to content

Commit

Permalink
feat(core): approve
Browse files Browse the repository at this point in the history
  • Loading branch information
remiroyc committed Jan 3, 2024
1 parent a11f419 commit d73b29f
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions packages/react/hooks/useCreateListing.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
import { useState } from "react";

import { Account, AccountInterface } from "starknet";
import {
Account,
AccountInterface,
BigNumberish,
cairo,
CallData
} from "starknet";

import {
createListing as createListingCore,
ListingV1
} from "@ark-project/core";
import { getContractAddresses } from "@ark-project/core/src/constants";

import { useRpc } from "../components/ArkProvider/RpcContext";
import { Status } from "../types/hooks";
import { useOwner } from "./useOwner";

export default function useCreateListing() {
const { rpcProvider, network } = useRpc();

const [status, setStatus] = useState<Status>("idle");
const [response, setResponse] = useState<bigint | undefined>(undefined);
const owner = useOwner();
const { STARKNET_EXECUTOR_ADDRESS } = getContractAddresses(network);

async function authorizeStarknetERC721Transfer(
tokenAddress: string,
starknetAccount: AccountInterface,
tokenId: BigNumberish
) {
await starknetAccount.execute([
{
contractAddress: tokenAddress,
entrypoint: "approve",
calldata: CallData.compile({
to: STARKNET_EXECUTOR_ADDRESS,
token_id: cairo.uint256(tokenId)
})
}
]);
}

async function createListing(
starknetAccount: AccountInterface,
order: ListingV1
) {
const burner_address = localStorage.getItem("burner_address");
const burner_private_key = localStorage.getItem("burner_private_key");
const burner_public_key = localStorage.getItem("burner_public_key");

if (
burner_address === null ||
burner_private_key === null ||
Expand All @@ -34,6 +60,13 @@ export default function useCreateListing() {

try {
setStatus("loading");

await authorizeStarknetERC721Transfer(
order.tokenAddress.toString(),
starknetAccount,
order.tokenId
);

const orderHash = await createListingCore(
network,
rpcProvider,
Expand Down

0 comments on commit d73b29f

Please sign in to comment.