implement erc1155
MODIFY
src/enums/log_event_signature.ts
src/enums/log_event_signature.ts
export enum Log_Event_Signature {
- PlasmaErc20WithdrawEventSig = '0xebff2602b3f468259e1e99f613fed6691f3a6526effe6ef3e768ba7ae7a36c4f',
- PlasmaErc721WithdrawEventSig = '0x9b1bfa7fa9ee420a16e124f794c35ac9f90472acc99140eb2f6447c714cad8eb',
+ // PlasmaErc20WithdrawEventSig = '0xebff2602b3f468259e1e99f613fed6691f3a6526effe6ef3e768ba7ae7a36c4f',
+ // PlasmaErc721WithdrawEventSig = '0x9b1bfa7fa9ee420a16e124f794c35ac9f90472acc99140eb2f6447c714cad8eb',
Erc20Transfer = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
Erc721Transfer = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
+ Erc1155Transfer = '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62',
Erc721BatchTransfer = '0xf871896b17e9cb7a64941c62c188a4f5c621b86800e3d15452ece01ce56073df',
+ Erc1155BatchTransfer = '0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb',
+
}
\ No newline at end of file

MODIFY
src/interfaces/pos_client_config.ts
src/interfaces/pos_client_config.ts
import { IBaseClientConfig } from "./base_client_config";
+export interface IPOSERC1155Address {
+ mintablePredicate?: string;
+}
+
export interface IPOSClientConfig extends IBaseClientConfig {
rootChainManager?: string;
rootChain?: string;
+ erc1155?: IPOSERC1155Address;
}
\ No newline at end of file

ADD
src/pos/erc1155.ts
+import { IPOSClientConfig, ITransactionOption } from "../interfaces";
+import { Converter, promiseResolve, Web3SideChainClient } from "../utils";
+import { POSToken } from "./pos_token";
+import { Log_Event_Signature } from "../enums";
+import { IPOSContracts, IPOSERC1155Address } from "../interfaces";
+import { POSERC1155DepositBatchParam, POSERC1155DepositParam, POSERC1155TransferParam, TYPE_AMOUNT } from "..";
+
+export class ERC1155 extends POSToken {
+
+ mintablePredicateAddress: string;
+
+ addressConfig: IPOSERC1155Address;
+
+ constructor(
+ tokenAddress: string,
+ isParent: boolean,
+ client: Web3SideChainClient<IPOSClientConfig>,
+ getContracts: () => IPOSContracts
+ ) {
+ super({
+ isParent,
+ address: tokenAddress,
+ name: 'ChildERC1155',
+ bridgeType: 'pos'
+ }, client, getContracts);
+
+ this.addressConfig = client.config.erc1155 || {};
+ }
+
+ private getAddress_(value: string) {
+ const addresses = this.addressConfig;
+ if (addresses[value]) {
+ return promiseResolve(addresses[value]);
+ }
+
+ return this.client.getConfig(value);
+ }
+
+ /**
+ * get balance of a user for supplied token
+ *
+ * @param {string} userAddress
+ * @param {TYPE_AMOUNT} tokenId
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ getBalance(userAddress: string, tokenId: TYPE_AMOUNT, option?: ITransactionOption) {
+ return this.getContract().then(contract => {
+ const method = contract.method(
+ "balanceOf",
+ userAddress,
+ Converter.toHex(tokenId)
+ );
+ return this.processRead<string>(method, option);
+ });
+ }
+
+ /**
+ * check if a user is approved for all tokens
+ *
+ * @param {string} userAddress
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ isApprovedAll(userAddress: string, option?: ITransactionOption) {
+ this.checkForRoot("isApprovedAll");
+
+ return Promise.all([this.getContract(), this.getPredicateAddress()]).then(result => {
+ const [contract, predicateAddress] = result;
+ const method = contract.method(
+ "isApprovedForAll",
+ userAddress,
+ predicateAddress
+ );
+ return this.processRead<boolean>(method, option);
+ });
+
+ }
+
+ private approveAll_(predicateAddressPromise: Promise<string>, option: ITransactionOption) {
+ this.checkForRoot("approve");
+
+ return Promise.all([this.getContract(), predicateAddressPromise]).then(result => {
+ const [contract, predicateAddress] = result;
+ const method = contract.method(
+ "setApprovalForAll",
+ predicateAddress,
+ true
+ );
+ return this.processWrite(method, option);
+ });
+ }
+
+ /**
+ * approve all tokens
+ *
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ approveAll(option?: ITransactionOption) {
+ this.checkForRoot("approve");
+
+ return this.approveAll_(
+ this.getPredicateAddress(), option
+ );
+ }
+
+ /**
+ * approve all tokens for mintable token
+ *
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ approveAllForMintable(option?: ITransactionOption) {
+ this.checkForRoot("approveForMintable");
+ const addressPath = "Main.POSContracts.MintableERC1155PredicateProxy";
+ return this.approveAll_(
+ this.getAddress_(addressPath), option
+ );
+ }
+
+ /**
+ * deposit supplied amount of token for a user
+ *
+ * @param {POSERC1155DepositParam} param
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ deposit(param: POSERC1155DepositParam, option?: ITransactionOption) {
+ this.checkForRoot("deposit");
+ return this.depositMany({
+ amounts: [param.amount],
+ tokenIds: [param.tokenId],
+ userAddress: param.userAddress,
+ data: param.data
+ });
+ }
+
+ /**
+ * deposit supplied amount of multiple token for user
+ *
+ * @param {POSERC1155DepositBatchParam} param
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ depositMany(param: POSERC1155DepositBatchParam, option?: ITransactionOption) {
+ this.checkForRoot("depositMany");
+ const { tokenIds, amounts, data, userAddress } = param;
+ const amountInABI = this.client.parent.encodeParameters(
+ [
+ tokenIds.map(t => Converter.toHex(t)),
+ amounts.map(a => Converter.toHex(a)),
+ data || '0x0'
+ ],
+ ['uint256[]', 'uint256[]', 'bytes'],
+ );
+
+ return this.rootChainManager.deposit(
+ userAddress,
+ this.contractParam.address,
+ amountInABI,
+ option
+ );
+
+ }
+
+ /**
+ * start withdraw process by burning the required amount for a token
+ *
+ * @param {string} tokenId
+ * @param {TYPE_AMOUNT} amount
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ withdrawStart(tokenId: string, amount: TYPE_AMOUNT, option?: ITransactionOption) {
+ this.checkForChild("withdrawStart");
+
+ return this.getContract().then(contract => {
+ const method = contract.method(
+ "withdrawSingle",
+ Converter.toHex(tokenId),
+ Converter.toHex(amount)
+ );
+ return this.processWrite(method, option);
+ });
+ }
+
+ /**
+ * start the withdraw process by burning the supplied amount of multiple token at a time
+ *
+ * @param {TYPE_AMOUNT[]} tokenIds
+ * @param {TYPE_AMOUNT[]} amounts
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ withdrawStartMany(tokenIds: TYPE_AMOUNT[], amounts: TYPE_AMOUNT[], option?: ITransactionOption) {
+ this.checkForChild("withdrawStartMany");
+
+ const tokensInHex = tokenIds.map(t => {
+ return Converter.toHex(t);
+ });
+ const amountsInHex = amounts.map(t => {
+ return Converter.toHex(t);
+ });
+
+ return this.getContract().then(contract => {
+ const method = contract.method(
+ "withdrawBatch",
+ tokensInHex,
+ amountsInHex
+ );
+ return this.processWrite(method, option);
+ });
+ }
+
+ /**
+ * exit the withdraw process and get the burned amount on root chain
+ *
+ * @param {string} burnTransactionHash
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ withdrawExit(burnTransactionHash: string, option?: ITransactionOption) {
+ this.checkForRoot("withdrawExit");
+
+ return this.withdrawExitPOS(
+ burnTransactionHash,
+ Log_Event_Signature.Erc1155Transfer,
+ false,
+ option
+ );
+ }
+
+ /**
+ * exit the withdraw process and get the burned amount on root chain
+ *
+ * the process is faster because it uses proof api
+ *
+ * @param {string} burnTransactionHash
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ withdrawExitFaster(burnTransactionHash: string, option?: ITransactionOption) {
+ this.checkForRoot("withdrawExitFaster");
+
+ return this.withdrawExitPOS(
+ burnTransactionHash,
+ Log_Event_Signature.Erc1155Transfer,
+ true,
+ option
+ );
+ }
+
+ /**
+ * exit the withdraw process for many burned transaction and get the burned amount on root chain
+ *
+ * @param {string} burnTransactionHash
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ withdrawExitMany(burnTransactionHash: string, option?: ITransactionOption) {
+ this.checkForRoot("withdrawExitMany");
+
+
+ return this.withdrawExitPOS(
+ burnTransactionHash,
+ Log_Event_Signature.Erc1155BatchTransfer,
+ false,
+ option
+ );
+ }
+
+ /**
+ * exit the withdraw process for many burned transaction and get the burned amount on root chain
+ *
+ * the process is faster because it uses proof api
+ *
+ * @param {string} burnTransactionHash
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ withdrawExitFasterMany(burnTransactionHash: string, option?: ITransactionOption) {
+ this.checkForRoot("withdrawExitFasterMany");
+
+
+ return this.withdrawExitPOS(
+ burnTransactionHash,
+ Log_Event_Signature.Erc1155BatchTransfer,
+ true,
+ option
+ );
+ }
+
+ /**
+ * check if exit has been completed for a transaction hash
+ *
+ * @param {string} burnTxHash
+ * @return {*}
+ * @memberof ERC1155
+ */
+ isWithdrawExited(txHash: string) {
+ return this.isWithdrawn(
+ txHash, Log_Event_Signature.Erc1155Transfer
+ );
+ }
+
+ /**
+ * check if batch exit has been completed for a transaction hash
+ *
+ * @param {string} txHash
+ * @return {*}
+ * @memberof ERC1155
+ */
+ isWithdrawExitedMany(txHash: string) {
+ return this.isWithdrawn(
+ txHash, Log_Event_Signature.Erc1155BatchTransfer
+ );
+ }
+
+ /**
+ * transfer the required amount of a token to anothe user
+ *
+ * @param {POSERC1155TransferParam} param
+ * @param {ITransactionOption} [option]
+ * @return {*}
+ * @memberof ERC1155
+ */
+ transfer(param: POSERC1155TransferParam, option?: ITransactionOption) {
+ return this.transferERC1155(
+ param, option
+ );
+ }
+}
\ No newline at end of file

MODIFY
src/pos/index.ts
src/pos/index.ts
import { ERC20 } from "./erc20";
import { RootChainManager } from "./root_chain_manager";
-import { BaseToken, BridgeClient, Web3SideChainClient } from "../utils";
+import { BridgeClient } from "../utils";
import { IPOSClientConfig, IPOSContracts, ITransactionOption } from "../interfaces";
import { ExitUtil } from "./exit_util";
import { RootChain } from "./root_chain";
import { ERC721 } from "./erc721";
import { TYPE_AMOUNT } from "../types";
+import { ERC1155 } from "./erc1155";
export * from "./exit_util";
export * from "./root_chain_manager";
@@ -66,6 +67,15 @@ export class POSClient extends BridgeClient<IPOSClientConfig> {
);
}
+ erc1155(tokenAddress, isParent?: boolean) {
+ return new ERC1155(
+ tokenAddress,
+ isParent,
+ this.client,
+ this.getContracts_.bind(this)
+ );
+ }
+
depositEther(amount: TYPE_AMOUNT, userAddress: string, option: ITransactionOption) {
return new ERC20(
'', true, this.client,

MODIFY
src/pos/pos_token.ts
src/pos/pos_token.ts
import { BaseToken, Web3SideChainClient, promiseResolve } from "../utils";
-import { IContractInitParam, IPOSClientConfig } from "../interfaces";
+import { IContractInitParam, IPOSClientConfig, ITransactionOption } from "../interfaces";
import { IPOSContracts } from "../interfaces";
export class POSToken extends BaseToken<IPOSClientConfig> {
@@ -59,4 +59,16 @@ export class POSToken extends BaseToken<IPOSClientConfig> {
);
});
}
+
+ protected withdrawExitPOS(burnTxHash: string, eventSignature: string, isFast: boolean, option: ITransactionOption) {
+ return this.exitUtil.buildPayloadForExit(
+ burnTxHash,
+ eventSignature,
+ isFast
+ ).then(payload => {
+ return this.rootChainManager.exit(
+ payload, option
+ );
+ });
+ }
}
\ No newline at end of file

MODIFY
src/types/index.ts
src/types/index.ts
-import { BaseBigNumber } from "..";
+import { BaseBigNumber } from "../abstracts";
-// export * from "./side_chain_client_option";
+export * from "./pos_erc1155_deposit_param";
+export * from "./pos_erc1155_transfer_param";
export type TYPE_AMOUNT = BaseBigNumber | string | number;
\ No newline at end of file

ADD
src/types/pos_erc1155_deposit_param.ts
+import { TYPE_AMOUNT } from ".";
+
+export type POSERC1155DepositParam = {
+ tokenId: TYPE_AMOUNT;
+ amount: TYPE_AMOUNT;
+ userAddress: string;
+ data?: string,
+};
+
+export type POSERC1155DepositBatchParam = {
+ tokenIds: TYPE_AMOUNT[];
+ amounts: TYPE_AMOUNT[];
+ userAddress: string;
+ data?: string,
+};
\ No newline at end of file

ADD
src/types/pos_erc1155_transfer_param.ts
+import { TYPE_AMOUNT } from ".";
+
+export type POSERC1155TransferParam = {
+ tokenId: TYPE_AMOUNT;
+ amount: TYPE_AMOUNT;
+ from: string;
+ to: string;
+ data?: string;
+};
\ No newline at end of file

MODIFY
src/utils/base_token.ts
src/utils/base_token.ts
@@ -4,7 +4,7 @@ import { BaseContractMethod, BaseContract, BaseWeb3Client } from "../abstracts";
import { Converter, merge } from "../utils";
import { promiseResolve } from "./promise_resolve";
import { ERROR_TYPE } from "../enums";
-import { TYPE_AMOUNT } from "../types";
+import { POSERC1155TransferParam, TYPE_AMOUNT } from "../types";
import { ErrorHelper } from "./error_helper";
export interface ITransactionConfigParam {
@@ -243,8 +243,24 @@ export class BaseToken<T_CLIENT_CONFIG> {
protected checkForChild(methodName) {
if (this.contractParam.isParent) {
- this.client.logger.error(ERROR_TYPE.AllowedOnChild, "withdrawStart").throw();
+ this.client.logger.error(ERROR_TYPE.AllowedOnChild, methodName).throw();
}
}
+ protected transferERC1155(param: POSERC1155TransferParam, option: ITransactionOption) {
+ return this.getContract().then(contract => {
+ const method = contract.method(
+ "safeTransferFrom",
+ param.from,
+ param.to,
+ Converter.toHex(param.tokenId),
+ Converter.toHex(param.amount),
+ param.data || '0x'
+ );
+ return this.processWrite(
+ method, option
+ );
+ });
+ }
+
}
\ No newline at end of file

MODIFY
test/debug.js
test/debug.js
@@ -38,22 +38,24 @@ const execute = async () => {
const goerliERC20Token = client.erc20(goerliERC20, true);
const goerliERC721Token = client.erc721(pos.parent.erc721, true);
const mumbaiERC721Token = client.erc721(pos.child.erc721);
+ const goerliERC1155Token = client.erc1155(pos.parent.erc1155, true);
+ const mumbaiERC1155Token = client.erc1155(pos.child.erc1155);
- setProofApi("https://apis.matic.network");
+ // setProofApi("https://apis.matic.network");
- var result = await goerliERC20Token.withdrawExitFaster('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
- returnTransaction: true
- });
+ // var result = await goerliERC20Token.withdrawExitFaster('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
+ // returnTransaction: true
+ // });
- return console.log('result', result);
+ // return console.log('result', result);
// return console.log(await client.isDeposited('0x05b6d0d2280557c04de48d395f1f4ea9deb498fabb9bb09b9aec929db5ce62fa'));
- // const balance = await goerliERC20Token.getBalance(
- // from
- // );
- // return console.log("balance", balance);
+ const balance = await goerliERC1155Token.getBalance(
+ from,123
+ );
+ return console.log("balance", balance);
// const tokens = await goerliERC721Token.getAllTokens(
// from

implement erc1155