Created
February 14, 2024 15:13
-
-
Save monapasan/2c6206354cb7acf9549b6b02424e0177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ERC725 } from '@erc725/erc725.js'; | |
import { Contract, toBeHex } from 'ethers'; | |
import { ethers } from 'hardhat'; | |
import { Environment } from '../types'; | |
import { ENV_CONFIG } from '../constants'; | |
import { INTERFACE_IDS } from '@lukso/lsp-smart-contracts/dist/constants.cjs.js'; | |
import { keyManagerABI, universalProfileAbi } from '../../change-up-name/ABI'; | |
import { schemaLSP12IssuedAssets } from '../../../src/modules/blockchain/blockchain.schemas'; | |
const LSP7_INTERFACE_ID = INTERFACE_IDS.LSP7DigitalAsset; | |
export const setLSP12IssuedAssets = async ( | |
upAddress: string, | |
upKey: string, | |
asset: string, | |
) => { | |
const accounts = await ethers.getSigners(); | |
const deployer = accounts[0]; | |
console.log(upAddress, 'upAddress'); | |
const controllerAccount = new ethers.Wallet(upKey, deployer.provider); | |
console.log('Deploying contracts with EOA: ', controllerAccount.address); | |
const erc725 = new ERC725( | |
schemaLSP12IssuedAssets, | |
controllerAccount.address, | |
ENV_CONFIG[process.env.NODE_ENV as Environment].LPT.RPC_URL, | |
{ | |
ipfsGateway: process.env.IPFS_GATEWAY, | |
}, | |
); | |
const erc725Fetch = new ERC725( | |
schemaLSP12IssuedAssets, | |
upAddress, | |
ENV_CONFIG[process.env.NODE_ENV as Environment].LPT.RPC_URL, | |
); | |
const previousIssuedAssets = ( | |
await erc725Fetch.fetchData('LSP12IssuedAssets[]') | |
).value as string[]; | |
const encodedData = [ | |
{ | |
keyName: 'LSP12IssuedAssets[]', | |
value: [...previousIssuedAssets, asset], | |
}, | |
]; | |
const indexHex = toBeHex(previousIssuedAssets.length, 16); | |
const correctFormatValue = [LSP7_INTERFACE_ID, indexHex]; | |
// Preparing data for encoding the LSP12IssuedAssetsMap | |
const dataToEncode = { | |
keyName: `LSP12IssuedAssetsMap:<address>`, | |
dynamicKeyParts: asset, // Dynamic part of the key (creator's address) | |
value: correctFormatValue, // Assuming interface ID and index | |
}; | |
const encodedValueMap = LSP7_INTERFACE_ID + indexHex.substring(2); | |
encodedData.push(dataToEncode); | |
// Encoding data | |
const encoded = erc725.encodeData(encodedData); | |
// Setting the data using the contract instance | |
const myUniversalProfile = new Contract( | |
upAddress, | |
universalProfileAbi, | |
controllerAccount, | |
); | |
let setDataResult; | |
const keyManagerAddress = await myUniversalProfile.owner(); | |
console.log('KeyManagerAddress of the UP received', keyManagerAddress); | |
const keyManagerContract = new ethers.Contract( | |
keyManagerAddress, | |
keyManagerABI, | |
controllerAccount, | |
); | |
let abiPayload; | |
// there is bug with erc725 that encodes values of 16 bytes incorrectly | |
const values = encoded.values.map((value, index) => | |
index === encoded.values.length - 1 ? encodedValueMap : value, | |
); | |
try { | |
abiPayload = myUniversalProfile.interface.encodeFunctionData( | |
'setDataBatch', | |
[encoded.keys, values], | |
); | |
} catch (e) { | |
console.log('error when encoding function data', e); | |
} | |
// Execute via the KeyManager, passing the UP payload | |
const result = await keyManagerContract.execute(abiPayload, { | |
gasLimit: 300000, | |
}); | |
console.log( | |
`🗂 LSP12 issuers for the item ${upAddress} has been updated\n Transaction: ${result.hash}`, | |
); | |
return setDataResult; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment