bridgeErc1155

ERC-1155 (KIP-37) 토큰을 다른네트워크로 브릿지합니다.

const ethers = require('ethers');

const network = "ETHEREUM"
const fromAddress = "0x1400594A07925C7110B9D22791f220Ee924C0513"
const toNetwork = "KLAYTN"
const contract_address = "0xED5AF388653567Af2F388E6224dC7C4b3241C544"
const token_id = "3258"
const name = "wAzuki"
const symbol = "wAk"
const amount = "10"
const rpcUrl = "https://ethereum.publicnode.com"
const toNetworkHex = "0X4B4C4159544E"
const transferContractAddress = "0x9a1c0ef3989f944e692232d491fe5395927be9bd"
const bridgeSetupContractAddress = "0x3cf93d43251324c527346abf3e0559f4c7a713d1"

const bridgeErc1155Async = async(network, fromAddress, toNetwork, contract_address, token_id, amount, name, symbol) => {
    try {
        const provider = new ethers.providers.JsonRpcProvider(rpcUrl);

        const type = (await getNodeHomeAsync(network, toNetwork, contract_address "nft")).type; // type = "setup", "nft"

        let functionData;
        let toContractAddress;

        if (type === "setup") {
            const bridgeSetupContract = new ethers.Contract(
                bridgeSetupContractAddress,
                abiBridgeSetup,
                provider
            );

            functionData = bridgeSetupContract.interface.encodeFunctionData('setupFromERC1155', [
                toNetworkHex, name, symbol, fromAddress, contract_address, token_id, amount
            ]);

            toContractAddress = bridgeSetupContractAddress;
        } else {
            const nftTransferContract = new ethers.Contract(
                transferContractAddress,
                abiTransfer,
                provider
            );

            functionData = transferContract.interface.encodeFunctionData('moveFromERC1155', [
                toNetworkHex, contract_address, token_id, amount
            ]);

            toContractAddress = transferContractAddress;
        }

        const bridgeFee = (await getNetworkFeeAsync(network, toNetwork, type)).networkFee;

        const gasPrice = await provider.getGasPrice();

        const gasLimit = await provider.estimateGas({
            from: ownerAddress,
            to: toContractAddress,
            value: bridgeFee,
            data: functionData
        });

        let transactionDetails;

        if (network === "bnb") {
            transactionDetails = {
                from: ownerAddress,
                to: toContractAddress,
                gas: gasLimit.toHexString(),
                value: bridgeFee.toHexString(),
                data: functionData
            };
        } else {
            transactionDetails = {
                from: ownerAddress,
                to: toContractAddress,
                gasLimit: gasLimit.toHexString(),
                value: bridgeFee.toHexString(),
                data: functionData,
                maxPriorityFeePerGas : maxPriorityFeePerGas.toHexString(),
                maxFeePerGas :gasPrice.toHexString()
            };
            
        }
        return {
            result: "OK",
            transaction : transactionDetails
        }
    } catch (e) {
        return {
            result: "FAIL",
            error: e.message
        };
    }
}

Last updated