Skip to content

Commit

Permalink
add rarible test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielantonyxaviour committed Nov 14, 2023
1 parent f2caf39 commit d43abed
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 24 deletions.
16 changes: 8 additions & 8 deletions src/visualizer/rarible/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
}

Expand Down
19 changes: 10 additions & 9 deletions src/visualizer/rarible/utils.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
}

/**
Expand All @@ -21,7 +22,7 @@ export function buildAssetETH(asset: Asset): AssetInOut[] {
{
type: ASSET_TYPE.NATIVE,
amounts: [asset.value],
address: ethers.ZeroAddress,
address: ZERO_ADDRESS,
},
];
}
Expand All @@ -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(),
},
];
}
Expand All @@ -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];
Expand All @@ -60,7 +61,7 @@ export function buildAssetERC721(asset: Asset): AssetInOut[] {
type: ASSET_TYPE.ERC721,
amounts: ["1"],
id: tokenId.toString(),
address: address,
address: address.toLowerCase(),
},
];
}
Expand All @@ -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];
Expand All @@ -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(),
},
];
}
79 changes: 79 additions & 0 deletions test/visualizer/rarible/data.ts
Original file line number Diff line number Diff line change
@@ -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 };
14 changes: 7 additions & 7 deletions test/visualizer/rarible/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {

describe("rarible", () => {
const raribleDomain: Domain = {
verifyingContract: "0x9757F2d2b135150BBeb65308D4a91804107cd8D6",
verifyingContract: "0x9757F2d2b135150BBeb65308D4a91804107cd8D6".toLowerCase(),
name: "Exchange",
version: "2",
chainId: "1",
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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"],
Expand All @@ -76,7 +76,7 @@ describe("rarible", () => {
});

it("should successfully visualize ETH In ERC721 Out order", async () => {
const result = await visualize<RaribleOrder>(raribleERC20InERC1155Out, raribleDomain);
const result = await visualize<RaribleOrder>(raribleETHInERC721Out, raribleDomain);

expect(result).toEqual({
protocol: "EXCHANGE",
Expand All @@ -89,7 +89,7 @@ describe("rarible", () => {
],
assetsOut: [
{
address: "0xd9145cce52d386f254917e481eb44e9943f39138",
address: "0xd9145cce52d386f254917e481eb44e9943f39138".toLowerCase(),
type: "ERC721",
id: "69",
amounts: ["1"],
Expand All @@ -107,7 +107,7 @@ describe("rarible", () => {
protocol: "EXCHANGE",
assetsIn: [
{
address: "0xd9145cce52d386f254917e481eb44e9943f39138",
address: "0xd9145cce52d386f254917e481eb44e9943f39138".toLowerCase(),
type: "ERC1155",
id: "69",
amounts: ["6"],
Expand Down

0 comments on commit d43abed

Please sign in to comment.