Skip to content
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
72 changes: 62 additions & 10 deletions packages/dag4-network/src/api/v2/block-explorer-api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {RestApi} from '@stardust-collective/dag4-core';
import {DNC} from '../../DNC';
import { RestApi } from '@stardust-collective/dag4-core';
import { DNC } from '../../DNC';
import {
SnapshotV2,
TransactionV2,
RewardTransaction,
AddressBalanceV2,
ActionType,
ActionResponse,
AddressBalanceV2,
AllowSpendResponse,
BlockV2,
CurrencySnapshotV2
CurrencySnapshotV2,
RewardTransaction,
SnapshotV2,
TokenLockResponse,
TransactionV2
} from '../../dto/v2';

type HashOrOrdinal = string | number;
Expand Down Expand Up @@ -70,22 +74,34 @@ export class BlockExplorerV2Api {
limit = null,
searchAfter = null,
searchBefore = null,
next = null
next = null,
actionType = null,
active = false
} : {
limit?: number,
searchAfter?: string,
searchBefore?: string,
next?: string
next?: string,
actionType?: ActionType,
active?: boolean
}) {
let params;

if (limit || searchAfter || searchBefore || next) {
if (limit || searchAfter || searchBefore || next || active) {
params = {};

if (limit && limit > 0) {
params.limit = limit;
}

if (actionType) {
params.transactionType = actionType;
Comment thread
juandavidkincaid marked this conversation as resolved.
}

if (active) {
params.active = active;
}

// search_after, search_before and next are mutually exclusive
if (searchAfter) {
params.search_after = searchAfter;
Expand Down Expand Up @@ -173,11 +189,47 @@ export class BlockExplorerV2Api {
return this.service.$get<ResponseWithMetadata<TransactionV2[]>>(`/currency/${metagraphId}/addresses/${address}/transactions${searchPath}`, params);
}

async getCurrencyActionsByAddress(metagraphId: string, address: string, actionType?: ActionType, limit?: number, searchAfter?: string, searchBefore?: string, next?: string) {
const params = this.buildRequestParams({ limit, searchAfter, searchBefore, next, actionType });

return this.service.$get<ResponseWithMetadata<ActionResponse[]>>(`/currency/${metagraphId}/addresses/${address}/actions`, params);
}

async getCurrencyTransactionsBySnapshot(metagraphId: string, hashOrOrdinal: string, limit?: number, searchAfter?: string, searchBefore?: string, next?: string) {
const params = this.buildRequestParams({ limit, searchAfter, searchBefore, next });

return this.service.$get<ResponseWithMetadata<TransactionV2[]>>(`/currency/${metagraphId}/snapshots/${hashOrOrdinal}/transactions`, params);
}

async getCurrencyTokenLocksByAddress(metagraphId: string, address: string, limit?: number, searchAfter?: string, searchBefore?: string, next?: string, active?: boolean) {
const params = this.buildRequestParams({ limit, searchAfter, searchBefore, next, active });

return this.service.$get<ResponseWithMetadata<TokenLockResponse[]>>(`/currency/${metagraphId}/addresses/${address}/token-locks`, params);
}

async getCurrencyAllowSpendsByAddress(metagraphId: string, address: string, limit?: number, searchAfter?: string, searchBefore?: string, next?: string, active?: boolean) {
const params = this.buildRequestParams({ limit, searchAfter, searchBefore, next, active });

return this.service.$get<ResponseWithMetadata<AllowSpendResponse[]>>(`/currency/${metagraphId}/addresses/${address}/allow-spends`, params);
}

async getActionsByAddress(address: string, actionType?: ActionType, limit?: number, searchAfter?: string, searchBefore?: string, next?: string) {
const params = this.buildRequestParams({ limit, searchAfter, searchBefore, next, actionType });

return this.service.$get<ResponseWithMetadata<ActionResponse[]>>(`/addresses/${address}/actions`, params);
}

async getTokenLocksByAddress(address: string, limit?: number, searchAfter?: string, searchBefore?: string, next?: string, active?: boolean) {
const params = this.buildRequestParams({ limit, searchAfter, searchBefore, next, active });

return this.service.$get<ResponseWithMetadata<TokenLockResponse[]>>(`/addresses/${address}/token-locks`, params);
}

async getAllowSpendsByAddress(address: string, limit?: number, searchAfter?: string, searchBefore?: string, next?: string, active?: boolean) {
const params = this.buildRequestParams({ limit, searchAfter, searchBefore, next, active });

return this.service.$get<ResponseWithMetadata<AllowSpendResponse[]>>(`/addresses/${address}/allow-spends`, params);
}
}

export const blockExplorerApi = new BlockExplorerV2Api();
Expand Down
18 changes: 9 additions & 9 deletions packages/dag4-network/src/dag-network.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Subject} from 'rxjs';
import {NetworkInfo} from './types/network-info';
import {LoadBalancerApi} from './api/v1/load-balancer-api';
import {BlockExplorerApi} from './api/v1/block-explorer-api';
import {L0Api} from './api/v2/l0-api';
import {L1Api} from './api/v2/l1-api';
import {BlockExplorerV2Api} from './api/v2/block-explorer-api';
import {PostTransactionV2, PendingTransaction, TransactionV2, SnapshotV2} from './dto/v2';
import {PostTransaction, CbTransaction, Transaction, Snapshot} from './dto/v1';
import { Subject } from 'rxjs';
import { BlockExplorerApi } from './api/v1/block-explorer-api';
import { LoadBalancerApi } from './api/v1/load-balancer-api';
import { BlockExplorerV2Api } from './api/v2/block-explorer-api';
import { L0Api } from './api/v2/l0-api';
import { L1Api } from './api/v2/l1-api';
import { CbTransaction, PostTransaction, Snapshot, Transaction } from './dto/v1';
import { PendingTransaction, PostTransactionV2, SnapshotV2, TransactionV2 } from './dto/v2';
import { NetworkInfo } from './types/network-info';

export class DagNetwork {
private connectedNetwork: NetworkInfo = {
Expand Down
12 changes: 7 additions & 5 deletions packages/dag4-network/src/dto/v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export * from './snapshot';
export * from './transaction';
export * from './reward-transaction';
export * from './address-balance';
export * from './block';
export * from './metagraph';
export * from './peer';
export * from './total-supply';
export * from './reward-transaction';
export * from './snapshot';
export * from './staking';
export * from './swap-operations';
export * from './metagraph';
export * from './total-supply';
export * from './transaction';

47 changes: 47 additions & 0 deletions packages/dag4-network/src/dto/v2/staking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export type TokenLockResponse = {
currencyId: string | null;
hash: string;
ordinal: number;
amount: number;
source: string;
timestamp: string;
globalSnapshotHash: string;
globalSnapshotOrdinal: number;

unlockEpoch: number | null;
parentHash: string;
unlockedAtOrdinal: number | null;
};

export type AllowSpendResponse = {
currencyId: string | null;
hash: string;
ordinal: number;
amount: number;
source: string;
timestamp: string;
globalSnapshotHash: string;
globalSnapshotOrdinal: number;

destination: string;
lastValidEpochProgress: number;
fee: number;
snapshotHash: string;
}

export const Actions = ["TokenLock", "TokenUnlock", "AllowSpend", "ExpiredAllowSpend", "DelegateStakeCreate", "DelegateStakeWithdraw", "SpendTransaction", "FeeTransaction", "ExpiredSpendTransaction"] as const;
export type ActionType = (typeof Actions)[number];

export type ActionResponse = {
type: ActionType;
currencyId: string | null;
hash: string;
amount: number;
source: string;
destination: string | null;
unlockEpoch: number | null;
parentHash: string;
timestamp: string;
globalSnapshotHash: string;
globalSnapshotOrdinal: number;
}
1 change: 1 addition & 0 deletions packages/dag4-network/src/dto/v2/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber } from "bignumber.js";
import type { Transaction } from "../v1";

export type TransactionReference = {
hash: string
Expand Down
85 changes: 77 additions & 8 deletions packages/dag4-network/src/metagraph-token-network.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { MetagraphNetworkInfo } from "./types/network-info";
import { MetagraphTokenDataL1Api } from "./api/metagraph-token/data-l1-api";
import { MetagraphTokenL0Api } from "./api/metagraph-token/l0-api";
import { MetagraphTokenL1Api } from "./api/metagraph-token/l1-api";
import { BlockExplorerV2Api } from './api/v2/block-explorer-api';
import {
PostTransactionV2,
ActionType,
ActionResponse,
CurrencySnapshotV2,
PendingTransaction,
PostTransactionV2,
TransactionV2,
CurrencySnapshotV2
AllowSpendResponse,
TokenLockResponse
} from "./dto/v2";
import {BlockExplorerV2Api} from './api/v2/block-explorer-api';
import { MetagraphTokenL0Api } from "./api/metagraph-token/l0-api";
import { MetagraphTokenL1Api } from "./api/metagraph-token/l1-api";
import { MetagraphTokenDataL1Api } from "./api/metagraph-token/data-l1-api";
import { MetagraphNetworkInfo } from "./types/network-info";

class MetagraphTokenNetwork {
private connectedNetwork: MetagraphNetworkInfo;
Expand Down Expand Up @@ -69,6 +73,71 @@ class MetagraphTokenNetwork {
return response ? response.data : null;
}

async getActionsByAddress(
address: string,
actionType?: ActionType,
limit?: number,
searchAfter?: string,
searchBefore?: string,
next?: string
): Promise<ActionResponse[]> {
const actions = await this.beApi.getCurrencyActionsByAddress(this.connectedNetwork.metagraphId, address, actionType, limit, searchAfter, searchBefore, next);

if (!actions?.data?.length) return [];

return actions.data;
}

async getActiveTokenLocksTransactions(address: string, limit?: number, searchAfter?: string, searchBefore?: string): Promise<TokenLockResponse[]> {
const activeTokenLocks: TokenLockResponse[] = [];
let next: string | undefined;

do {
const response = await this.beApi.getCurrencyTokenLocksByAddress(
this.connectedNetwork.metagraphId,
address,
limit,
searchAfter,
searchBefore,
next,
true,
);

if (response?.data) {
activeTokenLocks.push(...response.data);
}

next = response?.meta?.next;
} while (next);

return activeTokenLocks;
};

async getActiveAllowSpendsTransactions(address: string, limit?: number, searchAfter?: string, searchBefore?: string): Promise<AllowSpendResponse[]> {
const activeAllowSpends: AllowSpendResponse[] = [];
let next: string | undefined;

do {
const response = await this.beApi.getCurrencyAllowSpendsByAddress(
this.connectedNetwork.metagraphId,
address,
limit,
searchAfter,
searchBefore,
next,
true,
);

if (response?.data) {
activeAllowSpends.push(...response.data);
}

next = response?.meta?.next;
} while (next);

return activeAllowSpends;
};

async getTransaction(
hash: string | null
): Promise<null | TransactionV2> {
Expand Down Expand Up @@ -121,4 +190,4 @@ class MetagraphTokenNetwork {
}
}

export { MetagraphTokenNetwork };
export { MetagraphTokenNetwork };
Loading