Skip to content

Instantly share code, notes, and snippets.

@monapasan
Created February 8, 2024 15:02
Show Gist options
  • Save monapasan/96037bb456233fe2a5b05a11008653eb to your computer and use it in GitHub Desktop.
Save monapasan/96037bb456233fe2a5b05a11008653eb to your computer and use it in GitHub Desktop.
export const deployWithSupply = async (item: Metadata): Promise<string> => {
console.log('๐Ÿ”— Start deploying the following item: ', item.name);
const { maxFeePerGas, maxPriorityFeePerGas } =
await ethers.provider.getFeeData();
const CONTRACT_NAME = 'DMATLSP7';
const customToken = await ethers.getContractFactory(CONTRACT_NAME);
console.log(`๐Ÿ”— Deploying contract (${item.name})...`);
let Token;
const symbol = item.name.replaceAll(' ', '');
// royalties:
const royaltiesForThisContract = ROYALTIES.find(
({ name }) => name === item.name,
);
const transformedRoyalties = Object.entries(
royaltiesForThisContract.royalties,
).map(([address, percentage]) => [
LSP0_INTERFACE_ID,
address,
Math.floor(percentage * 100000),
]);
console.log('๐Ÿ”— Royalties: ', transformedRoyalties);
if (transformedRoyalties.length === 0) {
throw new Error('โ€ผ๏ธ Royalties are not set for this contract!');
}
try {
Token = await customToken.deploy(
item.name,
symbol,
1,
item.totalSupply,
transformedRoyalties,
{
maxFeePerGas,
maxPriorityFeePerGas,
type: 2,
},
);
} catch (e) {
console.error(`๐Ÿ”— Error when deploying ${item.name}`);
throw e;
}
const token = await Token.waitForDeployment();
const tokenAddress = await token.getAddress();
console.log(
`๐Ÿ”— LSP7 Capped Token "${item.name}" deployed to: ${tokenAddress}`,
);
await setLSP4Metadata(tokenAddress, item);
const creators = Object.keys(royaltiesForThisContract.royalties);
await setLSP4CreatorsMap(tokenAddress, creators);
for (const creator of creators) {
console.log(`๐Ÿ”— Setting LSP12IssuedAssets for creator: ${creator}`);
// TODO: change the key for every creator
await setLSP12IssuedAssets(creator, DMAT_UP_KEY, tokenAddress);
}
return tokenAddress;
};
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;
import '@lukso/lsp-smart-contracts/contracts/LSP7DigitalAsset/presets/LSP7Mintable.sol';
import {LSP7CappedSupply} from '@lukso/lsp-smart-contracts/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupply.sol';
import {LSP7DigitalAssetCore} from '@lukso/lsp-smart-contracts/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol';
struct RoyaltyInfo {
bytes4 interfaceId;
address recipient;
uint32 points;
}
contract DMATLSP7 is LSP7Mintable, LSP7CappedSupply {
// Constants for LSP18Royalties
bytes32 constant LSP18RoyaltiesRecipients =
0xc0569ca6c9180acc2c3590f36330a36ae19015a19f4e85c28a7631e3317e6b9d;
bytes32 constant LSP18RoyaltiesEnforcePayment =
0x580d62ad353782eca17b89e5900e7df3b13b6f4ca9bbc2f8af8bceb0c3d1ecc6;
constructor(
string memory tokenName,
string memory tokenSymbol,
uint256 lsp4TokenType_,
uint256 tokenSupplyCap_,
RoyaltyInfo[] memory royalties
)
LSP7Mintable(tokenName, tokenSymbol, msg.sender, lsp4TokenType_, true)
LSP7CappedSupply(tokenSupplyCap_)
{
// Encode all royalties into a single bytes array
bytes memory royaltiesData = encodeRoyalties(royalties);
// Set the encoded royalties data using ERC725Y setData function
_setData(LSP18RoyaltiesRecipients, royaltiesData);
_setData(LSP18RoyaltiesEnforcePayment, abi.encode(true));
}
function _mint(
address to,
uint256 amount,
bool allowNonLSP1Recipient,
bytes memory data
) internal virtual override(LSP7DigitalAssetCore, LSP7CappedSupply) {
super._mint(to, amount, allowNonLSP1Recipient, data);
}
function encodeRoyalties(
RoyaltyInfo[] memory royalties
) internal pure returns (bytes memory) {
bytes memory royaltiesData = '';
for (uint i = 0; i < royalties.length; i++) {
// Encode each royalty entry
bytes memory encodedEntry = abi.encodePacked(
royalties[i].interfaceId,
royalties[i].recipient,
royalties[i].points
);
royaltiesData = abi.encodePacked(royaltiesData, encodedEntry);
}
return royaltiesData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment