@polkadot/types/interfaces#Extrinsic TypeScript Examples
The following examples show how to use
@polkadot/types/interfaces#Extrinsic.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts From bodhi.js with Apache License 2.0 | 6 votes |
isEVMExtrinsic = (e: Extrinsic): boolean => e.method.section.toUpperCase() === 'EVM'
Example #2
Source File: functions.ts From community-repo with GNU General Public License v3.0 | 6 votes |
export async function getChangeAction(api: ApiPromise, method: string, blockHeight:number, blockHash:Hash, eventIndex: number, event: EventRecord): Promise<ActionData|null> {
const getBlock = await api.rpc.chain.getBlock(blockHash) as SignedBlock
const extrinsics = getBlock.block.extrinsics as Vec<Extrinsic>
for (let n=0; n<extrinsics.length; n++) {
const extSection = extrinsics[n].method.section
const extMethod = extrinsics[n].method.method
let extrinscIndex = 0
console.log(`Extrinsics section=${extSection}, Event method=${extMethod}`)
if (extSection == "content" && extMethod == method) {
extrinscIndex +=1
if (eventIndex == extrinscIndex) {
const extrinsic = extrinsics[n]
const actor = extrinsic.args[0] as Actor
const ent = event.event.data[1]
let entityId:number = +(ent.toString())
const video:ActionData = {
blockHeight,
action: method,
entityId,
signer: extrinsic.signer.toString(),
actor: actor.toHuman()
}
return video
}
}
}
return null
}
Example #3
Source File: mintingAndBurning.ts From community-repo with GNU General Public License v3.0 | 6 votes |
processBurnTransfers = async (
api: ApiPromise,
blockNumber: number,
report: MintingAndBurningData
) => {
const { burning } = report;
const hash = await getBlockHash(api, blockNumber);
const block = await getBlock(api, hash);
const extrinsics = filterBlockExtrinsicsByMethod(block, "balances.transfer");
for (const ext of extrinsics) {
const extData = ext as unknown as Extrinsic;
const args = extData.method.args;
const tip = extData.tip.toNumber();
if (tip > 0 && args[0].toString() === BURN_ADDRESS) {
burning.tokensBurned += tip;
}
}
}
Example #4
Source File: mintingAndBurning.ts From community-repo with GNU General Public License v3.0 | 6 votes |
processTips = async (
api: ApiPromise,
events: EventRecord[],
report: MintingAndBurningData,
hash: BlockHash
) => {
const { burning } = report;
if (events.length > 0) {
const block = await getBlock(api, hash);
const burnExtrinsics = filterBlockExtrinsicsByMethods(block, [
"utility.batch",
"staking.bond",
"session.setKeys",
"staking.nominate",
"members.buyMembership",
]);
for (const item of burnExtrinsics) {
const ext = item as unknown as Extrinsic;
const tip = ext.tip.toNumber();
burning.tokensBurned += tip;
}
}
}
Example #5
Source File: block.ts From moonbeam with GNU General Public License v3.0 | 6 votes |
export function mapExtrinsics(
extrinsics: Extrinsic[] | any,
records: FrameSystemEventRecord[] | any,
fees?: RuntimeDispatchInfo[] | any
): TxWithEventAndFee[] {
return extrinsics.map((extrinsic, index): TxWithEventAndFee => {
let dispatchError: DispatchError | undefined;
let dispatchInfo: DispatchInfo | undefined;
const events = records
.filter(({ phase }) => phase.isApplyExtrinsic && phase.asApplyExtrinsic.eq(index))
.map(({ event }) => {
if (event.section === "system") {
if (event.method === "ExtrinsicSuccess") {
dispatchInfo = event.data[0] as any as DispatchInfo;
} else if (event.method === "ExtrinsicFailed") {
dispatchError = event.data[0] as any as DispatchError;
dispatchInfo = event.data[1] as any as DispatchInfo;
}
}
return event as any;
});
return { dispatchError, dispatchInfo, events, extrinsic, fee: fees ? fees[index] : undefined };
});
}
Example #6
Source File: Extrinsic.tsx From crust-apps with Apache License 2.0 | 6 votes |
function getEra ({ era }: Extrinsic, blockNumber?: BlockNumber): [number, number] | null {
if (blockNumber && era.isMortalEra) {
const mortalEra = era.asMortalEra;
return [mortalEra.birth(blockNumber.toNumber()), mortalEra.death(blockNumber.toNumber())];
}
return null;
}
Example #7
Source File: Call.tsx From crust-apps with Apache License 2.0 | 6 votes |
function CallDisplay (props: Props): React.ReactElement<Props> {
const { className = '', defaultValue: { value }, isDisabled, label, withLabel } = props;
if (!isDisabled) {
return (
<Unknown {...props} />
);
}
const call = value as Extrinsic;
const { method, section } = call.registry.findMetaCall(call.callIndex);
return (
<Bare>
<Static
className={`${className} full`}
label={label}
withLabel={withLabel}
>
{section}.{method}
</Static>
<Call value={call} />
</Bare>
);
}
Example #8
Source File: Call.tsx From subscan-multisig-react with Apache License 2.0 | 6 votes |
function CallDisplay(props: Props): React.ReactElement<Props> {
const {
className = '',
defaultValue: { value },
isDisabled,
label,
withLabel,
} = props;
if (!isDisabled) {
return <Unknown {...props} />;
}
const call = value as Extrinsic;
const { method, section } = call.registry.findMetaCall(call.callIndex);
return (
<Bare>
<Static className={`${className} full`} label={label} withLabel={withLabel}>
{section}.{method}
</Static>
<Call value={call} />
</Bare>
);
}
Example #9
Source File: QueryEvent.ts From squid with GNU General Public License v3.0 | 6 votes |
constructor(
eventRecord: EventRecord,
blockNumber: number,
indexInBlock: number,
blockTimestamp: number,
extrinsic?: Extrinsic
) {
this.eventRecord = eventRecord
this.extrinsic = extrinsic
this.blockNumber = blockNumber
this.indexInBlock = indexInBlock
this.blockTimestamp = blockTimestamp
}
Example #10
Source File: QueryEventBlock.ts From squid with GNU General Public License v3.0 | 6 votes |
export function getExtrinsic(eventInBlock: {
record: {
phase: { isApplyExtrinsic: boolean; asApplyExtrinsic: u32 }
}
extrinsics: Extrinsic[]
}): Extrinsic | undefined {
const extrinsicIndex = getExtrinsicIndex(eventInBlock.record)
return getOrUndefined(extrinsicIndex, eventInBlock.extrinsics)
}
Example #11
Source File: timestamp.ts From squid with GNU General Public License v3.0 | 6 votes |
/**
* All blocks have timestamp event except for the genesic block.
* This method looks up `timestamp.set` and reads off the block timestamp
*
* @param extrinsics block extrinsics
* @returns timestamp as set by a `timestamp.set` call
*/
export function getBlockTimestamp(extrinsics: Extrinsic[]): number {
const ex = extrinsics.find(
({ method: { method, section } }) =>
section === 'timestamp' && method === 'set'
)
return ex ? (ex.args[0].toJSON() as number) : 0
}
Example #12
Source File: general.ts From community-repo with GNU General Public License v3.0 | 5 votes |
async function main() {
// Initialise the provider to connect to the local node
const provider = new WsProvider('ws://127.0.0.1:9944');
//If you want to play around on our staging network, go ahead and connect to this staging network instead.
//const provider = new WsProvider('wss://testnet-rpc-2-singapore.joystream.org');
// Create the API and wait until ready
const api = await ApiPromise.create({ provider, types })
// get finalized head of chain, as number and hash:
const finalizedHeadNumber = await api.derive.chain.bestNumberFinalized()
const finalizedHeadHash = await api.rpc.chain.getFinalizedHead()
// get head of chain, as hash or number:
const headNumber = await api.derive.chain.bestNumber()
const headHash = await api.rpc.chain.getBlockHash(headNumber)
console.log(
`Finalized head of chain
- as number: ${finalizedHeadNumber}
- with hash: ${finalizedHeadHash}`
)
console.log(
`Head of chain
- as number: ${headNumber}
- with hash: ${headHash}`
)
// get current issuance
const issuance = await api.query.balances.totalIssuance() as Balance
console.log(`current issuance is: ${issuance.toNumber()}tJOY`)
// get events in newest block:
const events = await api.query.system.events() as Vec<EventRecord>;
for (let { event } of events) {
const section = event.section
const method = event.method
const data = event.data
console.log("section",section)
console.log("method",method)
console.log("data",data.toHuman())
console.log("")
}
// get extrinsics in finalized head block:
const getLatestBlock = await api.rpc.chain.getBlock(finalizedHeadHash) as SignedBlock
const extrinsics = getLatestBlock.block.extrinsics as Vec<Extrinsic>
for (let i=0; i<extrinsics.length; i++) {
const section = extrinsics[i].method.section
const method = extrinsics[i].method.method
console.log("section",section)
console.log("method",method)
console.log("")
// get signer of extrinsics if applicable
const signer = extrinsics[i].signer
if (!signer.isEmpty) {
console.log("signer",signer)
}
}
api.disconnect()
}
Example #13
Source File: get-events-and-extrinsics.ts From community-repo with GNU General Public License v3.0 | 5 votes |
async function main() {
// Initialise the provider to connect to the local node
const provider = new WsProvider('ws://127.0.0.1:9944');
//If you want to play around on our staging network, go ahead and connect to this staging network instead.
//const provider = new WsProvider('wss://testnet-rpc-2-singapore.joystream.org');
// Create the API and wait until ready
const api = await ApiPromise.create({ provider, types })
// get all extrinsic and event types in a range of blocks (only works for last 200 blocks unless you are querying an archival node)
// will take a loooong time if you check too many blocks :)
const firstBlock = 800000
const lastBlock = 801000
const eventTypes:string[] = []
const extrinsicTypes: string[] = []
for (let blockHeight=firstBlock; blockHeight<lastBlock; blockHeight++) {
const blockHash = await api.rpc.chain.getBlockHash(blockHeight)
const events = await api.query.system.events.at(blockHash) as Vec<EventRecord>;
const getBlock = await api.rpc.chain.getBlock(blockHash) as SignedBlock
const extrinsics = getBlock.block.extrinsics as Vec<Extrinsic>
for (let { event } of events) {
const section = event.section
const method = event.method
const eventType = section+`:`+method
if (!eventTypes.includes(eventType)) {
eventTypes.push(eventType)
}
}
for (let i=0; i<extrinsics.length; i++) {
const section = extrinsics[i].method.section
const method = extrinsics[i].method.method
const extrinsicType = section+`:`+method
if (!extrinsicTypes.includes(extrinsicType)) {
extrinsicTypes.push(extrinsicType)
}
}
}
console.log("number of unique event types in range:",eventTypes.length)
console.log("list of the unique event types in range:")
console.log(JSON.stringify(eventTypes, null, 4))
console.log("")
console.log("number of unique extrinsic types in range",extrinsicTypes.length)
console.log("list of the unique extrinsic types in range:")
console.log(JSON.stringify(extrinsicTypes, null, 4))
api.disconnect()
}
Example #14
Source File: mintingAndBurning.ts From community-repo with GNU General Public License v3.0 | 5 votes |
processWorkerRewardAmountUpdated = async (
api: ApiPromise,
blockNumber: number,
recurringRewards: RecurringRewards
) => {
const hash = await getBlockHash(api, blockNumber);
const block = await getBlock(api, hash);
const groups = [
"operationsWorkingGroup",
"contentDirectoryWorkingGroup",
"storageWorkingGroup",
];
for await (const group of groups) {
const extrinsics = filterBlockExtrinsicsByMethod(
block,
`${group}.updateRewardAmount`
);
for (const ext of extrinsics) {
const extData = ext as unknown as Extrinsic;
const args = extData.method.args;
const workerId = Number(args[0]);
const newAmount = Number(args[1]);
const worker = await getWorker(api, group, hash, workerId);
const relationship = Number(worker.reward_relationship.unwrap());
const previousBlockWorkerReward = recurringRewards.rewards[
blockNumber
].filter((r) => Number(r.recipient) === relationship);
if (previousBlockWorkerReward.length == 0) {
const reward = (
await api.query.recurringRewards.rewardRelationships.at(
hash,
relationship
)
).toJSON() as unknown as RewardRelationship;
recurringRewards.rewards[blockNumber].push(reward);
} else {
previousBlockWorkerReward.forEach(async (reward) => {
const mint = (
await getMint(api, hash, reward.mint_id)
).toJSON() as unknown as Mint;
reward.amount_per_payout = newAmount as unknown as u128;
});
}
}
}
}
Example #15
Source File: SubstrateExtrinsicEntity.ts From squid with GNU General Public License v3.0 | 5 votes |
export function fromBlockExtrinsic(data: {
e: Extrinsic
blockEntity: SubstrateBlockEntity
indexInBlock: number
}): SubstrateExtrinsicEntity {
const extr = new SubstrateExtrinsicEntity()
const { e, indexInBlock, blockEntity } = data
const { height, hash } = blockEntity
extr.id = formatId({
height,
index: indexInBlock,
hash,
})
extr.block = blockEntity
extr.blockNumber = height
extr.blockHash = hash
extr.indexInBlock = indexInBlock
extr.signature = e.signature ? e.signature.toString() : ''
extr.signer = e.signer ? e.signer.toString() : ''
extr.method = (e.method && e.method.method) || 'NO_METHOD'
extr.section = (e.method && e.method.section) || 'NO_SECTION'
extr.name = fullName(e.method)
extr.meta = e.meta?.toJSON() || {}
extr.hash = e.hash?.toString() || ''
extr.isSigned = e.isSigned
extr.tip = e.tip ? e.tip.toBigInt() : BigInt(0)
extr.versionInfo = e.version ? e.version.toString() : ''
extr.nonce = e.nonce ? e.nonce.toNumber() : 0
extr.era = e.era?.toJSON() || {}
extr.args = []
e.method.args.forEach((data, index) => {
const name = e.meta.args[index].name.toString()
const value = data.toJSON()
const type = data.toRawType()
extr.args.push({
type,
value,
name,
})
})
return extr
}
Example #16
Source File: QueryEvent.ts From squid with GNU General Public License v3.0 | 5 votes |
readonly extrinsic?: Extrinsic