-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rooms nft #125
base: master
Are you sure you want to change the base?
Rooms nft #125
Conversation
tanerdurmaz
commented
Aug 29, 2021
- Room ownership changed, now it is nft based
- I used some gas price and gaslimit but not sure how clever was it.
const HDWalletProvider = require("@truffle/hdwallet-provider"); | ||
const web3 = require( 'web3'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should use import instead of require whenever possible in typescript projects for proper type checking
might have to install @types/... to get it working
@@ -72,8 +98,54 @@ roomRouter.post("/:roomId", async (req, res) => { | |||
|
|||
await collection | |||
.doc(roomId) | |||
.set({ name: roomId, isLocked, lockedOwnerAddress: address ?? undefined , contractAddress: contractAddress ?? undefined}); | |||
.set({ name: roomId, isLocked, lockedOwnerAddress: "dynamic" , contractAddress: contractAddress ?? undefined}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if lockedOwnerAddress is dynamic every time from now on, do we need to set it as dynamic? Seems redundant
.set({ name: roomId, isLocked, lockedOwnerAddress: "dynamic" , contractAddress: contractAddress ?? undefined}); | ||
|
||
// get token count then add new metadata to database using it | ||
const tokenCount = await nftRooms.doc("token-count").get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't want to store this in the database, the database can get out of sync for whatever reason. We can instead query the blockchain directly for token balance using total supply https://docs.openzeppelin.com/contracts/3.x/api/token/erc721#IERC721Enumerable-totalSupply--
await nftRooms | ||
.doc(tokenId.toString()) | ||
.set({ name: roomId }); | ||
|
||
await nftRooms | ||
.doc("token-count") | ||
.set({ count: tokenId}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need nftRooms? Why not store the data in the room itself in the chatrooms collection?
const provider = new HDWalletProvider(MNEMONIC, "https://rpc-mainnet.maticvigil.com/v1/41b386d43bd3cfdf37a0fdef86b801ef9836fa7b"); | ||
const web3Instance = new web3(provider); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since provider and web3Instance don't change, these should be declared outside the function instead of being redeclared each time the function is called
const nftContract = new web3Instance.eth.Contract( | ||
NFT_ABI, | ||
NFT_CONTRACT_ADDRESS, | ||
{ gasLimit: "1000000", | ||
gasPrice: 15000000000 | ||
} | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be declared outside the function too since its value doesn't change whenever you recall the function
|
||
const reciever = address ? address : OWNER_ADDRESS; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
receiver not reciever
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, you can just do const receiver = address || OWNER_ADDRESS
} catch (e) { | ||
await nftRooms | ||
.doc("token-count") | ||
.set({ count: parseInt(tokenCount.data()!.count) - 1 }); | ||
console.log(e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we shouldn't need to manually keep track of count since we can query the blockchain at any time for token supply. If we want to do this, you should increase token count on success instead of always increasing token count and decreasing on failure
await axios.get('https://api.covalenthq.com/v1/137/address/' + address + '/balances_v2/?nft=true&key=ckey_c35e2c388e1b4efea8490fb8c83').then( async (result) => { | ||
let permission = false; | ||
for(let i = 0; i < result.data.data.items.length; i++){ | ||
if(NFT_CONTRACT_ADDRESS!.toLowerCase() === result.data.data.items[i].contract_address.toLowerCase()){ | ||
if(result.data.data.items[i].nft_data){ | ||
for(let j = 0; j < result.data.data.items[i].nft_data.length; j++){ | ||
const doc = await nftRooms.doc(result.data.data.items[i].nft_data[j].token_id).get(); | ||
const name = await doc.get("name"); | ||
if(name === roomId){ | ||
permission = true; | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if this were a separate method with a clear name explaining what it's doing. Currently it's very hard to read and understand what's happening, especially with nested for loops and very nested objects like result.data.data.items[i].nft_data
I'm not actually sure what this function is doing, can you explain it?