Skip to content

Commit

Permalink
Merge pull request #316 from alephium/add-get-royalty-amount
Browse files Browse the repository at this point in the history
Add fetchNFTRoyaltyAmount on NodeProvider
  • Loading branch information
polarker authored Feb 22, 2024
2 parents e98eb63 + 672e8c7 commit 6054f07
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
33 changes: 31 additions & 2 deletions packages/web3/src/api/node-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ export class NodeProvider implements NodeProviderApis {
}

// Only use this when the contract follows the NFT collection interface, check `guessFollowsNFTCollectionStd` first
fetchNFTCollectionMetaData = async (contractId: HexString): Promise<NFTCollectionMetaData> => {
const address = addressFromContractId(contractId)
fetchNFTCollectionMetaData = async (collectionId: HexString): Promise<NFTCollectionMetaData> => {
const address = addressFromContractId(collectionId)
const group = groupOfAddress(address)
const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }))
const result = await this.contracts.postContractsMulticallContract({ calls })
Expand All @@ -194,6 +194,30 @@ export class NodeProvider implements NodeProviderApis {
}
}

// Only use this when the contract follows the NFT collection with royalty interface, check `guessFollowsNFTCollectionWithRoyaltyStd` first
fetchNFTRoyaltyAmount = async (collectionId: HexString, tokenId: HexString, salePrice: bigint): Promise<bigint> => {
const address = addressFromContractId(collectionId)
const group = groupOfAddress(address)
const apiResult = await this.contracts.postContractsCallContract({
address: address,
group: group,
methodIndex: 4,
args: [
{
type: 'ByteVec',
value: tokenId
},
{
type: 'U256',
value: salePrice.toString()
}
]
})

const result = tryGetCallResult(apiResult)
return BigInt(result.returns[0].value as any as string)
}

guessStdInterfaceId = async (tokenId: HexString): Promise<HexString | undefined> => {
const address = addressFromTokenId(tokenId)
const group = groupOfAddress(address)
Expand All @@ -212,6 +236,11 @@ export class NodeProvider implements NodeProviderApis {
return !!interfaceId && interfaceId.startsWith(StdInterfaceIds.NFTCollection)
}

guessFollowsNFTCollectionWithRoyaltyStd = async (contractId: HexString): Promise<boolean> => {
const interfaceId = await this.guessStdInterfaceId(contractId)
return interfaceId === StdInterfaceIds.NFTCollectionWithRoyalty
}

guessStdTokenType = async (tokenId: HexString): Promise<'fungible' | 'non-fungible' | undefined> => {
const interfaceId = await this.guessStdInterfaceId(tokenId)
switch (interfaceId) {
Expand Down
18 changes: 15 additions & 3 deletions test/nft-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ describe('nft collection', function () {
const rawCollectionUri = 'https://cryptopunks.app/cryptopunks'
const collectionUri = stringToHex(rawCollectionUri)
let nftCollectionInstance: NFTCollectionTestInstance | NFTCollectionWithRoyaltyTestInstance
const royaltyRate = 200n // basis points

if (royalty) {
const royaltyRate = 200n // basis points
nftCollectionInstance = (
await NFTCollectionWithRoyaltyTest.deploy(signer, {
initialFields: {
Expand Down Expand Up @@ -183,8 +183,7 @@ describe('nft collection', function () {
expect((await nftCollectionInstance.methods.totalSupply()).returns).toEqual(0n)

const nodeProvider = web3.getCurrentNodeProvider()
const isFollowsNFTCollectionStd = await nodeProvider.guessFollowsNFTCollectionStd(nftCollectionInstance.contractId)
expect(isFollowsNFTCollectionStd).toEqual(true)
expect(await nodeProvider.guessFollowsNFTCollectionStd(nftCollectionInstance.contractId)).toEqual(true)
const nftCollectionMetadata = await nodeProvider.fetchNFTCollectionMetaData(nftCollectionInstance.contractId)
expect(nftCollectionMetadata).toEqual({
collectionUri: rawCollectionUri,
Expand All @@ -195,6 +194,8 @@ describe('nft collection', function () {
}

if (royalty) {
expect(await nodeProvider.guessFollowsNFTCollectionWithRoyaltyStd(nftCollectionInstance.contractId)).toEqual(true)

const balanceBefore = await nodeProvider.addresses.getAddressesAddressBalance(nftCollectionInstance.address)
expect(balanceBefore.balanceHint).toEqual('2 ALPH')
await WithdrawNFTCollectionTest.execute(signer, {
Expand All @@ -205,6 +206,17 @@ describe('nft collection', function () {
})
const balanceAfter = await nodeProvider.addresses.getAddressesAddressBalance(nftCollectionInstance.address)
expect(balanceAfter.balanceHint).toEqual('1 ALPH')

for (let i = 0n; i < 10n; i++) {
const nftPrice = 100n * ONE_ALPH
const nftContractId = subContractId(nftCollectionInstance.contractId, binToHex(encodeU256(i)), 0)
const royaltyAmount = await nodeProvider.fetchNFTRoyaltyAmount(
nftCollectionInstance.contractId,
nftContractId,
nftPrice
)
expect(royaltyAmount).toEqual((nftPrice * royaltyRate) / 10000n)
}
}
}

Expand Down

0 comments on commit 6054f07

Please sign in to comment.