Skip to content

Commit

Permalink
Merge pull request #239 from alephium/remove-get-collection-id
Browse files Browse the repository at this point in the history
Remove getCollectionId from NFT standard interface
  • Loading branch information
h0ngcha0 authored Aug 17, 2023
2 parents a68ec09 + 8c108d2 commit ceeeaf0
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 76 deletions.
10 changes: 5 additions & 5 deletions .project.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
},
"INFT": {
"sourceFile": "../packages/web3/std/nft_interface.ral",
"sourceCodeHash": "491b0726ad455d7f2df52f71e071f34c2630de3e17f1e3fb9fd5d7efd81461a0",
"sourceCodeHash": "1fefa4eb581554fa2b7b083b0eb232c787c101e67e2eeedd6d518bb0c8b93a2d",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
Expand Down Expand Up @@ -125,18 +125,18 @@
},
"NFTCollectionTest": {
"sourceFile": "nft/nft_collection_test.ral",
"sourceCodeHash": "61708c6b182adba9e3a143e693c3037bc6ef04610fc36bb6f41a071a0a150940",
"sourceCodeHash": "a166844ef1b4e5a6a41431e10ff08df9ae96ae841d51db659f831f6c64db7981",
"bytecodeDebugPatch": "",
"codeHashDebug": "898896f2a322e795b7d2a8908214eff18fa0ff1e14c4e945380838fac6852023",
"codeHashDebug": "62a74b8496ee1693de16f2d67d5b3ba3273e3a04cb59aec3a24c1ae5a51c2e0c",
"warnings": [
"No external caller check for function \"NFTCollectionTest.mint\". Please use \"checkCaller!(...)\" in the function or its callees, or disable it with \"@using(checkExternalCaller = false)\"."
]
},
"NFTTest": {
"sourceFile": "nft/nft_test.ral",
"sourceCodeHash": "0c4716a9246e851c1a5b54f3bdbe6074ec5f34079d7b8b6f44fc9e96fd8588dc",
"sourceCodeHash": "f1918b2d68dd1c03ed42738c0a7b91d93cfa9c50b4e44b5bd80b333ee4635bde",
"bytecodeDebugPatch": "",
"codeHashDebug": "657b46b2236b78c955660db6731d43b42dd8204bb6220181de3c5f8678d90da0",
"codeHashDebug": "c654f89a813259b6e270c8cf609200907f0824755bb394b00d8b151c3fddf95f",
"warnings": []
},
"Sub": {
Expand Down
4 changes: 2 additions & 2 deletions artifacts/nft/NFTCollectionTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"version": "v2.5.0",
"name": "NFTCollectionTest",
"bytecode": "04040912402d4070010000000102ce0102010000000102a0000201000102010f16000001310c7b160040cb17011601c50d7b16010201030105011db41701b116001406414c5048000313036413006417031702160113c40de0b6b3a7640000a2a00040ce00160216030d1601c91704a0000d2aa100160402",
"codeHash": "898896f2a322e795b7d2a8908214eff18fa0ff1e14c4e945380838fac6852023",
"bytecode": "04040912402d406f010000000102ce0102010000000102a0000201000102010f16000001310c7b160040cb17011601c50d7b16010201030105011cb4170116001406414c5048000313026413006417031702160113c40de0b6b3a7640000a2a00040ce00160216030d1601c91704a0000d2aa100160402",
"codeHash": "62a74b8496ee1693de16f2d67d5b3ba3273e3a04cb59aec3a24c1ae5a51c2e0c",
"fieldsSig": {
"names": [
"nftTemplateId",
Expand Down
19 changes: 2 additions & 17 deletions artifacts/nft/NFTTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
{
"version": "v2.5.0",
"name": "NFTTest",
"bytecode": "03020912010000000102ce0102010000000102ce0002",
"codeHash": "657b46b2236b78c955660db6731d43b42dd8204bb6220181de3c5f8678d90da0",
"bytecode": "020109010000000102ce0002",
"codeHash": "c654f89a813259b6e270c8cf609200907f0824755bb394b00d8b151c3fddf95f",
"fieldsSig": {
"names": [
"collectionId",
"uri",
"__stdInterfaceId"
],
"types": [
"ByteVec",
"ByteVec",
"ByteVec"
],
"isMutable": [
false,
false,
false
]
Expand All @@ -33,18 +30,6 @@
"returnTypes": [
"ByteVec"
]
},
{
"name": "getCollectionId",
"usePreapprovedAssets": false,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": [
"ByteVec"
]
}
],
"constants": [],
Expand Down
2 changes: 1 addition & 1 deletion artifacts/ts/NFTCollectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const NFTCollectionTest = new Factory(
Contract.fromJson(
NFTCollectionTestContractJson,
"",
"898896f2a322e795b7d2a8908214eff18fa0ff1e14c4e945380838fac6852023"
"62a74b8496ee1693de16f2d67d5b3ba3273e3a04cb59aec3a24c1ae5a51c2e0c"
)
);

Expand Down
23 changes: 1 addition & 22 deletions artifacts/ts/NFTTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { getContractByCodeHash } from "./contracts";
// Custom types for the contract
export namespace NFTTestTypes {
export type Fields = {
collectionId: HexString;
uri: HexString;
};

Expand All @@ -41,10 +40,6 @@ export namespace NFTTestTypes {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<HexString>;
};
getCollectionId: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<HexString>;
};
}
export type CallMethodParams<T extends keyof CallMethodTable> =
CallMethodTable[T]["params"];
Expand All @@ -71,11 +66,6 @@ class Factory extends ContractFactory<NFTTestInstance, NFTTestTypes.Fields> {
): Promise<TestContractResult<HexString>> => {
return testMethod(this, "getTokenUri", params);
},
getCollectionId: async (
params: Omit<TestContractParams<NFTTestTypes.Fields, never>, "testArgs">
): Promise<TestContractResult<HexString>> => {
return testMethod(this, "getCollectionId", params);
},
};
}

Expand All @@ -84,7 +74,7 @@ export const NFTTest = new Factory(
Contract.fromJson(
NFTTestContractJson,
"",
"657b46b2236b78c955660db6731d43b42dd8204bb6220181de3c5f8678d90da0"
"c654f89a813259b6e270c8cf609200907f0824755bb394b00d8b151c3fddf95f"
)
);

Expand All @@ -110,17 +100,6 @@ export class NFTTestInstance extends ContractInstance {
getContractByCodeHash
);
},
getCollectionId: async (
params?: NFTTestTypes.CallMethodParams<"getCollectionId">
): Promise<NFTTestTypes.CallMethodResult<"getCollectionId">> => {
return callMethod(
NFTTest,
this,
"getCollectionId",
params === undefined ? {} : params,
getContractByCodeHash
);
},
};

async multicall<Calls extends NFTTestTypes.MultiCallParams>(
Expand Down
2 changes: 1 addition & 1 deletion contracts/nft/nft_collection_test.ral
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Contract NFTCollectionTest(
pub fn mint(nftUri: ByteVec) -> (ByteVec) {
let minter = callerAddress!()

let (initialImmState, initialMutState) = NFTTest.encodeFields!(selfContractId!(), nftUri)
let (initialImmState, initialMutState) = NFTTest.encodeFields!(nftUri)

let contractId = copyCreateSubContractWithToken!{minter -> ALPH: 1 alph}(
toByteVec!(totalSupply),
Expand Down
5 changes: 0 additions & 5 deletions contracts/nft/nft_test.ral
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import "std/nft_interface"

Contract NFTTest(
collectionId: ByteVec,
uri: ByteVec
) implements INFT {
pub fn getTokenUri() -> ByteVec {
return uri
}

pub fn getCollectionId() -> ByteVec {
return collectionId
}
}
9 changes: 2 additions & 7 deletions packages/web3/src/api/node-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,9 @@ export class NodeProvider implements NodeProviderApis {
fetchNFTMetaData = async (tokenId: HexString): Promise<NFTMetaData> => {
const address = addressFromTokenId(tokenId)
const group = groupOfAddress(address)
const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }))
const result = await this.contracts.postContractsMulticallContract({
calls: calls
})
const callResults = result.results.map((r) => tryGetCallResult(r))
const result = await this.contracts.postContractsCallContract({ methodIndex: 0, group, address })
return {
tokenUri: hexToString(callResults[0].returns[0].value as any as string),
collectionAddress: addressFromContractId(callResults[1].returns[0].value as any as string)
tokenUri: hexToString(tryGetCallResult(result).returns[0].value as any as string)
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/web3/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ export interface FungibleTokenMetaData {
}

export interface NFTMetaData {
collectionAddress: string
tokenUri: string
}

Expand Down
2 changes: 1 addition & 1 deletion packages/web3/src/token/nft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,5 @@ describe('nft', function () {

const invalidUri = 'https://ipfs.io/ipfs/invalid'
expect(async () => await validateNFTBaseUri(invalidUri, 2)).rejects.toThrow(Error)
})
}, 10000)
})
2 changes: 0 additions & 2 deletions packages/web3/std/nft_interface.ral
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@ Interface INFT {
// }
// }
pub fn getTokenUri() -> ByteVec

pub fn getCollectionId() -> ByteVec
}
13 changes: 1 addition & 12 deletions test/nft-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ describe('nft collection', function () {
const nftTest = (
await NFTTest.deploy(signer, {
initialFields: {
collectionId: stringToHex('collection-id'),
uri: nftUri
}
})
Expand All @@ -66,20 +65,17 @@ describe('nft collection', function () {
}
})
).contractInstance

expect((await nftCollectionTest.methods.getCollectionUri()).returns).toEqual(collectionUri)
expect((await nftCollectionTest.methods.totalSupply()).returns).toEqual(0n)

const nodeProvider = web3.getCurrentNodeProvider()
const isFollowsNFTCollectionStd = await nodeProvider.guessFollowsNFTCollectionStd(nftCollectionTest.contractId)
expect(isFollowsNFTCollectionStd).toEqual(true)

const nftCollectionMetadata = await nodeProvider.fetchNFTCollectionMetaData(nftCollectionTest.contractId)
expect(nftCollectionMetadata).toEqual({
collectionUri: rawCollectionUri,
totalSupply: 0n
})

for (let i = 0n; i < 10n; i++) {
await mintAndVerify(nftCollectionTest, nftUri, i)
}
Expand All @@ -96,23 +92,16 @@ describe('nft collection', function () {
})
const nftContractId = subContractId(nftCollectionTest.contractId, binToHex(encodeU256(tokenIndex)), 0)
expect((await nftCollectionTest.methods.nftByIndex({ args: { index: tokenIndex } })).returns).toEqual(nftContractId)

const nftInstance = NFTTest.at(addressFromContractId(nftContractId))
const nftFields = (await nftInstance.fetchState()).fields
expect(nftFields.uri).toEqual(nftUri)
expect(nftFields.collectionId).toEqual(nftCollectionTest.contractId)
expect((await nftInstance.methods.getCollectionId()).returns).toEqual(nftCollectionTest.contractId)

const stdInterfaceId = await web3.getCurrentNodeProvider().guessStdInterfaceId(nftInstance.contractId)
expect(stdInterfaceId).toEqual('0003')

const tokenType = await web3.getCurrentNodeProvider().guessStdTokenType(nftInstance.contractId)
expect(tokenType).toEqual('non-fungible')

const nftMetadata = await web3.getCurrentNodeProvider().fetchNFTMetaData(nftInstance.contractId)
expect(nftMetadata).toEqual({
tokenUri: hexToString(nftUri),
collectionAddress: nftCollectionTest.address
tokenUri: hexToString(nftUri)
})
}

Expand Down

0 comments on commit ceeeaf0

Please sign in to comment.