Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Bugfix: Nfts ops treated in the wrong order to infer balance #1865

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/families/ethereum/nft.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ describe("nft merging", () => {

describe("OpenSea lazy minting bs", () => {
test("should have a correct on-chain nft amount even with OpenSea lazy minting", () => {
const makeNftOperation = (type: Operation["type"], value): Operation => {
const makeNftOperation = (
type: Operation["type"],
value: string | number,
dateOrder: number
): Operation => {
if (!["NFT_IN", "NFT_OUT"].includes(type)) {
return {} as Operation;
}
Expand All @@ -101,6 +105,7 @@ describe("OpenSea lazy minting bs", () => {
const fee = new BigNumber(0);
const tokenId = "42069";
const hash = "FaKeHasH";
const date = new Date(Date.now() + dateOrder ?? 0);

return {
id,
Expand All @@ -114,17 +119,18 @@ describe("OpenSea lazy minting bs", () => {
value: new BigNumber(value),
type,
accountId: id,
date,
} as Operation;
};

// scenario with bob lazy minting 10 NFTs
const ops = [
makeNftOperation("NFT_OUT", 5), // lazy mint sending 5 NFT
makeNftOperation("NFT_IN", 1), // receiving 1 of them back
makeNftOperation("NFT_IN", 2), // receiving 2 of them back
makeNftOperation("NFT_OUT", 2), // lazy mint sending 5 NFT (transformed by OpenSea in 2 txs) 1/2 (off-chain)
makeNftOperation("NFT_OUT", 3), // lazy mint sending 5 NFT (transformed by OpenSea in 2 txs) 2/2 (on-chain)
makeNftOperation("NFT_IN", 1), // receiving 1 back
makeNftOperation("NFT_OUT", 5, 0), // lazy mint sending 5 NFT
makeNftOperation("NFT_IN", 1, 1), // receiving 1 of them back
makeNftOperation("NFT_IN", 2, 2), // receiving 2 of them back
makeNftOperation("NFT_OUT", 2, 3), // lazy mint sending 5 NFT (transformed by OpenSea in 2 txs) 1/2 (off-chain)
makeNftOperation("NFT_OUT", 3, 4), // lazy mint sending 5 NFT (transformed by OpenSea in 2 txs) 2/2 (on-chain)
makeNftOperation("NFT_IN", 1, 5), // receiving 1 back
];

// What happened for bob:
Expand Down
2 changes: 2 additions & 0 deletions src/nft/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { API, apiForCurrency } from "../api/Ethereum";

export const nftsFromOperations = (ops: Operation[]): ProtoNFT[] => {
const nftsMap = ops
// make sure we have the operation in chronological order (older first)
.sort((a, b) => a.date.getTime() - b.date.getTime())
// if ops are Operations get the prop nftOperations, else ops are considered nftOperations already
.flatMap((op) => (op?.nftOperations?.length ? op.nftOperations : op))
.reduce((acc: Record<string, ProtoNFT>, nftOp: Operation) => {
Expand Down