Skip to content

Commit

Permalink
distributing royalties
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsmkl committed Jun 1, 2022
1 parent 9d2f46f commit 494da93
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 38 deletions.
1 change: 1 addition & 0 deletions contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"EscrowComputeExecutionTemplate",
"DistributeNFTCollateralCondition",
"StandardRoyalties",
"Distributor",
"AaveBorrowCondition",
"AaveCollateralDepositCondition",
"AaveCollateralWithdrawCondition",
Expand Down
16 changes: 16 additions & 0 deletions contracts/registry/DIDRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ contract DIDRegistry is DIDFactory {
}

event DIDRoyaltiesAdded(bytes32 indexed did, address indexed addr);
event DIDRoyaltyRecipientChanged(bytes32 indexed did, address indexed addr);

function setDIDRoyalties(
bytes32 _did,
Expand All @@ -70,6 +71,21 @@ contract DIDRegistry is DIDFactory {
);
}

function setDIDRoyaltyRecipient(
bytes32 _did,
address _recipient
)
public
{
require(didRegisterList.didRegisters[_did].creator == msg.sender, 'Only creator can set royalties');
didRegisterList.didRegisters[_did].royaltyRecipient = _recipient;

emit DIDRoyaltyRecipientChanged(
_did,
_recipient
);
}

/**
* @notice Register a Mintable DID using NFTs based in the ERC-1155 standard.
*
Expand Down
2 changes: 1 addition & 1 deletion contracts/royalties/Distributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract Distributor is Initializable {

// If receivers is changed, the unclaimed rewards will be divided only for first takers ...
function setReceivers(bytes32 _did, address[] memory _addr) public {
require(msg.sender == registry.getDIDCreator(_did), 'only owner can change');
require(msg.sender == registry.getDIDCreator(_did), 'only creator can change');
receivers[_did] = _addr;
}

Expand Down
17 changes: 17 additions & 0 deletions scripts/deploy/truffle-wrapper/deploy/initializeContracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,23 @@ async function initializeContracts({
}
}

if (getAddress('ConditionStoreManager') &&
getAddress('EscrowPaymentCondition') &&
getAddress('DIDRegistry')) {
if (contracts.indexOf('Distributor') > -1) {
addressBook.Distributor = await zosCreate({
contract: 'Distributor',
ctx,
args: [
getAddress('DIDRegistry'),
getAddress('ConditionStoreManager'),
getAddress('EscrowPaymentCondition')
],
verbose
})
}
}

if (getAddress('ConditionStoreManager') &&
getAddress('DIDRegistry')) {
if (contracts.indexOf('LockPaymentCondition') > -1) {
Expand Down
106 changes: 69 additions & 37 deletions test/int/nft/NFT721_e2e.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ contract('End to End NFT721 Scenarios', (accounts) => {
collector2,
gallery,
market,
someone
someone,
recipient1,
recipient2
] = accounts

const owner = accounts[8]
Expand All @@ -48,7 +50,8 @@ contract('End to End NFT721 Scenarios', (accounts) => {
const numberNFTs2 = 1
const nftPrice2 = 100
const amounts2 = [90, 10]
const receivers2 = [collector1, artist]
let receivers2
const recipients = [recipient1, recipient2]

let
didRegistry,
Expand All @@ -64,6 +67,7 @@ contract('End to End NFT721 Scenarios', (accounts) => {
escrowCondition,
nftHolderCondition,
accessCondition,
distributor,
getBalance

async function setupTest() {
Expand Down Expand Up @@ -93,6 +97,13 @@ contract('End to End NFT721 Scenarios', (accounts) => {
token
))

distributor = await testUtils.deploy('Distributor', [
didRegistry.address,
conditionStoreManager.address,
escrowCondition.address], deployer)

receivers2 = [collector1, distributor.address]

transferCondition = await testUtils.deploy('TransferNFT721Condition', [owner,
conditionStoreManager.address,
didRegistry.address,
Expand Down Expand Up @@ -360,17 +371,55 @@ contract('End to End NFT721 Scenarios', (accounts) => {
})

describe('As collector1 I want to sell my NFT to a different collector2 for a higher price', () => {
let agreementId2, conditionIds
it('A sale without proper royalties can not happen', async () => {
const agreementIdNoRoyalties = testUtils.generateId()
const amountsNoRoyalties = [99, 1]
const receiversNoRoyalties = [collector1, artist]

// Collector2: Create NFT sales agreement
const { nftSalesAgreement } = await prepareNFTSaleAgreement({
did: did,
agreementId: agreementIdNoRoyalties,
_amounts: amountsNoRoyalties,
_receivers: receiversNoRoyalties,
_seller: collector1,
_buyer: collector2,
_numberNFTs: numberNFTs2
})

const result = await nftSalesTemplate.createAgreement(...Object.values(nftSalesAgreement))

testUtils.assertEmitted(result, 1, 'AgreementCreated')

// Collector2: Lock the payment
await token.mint(collector2, nftPrice2, { from: owner })
await token.approve(lockPaymentCondition.address, nftPrice2, { from: collector2 })
await token.approve(escrowCondition.address, nftPrice2, { from: collector2 })

await assert.isRejected(
lockPaymentCondition.fulfill(agreementIdNoRoyalties, did, escrowCondition.address, token.address, amountsNoRoyalties, receiversNoRoyalties, { from: collector2 }),
/Royalties are not satisfied/
)
await token.transfer(didRegistry.address, nftPrice2, { from: collector2 })
})

it('Artist sets up royalty recipients', async () => {
await distributor.setReceivers(did, recipients, {from: artist})
await didRegistry.setDIDRoyaltyRecipient(did, distributor.address, {from: artist})
})
it('As collector2 I setup an agreement for buying an NFT to collector1', async () => {
// Collector2: Create NFT sales agreement
const { agreementFullfill, conditionIds, agreementId: agreementId2 } = await prepareNFTSaleAgreement({
let agreementFullfill
({ agreementFullfill, conditionIds, agreementId: agreementId2 } = await prepareNFTSaleAgreement({
did: did,
_amounts: amounts2,
_receivers: receivers2,
_seller: collector1,
_buyer: collector2,
_numberNFTs: numberNFTs2,
_from: collector2
})
}));

// Collector2: Lock the payment
await token.mint(collector2, nftPrice2, { from: owner })
Expand Down Expand Up @@ -419,41 +468,24 @@ contract('End to End NFT721 Scenarios', (accounts) => {
})

it('As artist I want to receive royalties for the NFT I created and was sold in the secondary market', async () => {
// Artist check the balance and has the royalties
assert.strictEqual(await getBalance(token, artist), amounts[0] + amounts2[1])
})

//
it('A sale without proper royalties can not happen', async () => {
const agreementIdNoRoyalties = testUtils.generateId()
const amountsNoRoyalties = [99, 1]
const receiversNoRoyalties = [collector1, artist]

// Collector2: Create NFT sales agreement
const { nftSalesAgreement } = await prepareNFTSaleAgreement({
did: did,
agreementId: agreementIdNoRoyalties,
_amounts: amountsNoRoyalties,
_receivers: receiversNoRoyalties,
_seller: collector1,
_buyer: collector2,
_numberNFTs: numberNFTs2
})

const result = await nftSalesTemplate.createAgreement(...Object.values(nftSalesAgreement))

testUtils.assertEmitted(result, 1, 'AgreementCreated')

// Collector2: Lock the payment
await token.mint(collector2, nftPrice2, { from: owner })
await token.approve(lockPaymentCondition.address, nftPrice2, { from: collector2 })
await token.approve(escrowCondition.address, nftPrice2, { from: collector2 })

await assert.isRejected(
lockPaymentCondition.fulfill(agreementIdNoRoyalties, did, escrowCondition.address, token.address, amountsNoRoyalties, receiversNoRoyalties, { from: collector2 }),
/Royalties are not satisfied/
// Distributor check the balance and has the royalties
assert.strictEqual(await getBalance(token, distributor.address), amounts2[1])
// Distribute royalties
await distributor.claimReward(
agreementId2,
did,
amounts2,
receivers2,
collector2,
escrowCondition.address,
token.address,
conditionIds[0],
[conditionIds[1]]
)
assert.strictEqual(await getBalance(token, recipient1), amounts2[1]/2)
assert.strictEqual(await getBalance(token, recipient2), amounts2[1]/2)
})

})
}

Expand Down

0 comments on commit 494da93

Please sign in to comment.