Skip to content

Commit

Permalink
Use getMainAddress in /wallet/X
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceManiac committed May 1, 2024
1 parent ff0c267 commit 7692a6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type Player,
getNftOwner,
} from '@paima/utils-backend';
import { getAchievementTypes, getAchievementProgress } from '@paima/db';
import { getAchievementTypes, getAchievementProgress, getMainAddress } from '@paima/db';

// ----------------------------------------------------------------------------
// Controller and routes per PRC-1
Expand Down Expand Up @@ -60,10 +60,16 @@ export class AchievementsController extends Controller {
/** Comma-separated list. */
@Query() name?: string
): Promise<PlayerAchievements> {
const names = name ? name.split(',') : ['*'];
const player: Player = { wallet }; // TODO: walletType, userId, userName

const db = EngineService.INSTANCE.getSM().getReadonlyDbConn();
const { address, id } = await getMainAddress(wallet, db);

const player: Player = {
wallet: address,
userId: String(id),
// TODO: walletType, userName
};

const names = name ? name.split(',') : ['*'];
const rows = await getAchievementProgress.run({ wallet, names }, db);

this.setHeader('Content-Language', 'en');
Expand Down Expand Up @@ -100,14 +106,15 @@ export class AchievementsController extends Controller {
const wallet = await getNftOwner(db, cde, BigInt(token_id));
if (wallet) {
return await this.wallet(wallet, name);
} else {
// TODO: throw a different error if no CDE with that name exists
this.setStatus(404);
throw new Error('No owner for that NFT');
}
break;
case 'erc6551':
// TODO
break;
// Future expansion: erc6551
default:
this.setStatus(404);
throw new Error(`No support for /erc/${erc}`);
}

this.setStatus(404);
throw new Error('Not found');
}
}
3 changes: 2 additions & 1 deletion packages/node-sdk/paima-db/src/delegate-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getMainAddressFromAddress,
} from './sql/wallet-delegation.queries.js';
import type { PoolClient, Notification, Client } from 'pg';
import type { IDatabaseConnection } from '@pgtyped/runtime/lib/tag';

export type WalletDelegate = { address: string; id: number };
export const NO_USER_ID = -1;
Expand All @@ -26,7 +27,7 @@ export const addressCache = new Map<string, WalletDelegate>();
// If wallet does not exist, It will NOT be created in address table.
export async function getMainAddress(
_address: string,
DBConn: PoolClient
DBConn: IDatabaseConnection
): Promise<WalletDelegate> {
const address = _address.toLocaleLowerCase();
let addressMapping: WalletDelegate | undefined = addressCache.get(address);
Expand Down

0 comments on commit 7692a6c

Please sign in to comment.