Skip to content
Snippets Groups Projects
Verified Commit 7b27b48d authored by Mateusz Tyszczak's avatar Mateusz Tyszczak :scroll:
Browse files

Add exchange transfer trigger to the bot

parent a84ce9df
No related branches found
No related tags found
1 merge request!25Add missing filters and providers
Pipeline #112325 passed
import type { WorkerBee } from "../../bot";
import { isExchange } from "../../utils/known-exchanges";
import { OperationClassifier } from "../classifiers";
import type { TRegisterEvaluationContext } from "../classifiers/collector-classifier-base";
import type { DataEvaluationContext } from "../factories/data-evaluation-context";
import { FilterBase } from "./filter-base";
export class ExchangeTransferFilter extends FilterBase {
public constructor(
worker: WorkerBee
) {
super(worker);
}
public usedContexts(): Array<TRegisterEvaluationContext> {
return [
OperationClassifier
];
}
public async match(data: DataEvaluationContext): Promise<boolean> {
const operations = await data.get(OperationClassifier);
const transfer = operations.operationsPerType["transfer"];
const fromSavings = operations.operationsPerType["transfer_from_savings"];
const escrow = operations.operationsPerType["escrow_transfer"];
const recurrent = operations.operationsPerType["recurrent_transfer"];
if(transfer)
for(const op of transfer)
if(isExchange(op.operation.from_account))
return true;
if(fromSavings)
for(const op of fromSavings)
if(isExchange(op.operation.from_account))
return true;
if(escrow)
for(const op of escrow)
if(isExchange(op.operation.from_account))
return true;
if(recurrent)
for(const op of recurrent)
if(isExchange(op.operation.from_account))
return true;
return false;
}
}
import type { asset } from "@hiveio/wax";
import { WorkerBeeIterable } from "../../types/iterator";
import { Exchange, isExchange } from "../../utils/known-exchanges";
import { TRegisterEvaluationContext } from "../classifiers/collector-classifier-base";
import { OperationClassifier, IOperationTransactionPair } from "../classifiers/operation-classifier";
import { DataEvaluationContext } from "../factories/data-evaluation-context";
import { ProviderBase } from "./provider-base";
export interface IExchangeTransferMetadata {
from: string;
to: string;
amount: asset;
exchange: Exchange;
}
export interface IExchangeTransferProviderData {
exchangeTransferOperations: WorkerBeeIterable<IOperationTransactionPair<IExchangeTransferMetadata>>;
};
/**
* Note: On escrow_transfer operation, hbd_amount is used as the general-purpose amount field.
* This is because the escrow_transfer operation is used for both HIVE and HBD transfers.
* If you want to extract the HIVE amount, you should extract it directly from the provided operations within transaction.
*/
export class ExchangeTransferProvider extends ProviderBase {
public usedContexts(): Array<TRegisterEvaluationContext> {
return [
OperationClassifier
]
}
public async provide(data: DataEvaluationContext): Promise<IExchangeTransferProviderData> {
const operations = await data.get(OperationClassifier);
const transfer = operations.operationsPerType["transfer"];
const fromSavings = operations.operationsPerType["transfer_from_savings"];
const escrow = operations.operationsPerType["escrow_transfer"];
const recurrent = operations.operationsPerType["recurrent_transfer"];
const exchangeTransfers: IOperationTransactionPair<IExchangeTransferMetadata>[] = [];
if(transfer)
for(const op of transfer) {
const exchange = isExchange(op.operation.from_account);
if (exchange)
exchangeTransfers.push({
operation: {
from: op.operation.from_account,
to: op.operation.to_account,
amount: op.operation.amount!,
exchange
},
transaction: op.transaction
});
}
if(fromSavings)
for(const op of fromSavings) {
const exchange = isExchange(op.operation.from_account);
if (exchange)
exchangeTransfers.push({
operation: {
from: op.operation.from_account,
to: op.operation.to_account,
amount: op.operation.amount!,
exchange
},
transaction: op.transaction
});
}
if(escrow)
for(const op of escrow) {
const exchange = isExchange(op.operation.from_account);
if (exchange)
exchangeTransfers.push({
operation: {
from: op.operation.from_account,
to: op.operation.to_account,
amount: op.operation.hbd_amount!,
exchange
},
transaction: op.transaction
});
}
if(recurrent)
for(const op of recurrent) {
const exchange = isExchange(op.operation.from_account);
if (exchange)
exchangeTransfers.push({
operation: {
from: op.operation.from_account,
to: op.operation.to_account,
amount: op.operation.amount!,
exchange
},
transaction: op.transaction
});
}
return {
exchangeTransferOperations: new WorkerBeeIterable(exchangeTransfers)
};
}
}
......@@ -8,6 +8,7 @@ import { BalanceChangeFilter } from "./chain-observers/filters/balance-change-fi
import { BlockNumberFilter } from "./chain-observers/filters/block-filter";
import { LogicalAndFilter, LogicalOrFilter } from "./chain-observers/filters/composite-filter";
import { CustomOperationFilter } from "./chain-observers/filters/custom-operation-filter";
import { ExchangeTransferFilter } from "./chain-observers/filters/exchange-transfer-filter";
import type { FilterBase } from "./chain-observers/filters/filter-base";
import { FollowFilter } from "./chain-observers/filters/follow-filter";
import { ImpactedAccountFilter } from "./chain-observers/filters/impacted-account-filter";
......@@ -21,6 +22,7 @@ import { WhaleAlertFilter } from "./chain-observers/filters/whale-alert-filter";
import { AccountProvider } from "./chain-observers/providers/account-provider";
import { BlockHeaderProvider } from "./chain-observers/providers/block-header-provider";
import { BlockProvider } from "./chain-observers/providers/block-provider";
import { ExchangeTransferProvider } from "./chain-observers/providers/exchange-transfer-provider";
import { MentionedAccountProvider } from "./chain-observers/providers/mention-provider";
import { ProviderBase } from "./chain-observers/providers/provider-base";
import { RcAccountProvider } from "./chain-observers/providers/rc-account-provider";
......@@ -189,6 +191,13 @@ export class QueenBee<TPreviousSubscriberData extends object = {}> {
return this;
}
public onExchangeTransfer(): QueenBee<TPreviousSubscriberData & Awaited<ReturnType<ExchangeTransferProvider["provide"]>>> {
this.operands.push(new ExchangeTransferFilter(this.worker));
this.providers.push(new ExchangeTransferProvider());
return this;
}
public provideBlockData(): QueenBee<TPreviousSubscriberData & Awaited<ReturnType<BlockProvider["provide"]>>> {
this.providers.push(new BlockProvider());
......
const BinanceExchange = Symbol("Binance");
const HTXExchange = Symbol("HTX");
const MEXCExchange = Symbol("MEXC");
const ProBitExchange = Symbol("ProBit");
const UpbitExchange = Symbol("Upbit");
export type Exchange = typeof BinanceExchange | typeof HTXExchange | typeof MEXCExchange | typeof ProBitExchange | typeof UpbitExchange;
export const KnownExchanges = Object.freeze({
"bdhivesteem": BinanceExchange,
"binance-hot2": BinanceExchange,
"deepcrypto8": BinanceExchange,
"huobi-pro": HTXExchange,
"huobi-withdrawal": HTXExchange,
"mxchive": MEXCExchange,
"probithive": ProBitExchange,
"probitred": ProBitExchange,
"user.dunamu": UpbitExchange
});
export const isExchange = (account: string): (false | Exchange) => KnownExchanges[account] || false;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment