Skip to content

Commit

Permalink
Fix soon claim with multiple addresses
Browse files Browse the repository at this point in the history
Fixes

Fixes
  • Loading branch information
Boldizsar Mezei committed May 10, 2024
1 parent 6a7597a commit b330aab
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 70 deletions.
113 changes: 57 additions & 56 deletions .github/workflows/functions_tangle-unit-tests.yml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/database/src/pg/interfaces/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class ICollection<T, Q extends BaseRecord, U extends Update> {
value: Q[F] | undefined | null,
) => this.createQuery().where(fieldPath, operator, value);

whereIn = <F extends keyof Q>(fieldPath: F, value: Q[F][]): IQuery<T, Q> =>
this.createQuery().whereIn(fieldPath, value);

update = async <F extends keyof Q>(data: U, where: Record<F, Q[F]>) => {
await this.con(this.table).update(undefinedToNull(data)).where(where);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ValidatedAddress,
WenError,
} from '@buildcore/interfaces';
import { get, isEmpty } from 'lodash';
import { get, isEmpty, set } from 'lodash';
import { getOutputMetadata } from '../../../utils/basic-output.utils';
import { invalidArgument } from '../../../utils/error.utils';
import { logger } from '../../../utils/logger';
Expand Down Expand Up @@ -74,16 +74,16 @@ export class TangleRequestService extends BaseTangleService<TangleResponse> {
if (!payment) {
payment = await this.transactionService.createPayment({ ...order, member: owner }, match);
}
this.transactionService.createTangleCredit(
payment,
match,
{
status: 'error',
code: get(error, 'eCode', 1000),
message: get(error, 'eKey', 'none'),
},
tranEntry.outputId!,
);

const response = {
status: 'error',
code: get(error, 'eCode', 1000),
message: get(error, 'eKey', 'none'),
};
if (get(error, 'eMessage', '')) {
set(response, 'exp', get(error, 'eMessage', ''));
}
this.transactionService.createTangleCredit(payment, match, response, tranEntry.outputId!);
}

return {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { TangleRequestType, VerifyEthForB5TangleRequest } from '@buildcore/interfaces';
import { CommonJoi, toJoiObject } from '../../../joi/common';
import Joi from 'joi';
import { toJoiObject } from '../../../joi/common';
import { baseTangleSchema } from '../common';

const maxAddressLength = 10 * 255;
export const verifyEthForB5TangleSchema = toJoiObject<VerifyEthForB5TangleRequest>({
...baseTangleSchema(TangleRequestType.VERIFY_ETH_ADDRESS),
ethAddress: CommonJoi.uid().description('Ethereum address.'),

ethAddress: Joi.string()
.regex(/^[a-zA-Z0-9,]+$/)
.max(maxAddressLength)
.lowercase()
.description('Ethereum address.')
.required(),
})
.description('Tangle request object to verify Ethereum address.')
.meta({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { database } from '@buildcore/database';
import { COL, TangleResponse } from '@buildcore/interfaces';
import axios from 'axios';
import { logger } from '../../../../utils/logger';
import { assertValidationAsync } from '../../../../utils/schema.utils';
import { BaseTangleService, HandlerParams } from '../../base';
import { Action } from '../../transaction-service';
Expand Down Expand Up @@ -42,7 +43,12 @@ const getFidForEth = async (ethAddress: string) =>
axios(`https://api.neynar.com/v2/farcaster/user/bulk-by-address`, {
params: { addresses: ethAddress },
headers: { api_key: process.env.NEYNAR_API_KEY },
}).then((r) => r.data[ethAddress]?.[0]?.fid);
})
.then((r) => r.data[ethAddress]?.[0]?.fid)
.catch((error) => {
logger.error('getFidForEth', error);
return 0;
});

const isUserFollowingChannel = async (fid: number) =>
fetch(`https://api.neynar.com/v2/farcaster/channel?id=justbuild&viewer_fid=${fid}`, {
Expand Down
83 changes: 83 additions & 0 deletions packages/functions/test-tangle/soon_snap/soon.snap.claim.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { database } from '@buildcore/database';
import {
COL,
MIN_IOTA_AMOUNT,
Network,
TangleRequestType,
Transaction,
} from '@buildcore/interfaces';
import { getRandomEthAddress } from '../../src/utils/wallet.utils';
import { wait } from '../../test/controls/common';
import { getWallet } from '../../test/set-up';
import { getTangleOrder } from '../common';
import { requestFundsFromFaucet } from '../faucet';

describe('Soon snapshot claim', () => {
let tangleOrder: Transaction;

beforeAll(async () => {
tangleOrder = await getTangleOrder(Network.RMS);
});

it('Claim soon snap with single eth address', async () => {
const wallet = await getWallet(Network.RMS);
const address = await wallet.getNewIotaAddressDetails();

await requestFundsFromFaucet(Network.RMS, address.bech32, MIN_IOTA_AMOUNT);

const ethAddress = ['0x69252ebdc3b77624c6e640a81a086aaa720734d3'];

const soonSnaps = [
{ uid: address.bech32, count: 12, paidOut: 0, ethAddress: '', ethAddressVerified: false },
];
await database().getCon()(COL.SOON_SNAP).insert(soonSnaps);

await wallet.send(address, tangleOrder.payload.targetAddress!, MIN_IOTA_AMOUNT, {
customMetadata: {
request: {
requestType: TangleRequestType.VERIFY_ETH_ADDRESS,
ethAddress: ethAddress.join(','),
},
},
});

await wait(async () => {
const soonSnaps = await database().collection(COL.SOON_SNAP).whereIn('uid', ethAddress).get();
return soonSnaps.reduce((acc, act) => acc && act.ethAddressVerified, true);
});
});

it('Claim soon snap with multiple eth address', async () => {
const wallet = await getWallet(Network.RMS);
const address = await wallet.getNewIotaAddressDetails();

await requestFundsFromFaucet(Network.RMS, address.bech32, MIN_IOTA_AMOUNT);

const ethAddress = [
'0x69252ebdc3b77624c6e640a81a086aaa720734d3',
getRandomEthAddress(),
getRandomEthAddress(),
];

const soonSnaps = [
{ uid: address.bech32, count: 12, paidOut: 0, ethAddress: '', ethAddressVerified: false },
{ uid: ethAddress[1], count: 12, paidOut: 0, ethAddress: '', ethAddressVerified: false },
{ uid: ethAddress[2], count: 12, paidOut: 0, ethAddress: '', ethAddressVerified: false },
];
await database().getCon()(COL.SOON_SNAP).insert(soonSnaps);

await wallet.send(address, tangleOrder.payload.targetAddress!, MIN_IOTA_AMOUNT, {
customMetadata: {
request: {
requestType: TangleRequestType.VERIFY_ETH_ADDRESS,
ethAddress: ethAddress.join(','),
},
},
});

await wait(async () => {
const soonSnaps = await database().collection(COL.SOON_SNAP).whereIn('uid', ethAddress).get();
return soonSnaps.reduce((acc, act) => acc && act.ethAddressVerified, true);
});
});
});

0 comments on commit b330aab

Please sign in to comment.