Skip to content
This repository was archived by the owner on Feb 8, 2025. It is now read-only.

Commit 536d7ec

Browse files
authored
Merge pull request #284 from zallo-labs:Z-333-simplified-auth
Z-333-simplified-auth
2 parents 9d44b40 + 4f74875 commit 536d7ec

30 files changed

+726
-676
lines changed

api/dbschema/edgeql-js/__spec__.ts

+72-72
Large diffs are not rendered by default.

api/dbschema/edgeql-js/modules/default.ts

+86-85
Large diffs are not rendered by default.

api/dbschema/edgeql-js/modules/schema.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -704,27 +704,27 @@ export type $ObjectTypeλShape = $.typeutil.flatten<$SourceλShape & Omit<$Consi
704704
"<__type__[is Token]": $.LinkDesc<_default.$Token, $.Cardinality.Many, {}, false, false, false, false>;
705705
"<__type__[is Approver]": $.LinkDesc<_default.$Approver, $.Cardinality.Many, {}, false, false, false, false>;
706706
"<__type__[is current_approver]": $.LinkDesc<_default.$current_approver, $.Cardinality.Many, {}, false, false, false, false>;
707-
"<__type__[is OptimisticSuccess]": $.LinkDesc<_default.$OptimisticSuccess, $.Cardinality.Many, {}, false, false, false, false>;
708707
"<__type__[is User]": $.LinkDesc<_default.$User, $.Cardinality.Many, {}, false, false, false, false>;
709708
"<__type__[is current_user]": $.LinkDesc<_default.$current_user, $.Cardinality.Many, {}, false, false, false, false>;
710709
"<__type__[is Contact]": $.LinkDesc<_default.$Contact, $.Cardinality.Many, {}, false, false, false, false>;
711710
"<__type__[is GlobalLabel]": $.LinkDesc<_default.$GlobalLabel, $.Cardinality.Many, {}, false, false, false, false>;
712711
"<__type__[is Event]": $.LinkDesc<_default.$Event, $.Cardinality.Many, {}, false, false, false, false>;
713712
"<__type__[is Transferlike]": $.LinkDesc<_default.$Transferlike, $.Cardinality.Many, {}, false, false, false, false>;
714713
"<__type__[is Transfer]": $.LinkDesc<_default.$Transfer, $.Cardinality.Many, {}, false, false, false, false>;
714+
"<__type__[is Contract]": $.LinkDesc<_default.$Contract, $.Cardinality.Many, {}, false, false, false, false>;
715715
"<__type__[is TransferApproval]": $.LinkDesc<_default.$TransferApproval, $.Cardinality.Many, {}, false, false, false, false>;
716716
"<__type__[is Rejection]": $.LinkDesc<_default.$Rejection, $.Cardinality.Many, {}, false, false, false, false>;
717717
"<__type__[is ActionFunction]": $.LinkDesc<_default.$ActionFunction, $.Cardinality.Many, {}, false, false, false, false>;
718718
"<__type__[is Action]": $.LinkDesc<_default.$Action, $.Cardinality.Many, {}, false, false, false, false>;
719719
"<__type__[is TransferLimit]": $.LinkDesc<_default.$TransferLimit, $.Cardinality.Many, {}, false, false, false, false>;
720720
"<__type__[is TransfersConfig]": $.LinkDesc<_default.$TransfersConfig, $.Cardinality.Many, {}, false, false, false, false>;
721721
"<__type__[is PolicyState]": $.LinkDesc<_default.$PolicyState, $.Cardinality.Many, {}, false, false, false, false>;
722-
"<__type__[is Contract]": $.LinkDesc<_default.$Contract, $.Cardinality.Many, {}, false, false, false, false>;
723722
"<__type__[is Policy]": $.LinkDesc<_default.$Policy, $.Cardinality.Many, {}, false, false, false, false>;
724723
"<__type__[is RemovedPolicy]": $.LinkDesc<_default.$RemovedPolicy, $.Cardinality.Many, {}, false, false, false, false>;
724+
"<__type__[is Failure]": $.LinkDesc<_default.$Failure, $.Cardinality.Many, {}, false, false, false, false>;
725725
"<__type__[is Result]": $.LinkDesc<_default.$Result, $.Cardinality.Many, {}, false, false, false, false>;
726726
"<__type__[is Confirmed]": $.LinkDesc<_default.$Confirmed, $.Cardinality.Many, {}, false, false, false, false>;
727-
"<__type__[is Failure]": $.LinkDesc<_default.$Failure, $.Cardinality.Many, {}, false, false, false, false>;
727+
"<__type__[is OptimisticSuccess]": $.LinkDesc<_default.$OptimisticSuccess, $.Cardinality.Many, {}, false, false, false, false>;
728728
"<__type__[is ConfirmedFailure]": $.LinkDesc<_default.$ConfirmedFailure, $.Cardinality.Many, {}, false, false, false, false>;
729729
"<__type__[is Success]": $.LinkDesc<_default.$Success, $.Cardinality.Many, {}, false, false, false, false>;
730730
"<__type__[is ConfirmedSuccess]": $.LinkDesc<_default.$ConfirmedSuccess, $.Cardinality.Many, {}, false, false, false, false>;

api/dbschema/events.esdl

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ module default {
1111
rewrite insert, update using (exists __subject__.result);
1212
};
1313
required confirmed: bool {
14-
default := true;
15-
rewrite insert, update using ((__subject__.result is Confirmed) ?? true);
14+
# rewrite insert, update using ((__subject__.result is Confirmed) ?? true);
1615
}
1716

1817
access policy members_can_select
1918
allow select
2019
using (is_member(.account));
20+
21+
constraint exclusive on ((.account, .block, .logIndex)) except (not .confirmed);
2122
}
2223

2324
abstract type Transferlike extending Event {
@@ -40,8 +41,6 @@ module default {
4041
allow select, insert
4142
using (is_member(.account));
4243

43-
constraint exclusive on ((.account, .block, .logIndex, .systxHash, .result));
44-
4544
index on ((.tokenAddress, .confirmed, .spentBy));
4645
}
4746

api/dbschema/interfaces.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ export namespace $default {
170170
"systxHash"?: string | null;
171171
"timestamp": Date;
172172
"result"?: Result | null;
173-
"confirmed": boolean;
174173
"internal": boolean;
174+
"confirmed": boolean;
175175
}
176176
export interface Function extends std.$Object {
177177
"selector": string;
@@ -281,8 +281,8 @@ export namespace $default {
281281
"systx"?: SystemTx | null;
282282
"results": Result[];
283283
"systxs": SystemTx[];
284-
"status": TransactionStatus;
285284
"executable": boolean;
285+
"status": TransactionStatus;
286286
}
287287
export type TransactionStatus = "Pending" | "Scheduled" | "Executing" | "Successful" | "Failed" | "Cancelled";
288288
export interface Transferlike extends Event {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
CREATE MIGRATION m17qamgbzvrq5axhdxhdn526i3wz42l5yt3fjdgog73ihlovmrb2sq
2+
ONTO m15nf2s2pczdhpncvwih5festgbvzushr5qfunppqaqahdonqo3nba
3+
{
4+
ALTER TYPE default::Event {
5+
ALTER PROPERTY confirmed {
6+
RESET default;
7+
DROP REWRITE
8+
INSERT ;
9+
DROP REWRITE
10+
UPDATE ;
11+
};
12+
CREATE CONSTRAINT std::exclusive ON ((.account, .block, .logIndex)) EXCEPT (NOT (.confirmed));
13+
};
14+
ALTER TYPE default::Transaction {
15+
ALTER PROPERTY status {
16+
USING (WITH
17+
result :=
18+
.result
19+
SELECT
20+
std::assert_exists((default::TransactionStatus.Pending IF (((result IS default::SimulatedSuccess) OR (result IS default::SimulatedFailure)) ?? true) ELSE (default::TransactionStatus.Executing IF (result IS default::OptimisticSuccess) ELSE (default::TransactionStatus.Successful IF (result IS default::ConfirmedSuccess) ELSE (default::TransactionStatus.Failed IF (result IS default::ConfirmedFailure) ELSE (default::TransactionStatus.Scheduled IF NOT (result[IS default::Scheduled].cancelled) ELSE default::TransactionStatus.Cancelled))))))
21+
);
22+
};
23+
};
24+
ALTER TYPE default::Transferlike {
25+
DROP CONSTRAINT std::exclusive ON ((.account, .block, .logIndex, .systxHash, .result));
26+
};
27+
};

api/dbschema/transaction.esdl

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module default {
3636
required property status := (
3737
with result := .result
3838
select assert_exists((
39-
TransactionStatus.Pending if (not exists result or result is SimulatedSuccess or result is SimulatedFailure) else
39+
TransactionStatus.Pending if ((result is SimulatedSuccess or result is SimulatedFailure) ?? true) else
4040
TransactionStatus.Executing if (result is OptimisticSuccess) else
4141
TransactionStatus.Successful if (result is ConfirmedSuccess) else
4242
TransactionStatus.Failed if (result is ConfirmedFailure) else

api/schema.graphql

+5-4
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ type ConfirmedFailure implements Confirmed & Failure & Node & Result {
186186
gasUsed: BigInt!
187187
id: ID!
188188
networkEthFee: Decimal!
189-
reason: String!
189+
reason: String
190190
response: Bytes!
191191
systemTx: SystemTx
192192
timestamp: DateTime!
@@ -309,7 +309,7 @@ interface Failure implements Node & Result {
309309
events: [Event!]!
310310
gasUsed: BigInt!
311311
id: ID!
312-
reason: String!
312+
reason: String
313313
response: Bytes!
314314
systemTx: SystemTx
315315
timestamp: DateTime!
@@ -731,19 +731,20 @@ type SimulatedFailure implements Failure & Node & Result {
731731
events: [Event!]!
732732
gasUsed: BigInt!
733733
id: ID!
734-
reason: String!
734+
reason: String
735735
response: Bytes!
736736
systemTx: SystemTx
737737
timestamp: DateTime!
738738
transaction: Transaction!
739739
transfers: [Transfer!]!
740+
validationErrors: [String!]!
740741
}
741742

742743
type SimulatedSuccess implements Node & Result & Success {
743744
events: [Event!]!
744745
gasUsed: BigInt!
745746
id: ID!
746-
reason: String!
747+
reason: String
747748
response: Bytes!
748749
systemTx: SystemTx
749750
timestamp: DateTime!

api/src/feat/faucet/faucet.service.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { BalancesService } from '~/core/balances/balances.service';
99
import { and } from '~/core/database';
1010
import { CHAINS } from 'chains';
1111
import { getUserCtx } from '~/core/context';
12+
import { InjectRedis } from '@songkeys/nestjs-redis';
13+
import { Redis } from 'ioredis';
14+
15+
const COOLDOWN = 3600; // 1 hour
1216

1317
@Injectable()
1418
export class FaucetService implements OnModuleInit {
@@ -18,6 +22,7 @@ export class FaucetService implements OnModuleInit {
1822
private networks: NetworksService,
1923
private db: DatabaseService,
2024
private balances: BalancesService,
25+
@InjectRedis() private redis: Redis,
2126
) {}
2227

2328
async onModuleInit() {
@@ -42,11 +47,14 @@ export class FaucetService implements OnModuleInit {
4247

4348
this.tokens = tokens.map((t) => ({
4449
address: asUAddress(t.address),
45-
amount: parseUnits(isEthToken(asAddress(t.address)) ? '0.005' : '1', t.decimals),
50+
amount: parseUnits(isEthToken(asAddress(t.address)) ? '0.01' : '1', t.decimals),
4651
}));
4752
}
4853

4954
async requestableTokens(account: UAddress): Promise<Address[]> {
55+
const alreadyRequested = await this.redis.get(alreadyUsedKey(account));
56+
if (alreadyRequested) return [];
57+
5058
return (await this.getTokensToSend(account)).map((token) => token.address);
5159
}
5260

@@ -73,6 +81,8 @@ export class FaucetService implements OnModuleInit {
7381
);
7482
});
7583

84+
await this.redis.set(alreadyUsedKey(account), 'true', 'EX', COOLDOWN);
85+
7686
return tokensToSend.map((t) => t.address);
7787
}
7888

@@ -99,3 +109,7 @@ export class FaucetService implements OnModuleInit {
99109
).map((t) => ({ ...t, address: asAddress(t.address) }));
100110
}
101111
}
112+
113+
function alreadyUsedKey(account: UAddress) {
114+
return `faucet:already-used:${account}`;
115+
}

api/src/feat/simulations/simulations.worker.ts

-2
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,6 @@ function encodeEvent<
329329
return {
330330
topics: encodeEventTopics(parameters).filter(Boolean) as [Hex, ...Hex[]],
331331
data: encodeEventData(parameters),
332-
// eventName: parameters.eventName,
333-
// args: parameters.args,
334332
blockHash: null,
335333
blockNumber: null,
336334
transactionHash: null,

api/src/feat/system-txs/results.model.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ export class Success extends Result {}
3939

4040
@NodeInterface({ implements: [Result] })
4141
export class Failure extends Result {
42-
@Field(() => String)
43-
reason: string;
42+
@Field(() => String, { nullable: true })
43+
reason?: string;
4444
}
4545

4646
@NodeType({ implements: [Result, Success] })
4747
export class SimulatedSuccess extends Failure {}
4848

4949
@NodeType({ implements: [Result, Failure] })
50-
export class SimulatedFailure extends Failure {}
50+
export class SimulatedFailure extends Failure {
51+
@Field(() => [String])
52+
validationErrors: string[];
53+
}
5154

5255
@NodeType({ implements: [Result, Success] })
5356
export class OptimisticSuccess extends Success {}

api/src/feat/transfers/transfers.events.ts

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class TransfersEvents {
8383
logIndex: event.logIndex,
8484
block: event.block,
8585
timestamp: event.timestamp,
86+
confirmed: e.op(e.op('exists', result.is(e.Confirmed)), '??', true),
8687
from: localFrom,
8788
to: localTo,
8889
tokenAddress: token,
@@ -175,6 +176,7 @@ export class TransfersEvents {
175176
logIndex: event.logIndex,
176177
block: event.block,
177178
timestamp: event.timestamp,
179+
confirmed: e.op(e.op('exists', result.is(e.Confirmed)), '??', true),
178180
from: localFrom,
179181
to: localTo,
180182
tokenAddress: token,

api/src/feat/transfers/transfers.service.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import e from '~/edgeql-js';
2020
import { v1 as uuidv1 } from 'uuid';
2121
import { InsertShape } from '~/edgeql-js/insert';
2222
import { $Transfer } from '~/edgeql-js/modules/default';
23-
import { zeroHash } from 'viem';
2423

2524
describe(TransfersService.name, () => {
2625
let service: TransfersService;
@@ -71,6 +70,7 @@ describe(TransfersService.name, () => {
7170
internal: false,
7271
logIndex: 0,
7372
block: BigInt(Math.floor(Math.random() * 1000)),
73+
confirmed: true,
7474
from: ZERO_ADDR,
7575
to: asAddress(account.address),
7676
tokenAddress: asUAddress(ETH_ADDRESS, asChain(account.address)),

0 commit comments

Comments
 (0)