@polkadot/types/types#InterfaceTypes TypeScript Examples
The following examples show how to use
@polkadot/types/types#InterfaceTypes.
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: index.ts From parity-bridges-ui with GNU General Public License v3.0 | 6 votes |
export function getTransactionDisplayPayload({
payload,
account,
createType,
sourceTargetDetails
}: PayloadInput): Output {
const {
sourceChainDetails: {
chain: sourceChain,
configs: { ss58Format }
},
targetChainDetails: { chain: targetChain }
} = sourceTargetDetails;
const payloadType = createType(sourceChain as keyof InterfaceTypes, 'OutboundPayload', payload);
const payloadHex = payloadType.toHex();
const callType = createType(targetChain as keyof InterfaceTypes, 'BridgedOpaqueCall', payload.call);
const call = createType(targetChain as keyof InterfaceTypes, 'Call', callType.toHex());
const formatedAccount = encodeAddress(account, ss58Format);
const transactionDisplayPayload = {} as TransactionDisplayPayload;
const { spec_version, weight, dispatch_fee_payment } = payload;
transactionDisplayPayload.call = JSON.parse(call);
transactionDisplayPayload.origin = {
SourceAccount: formatedAccount
};
transactionDisplayPayload.weight = weight;
transactionDisplayPayload.dispatch_fee_payment = dispatch_fee_payment;
transactionDisplayPayload.spec_version = spec_version;
return { transactionDisplayPayload, payloadHex };
}
Example #2
Source File: shared.ts From commonwealth with GNU General Public License v3.0 | 5 votes |
public createType<K extends keyof InterfaceTypes>(type: K, ...params: any[]) {
return this.api.registry.createType(type, ...params);
}
Example #3
Source File: useSendMessage.ts From parity-bridges-ui with GNU General Public License v3.0 | 4 votes |
function useSendMessage({ input, type }: Props) {
const { estimatedSourceFee, receiverAddress, payload, transferAmount } = useTransactionContext();
const { dispatchTransaction } = useUpdateTransactionContext();
const laneId = useLaneId();
const sourceTargetDetails = useSourceTarget();
const {
sourceChainDetails: {
apiConnection: { api: sourceApi },
chain: sourceChain
},
targetChainDetails: {
chain: targetChain,
apiConnection: { api: targetApi }
}
} = sourceTargetDetails;
const { account, companionAccount } = useAccountContext();
const { createType } = useApiCalls();
const { dispatchMessage } = useUpdateMessageContext();
const makeCall = useCallback(
async (id: string) => {
try {
if (!account || !payload) {
return;
}
const payloadType = createType(sourceChain as keyof InterfaceTypes, 'OutboundPayload', payload);
const payloadHex = payloadType.toHex();
const { bridgedMessages } = getSubstrateDynamicNames(targetChain);
const bridgeMessage = sourceApi.tx[bridgedMessages].sendMessage(laneId, payload, estimatedSourceFee);
logger.info(`bridge::sendMessage ${bridgeMessage.toHex()}`);
const options: Partial<SignerOptions> = {
nonce: -1
};
let sourceAccount: string | KeyringPair = account;
if (account.meta.isInjected) {
const injector = await web3FromSource(account.meta.source as string);
options.signer = injector.signer;
sourceAccount = account.address;
}
const { transactionDisplayPayload } = getTransactionDisplayPayload({
payload,
account: account.address,
createType,
sourceTargetDetails
});
const formattedTransferAmount = getFormattedAmount(targetApi, transferAmount, type);
const signed = await bridgeMessage.signAsync(sourceAccount, { ...options });
dispatchTransaction(TransactionActionCreators.setTransactionToBeExecuted(false));
dispatchTransaction(TransactionActionCreators.setTransactionRunning(true));
const unsub = await signed.send(({ events = [], status }) => {
if (status.isReady) {
dispatchTransaction(
TransactionActionCreators.createTransactionStatus({
block: null,
blockHash: null,
id,
input,
messageNonce: null,
receiverAddress,
sourceAccount: account.address,
senderName: getName(account),
companionAccount,
sourceChain,
transferAmount: formattedTransferAmount,
status: TransactionStatusEnum.CREATED,
targetChain,
type,
payloadHex,
transactionDisplayPayload,
deliveryBlock: null,
steps: createEmptySteps(sourceChain, targetChain)
})
);
}
if (status.isBroadcast) {
dispatchMessage(MessageActionsCreators.triggerInfoMessage({ message: 'Transaction was broadcasted' }));
dispatchTransaction(TransactionActionCreators.reset());
}
if (status.isInBlock) {
dispatchTransaction(TransactionActionCreators.setTransactionRunning(false));
events.forEach(({ event: { data, method } }) => {
if (method.toString() === 'MessageAccepted') {
const messageNonce = data.toArray()[1].toString();
(sourceApi.rpc.chain.getBlock(status.asInBlock) as Promise<SignedBlock>)
.then((res) => {
const block = res.block.header.number.toString();
dispatchTransaction(
TransactionActionCreators.updateTransactionStatus(
{
block,
blockHash: status.asInBlock.toString(),
messageNonce,
status: TransactionStatusEnum.IN_PROGRESS
},
id
)
);
})
.catch((e) => {
logger.error(e.message);
throw new Error('Issue reading block information.');
});
}
});
}
if (status.isFinalized) {
logger.info(`Transaction finalized at blockHash ${status.asFinalized}`);
unsub();
}
});
} catch (e) {
if (e instanceof Error) {
logger.error(e.message);
if (e.message === TX_CANCELLED) {
dispatchTransaction(TransactionActionCreators.enableTxButton());
dispatchTransaction(TransactionActionCreators.setTransactionToBeExecuted(false));
dispatchTransaction(TransactionActionCreators.setTransactionRunning(false));
return dispatchMessage(
MessageActionsCreators.triggerErrorMessage({ message: 'Transaction was cancelled from the extension.' })
);
}
dispatchMessage(MessageActionsCreators.triggerErrorMessage({ message: e.message }));
}
} finally {
dispatchTransaction(TransactionActionCreators.setTransactionToBeExecuted(false));
}
},
[
account,
companionAccount,
createType,
dispatchMessage,
dispatchTransaction,
estimatedSourceFee,
input,
laneId,
payload,
receiverAddress,
sourceApi.rpc.chain,
sourceApi.tx,
sourceChain,
sourceTargetDetails,
targetApi,
targetChain,
transferAmount,
type
]
);
const sendLaneMessage = useCallback(() => {
const id = Date.now().toString();
dispatchTransaction(TransactionActionCreators.setTransactionToBeExecuted(true));
return makeCall(id);
}, [dispatchTransaction, makeCall]);
return sendLaneMessage;
}
Example #4
Source File: useEstimatedFeePayload.ts From parity-bridges-ui with GNU General Public License v3.0 | 4 votes |
useEstimatedFeePayload = (
transactionState: TransactionState,
dispatchTransaction: Dispatch<TransactionsActionType>
) => {
const { createType, stateCall } = useApiCallsContext();
const laneId = useLaneId();
const sourceTargetDetails = useSourceTarget();
const {
sourceChainDetails: {
chain: sourceChain,
apiConnection: { api: sourceApi }
},
targetChainDetails: {
apiConnection: { api: targetApi },
chain: targetChain
}
} = sourceTargetDetails;
const { account, senderAccountBalance, senderCompanionAccountBalance } = useAccountContext();
const { action, isBridged } = useGUIContext();
const { estimatedFeeMethodName } = getSubstrateDynamicNames(targetChain);
const previousPayloadEstimatedFeeLoading = usePrevious(transactionState.payloadEstimatedFeeLoading);
const { bridgedMessages } = getSubstrateDynamicNames(targetChain);
const dispatch = useCallback(
(error: string | null, data: PayloadEstimatedFee | null, loading: boolean) =>
dispatchTransaction(
TransactionActionCreators.setPayloadEstimatedFee({
payloadEstimatedFeeError: error,
payloadEstimatedFee: data,
payloadEstimatedFeeLoading: loading,
sourceTargetDetails,
createType,
isBridged,
senderAccountBalance,
senderCompanionAccountBalance,
chainDecimals: targetApi.registry.chainDecimals[0]
})
),
[
createType,
dispatchTransaction,
isBridged,
senderAccountBalance,
senderCompanionAccountBalance,
sourceTargetDetails,
targetApi.registry.chainDecimals
]
);
const calculateFeeAndPayload = useCallback(
async (currentTransactionState: TransactionState) => {
if (currentTransactionState.action === TransactionTypes.INTERNAL_TRANSFER) {
const { estimatedFee, weight } = await getFeeAndWeightForInternals({
api: sourceApi,
transactionState: currentTransactionState
});
const payload = {
sourceAccount: currentTransactionState.senderAccount,
transferAmount: currentTransactionState.transferAmount!.toNumber(),
receiverAddress: currentTransactionState.receiverAddress,
weight
};
return { estimatedSourceFee: estimatedFee, payload };
}
const { call, weight } = await getTransactionCallWeight({
action,
account,
targetApi,
transactionState: currentTransactionState
});
if (!call || !weight) {
return emptyData;
}
const callToCompact = currentTransactionState.action === TransactionTypes.CUSTOM ? call : call.slice(2);
const payload = {
call: compactAddLength(callToCompact),
origin: {
SourceAccount: account!.addressRaw
},
dispatch_fee_payment: currentTransactionState.payFee,
spec_version: targetApi.consts.system.version.specVersion.toNumber(),
weight
};
const payloadType = createType(sourceChain as keyof InterfaceTypes, 'OutboundPayload', payload);
logger.info(`OutboundPayload: ${JSON.stringify(payload)}`);
logger.info(`OutboundPayload.toHex(): ${payloadType.toHex()}`);
const messageFeeType = createType(sourceChain as keyof InterfaceTypes, 'MessageFeeData', {
lane_id: laneId,
payload: payloadType.toHex()
});
// estimatedFeeMessageDelivery
const estimatedFeeCall = await stateCall(sourceChain, estimatedFeeMethodName, messageFeeType.toHex());
const estimatedFeeType = createType(sourceChain as keyof InterfaceTypes, 'Option<Balance>', estimatedFeeCall);
const estimatedFeeMessageDelivery = estimatedFeeType.toString();
// estimatedFeeBridgeCall
const bridgeMessage = sourceApi.tx[bridgedMessages].sendMessage(laneId, payload, estimatedFeeCall);
const submitMessageTransactionFee = await sourceApi.rpc.payment.queryFeeDetails(bridgeMessage.toHex());
const estimatedFeeBridgeCallBalance = (submitMessageTransactionFee as FeeDetails).inclusionFee.unwrap()
.adjustedWeightFee;
const estimatedFeeBridgeCall = estimatedFeeBridgeCallBalance.toString();
// estimatedSourceFee calculation based on the sum of estimatedFeeMessageDelivery + estimatedFeeBridgeCallBalance
const estimatedSourceFeeBN = new BN(estimatedFeeMessageDelivery).add(estimatedFeeBridgeCallBalance.toBn());
const estimatedSourceFee = estimatedSourceFeeBN.toString();
// estimatedTargetFee
const targetFeeDetails = await targetApi.rpc.payment.queryFeeDetails(u8aToHex(call));
const estimatedTargetFee = (targetFeeDetails as FeeDetails).inclusionFee.unwrap().adjustedWeightFee.toString();
return {
estimatedSourceFee,
estimatedFeeMessageDelivery,
estimatedFeeBridgeCall,
estimatedTargetFee,
payload
};
},
[
account,
action,
bridgedMessages,
createType,
estimatedFeeMethodName,
laneId,
sourceApi,
sourceChain,
stateCall,
targetApi
]
);
useEffect(() => {
const { shouldEvaluatePayloadEstimatedFee, payloadEstimatedFeeLoading } = transactionState;
if (shouldEvaluatePayloadEstimatedFee && payloadEstimatedFeeLoading) {
logger.info(
'Transaction information changed while estimated fee is being calculating. Batching the new calculation.'
);
dispatchTransaction(TransactionActionCreators.setBatchedEvaluationPayloadEstimatedFee(transactionState));
}
if (shouldEvaluatePayloadEstimatedFee && !payloadEstimatedFeeLoading) {
genericCall({
//@ts-ignore
call: () => calculateFeeAndPayload(transactionState),
dispatch,
emptyData
});
}
}, [calculateFeeAndPayload, dispatch, dispatchTransaction, transactionState]);
useEffect(() => {
const { batchedTransactionState, payloadEstimatedFeeLoading } = transactionState;
if (
previousPayloadEstimatedFeeLoading &&
!payloadEstimatedFeeLoading &&
batchedTransactionState &&
senderAccountBalance &&
senderCompanionAccountBalance
) {
genericCall({
//@ts-ignore
call: () => calculateFeeAndPayload(batchedTransactionState),
dispatch,
emptyData
});
dispatchTransaction(TransactionActionCreators.setBatchedEvaluationPayloadEstimatedFee(null));
}
}, [
account,
calculateFeeAndPayload,
dispatch,
dispatchTransaction,
previousPayloadEstimatedFeeLoading,
senderAccountBalance,
senderCompanionAccountBalance,
transactionState
]);
}
Example #5
Source File: transactionReducer.ts From parity-bridges-ui with GNU General Public License v3.0 | 4 votes |
export default function transactionReducer(state: TransactionState, action: TransactionsActionType): TransactionState {
const transactionReadyToExecute = isReadyToExecute({ ...state, ...action.payload });
switch (action.type) {
case TransactionActionTypes.SET_PAYLOAD_ESTIMATED_FEE: {
const {
payloadEstimatedFeeError,
payloadEstimatedFee: {
estimatedSourceFee,
estimatedFeeMessageDelivery,
estimatedFeeBridgeCall,
estimatedTargetFee,
payload
},
payloadEstimatedFeeLoading,
sourceTargetDetails,
createType,
isBridged,
senderAccountBalance,
senderCompanionAccountBalance
} = action.payload;
const { senderAccount, receiverAddress, transferAmount } = state;
const { evaluateTransactionStatusError, notEnoughFundsToTransfer, notEnoughToPayFee } = enoughFundsEvaluation({
transferAmount,
senderCompanionAccountBalance,
senderAccountBalance,
estimatedSourceFee,
estimatedTargetFee,
action: state.action
});
const readyToExecute = payloadEstimatedFeeLoading
? false
: transactionReadyToExecute && !notEnoughToPayFee && !notEnoughFundsToTransfer;
let payloadHex = null;
let transactionDisplayPayload = null;
if (senderAccount && payload) {
if (isBridged) {
const updated = getTransactionDisplayPayload({
payload,
account: senderAccount,
createType,
sourceTargetDetails
});
payloadHex = updated.payloadHex;
transactionDisplayPayload = updated.transactionDisplayPayload;
}
if (!isBridged && receiverAddress && transferAmount) {
transactionDisplayPayload = {
sourceAccount: senderAccount,
transferAmount: transferAmount.toNumber(),
receiverAddress: receiverAddress,
weight: payload.weight
};
}
}
return {
...state,
estimatedSourceFee: !payloadEstimatedFeeError && transactionReadyToExecute ? estimatedSourceFee : null,
estimatedTargetFee: !payloadEstimatedFeeError && transactionReadyToExecute ? estimatedTargetFee : null,
estimatedFeeMessageDelivery:
!payloadEstimatedFeeError && transactionReadyToExecute ? estimatedFeeMessageDelivery : null,
estimatedFeeBridgeCall: !payloadEstimatedFeeError && transactionReadyToExecute ? estimatedFeeBridgeCall : null,
payloadEstimatedFeeError,
payloadEstimatedFeeLoading,
payload: payloadEstimatedFeeError ? null : payload,
transactionReadyToExecute: readyToExecute,
shouldEvaluatePayloadEstimatedFee: false,
payloadHex,
transactionDisplayPayload,
evaluateTransactionStatusError
};
}
case TransactionActionTypes.SET_BATCH_PAYLOAD_ESTIMATED_FEE: {
const { batchedTransactionState } = action.payload;
return {
...state,
batchedTransactionState,
transactionReadyToExecute: Boolean(batchedTransactionState && state.estimatedSourceFee),
shouldEvaluatePayloadEstimatedFee: false
};
}
case TransactionActionTypes.SET_TRANSFER_AMOUNT: {
const { transferAmount, chainDecimals } = action.payload;
const [actualValue, message] = evalUnits(transferAmount, chainDecimals);
if (!transferAmount) {
return {
...state,
transferAmount,
transferAmountError: null,
transactionReadyToExecute: false,
estimatedSourceFee: null,
estimatedFeeMessageDelivery: null,
estimatedFeeBridgeCall: null,
estimatedTargetFee: null,
payload: null
};
}
const shouldEvaluatePayloadEstimatedFee = shouldCalculatePayloadFee(state, { transferAmount: actualValue });
return {
...state,
transferAmount: actualValue || null,
transferAmountError: message,
transactionReadyToExecute: transactionReadyToExecute && !message,
estimatedSourceFee: null,
estimatedFeeMessageDelivery: null,
estimatedFeeBridgeCall: null,
estimatedTargetFee: null,
shouldEvaluatePayloadEstimatedFee
};
}
case TransactionActionTypes.SET_REMARK_INPUT: {
const { remarkInput } = action.payload;
if (remarkInput.startsWith('0x')) {
if (isHex(remarkInput)) {
const shouldEvaluatePayloadEstimatedFee = shouldCalculatePayloadFee(state, { remarkInput });
return {
...state,
remarkInput: remarkInput as string,
transactionReadyToExecute,
shouldEvaluatePayloadEstimatedFee
};
}
}
return {
...state,
remarkInput,
transactionReadyToExecute: false,
shouldEvaluatePayloadEstimatedFee: false,
estimatedSourceFee: null,
estimatedFeeMessageDelivery: null,
estimatedFeeBridgeCall: null,
estimatedTargetFee: null,
payload: null,
payloadEstimatedFeeError: 'Invalid remark input'
};
}
case TransactionActionTypes.SET_CUSTOM_CALL_INPUT: {
const { customCallInput, createType, targetChain } = action.payload;
if (!customCallInput) {
return {
...state,
customCallInput,
transactionReadyToExecute: false,
customCallError: null,
shouldEvaluatePayloadEstimatedFee: false
};
}
let customCallError = null;
let shouldEvaluatePayloadEstimatedFee = false;
try {
createType(targetChain as keyof InterfaceTypes, 'Call', customCallInput);
shouldEvaluatePayloadEstimatedFee = shouldCalculatePayloadFee(state, { customCallInput });
} catch (e) {
customCallError = e;
logger.error('Wrong call', e);
}
return {
...state,
customCallInput,
transactionReadyToExecute: transactionReadyToExecute && !customCallError,
estimatedSourceFee: customCallError || !customCallInput ? null : state.estimatedSourceFee,
estimatedTargetFee: customCallError || !customCallInput ? null : state.estimatedTargetFee,
estimatedFeeMessageDelivery: customCallError || !customCallInput ? null : state.estimatedFeeMessageDelivery,
estimatedFeeBridgeCall: customCallError || !customCallInput ? null : state.estimatedFeeBridgeCall,
customCallError,
shouldEvaluatePayloadEstimatedFee
};
}
case TransactionActionTypes.SET_WEIGHT_INPUT: {
const { weightInput } = action.payload;
const shouldEvaluatePayloadEstimatedFee = shouldCalculatePayloadFee(state, { weightInput });
return {
...state,
weightInput: weightInput,
transactionReadyToExecute: transactionReadyToExecute && !state.customCallError,
shouldEvaluatePayloadEstimatedFee,
estimatedSourceFee: weightInput ? state.estimatedSourceFee : null,
estimatedFeeMessageDelivery: weightInput ? state.estimatedFeeMessageDelivery : null,
estimatedFeeBridgeCall: weightInput ? state.estimatedFeeBridgeCall : null
};
}
case TransactionActionTypes.RESET:
return {
...state,
evaluateTransactionStatusError: null,
resetedAt: Date.now().toString(),
derivedReceiverAccount: null,
estimatedSourceFee: null,
estimatedFeeMessageDelivery: null,
estimatedFeeBridgeCall: null,
estimatedTargetFee: null,
payloadEstimatedFeeError: null,
shouldEvaluatePayloadEstimatedFee: false,
batchedTransactionState: null,
genericReceiverAccount: null,
receiverAddress: null,
transferAmount: null,
transferAmountError: null,
remarkInput: '',
customCallInput: '',
customCallError: null,
weightInput: '',
unformattedReceiverAddress: null,
addressValidationError: null,
payload: null,
transactionDisplayPayload: null,
payloadHex: null,
showBalance: false,
formatFound: null,
transactionReadyToExecute: false
};
case TransactionActionTypes.SET_RECEIVER_ADDRESS: {
const { receiverAddress } = action.payload;
const shouldEvaluatePayloadEstimatedFee = shouldCalculatePayloadFee(state, { receiverAddress });
return {
...state,
receiverAddress,
transactionReadyToExecute,
shouldEvaluatePayloadEstimatedFee
};
}
case TransactionActionTypes.CREATE_TRANSACTION_STATUS:
return { ...state, transactions: [action.payload.initialTransaction, ...state.transactions] };
case TransactionActionTypes.UPDATE_CURRENT_TRANSACTION_STATUS:
return updateTransaction(state, action.payload);
case TransactionActionTypes.SET_RECEIVER:
return setReceiver(state, action.payload.receiverPayload);
case TransactionActionTypes.SET_TRANSACTION_RUNNING:
return {
...state,
transactionRunning: action.payload.transactionRunning,
transactionReadyToExecute: action.payload.transactionRunning ? false : state.transactionReadyToExecute
};
case TransactionActionTypes.SET_TRANSACTION_TO_BE_EXECUTED: {
const { transactionToBeExecuted } = action.payload;
return {
...state,
transactionToBeExecuted
};
}
case TransactionActionTypes.SET_ACTION: {
const { action: transactionType } = action.payload;
return {
...state,
action: transactionType
};
}
case TransactionActionTypes.SET_SENDER: {
const { senderAccount } = action.payload;
return {
...state,
senderAccount
};
}
case TransactionActionTypes.UPDATE_SENDER_BALANCES: {
const { action: transactionType, senderAccount } = state;
const shouldEvaluatePayloadEstimatedFee = shouldCalculatePayloadFee(state, {
senderAccount,
action: transactionType
});
return {
...state,
shouldEvaluatePayloadEstimatedFee,
transactionReadyToExecute: false
};
}
case TransactionActionTypes.UPDATE_TRANSACTIONS_STATUS: {
const { evaluateTransactionStatusError, transactions, evaluatingTransactions } = action.payload;
return {
...state,
transactions: transactions && !evaluateTransactionStatusError ? transactions : state.transactions,
evaluatingTransactions,
evaluateTransactionStatusError
};
}
case TransactionActionTypes.SET_TRANSFER_TYPE: {
const { transferType } = action.payload;
return {
...state,
action: transferType
};
}
case TransactionActionTypes.ENABLE_TX_BUTTON: {
return {
...state,
transactionReadyToExecute: true
};
}
case TransactionActionTypes.DISABLE_TX_BUTTON: {
return {
...state,
transactionReadyToExecute: false
};
}
case TransactionActionTypes.CHANGE_DISPATCH_FEE_PAY_CHAIN: {
return {
...state,
payFee: action.payload.payFee,
shouldEvaluatePayloadEstimatedFee: true
};
}
default:
throw new Error(`Unknown type: ${action.type}`);
}
}