Skip to content

Instantly share code, notes, and snippets.

@PFC-developer
Created December 5, 2021 00:12
Show Gist options
  • Save PFC-developer/07c55d9f8fea19ddf8fbb09a94431095 to your computer and use it in GitHub Desktop.
Save PFC-developer/07c55d9f8fea19ddf8fbb09a94431095 to your computer and use it in GitHub Desktop.
getOwnedTokens
async function getOwnedTokens(
client: SigningCosmWasmClient,
account: Account
): Promise<string[]> {
const PLAYER_CONTRACT = process.env.REACT_APP_PLAYER_NFT as string;
return client.getChainId().then((chainId) => {
return gen_signature(client, chainId, account).then((signatures) => {
return tokens_for_owner_private(
client,
PLAYER_CONTRACT,
account,
chainId,
signatures
).then((tokens) => {
console.log(tokens);
return tokens;
});
});
});
}
export function gen_signature(
client: SigningCosmWasmClient,
chainId: string,
account: Account
): Promise<readonly StdSignature[]> {
const msgs: Msg[] = [
{
type: "query_permit", // Must be "query_permit"
value: {
permit_name: permitName,
allowed_tokens: allowedTokens,
permissions: permissions,
},
},
];
const fee: StdFee = {
amount: [{ denom: "uscrt", amount: "0" }], // Must be 0 uscrt
gas: "1", // Must be 1
};
return client.signAdapter(msgs, fee, chainId, "", 0, 0).then((tx) => {
return tx.signatures;
});
}
export function tokens_for_owner_private(
client: SigningCosmWasmClient,
contractAddress: string,
accountAddress: Account,
chainId: string,
signatures: readonly StdSignature[]
): Promise<string[]> {
const queryMsg = {
with_permit: {
query: {
tokens: {
owner: accountAddress,
},
},
permit: {
permit_name: permitName,
allowed_tokens: allowedTokens,
chain_id: chainId,
permissions: permissions,
},
signatures: signatures,
},
};
return client.queryContractSmart(contractAddress, queryMsg).then((result) => {
console.log(result);
if (result.token_list && result.token_list.tokens) {
return result.token_list.tokens;
} else {
return [];
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment