Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Z 320 address details #292

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/src/feat/operations/operations.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Operation, OperationFunction } from './operations.model';
import { OperationsService } from './operations.service';
import { Shape } from '~/core/database';
import { asAddress, asHex } from 'lib';
import { getChain } from 'chains';
import { getChainConfig } from 'chains';

const functionDeps = {
to: true,
Expand All @@ -29,7 +29,7 @@ export class OperationsResolver {
value: deps.value ?? undefined,
data: asHex(deps.data),
},
getChain(deps['<unorderedOperations[is Transaction]']!.account.chain).key,
getChainConfig(deps['<unorderedOperations[is Transaction]']!.account.chain).key,
);
}
}
26 changes: 26 additions & 0 deletions api/src/feat/transfers/insert-transfer-approval.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
with accountAddress := <UAddress>$account,
localAccount := as_address(accountAddress),
account := (select Account filter .address = accountAddress),
resultId := <optional uuid>$result,
result := assert_single((select Result filter .id = resultId and .transaction.account = account) if exists resultId else {}),
from := <Address>$from,
to := <Address>$to,
transfer := (
insert TransferApproval {
account := account,
systxHash := <optional Bytes32>$systxHash,
result := result,
block := <bigint>$block,
logIndex := <uint32>$logIndex,
timestamp := <datetime>$timestamp,
confirmed := (result is Confirmed ?? true),
from := from,
to := to,
tokenAddress := <UAddress>$token,
amount := <decimal>$amount,
incoming := (to = localAccount),
outgoing := (from = localAccount),
isFeeTransfer := ((result.transaction.paymaster in {from, to}) ?? false)
} unless conflict
)
select transfer;
51 changes: 51 additions & 0 deletions api/src/feat/transfers/insert-transfer-approval.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// GENERATED by @edgedb/generate v0.5.4

import type {Executor} from "edgedb";

export type InsertTransferApprovalArgs = {
readonly "account": string;
readonly "result"?: string | null;
readonly "from": string;
readonly "to": string;
readonly "systxHash"?: string | null;
readonly "block": bigint;
readonly "logIndex": number;
readonly "timestamp": Date;
readonly "token": string;
readonly "amount": string;
};

export type InsertTransferApprovalReturns = {
"id": string;
} | null;

export function insertTransferApproval(client: Executor, args: InsertTransferApprovalArgs): Promise<InsertTransferApprovalReturns> {
return client.querySingle(`\
with accountAddress := <UAddress>$account,
localAccount := as_address(accountAddress),
account := (select Account filter .address = accountAddress),
resultId := <optional uuid>$result,
result := assert_single((select Result filter .id = resultId and .transaction.account = account) if exists resultId else {}),
from := <Address>$from,
to := <Address>$to,
transfer := (
insert TransferApproval {
account := account,
systxHash := <optional Bytes32>$systxHash,
result := result,
block := <bigint>$block,
logIndex := <uint32>$logIndex,
timestamp := <datetime>$timestamp,
confirmed := (result is Confirmed ?? true),
from := from,
to := to,
tokenAddress := <UAddress>$token,
amount := <decimal>$amount,
incoming := (to = localAccount),
outgoing := (from = localAccount),
isFeeTransfer := ((result.transaction.paymaster in {from, to}) ?? false)
} unless conflict
)
select transfer;`, args);

}
31 changes: 31 additions & 0 deletions api/src/feat/transfers/insert-transfer.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
with accountAddress := <UAddress>$account,
localAccount := as_address(accountAddress),
account := (select Account filter .address = accountAddress),
resultId := <optional uuid>$result,
result := assert_single((select Result filter .id = resultId and .transaction.account = account) if exists resultId else {}),
from := <Address>$from,
to := <Address>$to,
transfer := (
insert Transfer {
account := account,
systxHash := <optional Bytes32>$systxHash,
result := result,
block := <bigint>$block,
logIndex := <uint32>$logIndex,
timestamp := <datetime>$timestamp,
confirmed := (result is Confirmed ?? true),
from := from,
to := to,
tokenAddress := <UAddress>$token,
amount := <decimal>$amount,
incoming := (to = localAccount),
outgoing := (from = localAccount),
isFeeTransfer := ((result.transaction.paymaster in {from, to}) ?? false)
} unless conflict
)
select transfer {
id,
internal,
isFeeTransfer,
accountUsers := .account.approvers.user.id
}
59 changes: 59 additions & 0 deletions api/src/feat/transfers/insert-transfer.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// GENERATED by @edgedb/generate v0.5.4

import type {Executor} from "edgedb";

export type InsertTransferArgs = {
readonly "account": string;
readonly "result"?: string | null;
readonly "from": string;
readonly "to": string;
readonly "systxHash"?: string | null;
readonly "block": bigint;
readonly "logIndex": number;
readonly "timestamp": Date;
readonly "token": string;
readonly "amount": string;
};

export type InsertTransferReturns = {
"id": string;
"internal": boolean;
"isFeeTransfer": boolean;
"accountUsers": Array<string>;
} | null;

export function insertTransfer(client: Executor, args: InsertTransferArgs): Promise<InsertTransferReturns> {
return client.querySingle(`\
with accountAddress := <UAddress>$account,
localAccount := as_address(accountAddress),
account := (select Account filter .address = accountAddress),
resultId := <optional uuid>$result,
result := assert_single((select Result filter .id = resultId and .transaction.account = account) if exists resultId else {}),
from := <Address>$from,
to := <Address>$to,
transfer := (
insert Transfer {
account := account,
systxHash := <optional Bytes32>$systxHash,
result := result,
block := <bigint>$block,
logIndex := <uint32>$logIndex,
timestamp := <datetime>$timestamp,
confirmed := (result is Confirmed ?? true),
from := from,
to := to,
tokenAddress := <UAddress>$token,
amount := <decimal>$amount,
incoming := (to = localAccount),
outgoing := (from = localAccount),
isFeeTransfer := ((result.transaction.paymaster in {from, to}) ?? false)
} unless conflict
)
select transfer {
id,
internal,
isFeeTransfer,
accountUsers := .account.approvers.user.id
}`, args);

}
125 changes: 30 additions & 95 deletions api/src/feat/transfers/transfers.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Injectable, Logger } from '@nestjs/common';
import { Address, UAddress, asAddress, asUAddress, isTruthy, ETH_ADDRESS, isEthToken } from 'lib';
import { ERC20 } from 'lib/dapps';
import { EventsService, OptimisticEvent, ConfirmedEvent } from '../events/events.service';
import { and, DatabaseService } from '~/core/database';
import { DatabaseService } from '~/core/database';
import e from '~/edgeql-js';
import { selectAccount } from '../accounts/accounts.util';
import { uuid } from 'edgedb/dist/codecs/ifaces';
import { EventPayload, PubsubService } from '~/core/pubsub/pubsub.service';
import { getAbiItem } from 'viem';
Expand All @@ -14,6 +13,8 @@ import { BalancesService } from '~/core/balances/balances.service';
import Decimal from 'decimal.js';
import { TokensService } from '~/feat/tokens/tokens.service';
import { ampli } from '~/util/ampli';
import { insertTransfer } from './insert-transfer.query';
import { insertTransferApproval } from './insert-transfer-approval.query';

export const transferTrigger = (account: UAddress) => `transfer.account.${account}`;
export interface TransferSubscriptionPayload extends EventPayload<'transfer'> {
Expand Down Expand Up @@ -66,55 +67,19 @@ export class TransfersEvents {

await Promise.all(
accounts.map(async (account) => {
const selectedAccount = selectAccount(account);
const result = event.result
? e.assert_single(
e.select(e.Result, (r) => ({
filter: and(
e.op(r.id, '=', e.uuid(event.result!)),
e.op(r.transaction.account, '=', selectedAccount),
),
})),
)
: e.cast(e.Result, e.set());

const transfer = await this.db.query(
e.select(
e
.insert(e.Transfer, {
account: selectedAccount,
systxHash: log.transactionHash,
result,
logIndex: event.logIndex,
block: event.block,
timestamp: event.timestamp,
confirmed: e.op(e.op('exists', result.is(e.Confirmed)), '??', true),
from: localFrom,
to: localTo,
tokenAddress: token,
amount:
from === to
? '0'
: to === account
? amount.toString()
: amount.negated().toString(),
incoming: account === to,
outgoing: account === from,
isFeeTransfer: e.op(
e.op(result.transaction.paymaster, 'in', e.set(localFrom, localTo)),
'??',
false,
),
})
.unlessConflict(),
(t) => ({
id: true,
internal: true,
isFeeTransfer: true,
accountUsers: t.account.approvers.user.id,
}),
),
);
const transfer = await this.db.exec(insertTransfer, {
account,
systxHash: log.transactionHash,
result: event.result,
block: event.block,
logIndex: event.logIndex,
timestamp: event.timestamp,
from: localFrom,
to: localTo,
token,
amount:
from === to ? '0' : to === account ? amount.toString() : amount.negated().toString(),
});
if (!transfer) return; // Already processed

this.balances.invalidate({ account, token });
Expand Down Expand Up @@ -167,50 +132,20 @@ export class TransfersEvents {

await Promise.all(
accounts.map(async (account) => {
const selectedAccount = selectAccount(account);
const result = event.result
? e.assert_single(
e.select(e.Result, (r) => ({
filter: and(
e.op(r.id, '=', e.uuid(event.result!)),
e.op(r.transaction.account, '=', selectedAccount),
),
})),
)
: e.cast(e.Result, e.set());

const approval = await this.db.query(
e
.insert(e.TransferApproval, {
account: selectedAccount,
systxHash: log.transactionHash,
result,
logIndex: event.logIndex,
block: event.block,
timestamp: event.timestamp,
confirmed: e.op(e.op('exists', result.is(e.Confirmed)), '??', true),
from: localFrom,
to: localTo,
tokenAddress: token,
amount:
from === to
? '0'
: to === account
? amount.toString()
: amount.negated().toString(),
incoming: account === to,
outgoing: account === from,
isFeeTransfer: e.op(
e.op(result.transaction.paymaster, 'in', e.set(localFrom, localTo)),
'??',
false,
),
})
.unlessConflict((t) => ({
on: e.tuple([t.account, t.block, t.logIndex]),
})),
);
if (!approval) return; // Already processed
const transfer = await this.db.exec(insertTransferApproval, {
account,
systxHash: log.transactionHash,
result: event.result,
block: event.block,
logIndex: event.logIndex,
timestamp: event.timestamp,
from: localFrom,
to: localTo,
token,
amount:
from === to ? '0' : to === account ? amount.toString() : amount.negated().toString(),
});
if (!transfer) return; // Already processed

if (to === account) {
this.log.debug(`Transfer approval ${token}: ${from} -> ${to}`);
Expand Down
Loading
Loading