Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same block mint into+resadd #113

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/rmrk2.0.0/classes/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ export class NFT {
let recipientEncoded = recipient;
if (isValidAddressPolkadotAddress(recipient)) {
recipientEncoded = encodeAddress(recipient, ss58Format);
} else {
const splitRecipient = String(recipientEncoded).split("-");
if (splitRecipient[0] === "0") {
splitRecipient[0] = block;
recipientEncoded = splitRecipient.join("-");
}
}
const obj = getRemarkData(dataString);
return new this({
Expand Down
16 changes: 14 additions & 2 deletions src/rmrk2.0.0/classes/resadd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export class Resadd {
this.replace = replaceId;
}

static fromRemark(remark: string): Resadd | string {
static fromRemark(
remark: string,
block?: number
): Resadd | string {
if (!block) {
block = 0;
}
try {
validateResadd(remark);
const [
Expand All @@ -45,7 +51,13 @@ export class Resadd {
replaceId,
] = remark.split("::");
const resourceObj: Resource = getRemarkData(resource);
return new this(nftId, resourceObj, replaceId);
let the_nftId = nftId;
const splitNftId = String(nftId).split("-");
if (splitNftId[0] === "0") {
splitNftId[0] = block;
the_nftId = splitNftId.join("-");
}
return new this(the_nftId, resourceObj, replaceId);
} catch (e: any) {
console.error(e.message);
console.log(`RESADD error: full input was ${remark}`);
Expand Down
2 changes: 1 addition & 1 deletion src/rmrk2.0.0/tools/consolidator/consolidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ export class Consolidator {
const invalidate = this.updateInvalidCalls(OP_TYPES.RESADD, remark).bind(
this
);
const resaddEntity = Resadd.fromRemark(remark.remark);
const resaddEntity = Resadd.fromRemark(remark.remark, remark.block);
if (typeof resaddEntity === "string") {
invalidate(
remark.remark,
Expand Down
67 changes: 67 additions & 0 deletions test/2.0.0/consolidator/__snapshots__/mint.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/2.0.0/consolidator/mint.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Consolidator, NFT } from "../../../src/rmrk2.0.0";
import {
createCollectionMock,
createBatchMock,
getBlockCallsMock,
getAliceKey,
getBobKey,
Expand Down Expand Up @@ -117,4 +118,13 @@ describe("rmrk2.0.0 Consolidator: MINT", () => {
"Attempted to mint into maxed out collection d43593c715a56da27d-KANARIABIRDS"
);
});

it("Should allow minting another NFT into a NFT minted in the same block", async () => {
const remarks = getRemarksFromBlocksMock([
...getBlockCallsMock(createCollectionMock().create()),
...createBatchMock(mintNftMock().mint(), mintNftMock2().mint("0-d43593c715a56da27d-KANARIABIRDS-KANR-00000777")),
]);
const consolidator = new Consolidator();
expect(await consolidator.consolidate(remarks)).toMatchSnapshot();
});
});
19 changes: 19 additions & 0 deletions test/2.0.0/consolidator/resadd.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Consolidator } from "../../../src/rmrk2.0.0";
import {
createCollectionMock,
createBatchMock,
getBlockCallsMock,
getBobKey,
getRemarksFromBlocksMock,
Expand Down Expand Up @@ -96,4 +97,22 @@ describe("rmrk2.0.0 Consolidator: RESADD", () => {
consolidatedResult.nfts[mintNftMock(3).getId()].resources[0].pending
).toBeTruthy();
});

it("Should allow adding a resource to a NFT in the same block it was minted", async () => {
const remarks = getRemarksFromBlocksMock([
...getBlockCallsMock(createCollectionMock().create()),
...createBatchMock(mintNftMock().mint(), "RMRK::RESADD::2.0.0::0-d43593c715a56da27d-KANARIABIRDS-KANR-00000777::%7B%22id%22%3A%22foo%22%2C%22metadata%22%3A%22ipfs%3A%2F%2Fipfs%2F123%22%7D"),
]);
const consolidator = new Consolidator();
const consolidatedResult = await consolidator.consolidate(remarks);
expect(
consolidatedResult.nfts[mintNftMock(3).getId()].resources[0].pending
).toBeFalsy();
expect(
consolidatedResult.nfts[mintNftMock(3).getId()].resources[0].metadata
).toEqual("ipfs://ipfs/123");
expect(consolidatedResult.nfts[mintNftMock(3).getId()].priority[0]).toEqual(
consolidatedResult.nfts[mintNftMock(3).getId()].resources[0].id
);
});
});
32 changes: 32 additions & 0 deletions test/2.0.0/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,38 @@ export const getBlockCallsMock = (
return blockCall;
};

export const createBatchMock = (
remark1: string,
remark2: string,
caller: string = getAliceKey().address,
extras?: BlockCall[]
): Block[] => {
block = block + 1;
const blockCall: Block[] = [
{
block: block,
calls: [
{
call: "system.remark",
value: stringToHex(remark1),
caller: caller,
},
{
call: "system.remark",
value: stringToHex(remark2),
caller: caller,
},
],
},
];

if (extras) {
blockCall[0].calls[0].extras = extras;
}

return blockCall;
};

export const getRemarksFromBlocksMock = (blockCalls: Block[]): Remark[] => {
block = 1;
return getRemarksFromBlocks(blockCalls, ["0x726d726b", "0x524d524b"]);
Expand Down