@funded-labs/plug-controller
Version:
Internet Computer Plug wallet's controller
113 lines (112 loc) • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatXTCTransaction = void 0;
const tokens_1 = require("../../../constants/tokens");
const XTC_DECIMALS = 12;
const formatTransfer = (principalId, { event }, details) => {
if (!('Transfer' in event.kind))
throw Error();
const transaction = { details };
transaction.details.from = event.kind.Transfer.from;
transaction.details.to = event.kind.Transfer.to;
transaction.caller = event.kind.Transfer.from;
transaction.type =
transaction.details.to === principalId ? 'RECEIVE' : 'SEND';
return transaction;
};
const formatTransferFrom = (principalId, { event }, details) => {
if (!('TransferFrom' in event.kind))
throw Error();
const transaction = { details };
transaction.details.from = event.kind.TransferFrom.from;
transaction.details.to = event.kind.TransferFrom.to;
transaction.caller = event.kind.TransferFrom.caller;
transaction.type =
transaction.details.to === principalId ? 'RECEIVE' : 'SEND';
return transaction;
};
const formatBurn = (_principalId, { event }, details) => {
if (!('Burn' in event.kind))
throw Error();
const transaction = { details };
transaction.details.from = event.kind.Burn.from;
transaction.details.to = event.kind.Burn.to;
transaction.caller = event.kind.Burn.from;
transaction.type = 'BURN';
return transaction;
};
const formatApprove = (_principalId, { event }, details) => {
if (!('Approve' in event.kind))
throw Error();
const transaction = { details };
transaction.details.from = event.kind.Approve.from;
transaction.details.to = event.kind.Approve.to;
transaction.caller = event.kind.Approve.from;
transaction.type = 'APPROVE';
return transaction;
};
const formatMint = (_principalId, { event }, details) => {
if (!('Mint' in event.kind))
throw Error();
const transaction = { details };
transaction.details.from = 'Mint';
transaction.details.to = event.kind.Mint.to;
transaction.caller = _principalId;
transaction.type = 'MINT';
return transaction;
};
const formatCanisterCalled = (_principalId, { event }, details) => {
if (!('CanisterCalled' in event.kind))
throw Error();
const transaction = { details };
transaction.details.from = event.kind.CanisterCalled.from;
transaction.caller = event.kind.CanisterCalled.from;
transaction.details.to = `${event.kind.CanisterCalled.canister}_${event.kind.CanisterCalled.method_name}`;
transaction.type = 'CANISTER_CALLED';
return transaction;
};
const formatCanisterCreated = (_principalId, { event }, details) => {
if (!('CanisterCreated' in event.kind))
throw Error();
const transaction = { details };
transaction.details.from = event.kind.CanisterCreated.from;
transaction.caller = event.kind.CanisterCreated.from;
transaction.details.to = event.kind.CanisterCreated.canister;
transaction.type = 'CANISTER_CREATED';
return transaction;
};
const formatXTCTransaction = (principalId, xtcTransaction) => {
const transactionEvent = xtcTransaction.event;
const transaction = {};
transaction.hash = xtcTransaction.txnId;
transaction.timestamp = xtcTransaction.event.timestamp;
const details = {
canisterId: tokens_1.TOKENS.XTC.canisterId,
amount: transactionEvent.cycles.toString(),
currency: { symbol: 'XTC', decimals: XTC_DECIMALS },
fee: {
amount: transactionEvent.fee.toString(),
currency: { symbol: 'XTC', decimals: XTC_DECIMALS },
},
status: 'COMPLETED',
};
switch (Object.keys(transactionEvent.kind)[0]) {
case 'Transfer':
return Object.assign(Object.assign({}, transaction), formatTransfer(principalId, xtcTransaction, details));
case 'Burn':
return Object.assign(Object.assign({}, transaction), formatBurn(principalId, xtcTransaction, details));
case 'Mint':
return Object.assign(Object.assign({}, transaction), formatMint(principalId, xtcTransaction, details));
case 'CanisterCalled':
return Object.assign(Object.assign({}, transaction), formatCanisterCalled(principalId, xtcTransaction, details));
case 'CanisterCreated':
return Object.assign(Object.assign({}, transaction), formatCanisterCreated(principalId, xtcTransaction, details));
case 'Approve':
return Object.assign(Object.assign({}, transaction), formatApprove(principalId, xtcTransaction, details));
case 'TransferFrom':
return Object.assign(Object.assign({}, transaction), formatTransferFrom(principalId, xtcTransaction, details));
default:
throw Error;
}
};
exports.formatXTCTransaction = formatXTCTransaction;