I am working on a web3 project. I have sanity as a backend, where I get contract addresses for different tokens.
// Requesting Data from sanity
async sanityCoins() {
try{
const coins =await fetch("https://i7ml51e6.api.sanity.io/v2021-10-21/data/query/production?query=*%5B_type%3D%3D'coins'%5D%7B%0A%20%20name%2C%0A%20%20symbol%2C%0A%20%20usdPrice%2C%0A%20%20ContractAddress%2C%0A%20%20logo%0A%7D")
const data = (await coins.json()).result;
console.log(data)
}
catch(err){
console.log(err)
}
}
The async function returns this
(3) [{…}, {…}, {…}]
0: {ContractAddress: '0xBbCd8c1b5993062F2761CF07e27C2adebc55766F', logo: {…}, name: 'Ethereum', symbol: 'ETH', usdPrice: '2,088'}
1: {ContractAddress: '0xb74FbCF3b2e6b29B0b86f005FCC80eC649de7E30', logo: {…}, name: 'Bitcon', symbol: 'BTC', usdPrice: '$30,947'}
2: {ContractAddress: '0x7a6DC36DD8CC525E15C2B14a415253153909fc24', logo: {…}, name: 'Solana', symbol: 'SOL', usdPrice: '52'}
length: 3
I want to pass these contract addresses as a parameter to the SDK . Where the SDK takes Address: string as a parameter(Thirdweb SDK -https://portal.thirdweb.com/typescript).
sdk = new ThirdwebSDK(
new ethers.Wallet(
environment.metamask_Key,
ethers.getDefaultProvider('https://rinkeby.infura.io/v3/94ef7ea56a0e4585834ffbb4dfb3f8b8'))
)
token = this.sdk.getToken("contract Address");//different contract addres goes here
async data() {
const tokens = await this.token.balanceOf("0x47444Bfc280Bd50e3a96ccFd031f7539d6B6E97A");//Wallet address
console.log(tokens);
try {
}
catch(err){
console.log(err)
}
}
token = this.sdk.getToken("contract Address"); I want to pass the contract address from sanity to the SDK here. Can this be done. I'm asking here as a last resort. I'm not sure how to assign async value to a variable and use it later("If that's how its done") . Every where I go I only see ReactJS guides for web3. I really don't know how to do this if it is possible.
The SDK still doesn't have a feature where it returns all the tokens for the connected wallet. I asked about this to the team on Discord.
Im not sure how to assign async value to a variable and use it later("If thats how its done")
Just assign data in a global variable and use it later.
async sanityCoins() {
try{
const coins =await fetch("https://i7ml51e6.api.sanity.io/v2021-10-21/data/query/production?query=*%5B_type%3D%3D'coins'%5D%7B%0A%20%20name%2C%0A%20%20symbol%2C%0A%20%20usdPrice%2C%0A%20%20ContractAddress%2C%0A%20%20logo%0A%7D")
const data = (await coins.json()).result;
console.log(data)
//assign data
this.coins = data;
}
catch(err){
console.log(err)
}}
If you want to call sdk to get tokens right after sanityCoins, return data in sanityCoins
async function sanityCoins() {
//...do the fetch, etc.
return data;
}
// get tokens by contractAddress
async function getTokens(contractAddress) {
const sdk = new ThirdwebSDK(...);
const token = sdk.getToken(contractAddress);
const tokens = await token.balanceOf(...);
return tokens;
}
//do where you want to get tokens
const coins = await sanityCoins();
const promises = coins.map(coin => getTokens(coin.ContractAddress));
//get all tokens of coins, tokensArray is like [tokens,tokens,...]
const tokensArray = await Promise.all(promises);