-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
309 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { TxWithIdAndEvent } from '../../../types/interfaces.js' | ||
import { Flattener } from '../interfaces.js' | ||
import { findParser } from './index.js' | ||
|
||
export class BasicFlattener implements Flattener { | ||
private calls: TxWithIdAndEvent[] | ||
private tx: TxWithIdAndEvent | ||
|
||
constructor(tx: TxWithIdAndEvent) { | ||
this.tx = tx | ||
this.calls = [] | ||
} | ||
|
||
flatten(id = '0') { | ||
this.tx.levelId = id | ||
this.calls.push(this.tx) | ||
|
||
const parser = findParser(this.tx) | ||
|
||
if (parser) { | ||
const nestedCalls = parser(this.tx) | ||
for (let i = nestedCalls.length - 1; i >= 0; i--) { | ||
this.tx = nestedCalls[i] | ||
this.flatten(`${id}.${i}`) | ||
} | ||
} | ||
} | ||
|
||
get flattenedCalls(): TxWithIdAndEvent[] { | ||
return this.calls | ||
} | ||
} |
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,22 @@ | ||
import { TxWithIdAndEvent } from '../../../types/interfaces.js' | ||
|
||
import { extractAsMultiCall } from './multisig.js' | ||
import { extractProxyCalls } from './proxy.js' | ||
import { extractAnyBatchCalls, extractAsDerivativeCall } from './utility.js' | ||
|
||
type CallParser = (tx: TxWithIdAndEvent) => TxWithIdAndEvent[] | ||
|
||
const parsers: Record<string, CallParser> = { | ||
'proxy.proxy': extractProxyCalls, | ||
'proxy.proxyAnnounced': extractProxyCalls, | ||
'multisig.asMulti': extractAsMultiCall, | ||
'multisig.asMultiThreshold1': extractAsMultiCall, | ||
'utility.batch': extractAnyBatchCalls, | ||
'utility.batchAll': extractAnyBatchCalls, | ||
'utility.forceBatch': extractAnyBatchCalls, | ||
'utility.asDerivative': extractAsDerivativeCall, | ||
} | ||
|
||
export function findParser({ extrinsic: { method } }: TxWithIdAndEvent): CallParser | undefined { | ||
return parsers[`${method.section}.${method.method}`] | ||
} |
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,25 @@ | ||
import type { Call } from '@polkadot/types/interfaces/runtime' | ||
|
||
import { TxWithIdAndEvent } from '../../../types/interfaces.js' | ||
import { callAsTx, getArgValueFromTx, getMultisigAddres } from '../util.js' | ||
|
||
export function extractAsMultiCall(tx: TxWithIdAndEvent) { | ||
let extraSigner | ||
try { | ||
const multisigAddress = getMultisigAddres(tx.extrinsic) | ||
extraSigner = { | ||
type: 'multisig', | ||
address: multisigAddress, | ||
} | ||
} catch { | ||
// | ||
} | ||
const call = getArgValueFromTx(tx.extrinsic, 'call') as Call | ||
return [ | ||
callAsTx({ | ||
call, | ||
tx, | ||
extraSigner, | ||
}), | ||
] | ||
} |
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,19 @@ | ||
import { MultiAddress } from '@polkadot/types/interfaces' | ||
import type { Call } from '@polkadot/types/interfaces/runtime' | ||
|
||
import { TxWithIdAndEvent } from '../../../types/interfaces.js' | ||
import { callAsTx, getArgValueFromTx } from '../util.js' | ||
|
||
export function extractProxyCalls(tx: TxWithIdAndEvent) { | ||
const { extrinsic } = tx | ||
const real = getArgValueFromTx(extrinsic, 'real') as MultiAddress | ||
const call = getArgValueFromTx(extrinsic, 'call') as Call | ||
|
||
return [ | ||
callAsTx({ | ||
call, | ||
tx, | ||
extraSigner: { type: 'proxied', address: real }, | ||
}), | ||
] | ||
} |
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,29 @@ | ||
import type { u16 } from '@polkadot/types-codec' | ||
|
||
import type { AnyTuple, CallBase } from '@polkadot/types-codec/types' | ||
import type { FunctionMetadataLatest } from '@polkadot/types/interfaces' | ||
|
||
import { TxWithIdAndEvent } from '../../../types/interfaces.js' | ||
import { callAsTx } from '../util.js' | ||
|
||
export function extractAnyBatchCalls(tx: TxWithIdAndEvent) { | ||
const { extrinsic } = tx | ||
const calls = extrinsic.args[0] as unknown as CallBase<AnyTuple, FunctionMetadataLatest>[] | ||
return calls.map((call) => | ||
callAsTx({ | ||
call, | ||
tx, | ||
}) | ||
) | ||
} | ||
|
||
export function extractAsDerivativeCall(tx: TxWithIdAndEvent) { | ||
const [_, call] = tx.extrinsic.args as unknown as [u16, CallBase<AnyTuple, FunctionMetadataLatest>] | ||
|
||
return [ | ||
callAsTx({ | ||
call, | ||
tx, | ||
}), | ||
] | ||
} |
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,49 @@ | ||
// Copyright 2023-2024 SO/DA zone | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { TxWithIdAndEvent } from '../../../types/interfaces.js' | ||
import { Boundary, CorrelatedFlattener } from './flattener.js' | ||
import { extractAsMultiCall, extractAsMutiThreshold1Call } from './multisig.js' | ||
import { extractProxyCalls } from './proxy.js' | ||
import { extractAsDerivativeCall, extractBatchAllCalls, extractBatchCalls, extractForceBatchCalls } from './utility.js' | ||
|
||
/** | ||
* Type that represents an extractor function. | ||
*/ | ||
type CallParser = ( | ||
tx: TxWithIdAndEvent, | ||
flattener: CorrelatedFlattener | ||
) => { | ||
call: TxWithIdAndEvent | ||
boundary?: Boundary | ||
}[] | ||
|
||
/** | ||
* Parsers object which maps method signatures to their corresponding extractor functions. | ||
* Extractor functions take a transaction as input and return the nested call(s) | ||
* as an array of transactions, a single transaction, or undefined based on the extraction logic. | ||
*/ | ||
export const parsers: Record<string, CallParser> = { | ||
'proxy.proxy': extractProxyCalls, | ||
'proxy.proxyAnnounced': extractProxyCalls, | ||
'multisig.asMulti': extractAsMultiCall, | ||
'multisig.asMultiThreshold1': extractAsMutiThreshold1Call, | ||
'utility.batch': extractBatchCalls, | ||
'utility.batchAll': extractBatchAllCalls, | ||
'utility.forceBatch': extractForceBatchCalls, | ||
'utility.asDerivative': extractAsDerivativeCall, | ||
} | ||
|
||
/** | ||
* Returns a call parser matching the extrinsic call name or undefined. | ||
*/ | ||
export function findParser({ extrinsic: { method } }: TxWithIdAndEvent): CallParser | undefined { | ||
return parsers[`${method.section}.${method.method}`] | ||
} | ||
|
||
/** | ||
* Returns true if a parser exists for the given extrinsic call name. | ||
*/ | ||
export function hasParser(tx: TxWithIdAndEvent): boolean { | ||
return findParser(tx) !== undefined | ||
} |
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
Oops, something went wrong.