Skip to content

Commit

Permalink
fix(core): use optional offer currency address param (#435)
Browse files Browse the repository at this point in the history
## Description

Use optional offer currency address param if provided.

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

- [ ] 🍕 Feature (`feat:`)
- [x] 🐛 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:`)

## Added tests?

- [x] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help
  • Loading branch information
gershon authored Sep 4, 2024
1 parent bc93c21 commit 50a694d
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-cows-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ark-project/core": patch
---

use offer currency address optional param
3 changes: 2 additions & 1 deletion packages/core/src/actions/order/createAuction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const createAuction = async (

const order: OrderV1 = {
route: RouteType.Erc721ToErc20,
currencyAddress: config.starknetCurrencyContract,
currencyAddress:
baseOrder.currencyAddress ?? config.starknetCurrencyContract,
currencyChainId: chainId,
salt: 1,
offerer: starknetAccount.address,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/actions/order/createListing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const createListing = async (

const order: OrderV1 = {
route: RouteType.Erc721ToErc20,
currencyAddress: config.starknetCurrencyContract,
currencyAddress:
baseOrder.currencyAddress ?? config.starknetCurrencyContract,
currencyChainId: chainId,
salt: 1,
offerer: starknetAccount.address,
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/actions/order/createOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ const createOffer = async (
currentDate.setDate(currentDate.getDate() + 30);
const startDate = baseOrder.startDate || Math.floor(Date.now() / 1000);
const endDate = baseOrder.endDate || Math.floor(currentDate.getTime() / 1000);
const currencyAddress =
baseOrder.currencyAddress || config.starknetCurrencyContract;

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

const chainId = await config.starknetProvider.getChainId();
const currentAllowance = await getAllowance(
config,
Expand All @@ -55,7 +62,8 @@ const createOffer = async (
const allowance = currentAllowance + approveInfo.amount;
const order: OrderV1 = {
route: RouteType.Erc20ToErc721,
currencyAddress: config.starknetCurrencyContract,
currencyAddress:
baseOrder.currencyAddress ?? config.starknetCurrencyContract,
currencyChainId: chainId,
salt: 1,
offerer: starknetAccount.address,
Expand Down
27 changes: 27 additions & 0 deletions packages/core/tests/createAuction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ describe("createAuction", () => {
expect(orderType).toEqual("AUCTION");
}, 50_000);

it("default: with custom currency", async () => {
const { seller, listingBroker } = accounts;
const tokenId = await mintERC721({ account: seller });

const orderHash = await createAuction(config, {
starknetAccount: seller,
order: {
brokerId: listingBroker.address,
tokenAddress: STARKNET_NFT_ADDRESS,
tokenId,
startAmount: BigInt(1),
endAmount: BigInt(10),
currencyAddress:
"0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"
},
approveInfo: {
tokenAddress: STARKNET_NFT_ADDRESS,
tokenId
}
});

const orderTypeCairo = await getOrderType(config, { orderHash });
const orderType = getTypeFromCairoCustomEnum(orderTypeCairo.orderType);

expect(orderType).toEqual("AUCTION");
}, 50_000);

it("error: invalid start date", async () => {
const { seller, listingBroker } = accounts;
const tokenId = await mintERC721({ account: seller });
Expand Down
24 changes: 24 additions & 0 deletions packages/core/tests/createListing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,28 @@ describe("createListing", () => {

expect(orderStatus).toBe("Open");
}, 50_000);

it("default: with custom currency", async () => {
const { seller } = accounts;
const tokenId = await mintERC721({ account: seller });

const orderHash = await createListing(config, {
starknetAccount: seller,
order: {
brokerId: accounts.listingBroker.address,
tokenAddress: STARKNET_NFT_ADDRESS,
tokenId,
startAmount: BigInt(1),
currencyAddress:
"0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"
},
approveInfo: {
tokenAddress: STARKNET_NFT_ADDRESS,
tokenId
}
});
const { orderStatus } = await getOrderStatus(config, { orderHash });

expect(orderStatus).toBe("Open");
}, 50_000);
});
59 changes: 57 additions & 2 deletions packages/core/tests/createOffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ describe("createOffer", () => {
it("default", async () => {
const { seller, buyer } = accounts;
const tokenId = await mintERC721({ account: seller });
await mintERC20({ account: buyer, amount: 1 });
await mintERC20({ account: buyer, amount: 1000 });

const orderHash = await createOffer(config, {
starknetAccount: seller,
starknetAccount: buyer,
offer: {
brokerId: accounts.listingBroker.address,
tokenAddress: STARKNET_NFT_ADDRESS,
Expand All @@ -35,4 +35,59 @@ describe("createOffer", () => {

expect(orderStatusAfter).toBe("Open");
}, 50_000);

// it("default: custom currency", async () => {
// const { seller, buyer } = accounts;
// const tokenId = await mintERC721({ account: seller });
// await mintERC20({ account: buyer, amount: 1000 });

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

// await new Promise((resolve) => setTimeout(resolve, 5_000));

// const { orderStatus: orderStatusAfter } = await getOrderStatus(config, {
// orderHash
// });

// expect(orderStatusAfter).toBe("Open");
// }, 50_000);

it("error: invalid currency address", async () => {
const { seller, buyer } = accounts;
const tokenId = await mintERC721({ account: seller });
await mintERC20({ account: buyer, amount: 1 });

await expect(
createOffer(config, {
starknetAccount: buyer,
offer: {
brokerId: accounts.listingBroker.address,
tokenAddress: STARKNET_NFT_ADDRESS,
tokenId,
startAmount: BigInt(10),
currencyAddress:
"0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"
},
approveInfo: {
currencyAddress: config.starknetCurrencyContract,
amount: BigInt(10)
}
})
).rejects.toThrow();
}, 50_000);
});

0 comments on commit 50a694d

Please sign in to comment.