Add withdrawExitOnIndex function
MODIFY
src/pos/erc20.ts
src/pos/erc20.ts
@@ -144,6 +144,7 @@ export class ERC20 extends POSToken {
return this.exitUtil.buildPayloadForExit(
burnTransactionHash,
+ 0,
eventSignature,
isFast
).then(payload => {

MODIFY
src/pos/erc721.ts
src/pos/erc721.ts
@@ -235,9 +235,9 @@ export class ERC721 extends POSToken {
withdrawExit(burnTransactionHash: string, option?: ITransactionOption) {
this.checkForRoot("withdrawExit");
-
return this.exitUtil.buildPayloadForExit(
burnTransactionHash,
+ 0,
Log_Event_Signature.Erc721Transfer,
false
).then(payload => {
@@ -247,27 +247,47 @@ export class ERC721 extends POSToken {
});
}
- withdrawExitMany(burnTransactionHash: string, option?: ITransactionOption) {
- this.checkForRoot("withdrawExitMany");
+ withdrawExitOnIndex(burnTransactionHash: string, index: number, option?: ITransactionOption) {
+ this.checkForRoot("withdrawExit");
- return this.exitUtil.buildMultiplePayloadsForExit(
+ return this.exitUtil.buildPayloadForExit(
burnTransactionHash,
- Log_Event_Signature.Erc721BatchTransfer,
+ index,
+ Log_Event_Signature.Erc721Transfer,
false
- ).then(payloads => {
- return payloads.map(payload => this.rootChainManager.exit(
+ ).then(payload => {
+ return this.rootChainManager.exit(
payload, option
- )
);
});
}
+ // async withdrawExitMany(burnTransactionHash: string, option?: ITransactionOption) {
+ // this.checkForRoot("withdrawExitMany");
+
+ // return this.exitUtil.buildMultiplePayloadsForExit(
+ // burnTransactionHash,
+ // Log_Event_Signature.Erc721BatchTransfer,
+ // false
+ // ).then(async payloads => {
+ // const exitTxs = [];
+ // if()
+ // for(const i in payloads) {
+ // exitTxs.push(this.rootChainManager.exit(
+ // payloads[i], option
+ // ));
+ // }
+ // return Promise.all(exitTxs);
+ // });
+ // }
+
withdrawExitFaster(burnTransactionHash: string, option?: ITransactionOption) {
this.checkForRoot("withdrawExitFaster");
return this.exitUtil.buildPayloadForExit(
burnTransactionHash,
+ 0,
Log_Event_Signature.Erc721Transfer,
true
).then(payload => {
@@ -277,20 +297,20 @@ export class ERC721 extends POSToken {
});
}
- withdrawExitFasterMany(burnTransactionHash: string, option?: ITransactionOption) {
- this.checkForRoot("withdrawExitFasterMany");
+ // withdrawExitFasterMany(burnTransactionHash: string, option?: ITransactionOption) {
+ // this.checkForRoot("withdrawExitFasterMany");
- return this.exitUtil.buildPayloadForExit(
- burnTransactionHash,
- Log_Event_Signature.Erc721BatchTransfer,
- true
- ).then(payload => {
- return this.rootChainManager.exit(
- payload, option
- );
- });
- }
+ // return this.exitUtil.buildPayloadForExit(
+ // burnTransactionHash,
+ // Log_Event_Signature.Erc721BatchTransfer,
+ // true
+ // ).then(payload => {
+ // return this.rootChainManager.exit(
+ // payload, option
+ // );
+ // });
+ // }
isWithdrawExited(txHash: string) {
return this.isWithdrawn(

MODIFY
src/pos/exit_util.ts
src/pos/exit_util.ts
@@ -218,12 +218,16 @@ export class ExitUtil {
});
}
- buildPayloadForExit(burnTxHash: string, logEventSig: string, isFast: boolean) {
+ buildPayloadForExit(burnTxHash: string, index: number, logEventSig: string, isFast: boolean) {
if (isFast && !service.network) {
new ErrorHelper(ERROR_TYPE.ProofAPINotSet).throw();
}
+ if (index < 0) {
+ throw new Error('Index must not be a negative integer');
+ }
+
let txBlockNumber: number,
rootBlockInfo: IRootBlockInfo,
receipt: ITransactionReceipt,
@@ -271,10 +275,37 @@ export class ExitUtil {
this.requestConcurrency
);
}).then((receiptProof: any) => {
+ // step 6 - encode payload, convert into hex
+
+ // when token index is not 0
+ if(index > 0) {
+ const logIndices = this.getAllLogIndices_(
+ logEventSig, receipt
+ );
+
+ if(index >= logIndices.length) {
+ throw new Error('Index is grater than the number of tokens in this transaction');
+ }
+
+ return this.encodePayload_(
+ rootBlockInfo.headerBlockNumber.toNumber(),
+ blockProof,
+ txBlockNumber,
+ block.timestamp,
+ Buffer.from(block.transactionsRoot.slice(2), 'hex'),
+ Buffer.from(block.receiptsRoot.slice(2), 'hex'),
+ ProofUtil.getReceiptBytes(receipt), // rlp encoded
+ receiptProof.parentNodes,
+ receiptProof.path,
+ logIndices[index]
+ );
+ }
+
+ // when token index is 0
const logIndex = this.getLogIndex_(
logEventSig, receipt
);
- // step 6 - encode payload, convert into hex
+
return this.encodePayload_(
rootBlockInfo.headerBlockNumber.toNumber(),
blockProof,

MODIFY
src/pos/pos_token.ts
src/pos/pos_token.ts
@@ -63,6 +63,7 @@ export class POSToken extends BaseToken<IPOSClientConfig> {
protected withdrawExitPOS(burnTxHash: string, eventSignature: string, isFast: boolean, option: ITransactionOption) {
return this.exitUtil.buildPayloadForExit(
burnTxHash,
+ 0,
eventSignature,
isFast
).then(payload => {

Add withdrawExitOnIndex function