Hi,
I can’t use a variable with the same values in the getNFTs() options, this is because it expects:
chain?: "eth" | "0x1" | "ropsten" | "0x3" | "rinkeby" | "0x4" | "goerli" | "0x5" | "kovan" | "0x2a" | "polygon" | "0x89" | "mumbai" | "0x13881" | "bsc" | "0x38" | "bsc testnet" | "0x61" | "localdevchain" | "dev" | undefined;
But when I have a multichain application I would have to use a switch for each chain, whilst when being able to use a variable I can just update the value of the variable and not needing to use a switch statement. See below for example:
I can’t assign a variable to the options here since it needs a literal string. Of course if you update it to accept a String Object there could be errors when people don’t fill in the correct data, but I think in my opinion this would be a nice addition.
switch (networkName) {
case "eth": {
const userAddr = user?.get("ethAddress");
const fetchNFTs = async () => {
const response = await account.getNFTs({
chain: "eth",
address: userAddr,
});
const nftArray = response.result!;
nftArray.sort((a, b) =>
a.name > b.name ? 1 : b.name > a.name ? -1 : 0
);
let mapped: Map<string, NFT[]> = mapIt(nftArray);
setMappedItems(createMappingArray(mapped));
};
fetchNFTs();
break;
}
case "rinkeby": {
const userAddr = user?.get("ethAddress");
const fetchNFTs = async () => {
const response = await account.getNFTsForContract({
chain: "rinkeby",
address: userAddr,
});
const nftArray = response.result!;
nftArray.sort((a, b) =>
a.name > b.name ? 1 : b.name > a.name ? -1 : 0
);
let mapped: Map<string, NFT[]> = mapIt(nftArray);
setMappedItems(createMappingArray(mapped));
};
fetchNFTs();
break;
}
case "polygon": {
const userAddr = user?.get("ethAddress");
const fetchNFTs = async () => {
const response = await account.getNFTs({
chain: "polygon",
address: userAddr,
});
const nftArray = response.result!;
nftArray.sort((a, b) =>
a.name > b.name ? 1 : b.name > a.name ? -1 : 0
);
let mapped: Map<string, NFT[]> = mapIt(nftArray);
setMappedItems(createMappingArray(mapped));
};
fetchNFTs();
break;
}
}
For the current state I need to use 3 cases. While when I can just use a variable value:
const userAddr = user?.get("ethAddress");
const fetchNFTs = async () => {
const response = await account.getNFTs({
chain: chainName,
address: userAddr,
});
const nftArray = response.result!;
nftArray.sort((a, b) =>
a.name > b.name ? 1 : b.name > a.name ? -1 : 0
);
let mapped: Map<string, NFT[]> = mapIt(nftArray);
setMappedItems(createMappingArray(mapped));
};
fetchNFTs();
Notice the chain: chainName, I can use redux to assign this variable to a selector so it updates automatically when application state changes.
Don’t know if this update is possible but would be nice
Happy to know the answer