Skip to content

Commit

Permalink
Merge branch 'ref/moralisUpdateDeprecatedEndpoint' of github.com:Gamb…
Browse files Browse the repository at this point in the history
…oster/bitcore
  • Loading branch information
kajoseph committed Oct 23, 2024
2 parents 7cc2cee + c916f86 commit 0ff433a
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions packages/bitcore-wallet-service/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7346,19 +7346,61 @@ export class WalletService implements IWalletService {
}

moralisGetTokenAllowance(req): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
const response = await Moralis.EvmApi.token.getTokenAllowance({
address: req.body.address,
chain: req.body.chain,
ownerAddress: req.body.ownerAddress,
spenderAddress: req.body.spenderAddress,
});
return new Promise((resolve, reject) => {
let keys, headers;

return resolve(response.raw ?? response);
} catch (err) {
reject(err);
if (!config.moralis) return reject(new Error('Moralis missing credentials'));
if (!checkRequired(req.body, ['address']) && !checkRequired(req.body, ['ownerAddress'])) {
return reject(new ClientError('moralisGetTokenAllowance request missing arguments'));
}

const walletAddress = req.body.ownerAddress ?? req.body.address;

let qs = [];
if (req.body.chain) qs.push('chain=' + req.body.chain);
if (req.body.cursor) qs.push('cursor=' + req.body.cursor);
if (req.body.limit) qs.push('limit=' + req.body.limit);

const URL: string = `https://deep-index.moralis.io/api/v2.2/wallets/${walletAddress}/approvals${qs.length > 0 ? '?' + qs.join('&') : ''}`

headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Api-Key': config.moralis.apiKey,
};

this.request.get(
URL,
{
headers,
json: true
},
(err, data) => {
if (err) {
return reject(err.body ?? err);
} else {
const { spenderAddress, ownerAddress, address } = req.body;

if (spenderAddress && ownerAddress) {
// Workaround to keep older versions running
const spendersList = data?.body?.result;

if (Array.isArray(spendersList)) {
const spenderData = spendersList.find(s =>
s.spender?.address?.toLowerCase() === spenderAddress.toLowerCase() &&
s.token?.address?.toLowerCase() === address.toLowerCase()
);

data.body = {
allowance: spenderData?.value ?? "0"
};
}
}

return resolve(data.body ?? data);
}
}
);
});
}

Expand Down

0 comments on commit 0ff433a

Please sign in to comment.