TRANS BRIDGE DOCS
  • 🌟개념
    • TransBridge란 ?
      • 소개
      • 왜 TransBridge인가?
    • 배경
      • 브릿지란 무엇인가요?
      • 메세지 확인
    • 토큰브릿지
      • Architecture
      • TVL
    • NFT브릿지
    • 수수료구조
  • 💻개발자
    • API 참조
      • 소개
        • 메인넷 컨트렉트 주소
        • 테스트넷 컨트렉트 주소
      • getNetworkFeeIdxByName
      • getNetworkFeeByIdx
      • customNhid
      • getToNetwork
      • setupFromERC20
      • setupFromERC721
      • setupFromERC1155
      • moveFromETHER
      • moveFromERC20
      • moveFromERC721
      • moveFromERC1155
    • Example
      • getNetworkFee
      • getNodeHome
      • bridgeCoin
      • bridgeErc20
      • bridgeErc721
      • bridgeErc1155
      • 구현예제(Mainnet)
      • 구현예제(Testnet)
  • 고객센터
    • 문의 및 제안
Powered by GitBook
On this page
  1. 개발자
  2. Example

bridgeErc20

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

const ethers = require('ethers');

const network = "ETHEREUM"
const fromAddress = "0x1400594A07925C7110B9D22791f220Ee924C0513"
const toNetwork = "KLAYTN"
const contract_address = "0xdF9c65B589e1286D4361EcFFa516e1fbfA4526df"
const amount = "10"
const name = "Wrapped MATIC"
const symbol = "wMATIC"
const rpcUrl = "https://ethereum.publicnode.com"
const toNetworkHex = "0X4B4C4159544E"
const bridgeContractAddress = "0x7362fa30ada8ccf2130017f2a8f0b6be78aa38de"
const bridgeSetupContractAddress = "0x3cf93d43251324c527346abf3e0559f4c7a713d1"

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

        const type = (await getNodeHomeAsync(network, toNetwork, contract_address "token")).type;

        // type = "setup", "token"
        
        let decimal;
        try {
            const erc20ConfigContract = new ethers.Contract(contract_address, abiERC20, provider);
            const decimalsData = await erc20ConfigContract.decimals();
            decimal = decimalsData
        } catch (e) {
            console.log("decimal error", e.message);
        }

        let functionData;
        let toContractAddress;

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

            functionData = bridgeSetupContract.interface.encodeFunctionData('setupFromERC20', [
                toNetworkHex, name, symbol, fromAddress, contract_address, totalAmount
            ]);

            toContractAddress = bridgeSetupContractAddress;
        } else { // token
            const bridgeContract = new ethers.Contract(
                bridgeContractAddress,
                abiBridge,
                provider
            );

            functionData = bridgeContract.interface.encodeFunctionData('moveFromERC20', [
                toNetworkHex, contract_address, token_id
            ]);

            toContractAddress = bridgeContractAddress;
        }

        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
        };
    }
}
PreviousbridgeCoinNextbridgeErc721

Last updated 1 year ago

💻