-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch dapp staking user transactions history from SubsSquid Giant Squ…
…id (#102) * Basic Squid service * Call parsers implementation * swagger.json fix * PR comments fixes
- Loading branch information
Showing
12 changed files
with
230 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface UserEvent { | ||
timestamp: number; | ||
contractAddress?: string; | ||
transaction: string; | ||
amount?: string; | ||
transactionHash: string; | ||
transactionSuccess: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class Call { | ||
name: string; | ||
contractAddress?: string; | ||
amount?: bigint; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface Map { | ||
[key: string]: string; | ||
} | ||
|
||
export const CallNameMapping: Map = { | ||
bond_and_stake: 'BondAndStake', | ||
unbond_and_unstake: 'UnbondAndUnstake', | ||
nomination_transfer: 'NominationTransfer', | ||
withdraw_unbonded: 'Withdraw', | ||
withdraw_from_unregistered: 'WithdrawFromUnregistered', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { injectable } from 'inversify'; | ||
import { UserEvent } from '../../models/DappStaking'; | ||
import { DappStakingCallData } from './ResponseData'; | ||
import { CallNameMapping } from './CallNameMapping'; | ||
|
||
export interface ICallParser { | ||
parse(call: DappStakingCallData): UserEvent; | ||
} | ||
|
||
@injectable() | ||
export class CallParser implements ICallParser { | ||
public parse(call: DappStakingCallData): UserEvent { | ||
return { | ||
timestamp: new Date(call.timestamp).getTime(), | ||
transaction: CallNameMapping[call.callName], | ||
transactionHash: call.extrinsicHash, | ||
transactionSuccess: call.success, | ||
}; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class BondAndStakeParser extends CallParser implements ICallParser { | ||
public parse(call: DappStakingCallData): UserEvent { | ||
const result = super.parse(call); | ||
result.contractAddress = call.argsStr[1]; | ||
result.amount = call.argsStr[2]; | ||
|
||
return result; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class UnbondAndUnstakeParser extends CallParser implements ICallParser { | ||
public parse(call: DappStakingCallData): UserEvent { | ||
const result = super.parse(call); | ||
result.contractAddress = call.argsStr[1]; | ||
result.amount = call.argsStr[2]; | ||
|
||
return result; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class NominationTransferParser extends CallParser implements ICallParser { | ||
public parse(call: DappStakingCallData): UserEvent { | ||
const result = super.parse(call); | ||
result.contractAddress = call.argsStr[3]; | ||
result.amount = call.argsStr[4]; | ||
|
||
return result; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class WithdrawParser extends CallParser implements ICallParser { | ||
public parse(call: DappStakingCallData): UserEvent { | ||
const result = super.parse(call); | ||
|
||
return result; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class WithdrawFromUnbondedParser extends CallParser implements ICallParser { | ||
public parse(call: DappStakingCallData): UserEvent { | ||
const result = super.parse(call); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface DappStakingCallResponse { | ||
data: { | ||
calls: DappStakingCallData[]; | ||
}; | ||
} | ||
|
||
export interface DappStakingCallData { | ||
callName: string; | ||
argsStr: string[]; | ||
extrinsicHash: string; | ||
success: boolean; | ||
timestamp: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './CallParser'; | ||
export * from './Call'; | ||
export * from './ResponseData'; | ||
export * from './CallNameMapping'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { injectable } from 'inversify'; | ||
import axios from 'axios'; | ||
import { NetworkType } from '../networks'; | ||
import { Guard } from '../guard'; | ||
import { decodeAddress } from '@polkadot/util-crypto'; | ||
import { PeriodType, ServiceBase } from './ServiceBase'; | ||
import { UserEvent } from '../models/DappStaking'; | ||
import { DappStakingCallData, DappStakingCallResponse } from './GiantSquid/ResponseData'; | ||
import container from '../container'; | ||
import { ICallParser } from './GiantSquid/CallParser'; | ||
import { CallNameMapping } from './GiantSquid/CallNameMapping'; | ||
|
||
export interface IGiantSquidService { | ||
getUserCalls(network: NetworkType, address: string, period: PeriodType): Promise<UserEvent[]>; | ||
} | ||
|
||
// Handles calls to SubSquid giant squid indexer. | ||
@injectable() | ||
export class GiantSquidService extends ServiceBase implements IGiantSquidService { | ||
public async getUserCalls(network: NetworkType, address: string, period: PeriodType): Promise<UserEvent[]> { | ||
Guard.ThrowIfUndefined('network', network); | ||
Guard.ThrowIfUndefined('address', address); | ||
|
||
if (network !== 'shiden' && network !== 'astar' && network !== 'shibuya') { | ||
return []; | ||
} | ||
|
||
const privateKey = `0x${Buffer.from(decodeAddress(address)).toString('hex')}`; | ||
const range = this.getDateRange(period); | ||
|
||
const query = `query MyQuery { | ||
calls(where: { | ||
palletName_eq: "DappsStaking", | ||
callerPublicKey_eq: "${privateKey}", | ||
timestamp_gte: "${range.start.toISOString()}", | ||
timestamp_lte: "${range.end.toISOString()}", | ||
callName_not_contains: "claim" | ||
}, orderBy: block_id_DESC) { | ||
callName | ||
argsStr | ||
extrinsicHash | ||
success | ||
timestamp | ||
} | ||
}`; | ||
|
||
const result = await axios.post<DappStakingCallResponse>(this.getApiUrl(network), { | ||
operationName: 'MyQuery', | ||
query, | ||
}); | ||
|
||
return this.parseUserCalls(result.data.data.calls); | ||
} | ||
|
||
private getApiUrl(network: NetworkType): string { | ||
return `https://squid.subsquid.io/gs-explorer-${network}/graphql`; | ||
} | ||
|
||
private parseUserCalls(calls: DappStakingCallData[]): UserEvent[] { | ||
const result: UserEvent[] = []; | ||
|
||
for (const call of calls) { | ||
if (CallNameMapping[call.callName]) { | ||
const parser = container.get<ICallParser>(CallNameMapping[call.callName]); | ||
result.push(parser.parse(call)); | ||
} else { | ||
// Call is not supported. Do nothing. Currently only calls defined in CallNameMapping are supported. | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters