Skip to content

Commit

Permalink
feat: add endpoint to get current proof by name or fid (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayprabhu authored Jul 24, 2023
1 parent 31ecb8a commit 0035899
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { log } from './log.js';
import { generateCCIPSignature, signer, signerAddress } from './signature.js';
import {
createTransfer,
getCurrentUsername,
getLatestTransfer,
getTransferById,
getTransferHistory,
Expand Down Expand Up @@ -63,6 +64,25 @@ app.get('/transfers', async (req, res) => {
res.send({ transfers });
});

app.get('/transfers/current', async (req, res) => {
let name: string | undefined;
if (req.query.fid) {
name = await getCurrentUsername(parseInt(req.query.fid.toString()), db);
} else if (req.query.name) {
name = req.query.name.toString();
}
if (!name || name === '') {
res.status(404).send({ error: 'Could not resolve current name' }).end();
return;
}
const transfer = await getLatestTransfer(name, db);
if (!transfer || transfer.to === 0) {
res.status(404).send({ error: 'No transfer found' }).end();
return;
}
res.send({ transfer });
});

app.post('/transfers', async (req, res) => {
let tr;
try {
Expand Down
35 changes: 35 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,41 @@ describe('app', () => {
});
});

describe('get current transfer', () => {
test('returns error for unknown name', async () => {
let response = await request(app).get('/transfers/current?name=nonexistent');
expect(response.status).toBe(404);

// Name was burned
response = await request(app).get('/transfers/current?name=test3');
expect(response.status).toBe(404);
});
test('returns error for unknown fid', async () => {
let response = await request(app).get('/transfers/current?fid=129837123');
expect(response.status).toBe(404);

// Name was burned
response = await request(app).get('/transfers/current?fid=3');
expect(response.status).toBe(404);
});
test('returns error if no name or fid provided', async () => {
const response = await request(app).get('/transfers/current');
expect(response.status).toBe(404);
});
test('returns latest transfer for fid', async () => {
await createTestTransfer(db, { username: 'test-current', from: 0, to: 3, timestamp: now + 3 });
const response = await request(app).get('/transfers/current?fid=3');
expect(response.status).toBe(200);
expect(response.body.transfer).toMatchObject({ username: 'test-current', from: 0, to: 3, timestamp: now + 3 });
});
test('returns latest transfer for name', async () => {
await createTestTransfer(db, { username: 'test3', from: 0, to: 10, timestamp: now + 3 });
const response = await request(app).get('/transfers/current?name=test3');
expect(response.status).toBe(200);
expect(response.body.transfer).toMatchObject({ username: 'test3', from: 0, to: 10, timestamp: now + 3 });
});
});

describe('create transfer', () => {
const now = currentTimestamp();

Expand Down

0 comments on commit 0035899

Please sign in to comment.