UNPKG

@hiero-ledger/sdk

Version:
3 lines (2 loc) 5.66 kB
import e from"./TokenTransfer.js";import n from"./TokenNftTransfer.js";import t from"./TokenId.js";import r from"./NftId.js";import s from"../account/AccountId.js";import o from"../transaction/Transaction.js";import a from"../account/NullableTokenDecimalMap.js";import f from"../account/TokenNftTransferMap.js";import c from"../account/TokenTransferMap.js";import i from"../account/TokenTransferAccountMap.js";import{convertAmountToLong as d}from"../util.js";class u extends o{constructor(e={}){super(),this._tokenTransfers=[],this._nftTransfers=[];for(const n of null!=e.tokenTransfers?e.tokenTransfers:[])this.addTokenTransfer(n.tokenId,n.accountId,n.amount);for(const n of null!=e.nftTransfers?e.nftTransfers:[])this.addNftTransfer(n.tokenId,n.serial,n.sender,n.recipient)}addNftTransfer(e,n,t,r){return this._addNftTransfer(!1,e,n,t,r)}_addTokenTransfer(n,r,o,a,f){this._requireNotFrozen();const c=n instanceof t?n:t.fromString(n),i=r instanceof s?r:s.fromString(r),u=d(o);for(const e of this._tokenTransfers)if(0===e.tokenId.compare(c)&&0===e.accountId.compare(i))return e.amount=e.amount.add(u),e.expectedDecimals=f,this;return this._tokenTransfers.push(new e({tokenId:n,accountId:r,expectedDecimals:f,amount:o,isApproved:a})),this}addTokenTransfer(e,n,t){return this._addTokenTransfer(e,n,t,!1,null)}_addNftTransfer(e,o,a,f,c){let i,d,u;if(this._requireNotFrozen(),o instanceof r)i=o,d="string"==typeof a?s.fromString(a):a,u="string"==typeof f?s.fromString(f):f;else if(o instanceof t)i=new r(o,a),d="string"==typeof f?s.fromString(f):f,u="string"==typeof c?s.fromString(c):c;else try{i=r.fromString(o),d="string"==typeof a?s.fromString(a):a,u="string"==typeof f?s.fromString(f):f}catch(e){const n=t.fromString(o);i=new r(n,a),d="string"==typeof f?s.fromString(f):f,u="string"==typeof c?s.fromString(c):c}for(const e of this._nftTransfers)if(0===e.tokenId.compare(i.tokenId)&&0===e.serialNumber.compare(i.serial))return e.senderAccountId=d,e.receiverAccountId=u,this;return this._nftTransfers.push(new n({tokenId:i.tokenId,serialNumber:i.serial,senderAccountId:d,receiverAccountId:u,isApproved:e})),this}addApprovedNftTransfer(e,n,t,r){return this._addNftTransfer(!0,e,n,t,r)}addApprovedTokenTransfer(e,n,t){return this._addTokenTransfer(e,n,t,!0,null)}addTokenTransferWithDecimals(n,r,o,a){this._requireNotFrozen();const f=n instanceof t?n:t.fromString(n),c=r instanceof s?r:s.fromString(r),i=d(o);let u=!1;for(const e of this._tokenTransfers)if(0===e.tokenId.compare(f)){if(null!=e.expectedDecimals&&e.expectedDecimals!==a)throw new Error("expected decimals mis-match");e.expectedDecimals=a,0===e.accountId.compare(c)&&(e.amount=e.amount.add(i),e.expectedDecimals=a,u=!0)}return u||this._tokenTransfers.push(new e({tokenId:n,accountId:r,expectedDecimals:a,amount:o,isApproved:!1})),this}get tokenIdDecimals(){const e=new a;for(const n of this._tokenTransfers)e._set(n.tokenId,n.expectedDecimals);return e}get nftTransfers(){const e=new f;for(const n of this._nftTransfers){const t=e.get(n.tokenId),r={sender:n.senderAccountId,recipient:n.receiverAccountId,serial:n.serialNumber,isApproved:n.isApproved};null!=t?t.push(r):e._set(n.tokenId,[r])}return e}get tokenTransfers(){const e=new c;for(const n of this._tokenTransfers){let t=e.get(n.tokenId);null!=t?t._set(n.accountId,n.amount):(t=new i,t._set(n.accountId,n.amount),e._set(n.tokenId,t))}return e}_makeTransactionData(){const e=[];this._tokenTransfers.sort((e,n)=>{const t=e.tokenId.compare(n.tokenId);return 0!==t?t:e.accountId.compare(n.accountId)}),this._nftTransfers.sort((e,n)=>{const t=e.senderAccountId.compare(n.senderAccountId);if(0!=t)return t;const r=e.receiverAccountId.compare(n.receiverAccountId);return 0!=r?r:e.serialNumber.compare(n.serialNumber)});let n=0,t=0;for(;n<this._tokenTransfers.length||t<this._nftTransfers.length;)if(n<this._tokenTransfers.length&&t<this._nftTransfers.length){const r=this._tokenTransfers[n].tokenId,s=this._nftTransfers[t].tokenId,o=e.length>0?e[e.length-1]:null,a=null!=o?o.tokenId:null;if(null!=o&&null!=a&&0===a.compare(r)){o.transfers.push(this._tokenTransfers[n++]);continue}if(null!=o&&null!=a&&0===a.compare(s)){o.nftTransfers.push(this._nftTransfers[t++]);continue}const f=r.compare(s);0===f?e.push({tokenId:r,expectedDecimals:this._tokenTransfers[n].expectedDecimals,transfers:[this._tokenTransfers[n++]],nftTransfers:[this._nftTransfers[t++]]}):f<0?e.push({tokenId:r,expectedDecimals:this._tokenTransfers[n].expectedDecimals,transfers:[this._tokenTransfers[n++]],nftTransfers:[]}):e.push({tokenId:s,expectedDecimals:null,transfers:[],nftTransfers:[this._nftTransfers[t++]]})}else if(n<this._tokenTransfers.length){const t=this._tokenTransfers[n].tokenId;let r;for(const n of e)0===n.tokenId.compare(t)&&(r=n);const s=null!=r?r.tokenId:null;if(null!=r&&null!=s&&0===s.compare(t)){r.transfers.push(this._tokenTransfers[n++]);continue}e.push({tokenId:t,expectedDecimals:this._tokenTransfers[n].expectedDecimals,transfers:[this._tokenTransfers[n++]],nftTransfers:[]})}else if(t<this._nftTransfers.length){const n=this._nftTransfers[t].tokenId;let r;for(const t of e)0===t.tokenId.compare(n)&&(r=t);const s=null!=r?r.tokenId:null;if(null!=r&&null!=s&&0===s.compare(n)){r.nftTransfers.push(this._nftTransfers[t++]);continue}e.push({tokenId:n,expectedDecimals:null,transfers:[],nftTransfers:[this._nftTransfers[t++]]})}return{tokenTransfers:e.map(e=>({token:e.tokenId._toProtobuf(),expectedDecimals:null!=e.expectedDecimals?{value:e.expectedDecimals}:null,transfers:e.transfers.map(e=>e._toProtobuf()),nftTransfers:e.nftTransfers.map(e=>e._toProtobuf())}))}}}export{u as default}; //# sourceMappingURL=AbstractTokenTransferTransaction.js.map