From d43abed9e0ea4871a6afaafebbcf32dc91feab55 Mon Sep 17 00:00:00 2001 From: Gabriel Antony Xaviour Date: Tue, 14 Nov 2023 21:23:17 +0530 Subject: [PATCH] add rarible test fix --- src/visualizer/rarible/index.ts | 16 +++--- src/visualizer/rarible/utils.ts | 19 ++++--- test/visualizer/rarible/data.ts | 79 +++++++++++++++++++++++++++ test/visualizer/rarible/index.test.ts | 14 ++--- 4 files changed, 104 insertions(+), 24 deletions(-) create mode 100644 test/visualizer/rarible/data.ts diff --git a/src/visualizer/rarible/index.ts b/src/visualizer/rarible/index.ts index 4cce2c7..fcddfbf 100644 --- a/src/visualizer/rarible/index.ts +++ b/src/visualizer/rarible/index.ts @@ -42,30 +42,30 @@ export const visualize = (message: RaribleOrder, domain: Domain): VisualizationR /** Check the type of the taker asset to build AssetIn */ if (message.takeAsset.assetType.assetClass === getAssetClass("ETH")) { assetIn = buildAssetETH(message.takeAsset); - } else if (message.takeAsset.assetType.assetClass !== getAssetClass("ERC20")) { + } else if (message.takeAsset.assetType.assetClass === getAssetClass("ERC20")) { assetIn = buildAssetERC20(message.takeAsset); - } else if (message.takeAsset.assetType.assetClass !== getAssetClass("ERC721")) { + } else if (message.takeAsset.assetType.assetClass === getAssetClass("ERC721")) { assetIn = buildAssetERC721(message.takeAsset); - } else if (message.takeAsset.assetType.assetClass !== getAssetClass("ERC1155")) { + } else if (message.takeAsset.assetType.assetClass === getAssetClass("ERC1155")) { assetIn = buildAssetERC1155(message.takeAsset); } else { throw new WizardError( - `unknow rarible asset class: ${message.takeAsset.assetType.assetClass}` + `unknown rarible asset class: ${message.takeAsset.assetType.assetClass}` ); } /** Check the type of the maker asset to build AssetOut */ if (message.makeAsset.assetType.assetClass === getAssetClass("ETH")) { assetOut = buildAssetETH(message.makeAsset); - } else if (message.makeAsset.assetType.assetClass !== getAssetClass("ERC20")) { + } else if (message.makeAsset.assetType.assetClass === getAssetClass("ERC20")) { assetOut = buildAssetERC20(message.makeAsset); - } else if (message.makeAsset.assetType.assetClass !== getAssetClass("ERC721")) { + } else if (message.makeAsset.assetType.assetClass === getAssetClass("ERC721")) { assetOut = buildAssetERC721(message.makeAsset); - } else if (message.makeAsset.assetType.assetClass !== getAssetClass("ERC1155")) { + } else if (message.makeAsset.assetType.assetClass === getAssetClass("ERC1155")) { assetOut = buildAssetERC1155(message.makeAsset); } else { throw new WizardError( - `unknow rarible asset class: ${message.makeAsset.assetType.assetClass}` + `unknown rarible asset class: ${message.makeAsset.assetType.assetClass}` ); } diff --git a/src/visualizer/rarible/utils.ts b/src/visualizer/rarible/utils.ts index 6a4d91b..a334ceb 100644 --- a/src/visualizer/rarible/utils.ts +++ b/src/visualizer/rarible/utils.ts @@ -1,4 +1,5 @@ -import ethers from "ethers"; +import { keccak256, toUtf8Bytes, AbiCoder } from "ethers"; +import { ZERO_ADDRESS } from "../../utils"; import { ASSET_TYPE, AssetInOut } from "../../types"; import { Asset } from "../../types/rarible"; @@ -8,7 +9,7 @@ import { Asset } from "../../types/rarible"; * @returns assetClass selector in bytes4 format */ export function getAssetClass(assetType: string): string { - return ethers.keccak256(ethers.toUtf8Bytes(assetType)).slice(0, 10); + return keccak256(toUtf8Bytes(assetType)).slice(0, 10); } /** @@ -21,7 +22,7 @@ export function buildAssetETH(asset: Asset): AssetInOut[] { { type: ASSET_TYPE.NATIVE, amounts: [asset.value], - address: ethers.ZeroAddress, + address: ZERO_ADDRESS, }, ]; } @@ -32,14 +33,14 @@ export function buildAssetETH(asset: Asset): AssetInOut[] { * @returns Returns AssetInOut in the ERC6865 format */ export function buildAssetERC20(asset: Asset): AssetInOut[] { - const abiCoder = ethers.AbiCoder.defaultAbiCoder(); + const abiCoder = AbiCoder.defaultAbiCoder(); const address = abiCoder.decode(["address"], asset.assetType.data)[0]; return [ { type: ASSET_TYPE.ERC20, amounts: [asset.value], - address: address, + address: address.toLowerCase(), }, ]; } @@ -50,7 +51,7 @@ export function buildAssetERC20(asset: Asset): AssetInOut[] { * @returns Returns AssetInOut in the ERC6865 format */ export function buildAssetERC721(asset: Asset): AssetInOut[] { - const abiCoder = ethers.AbiCoder.defaultAbiCoder(); + const abiCoder = AbiCoder.defaultAbiCoder(); const decodedData = abiCoder.decode(["address", "uint256"], asset.assetType.data); const address = decodedData[0]; @@ -60,7 +61,7 @@ export function buildAssetERC721(asset: Asset): AssetInOut[] { type: ASSET_TYPE.ERC721, amounts: ["1"], id: tokenId.toString(), - address: address, + address: address.toLowerCase(), }, ]; } @@ -71,7 +72,7 @@ export function buildAssetERC721(asset: Asset): AssetInOut[] { * @returns Returns AssetInOut in the ERC6865 format */ export function buildAssetERC1155(asset: Asset): AssetInOut[] { - const abiCoder = ethers.AbiCoder.defaultAbiCoder(); + const abiCoder = AbiCoder.defaultAbiCoder(); const decodedData = abiCoder.decode(["address", "uint256"], asset.assetType.data); const address = decodedData[0]; @@ -81,7 +82,7 @@ export function buildAssetERC1155(asset: Asset): AssetInOut[] { type: ASSET_TYPE.ERC1155, amounts: [asset.value], id: tokenId.toString(), - address: address, + address: address.toLowerCase(), }, ]; } diff --git a/test/visualizer/rarible/data.ts b/test/visualizer/rarible/data.ts new file mode 100644 index 0000000..249cc19 --- /dev/null +++ b/test/visualizer/rarible/data.ts @@ -0,0 +1,79 @@ +import { getAssetClass } from "../../../src/visualizer/rarible/utils"; + +const raribleERC20InERC1155Out = { + maker: "0x000075B45Dff84C00Cf597d5C3E766108CeA0000".toLowerCase(), + makeAsset: { + assetType: { + assetClass: getAssetClass("ERC1155"), + data: "0x000000000000000000000000d9145cce52d386f254917e481eb44e9943f391380000000000000000000000000000000000000000000000000000000000000045", + }, + value: "6", + }, + taker: "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8".toLowerCase(), + takeAsset: { + assetType: { + assetClass: getAssetClass("ERC20"), + data: "0x000000000000000000000000f8e81D47203A594245E36C48e151709F0C19fBe8".toLowerCase(), + }, + value: "10000000000000000000", + }, + start: "1698643839", + end: "1798843839", + salt: "0", + dataType: "0x", + data: "0x", +}; + +const raribleETHInERC721Out = { + maker: "0x000075B45Dff84C00Cf597d5C3E766108CeA0000".toLowerCase(), + makeAsset: { + assetType: { + assetClass: getAssetClass("ERC721"), + data: "0x000000000000000000000000d9145cce52d386f254917e481eb44e9943f391380000000000000000000000000000000000000000000000000000000000000045", + }, + value: "1", + }, + taker: "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8".toLowerCase(), + takeAsset: { + assetType: { + assetClass: getAssetClass("ETH"), + data: "0x0000000000000000000000000000000000000000", + }, + value: "100000000000000000", + }, + start: "1698643839", + end: "1798843839", + salt: "0", + dataType: "0x", + data: "0x", +}; + +const raribleERC1155InETHOut = { + maker: "0x000075B45Dff84C00Cf597d5C3E766108CeA0000".toLowerCase(), + makeAsset: { + assetType: { + assetClass: getAssetClass("ETH"), + data: "0x0000000000000000000000000000000000000000", + }, + value: "100000000000000000", + }, + taker: "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8".toLowerCase(), + takeAsset: { + assetType: { + assetClass: getAssetClass("ERC1155"), + data: "0x000000000000000000000000d9145cce52d386f254917e481eb44e9943f391380000000000000000000000000000000000000000000000000000000000000045", + }, + value: "6", + }, + start: "1698643839", + end: "1798843839", + salt: "0", + dataType: "0x", + data: "0x", +}; + +Object.freeze(raribleERC1155InETHOut); +Object.freeze(raribleERC20InERC1155Out); +Object.freeze(raribleETHInERC721Out); + +export { raribleERC1155InETHOut, raribleERC20InERC1155Out, raribleETHInERC721Out }; diff --git a/test/visualizer/rarible/index.test.ts b/test/visualizer/rarible/index.test.ts index 80a2f87..f2be347 100644 --- a/test/visualizer/rarible/index.test.ts +++ b/test/visualizer/rarible/index.test.ts @@ -11,7 +11,7 @@ import { describe("rarible", () => { const raribleDomain: Domain = { - verifyingContract: "0x9757F2d2b135150BBeb65308D4a91804107cd8D6", + verifyingContract: "0x9757F2d2b135150BBeb65308D4a91804107cd8D6".toLowerCase(), name: "Exchange", version: "2", chainId: "1", @@ -47,7 +47,7 @@ describe("rarible", () => { }, raribleDomain ) - ).rejects.toThrowError("unknown rarible assetClass: XYZ"); + ).rejects.toThrowError(`unknown rarible asset class: ${getAssetClass("XYZ")}`); }); it("should successfully visualize ERC20 In ERC1155 Out order", async () => { @@ -57,14 +57,14 @@ describe("rarible", () => { protocol: "EXCHANGE", assetsIn: [ { - address: "0xf8e81D47203A594245E36C48e151709F0C19fBe8", + address: "0xf8e81D47203A594245E36C48e151709F0C19fBe8".toLowerCase(), type: "ERC20", amounts: ["10000000000000000000"], }, ], assetsOut: [ { - address: "0xd9145cce52d386f254917e481eb44e9943f39138", + address: "0xd9145cce52d386f254917e481eb44e9943f39138".toLowerCase(), type: "ERC1155", id: "69", amounts: ["6"], @@ -76,7 +76,7 @@ describe("rarible", () => { }); it("should successfully visualize ETH In ERC721 Out order", async () => { - const result = await visualize(raribleERC20InERC1155Out, raribleDomain); + const result = await visualize(raribleETHInERC721Out, raribleDomain); expect(result).toEqual({ protocol: "EXCHANGE", @@ -89,7 +89,7 @@ describe("rarible", () => { ], assetsOut: [ { - address: "0xd9145cce52d386f254917e481eb44e9943f39138", + address: "0xd9145cce52d386f254917e481eb44e9943f39138".toLowerCase(), type: "ERC721", id: "69", amounts: ["1"], @@ -107,7 +107,7 @@ describe("rarible", () => { protocol: "EXCHANGE", assetsIn: [ { - address: "0xd9145cce52d386f254917e481eb44e9943f39138", + address: "0xd9145cce52d386f254917e481eb44e9943f39138".toLowerCase(), type: "ERC1155", id: "69", amounts: ["6"],