您正在查看: 2021年8月

争议不断的EIP-1559即将到来,它会造成哪些影响?

伦敦升级来临

以太坊基金会于7月15日公布了伦敦升级最新进展,本次升级将在主网达到12965000区块高度时被激活,具体时间预计在8月3日至5日之间。

V神在7月24号的世界区块链大会上演讲时说到:“当现在的PoW链完成了它的使命,PoW链上的一切都转移到PoS链上之后,就算是进入了以太坊2.0。但在两条链合并前期,将会执行一些较小的升级,引入分片,搭配Rollup,理想状态下可实现每秒10万笔交易。”而即将在PoW链上进行的伦敦分叉就是合并前期的一次重要升级。

从2015年开始,以太坊经历了数次升级,它们帮助以太坊不断完善并走向2.0时代。

什么是EIP-1559

以太坊的网络升级通过链下治理实施,社区成员提出以太坊改进提案(Ethereum Improvement Proposal,EIP)并对升级所要采用的EIP内容达成一致,由开发团队更新客户端,最后矿工使用最新版的以太坊客户端并通过硬分叉升级。

伦敦硬分叉升级中包含了五个以太坊改进提案,包括EIP-1559、EIP-3198、EIP-3529、EIP-3541和EIP-3554。伦敦升级因EIP-1559备受瞩目,其将改进现有的手续费机制,并将持续销毁ETH。

目前以太坊的交易手续费(Gas费)计算机制中一部分由创建者设置,创建者设置的交易费用多少会影响到矿工打包的先后顺序。矿工提供了在以太坊上运行交易和智能合约的计算能力,他们通常会优先处理设定高Gas费的用户的交易,以最大限度地获得区块奖励。

同样,为了能够更快完成交易,交易创建者也会设置更高的手续费来激励矿工。因此,在这种机制的刺激下,导致竞争巨大、Gas费不断上升、效率极低,并导致验证节点总价超付,增加了成本。

为了改善用户体验,V神和以太坊核心开发人员提出EIP-1559改进提案,在EIP-1559提案中,交易手续费将被拆分为两部分:基础费+矿工小费。其中基础费是交易所需的最少花费,由系统直接销毁,小费归矿工所有。

基础费用是用户在以太坊上发送交易或完成操作所需的最低Gas价格。它根据以太坊上每个区块使用的空间大小而波动。理想情况下,以太坊上的每个区块最多可容纳1500万Gas。然而在网络拥堵时,EIP-1559将允许块大小增加到这个数量的两倍。如果区块包含的Gas超过1500万,基础费将提高12.5%,反之如果低于1500万,将降低12.5%,直至趋于零。这种调整旨在确保以太坊的Gas费趋向于平均每区块约1500万Gas。

矿工小费指的是用户可以选择在最低基本费用的基础上再支付额外费用,该费用将直接支付给矿工,以激励矿工优先处理某些交易。EIP-1559中的小费是可选的,仅用于需要最快网络确认时间的用户。

EIP-1559对我们会产生哪些影响?

那么,EIP-1559设定的相对固定的Gas基础费的作用有哪些呢?

其一,可以降低ETH的通胀率。目前,ETH的供应量是无限的,每产生一个新的区块,就会有两枚新的ETH诞生,因此,它是有通胀的属性在的。但EIP-1559对于Gas基础费的销毁机制可能会带来一种局面,在交易活跃度较高时,用户支付基础费用销毁的ETH总量可能会抵消并大于通过区块奖励新发行的ETH总额,这将意味着ETH供应量出现负增长。如果交易不活跃,销毁的基础费少于出块奖励,那也可以促进ETH数量不会过分减少,使其达到一种平衡状态。

其二,降低以太坊交易费用波动性。根据EIP-1559,Gas基础费只能增减12.5%,这为以太坊的交易费用带来了稳定性。但是,降低波动性不代表降低交易费用,高费用问题主要是由于处理交易的网络容量有限造成的。以太坊费用机制的变化不能改变网络一次能够处理多少交易。这属于以太坊的可拓展性问题,需要交给Layer 2扩容方案和未来的分片技术。

基于EIP-1559的手续费机制和作用,我们可以看到它将主要影响三种不同类型的利益相关者:ETH投资者、矿工和用户。

(一)ETH投资者

基础费的销毁降低了ETH的通胀率。如果以太坊交易非常活跃时,甚至可能出现其销毁量高于新增量的情况,在这种情况下,ETH会变成通缩资产。

现在每天ETH的产出是13000枚左右,年通胀率约为4%。Dune Analytics的数据显示,如果按照EIP-1559的方式,一年内预计销毁2937123枚以太坊,占增发量的61.9%,可以把通胀率降低到1.5%,这个数值要比比特币1.8%的通胀率更低。通过销毁基础费,在一定程度上会提升ETH的价值,有利于长期持有ETH的投资者们。

(二)矿工

表面来看,EIP-1559中基础费的销毁会极大地削减矿工收入。但这是绝对的吗?

我们首先来看一下矿工的收入来源是什么。目前,矿工收入来源于交易手续费和区块奖励。当EIP-1559落实之后,交易手续费拆分为基础费和矿工小费,其中基础费被销毁。但随着以太坊上DEX的崛起,还有一种矿工收入出现,即矿工可提取价值(MEV)。MEV是一种可变的收入来源,DEX交易者可能比普通用户更看重链上交易执行的速度和顺序,矿工可利用这一点从DEX交易者那里获得更多的回报。

由于DEX越来越受欢迎,MEV变得越来越有利可图。研究和开发组织Flashbots估计,MEV的日收入已从2021年初的50万美元增长到2021年6月的600多万美元。

因此,在EIP-1559之后,矿工的MEV部分的收益比例有可能会越来越多。EIP-1559对矿工的收益肯定会有影响,但可能不一定有想象中的大。矿工依然有机会获得不错的收益。此外,EIP-1559本身会对ETH带来增值效应,这对所有人都是有利的。

(三)用户

对于以太坊的用户来说,高昂的费用是目前最紧迫的问题之一。EIP-1559并不会解决这个问题,并没有改变以太坊本身一次只能处理有限数量交易的事实。EIP-1559的主要作用是可以优化和提高以太坊手续费的使用效率。

EIP-1559提高了以太坊费用方式的透明度和可见性。过去用户在支付手续费时,需要根据网络拥堵情况和近期Gas费估算手续费多少。如果过少等待时间会变长,过多则造成浪费。从区块层面看,EIP-1559的方案每次区块之间的基础费变化幅度最多为12.5%,根据规则用户能预测并支付相对准确的手续费,以提高用户体验。

EIP-1559面临的风险

EIP-1559关于手续费的改变可以为以太坊带来好的一面,但技术是双刃剑,有好处但也同样伴随着风险。

对于矿工来说,随着EIP-1559的激活,他们的收入将减少。矿工将失去大额的交易手续费,而只能从寻求交易优先权的用户那里获得小费。改变奖励机制本身不会影响以太坊处理区块或计算的能力。然而,却存在着不满的矿工离开网络、破坏网络或启动竞争链的可能性。如果有很大一部分以太坊矿工退出或造反,区块时间和网络安全将受到负面影响。

对于用户和DAPP开发者,EIP-1559的激活可能不会表现得像理论上那样有效。如果不能实现承诺的费用市场效率,可能会导致用户和开发者的幻想破灭。如果发生这种情况,以太坊的竞争对手,如Binance Smart Chain和Cardano(按市值计算仅次于以太坊的两个最大的智能合约区块链平台),无疑将抓住机会抢占市场份额。

最后,EIP-1559的激活还带来了不可预见的错误或恶意的用户行为的风险。这在私人测试网络上测试EIP-1559的过程中,已经发现了一些。

以太坊的新时代

EIP-1559通过让手续费更有效率来提升用户体验,但不能从根本上解决网络拥堵和高手续费的问题,这需要通过Layer 2扩容或者以太坊2.0来实现。

EIP-1559将极大地增强以太坊上处理交易的用户体验。当然,矿工们会担心EIP-1559的费用销毁方面,但EIP-1559的总体好处远远超过费用销毁,将对终端用户产生积极的影响。

随着EIP-1559、Layer 2以及PoS的推进,以太坊及ETH会有向好的变化,其中也会潜藏一些危机。不过,如果一切都能顺利落地,这会开启以太坊的新时代,对整个加密领域的格局产生非常重大的影响。

转载自:https://zhuanlan.zhihu.com/p/395153612

其他

https://notes.ethereum.org/@vbuterin/eip-1559-faq
https://zhuanlan.zhihu.com/p/393196358
https://zhuanlan.zhihu.com/p/396948190
https://zhuanlan.zhihu.com/p/361104358

bsc-eth-swap 编译及部署测试

背景

项目后期需要准备一个ETH体系的资产IBC服务,设计要求轻量稳定安全。
前面对poly做过技术调研,相对去中心化,但导致运行和运维成本过高,以及近期出现的安全性问题,所以暂时搁置。
本片文章将针对BSC提供的Swap服务做技术调研,由于没有找到官方的部署文档,只有技术实现讲解,所以本篇文章将从合约代码,以及Swap程序代码阅读方向进行部署文档的反推整理。

合约实现包含了可升级合约的实现架构,相关技术细节可参考《使用OpenZeppelin编写可升级合约

本篇文章先做bsc-eth-swap项目的部署和测试整理,后面将针对服务做二次开发或者模式技术参考,目标是开发出一个ETH体系多链资产和NFT轻量的IBC服务,后期再迭代成多节点去中心化模式。

项目组成

项目合约:https://github.com/binance-chain/eth-bsc-swap-contracts.git
项目程序:https://github.com/binance-chain/bsc-eth-swap.git

官方文档

https://github.com/binance-chain/bsc-eth-swap/blob/main/docs/spec.md

编译合约

git clone https://github.com/binance-chain/eth-bsc-swap-contracts.git
cd eth-bsc-swap-contracts/
sudo npm i -g truffle
npm i
npm run truffle:compile

部署合约

为了方便测试,以及etherscan区块浏览器对于数据的展示较完整,所以我们选择的测试网络为

  • Goerli Test Network
  • Ropsten Test Network

ETH合约部署在Goerli(主),BSC合约部署在Ropsten(侧)

ETH链合约部署

部署网络

Goerli Test Network

部署ETHSwapAgentImpl合约

使用编译后的build\contracts\ETHSwapAgentImpl.json中的abibytecode部署合约。

ETHSwapAgentImpl ABI
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"erc20Addr","type":"address"},{"indexed":true,"internalType":"bytes32","name":"bscTxHash","type":"bytes32"},{"indexed":true,"internalType":"address","name":"toAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"SwapFilled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sponsor","type":"address"},{"indexed":true,"internalType":"address","name":"erc20Addr","type":"address"},{"indexed":false,"internalType":"string","name":"name","type":"string"},{"indexed":false,"internalType":"string","name":"symbol","type":"string"},{"indexed":false,"internalType":"uint8","name":"decimals","type":"uint8"}],"name":"SwapPairRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"erc20Addr","type":"address"},{"indexed":true,"internalType":"address","name":"fromAddr","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeAmount","type":"uint256"}],"name":"SwapStarted","type":"event"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"filledBSCTx","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"registeredERC20","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"swapFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"address payable","name":"ownerAddr","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"fee","type":"uint256"}],"name":"setSwapFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"erc20Addr","type":"address"}],"name":"registerSwapPairToBSC","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"bscTxHash","type":"bytes32"},{"internalType":"address","name":"erc20Addr","type":"address"},{"internalType":"address","name":"toAddress","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"fillBSC2ETHSwap","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"erc20Addr","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"swapETH2BSC","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"}]
ETHSwapAgentImpl bytecode
0x608060405234801561001057600080fd5b506112d7806100206000396000f3fe60806040526004361061009c5760003560e01c806389b156041161006457806389b156041461017a5780638da5cb5b146101ad5780639867df11146101de578063b9927a9c14610227578063da35a26f14610253578063f2fde38b1461028c5761009c565b806334e19907146100a157806350877c77146100cd57806354cf2aeb1461010b5780635c13c15114610132578063715018a614610165575b600080fd5b3480156100ad57600080fd5b506100cb600480360360208110156100c457600080fd5b50356102bf565b005b3480156100d957600080fd5b506100f7600480360360208110156100f057600080fd5b503561031c565b604080519115158252519081900360200190f35b34801561011757600080fd5b50610120610331565b60408051918252519081900360200190f35b34801561013e57600080fd5b506100f76004803603602081101561015557600080fd5b50356001600160a01b0316610337565b34801561017157600080fd5b506100cb61081e565b34801561018657600080fd5b506100f76004803603602081101561019d57600080fd5b50356001600160a01b03166108c0565b3480156101b957600080fd5b506101c26108d5565b604080516001600160a01b039092168252519081900360200190f35b3480156101ea57600080fd5b506100f76004803603608081101561020157600080fd5b508035906001600160a01b036020820135811691604081013590911690606001356108e4565b6100f76004803603604081101561023d57600080fd5b506001600160a01b038135169060200135610a88565b34801561025f57600080fd5b506100cb6004803603604081101561027657600080fd5b50803590602001356001600160a01b0316610c94565b34801561029857600080fd5b506100cb600480360360208110156102af57600080fd5b50356001600160a01b0316610d58565b6102c7610e51565b6003546001600160a01b03908116911614610317576040805162461bcd60e51b81526020600482018190526024820152600080516020611258833981519152604482015290519081900360640190fd5b600455565b60026020526000908152604090205460ff1681565b60045481565b6001600160a01b03811660009081526001602052604081205460ff161561039a576040805162461bcd60e51b8152602060048201526012602482015271185b1c9958591e481c9959da5cdd195c995960721b604482015290519081900360640190fd5b6060826001600160a01b03166306fdde036040518163ffffffff1660e01b815260040160006040518083038186803b1580156103d557600080fd5b505afa1580156103e9573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052602081101561041257600080fd5b810190808051604051939291908464010000000082111561043257600080fd5b90830190602082018581111561044757600080fd5b825164010000000081118282018810171561046157600080fd5b82525081516020918201929091019080838360005b8381101561048e578181015183820152602001610476565b50505050905090810190601f1680156104bb5780820380516001836020036101000a031916815260200191505b5060405250505090506060836001600160a01b03166395d89b416040518163ffffffff1660e01b815260040160006040518083038186803b1580156104ff57600080fd5b505afa158015610513573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052602081101561053c57600080fd5b810190808051604051939291908464010000000082111561055c57600080fd5b90830190602082018581111561057157600080fd5b825164010000000081118282018810171561058b57600080fd5b82525081516020918201929091019080838360005b838110156105b85781810151838201526020016105a0565b50505050905090810190601f1680156105e55780820380516001836020036101000a031916815260200191505b5060405250505090506000846001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b15801561062957600080fd5b505afa15801561063d573d6000803e3d6000fd5b505050506040513d602081101561065357600080fd5b50518351909150610698576040805162461bcd60e51b815260206004820152600a602482015269656d707479206e616d6560b01b604482015290519081900360640190fd5b60008251116106dd576040805162461bcd60e51b815260206004820152600c60248201526b195b5c1d1e481cde5b589bdb60a21b604482015290519081900360640190fd5b6001600160a01b0385166000818152600160208181526040808420805460ff1916909317909255815160ff8616928101929092526060808352875190830152865133937ffe3bd005e346323fa452df8cafc28c55b99e3766ba8750571d139c6cf5bc08a0938993899389938392838101926080850192918901918190849084905b8381101561077657818101518382015260200161075e565b50505050905090810190601f1680156107a35780820380516001836020036101000a031916815260200191505b50838103825285518152855160209182019187019080838360005b838110156107d65781810151838201526020016107be565b50505050905090810190601f1680156108035780820380516001836020036101000a031916815260200191505b509550505050505060405180910390a3506001949350505050565b610826610e51565b6003546001600160a01b03908116911614610876576040805162461bcd60e51b81526020600482018190526024820152600080516020611258833981519152604482015290519081900360640190fd5b6003546040516000916001600160a01b0316907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0908390a3600380546001600160a01b0319169055565b60016020526000908152604090205460ff1681565b6003546001600160a01b031681565b60006108ee610e51565b6003546001600160a01b0390811691161461093e576040805162461bcd60e51b81526020600482018190526024820152600080516020611258833981519152604482015290519081900360640190fd5b60008581526002602052604090205460ff161561099a576040805162461bcd60e51b81526020600482015260156024820152746273632074782066696c6c656420616c726561647960581b604482015290519081900360640190fd5b6001600160a01b03841660009081526001602052604090205460ff166109fe576040805162461bcd60e51b81526020600482015260146024820152733737ba103932b3b4b9ba32b932b2103a37b5b2b760611b604482015290519081900360640190fd5b6000858152600260205260409020805460ff19166001179055610a316001600160a01b038516848463ffffffff610e5516565b826001600160a01b031685856001600160a01b03167f3bebd9a738291e69898b5dbfadb6329b4b09fc648bdef68762928e521463abd9856040518082815260200191505060405180910390a4506001949350505050565b6000610a9333610ea7565b15610ae5576040805162461bcd60e51b815260206004820152601f60248201527f636f6e7472616374206973206e6f7420616c6c6f77656420746f207377617000604482015290519081900360640190fd5b333214610b39576040805162461bcd60e51b815260206004820152601c60248201527f6e6f2070726f787920636f6e747261637420697320616c6c6f77656400000000604482015290519081900360640190fd5b6001600160a01b03831660009081526001602052604090205460ff16610b9d576040805162461bcd60e51b81526020600482015260146024820152733737ba103932b3b4b9ba32b932b2103a37b5b2b760611b604482015290519081900360640190fd5b6004543414610be8576040805162461bcd60e51b81526020600482015260126024820152711cddd85c08199959481b9bdd08195c5d585b60721b604482015290519081900360640190fd5b610c036001600160a01b03841633308563ffffffff610ead16565b3415610c44576003546040516001600160a01b03909116903480156108fc02916000818181858888f19350505050158015610c42573d6000803e3d6000fd5b505b60408051838152346020820152815133926001600160a01b038716927ff60309f865a6aa297da5fac6188136a02e5acfdf6e8f6d35257a9f4e9653170f929081900390910190a350600192915050565b600054610100900460ff1680610cad5750610cad610f0d565b80610cbb575060005460ff16155b610cf65760405162461bcd60e51b815260040180806020018281038252602e81526020018061122a602e913960400191505060405180910390fd5b600054610100900460ff16158015610d21576000805460ff1961ff0019909116610100171660011790555b6004839055600380546001600160a01b0319166001600160a01b0384161790558015610d53576000805461ff00191690555b505050565b610d60610e51565b6003546001600160a01b03908116911614610db0576040805162461bcd60e51b81526020600482018190526024820152600080516020611258833981519152604482015290519081900360640190fd5b6001600160a01b038116610df55760405162461bcd60e51b81526004018080602001828103825260268152602001806111de6026913960400191505060405180910390fd5b6003546040516001600160a01b038084169216907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a3600380546001600160a01b0319166001600160a01b0392909216919091179055565b3390565b604080516001600160a01b038416602482015260448082018490528251808303909101815260649091019091526020810180516001600160e01b031663a9059cbb60e01b179052610d53908490610f13565b3b151590565b604080516001600160a01b0380861660248301528416604482015260648082018490528251808303909101815260849091019091526020810180516001600160e01b03166323b872dd60e01b179052610f07908590610f13565b50505050565b303b1590565b6060610f68826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316610fc49092919063ffffffff16565b805190915015610d5357808060200190516020811015610f8757600080fd5b5051610d535760405162461bcd60e51b815260040180806020018281038252602a815260200180611278602a913960400191505060405180910390fd5b6060610fd38484600085610fdd565b90505b9392505050565b60608247101561101e5760405162461bcd60e51b81526004018080602001828103825260268152602001806112046026913960400191505060405180910390fd5b61102785610ea7565b611078576040805162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015290519081900360640190fd5b60006060866001600160a01b031685876040518082805190602001908083835b602083106110b75780518252601f199092019160209182019101611098565b6001836020036101000a03801982511681845116808217855250505050505090500191505060006040518083038185875af1925050503d8060008114611119576040519150601f19603f3d011682016040523d82523d6000602084013e61111e565b606091505b509150915061112e828286611139565b979650505050505050565b60608315611148575081610fd6565b8251156111585782518084602001fd5b8160405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b838110156111a257818101518382015260200161118a565b50505050905090810190601f1680156111cf5780820380516001836020036101000a031916815260200191505b509250505060405180910390fdfe4f776e61626c653a206e6577206f776e657220697320746865207a65726f2061646472657373416464726573733a20696e73756666696369656e742062616c616e636520666f722063616c6c496e697469616c697a61626c653a20636f6e747261637420697320616c726561647920696e697469616c697a65644f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65725361666545524332303a204552433230206f7065726174696f6e20646964206e6f742073756363656564a26469706673582212207d800cfe9cc73238c171437c8cfab5dbce431b61738d8c89459b39acb1d5769064736f6c63430006040033

部署后的地址是0x4e99e2A70664f30d14b1f703b8bbe077872F5Fb7

部署ETHSwapAgentUpgradeableProxy合约

使用编译后的build\contracts\ETHSwapAgentUpgradeableProxy.json中的abibytecode部署合约。

ETHSwapAgentUpgradeableProxy abi
[{"inputs":[{"internalType":"address","name":"logic","type":"address"},{"internalType":"address","name":"admin","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"previousAdmin","type":"address"},{"indexed":false,"internalType":"address","name":"newAdmin","type":"address"}],"name":"AdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address","name":"admin_","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newAdmin","type":"address"}],"name":"changeAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"implementation","outputs":[{"internalType":"address","name":"implementation_","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"stateMutability":"payable","type":"function"},{"stateMutability":"payable","type":"receive"}]
ETHSwapAgentUpgradeableProxy bytecode
0x608060405234801561001057600080fd5b506040516109b33803806109b38339818101604052606081101561003357600080fd5b8151602083015160408085018051915193959294830192918464010000000082111561005e57600080fd5b90830190602082018581111561007357600080fd5b825164010000000081118282018810171561008d57600080fd5b82525081516020918201929091019080838360005b838110156100ba5781810151838201526020016100a2565b50505050905090810190601f1680156100e75780820380516001836020036101000a031916815260200191505b5060408181527f656970313936372e70726f78792e696d706c656d656e746174696f6e0000000082525190819003601c0190208693508592508491508390829060008051602061095d8339815191526000199091011461014357fe5b610155826001600160e01b0361027a16565b80511561020d576000826001600160a01b0316826040518082805190602001908083835b602083106101985780518252601f199092019160209182019101610179565b6001836020036101000a038019825116818451168082178552505050505050905001915050600060405180830381855af49150503d80600081146101f8576040519150601f19603f3d011682016040523d82523d6000602084013e6101fd565b606091505b505090508061020b57600080fd5b505b5050604080517f656970313936372e70726f78792e61646d696e000000000000000000000000008152905190819003601301902060008051602061093d8339815191526000199091011461025d57fe5b61026f826001600160e01b036102da16565b5050505050506102f2565b61028d816102ec60201b61054e1760201c565b6102c85760405162461bcd60e51b815260040180806020018281038252603681526020018061097d6036913960400191505060405180910390fd5b60008051602061095d83398151915255565b60008051602061093d83398151915255565b3b151590565b61063c806103016000396000f3fe60806040526004361061004e5760003560e01c80633659cfe6146100655780634f1ef286146100985780635c60da1b146101185780638f28397014610149578063f851a4401461017c5761005d565b3661005d5761005b610191565b005b61005b610191565b34801561007157600080fd5b5061005b6004803603602081101561008857600080fd5b50356001600160a01b03166101ab565b61005b600480360360408110156100ae57600080fd5b6001600160a01b0382351691908101906040810160208201356401000000008111156100d957600080fd5b8201836020820111156100eb57600080fd5b8035906020019184600183028401116401000000008311171561010d57600080fd5b5090925090506101e5565b34801561012457600080fd5b5061012d610292565b604080516001600160a01b039092168252519081900360200190f35b34801561015557600080fd5b5061005b6004803603602081101561016c57600080fd5b50356001600160a01b03166102cf565b34801561018857600080fd5b5061012d610389565b6101996103b4565b6101a96101a4610414565b610439565b565b6101b361045d565b6001600160a01b0316336001600160a01b031614156101da576101d581610482565b6101e2565b6101e2610191565b50565b6101ed61045d565b6001600160a01b0316336001600160a01b031614156102855761020f83610482565b6000836001600160a01b031683836040518083838082843760405192019450600093509091505080830381855af49150503d806000811461026c576040519150601f19603f3d011682016040523d82523d6000602084013e610271565b606091505b505090508061027f57600080fd5b5061028d565b61028d610191565b505050565b600061029c61045d565b6001600160a01b0316336001600160a01b031614156102c4576102bd610414565b90506102cc565b6102cc610191565b90565b6102d761045d565b6001600160a01b0316336001600160a01b031614156101da576001600160a01b0381166103355760405162461bcd60e51b815260040180806020018281038252603a815260200180610555603a913960400191505060405180910390fd5b7f7e644d79422f17c01e4894b5f4f588d331ebfa28653d42ae832dc59e38c9798f61035e61045d565b604080516001600160a01b03928316815291841660208301528051918290030190a16101d5816104c2565b600061039361045d565b6001600160a01b0316336001600160a01b031614156102c4576102bd61045d565b6103bc61045d565b6001600160a01b0316336001600160a01b0316141561040c5760405162461bcd60e51b81526004018080602001828103825260428152602001806105c56042913960600191505060405180910390fd5b6101a96101a9565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5490565b3660008037600080366000845af43d6000803e808015610458573d6000f35b3d6000fd5b7fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d61035490565b61048b816104e6565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b7fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d610355565b6104ef8161054e565b61052a5760405162461bcd60e51b815260040180806020018281038252603681526020018061058f6036913960400191505060405180910390fd5b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc55565b3b15159056fe5472616e73706172656e745570677261646561626c6550726f78793a206e65772061646d696e20697320746865207a65726f20616464726573735570677261646561626c6550726f78793a206e657720696d706c656d656e746174696f6e206973206e6f74206120636f6e74726163745472616e73706172656e745570677261646561626c6550726f78793a2061646d696e2063616e6e6f742066616c6c6261636b20746f2070726f787920746172676574a2646970667358221220b76161e075f62521c54fef8e9af35ad8ae87258a9bd53744da1e6ed02b2aa47d64736f6c63430006040033b53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5570677261646561626c6550726f78793a206e657720696d706c656d656e746174696f6e206973206e6f74206120636f6e7472616374
部署参数
  • _logic (逻辑合约,也就是上面部署的ETHSwapAgentImpl合约地址)0x4e99e2A70664f30d14b1f703b8bbe077872F5Fb7
  • Admin_ (升级管理权限地址,需要一个单独的地址,只用于升级合约,不能与当前部署ETHSwapAgentImpl合约的ownerAddr地址相同)0x48F155527f25EB1d4cb2aa32b7e84692AA0025C0
  • _data (ETHSwapAgentImpl合约中的initialize初始化bytes数据)
    ETHSwapAgentImpl中的initialize实现如下
    function initialize(uint256 fee, address payable ownerAddr) public initializer {
      swapFee = fee;
      owner = ownerAddr;
    }

    其中的ownerAddr为逻辑合约管理权限地址,切记不能与升级管理权限地址相同,否则无法执行。fee为跨链需要缴纳的手续费。
    简单说就是部署ETHSwapAgentUpgradeableProxy合约时,会把ETHSwapAgentImpl合约一同初始化{_logic.delegatecall(_data)},此时测试时预定的参数如下

    • fee = 100000000
    • ownerAddr = 0xbb93FcC2bB90D46255c43DD398A1A372E12bE6Aa
      此时计算_data如下
      web3js.eth.abi.encodeFunctionCall({
      name: 'initialize',
      type: 'function',
      inputs: [{
        type: 'uint256',
        name: 'fee'
      },{
        type: 'address',
        name: 'ownerAddr'
      }]
      }, [100000000, '0xbb93FcC2bB90D46255c43DD398A1A372E12bE6Aa']);

      得到

      0xda35a26f0000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000bb93fcc2bb90d46255c43dd398a1a372e12be6aa

最终三个参数为

  • _logic0x4e99e2A70664f30d14b1f703b8bbe077872F5Fb7
  • Admin_0x48F155527f25EB1d4cb2aa32b7e84692AA0025C0
  • _data0xda35a26f0000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000bb93fcc2bb90d46255c43dd398a1a372e12be6aa

部署后的地址是0x31366023017888A1ef48ca60cbB5de26aCBe6b02

测试ETHSwapAgentImpl合约

通过逻辑合约管理权限地址0xbb93FcC2bB90D46255c43DD398A1A372E12bE6Aa使用ETHSwapAgentImpl ABI访问ETHSwapAgentUpgradeableProxy部署地址0x31366023017888A1ef48ca60cbB5de26aCBe6b02

  1. 查询swapFee等于100000000,然后使用setSwapFee修改值为100000000000000,再次查询确认值与修改值相同
  2. 使用registerSwapPairToBSC注册已发行ERC20代币,代币地址0x8ee79e91b1f7b882941f0c11bb91a760134bc644,并通过registeredERC20确认注册成功

测试ETHSwapAgentUpgradeableProxy合约

通过升级合约管理权限地址0x48F155527f25EB1d4cb2aa32b7e84692AA0025C0使用ETHSwapAgentUpgradeableProxy ABI访问ETHSwapAgentUpgradeableProxy部署地址0x31366023017888A1ef48ca60cbB5de26aCBe6b02

  1. 使用changeAdmin修改当前升级合约管理权限地址0x10210572d6b4924Af7Ef946136295e9b209E1FA0,然后验证权限生效,再修改回0x48F155527f25EB1d4cb2aa32b7e84692AA0025C0
    一定确认好执行的权限地址是对应的,否则只报执行合约出错
  2. ETHSwapAgentImp合约中增加一个方法,
    function hellowWord() public view returns (string memory) {
     return "https://www.bcskill.com";
    }

    并重新编译和部署,新部署的地址为0x63F27AEE1A7C228D05f67C43F6C5ED82d726b104
    使用upgradeTo修改新逻辑合约地址NewImplementation0x63F27AEE1A7C228D05f67C43F6C5ED82d726b104
    然后通过逻辑合约管理权限地址,使用修改后ETHSwapAgentImpl合约编译得到的ABI,访问ETHSwapAgentUpgradeableProxy部署地址0x31366023017888A1ef48ca60cbB5de26aCBe6b02,执行hellowWord
    正确返回https://www.bcskill.com
    并再次验证测试ETHSwapAgentImpl合约中的流程,保证结果都正确

此时ETHSwapAgent合约相关部署已完成,

BSC侧链合约部署

对于BSCSwapAgent相关侧链部署同理,再此不做详细讲解,只做数据的记录

部署网络

Ropsten Test Network

1. BEP20Token合约部署

BEP20TokenImplementation ABI

[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"uint8","name":"decimals","type":"uint8"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bool","name":"mintable","type":"bool"},{"internalType":"address","name":"owner","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"mintable","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"}],"name":"mintTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

BEP20TokenImplementation bytecode

0x608060405234801561001057600080fd5b506114b2806100206000396000f3fe608060405234801561001057600080fd5b50600436106101215760003560e01c8063715018a6116100ad578063a9059cbb11610071578063a9059cbb14610347578063b723b34e14610373578063dd62ed3e1461039f578063ef3ebcb8146103cd578063f2fde38b1461051957610121565b8063715018a6146102c8578063893d20e8146102d257806395d89b41146102f6578063a0712d68146102fe578063a457c2d71461031b57610121565b8063313ce567116100f4578063313ce56714610233578063395093511461025157806342966c681461027d5780634bf365df1461029a57806370a08231146102a257610121565b806306fdde0314610126578063095ea7b3146101a357806318160ddd146101e357806323b872dd146101fd575b600080fd5b61012e61053f565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610168578181015183820152602001610150565b50505050905090810190601f1680156101955780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101cf600480360360408110156101b957600080fd5b506001600160a01b0381351690602001356105d6565b604080519115158252519081900360200190f35b6101eb6105f3565b60408051918252519081900360200190f35b6101cf6004803603606081101561021357600080fd5b506001600160a01b038135811691602081013590911690604001356105f9565b61023b610686565b6040805160ff9092168252519081900360200190f35b6101cf6004803603604081101561026757600080fd5b506001600160a01b03813516906020013561068f565b6101cf6004803603602081101561029357600080fd5b50356106e3565b6101cf6106fe565b6101eb600480360360208110156102b857600080fd5b50356001600160a01b031661070e565b6102d0610729565b005b6102da6107d6565b604080516001600160a01b039092168252519081900360200190f35b61012e6107ea565b6101cf6004803603602081101561031457600080fd5b503561084b565b6101cf6004803603604081101561033157600080fd5b506001600160a01b038135169060200135610919565b6101cf6004803603604081101561035d57600080fd5b506001600160a01b038135169060200135610987565b6101cf6004803603604081101561038957600080fd5b50803590602001356001600160a01b031661099b565b6101eb600480360360408110156103b557600080fd5b506001600160a01b0381358116916020013516610a62565b6102d0600480360360c08110156103e357600080fd5b8101906020810181356401000000008111156103fe57600080fd5b82018360208201111561041057600080fd5b8035906020019184600183028401116401000000008311171561043257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929594936020810193503591505064010000000081111561048557600080fd5b82018360208201111561049757600080fd5b803590602001918460018302840111640100000000831117156104b957600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295505050813560ff169250506020810135906040810135151590606001356001600160a01b0316610a8d565b6102d06004803603602081101561052f57600080fd5b50356001600160a01b0316610ba7565b60048054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156105cb5780601f106105a0576101008083540402835291602001916105cb565b820191906000526020600020905b8154815290600101906020018083116105ae57829003601f168201915b505050505090505b90565b60006105ea6105e3610cb0565b8484610cb4565b50600192915050565b60035490565b6000610606848484610da0565b61067c84610612610cb0565b61067785604051806060016040528060288152602001611334602891396001600160a01b038a16600090815260026020526040812090610650610cb0565b6001600160a01b03168152602081019190915260400160002054919063ffffffff610efe16565b610cb4565b5060019392505050565b60065460ff1690565b60006105ea61069c610cb0565b8461067785600260006106ad610cb0565b6001600160a01b03908116825260208083019390935260409182016000908120918c16815292529020549063ffffffff610f9516565b60006106f66106f0610cb0565b83610ff6565b506001919050565b600654600160a81b900460ff1690565b6001600160a01b031660009081526001602052604090205490565b610731610cb0565b60065461010090046001600160a01b03908116911614610786576040805162461bcd60e51b8152602060048201819052602482015260008051602061138a833981519152604482015290519081900360640190fd5b60065460405160009161010090046001600160a01b0316907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0908390a360068054610100600160a81b0319169055565b60065461010090046001600160a01b031690565b60058054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156105cb5780601f106105a0576101008083540402835291602001916105cb565b6000610855610cb0565b60065461010090046001600160a01b039081169116146108aa576040805162461bcd60e51b8152602060048201819052602482015260008051602061138a833981519152604482015290519081900360640190fd5b600654600160a81b900460ff16610908576040805162461bcd60e51b815260206004820152601a60248201527f7468697320746f6b656e206973206e6f74206d696e7461626c65000000000000604482015290519081900360640190fd5b6106f6610913610cb0565b836110f2565b60006105ea610926610cb0565b84610677856040518060600160405280602581526020016113f36025913960026000610950610cb0565b6001600160a01b03908116825260208083019390935260409182016000908120918d1681529252902054919063ffffffff610efe16565b60006105ea610994610cb0565b8484610da0565b60006109a5610cb0565b60065461010090046001600160a01b039081169116146109fa576040805162461bcd60e51b8152602060048201819052602482015260008051602061138a833981519152604482015290519081900360640190fd5b600654600160a81b900460ff16610a58576040805162461bcd60e51b815260206004820152601a60248201527f7468697320746f6b656e206973206e6f74206d696e7461626c65000000000000604482015290519081900360640190fd5b6105ea82846110f2565b6001600160a01b03918216600090815260026020908152604080832093909416825291909152205490565b600054610100900460ff1680610aa65750610aa66111e4565b80610ab4575060005460ff16155b610aef5760405162461bcd60e51b815260040180806020018281038252602e81526020018061135c602e913960400191505060405180910390fd5b600054610100900460ff16158015610b1a576000805460ff1961ff0019909116610100171660011790555b60068054610100600160a81b0319166101006001600160a01b038516021790558651610b4d9060049060208a019061122c565b508551610b6190600590602089019061122c565b506006805460ff191660ff87161760ff60a81b1916600160a81b85151502179055610b8c82856110f2565b8015610b9e576000805461ff00191690555b50505050505050565b610baf610cb0565b60065461010090046001600160a01b03908116911614610c04576040805162461bcd60e51b8152602060048201819052602482015260008051602061138a833981519152604482015290519081900360640190fd5b6001600160a01b038116610c495760405162461bcd60e51b815260040180806020018281038252602681526020018061130e6026913960400191505060405180910390fd5b6006546040516001600160a01b0380841692610100900416907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a3600680546001600160a01b0390921661010002610100600160a81b0319909216919091179055565b3390565b6001600160a01b038316610cf95760405162461bcd60e51b81526004018080602001828103825260248152602001806112ea6024913960400191505060405180910390fd5b6001600160a01b038216610d3e5760405162461bcd60e51b815260040180806020018281038252602281526020018061145b6022913960400191505060405180910390fd5b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316610de55760405162461bcd60e51b81526004018080602001828103825260258152602001806112c56025913960400191505060405180910390fd5b6001600160a01b038216610e2a5760405162461bcd60e51b81526004018080602001828103825260238152602001806113d06023913960400191505060405180910390fd5b610e6d816040518060600160405280602681526020016113aa602691396001600160a01b038616600090815260016020526040902054919063ffffffff610efe16565b6001600160a01b038085166000908152600160205260408082209390935590841681522054610ea2908263ffffffff610f9516565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60008184841115610f8d5760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610f52578181015183820152602001610f3a565b50505050905090810190601f168015610f7f5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b600082820183811015610fef576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b9392505050565b6001600160a01b03821661103b5760405162461bcd60e51b81526004018080602001828103825260218152602001806114186021913960400191505060405180910390fd5b61107e81604051806060016040528060228152602001611439602291396001600160a01b038516600090815260016020526040902054919063ffffffff610efe16565b6001600160a01b0383166000908152600160205260409020556003546110aa908263ffffffff6111ea16565b6003556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b6001600160a01b03821661114d576040805162461bcd60e51b815260206004820152601f60248201527f42455032303a206d696e7420746f20746865207a65726f206164647265737300604482015290519081900360640190fd5b600354611160908263ffffffff610f9516565b6003556001600160a01b03821660009081526001602052604090205461118c908263ffffffff610f9516565b6001600160a01b03831660008181526001602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b303b1590565b6000610fef83836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815250610efe565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061126d57805160ff191683800117855561129a565b8280016001018555821561129a579182015b8281111561129a57825182559160200191906001019061127f565b506112a69291506112aa565b5090565b6105d391905b808211156112a657600081556001016112b056fe42455032303a207472616e736665722066726f6d20746865207a65726f206164647265737342455032303a20617070726f76652066726f6d20746865207a65726f20616464726573734f776e61626c653a206e6577206f776e657220697320746865207a65726f206164647265737342455032303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e6365496e697469616c697a61626c653a20636f6e747261637420697320616c726561647920696e697469616c697a65644f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657242455032303a207472616e7366657220616d6f756e7420657863656564732062616c616e636542455032303a207472616e7366657220746f20746865207a65726f206164647265737342455032303a2064656372656173656420616c6c6f77616e63652062656c6f77207a65726f42455032303a206275726e2066726f6d20746865207a65726f206164647265737342455032303a206275726e20616d6f756e7420657863656564732062616c616e636542455032303a20617070726f766520746f20746865207a65726f2061646472657373a2646970667358221220d28ad019253e134d8321300a2878b923db889aa49087e15f1ec77fc72393491a64736f6c63430006040033

部署地址:0x41941cC749d561A13A3a8295f355033D23fDc060

2. BSCSwapAgent合约部署

BSCSwapAgentImpl合约ABI
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"bep20Addr","type":"address"},{"indexed":true,"internalType":"bytes32","name":"ethTxHash","type":"bytes32"},{"indexed":true,"internalType":"address","name":"toAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"SwapFilled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"ethRegisterTxHash","type":"bytes32"},{"indexed":true,"internalType":"address","name":"bep20Addr","type":"address"},{"indexed":true,"internalType":"address","name":"erc20Addr","type":"address"},{"indexed":false,"internalType":"string","name":"symbol","type":"string"},{"indexed":false,"internalType":"string","name":"name","type":"string"},{"indexed":false,"internalType":"uint8","name":"decimals","type":"uint8"}],"name":"SwapPairCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"bep20Addr","type":"address"},{"indexed":true,"internalType":"address","name":"erc20Addr","type":"address"},{"indexed":true,"internalType":"address","name":"fromAddr","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeAmount","type":"uint256"}],"name":"SwapStarted","type":"event"},{"inputs":[],"name":"bep20Implementation","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"bep20ProxyAdmin","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"filledETHTx","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"swapFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"swapMappingBSC2ETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"swapMappingETH2BSC","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"bep20Impl","type":"address"},{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"address payable","name":"ownerAddr","type":"address"},{"internalType":"address","name":"bep20ProxyAdminAddr","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"fee","type":"uint256"}],"name":"setSwapFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"ethTxHash","type":"bytes32"},{"internalType":"address","name":"erc20Addr","type":"address"},{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"uint8","name":"decimals","type":"uint8"}],"name":"createSwapPair","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"ethTxHash","type":"bytes32"},{"internalType":"address","name":"erc20Addr","type":"address"},{"internalType":"address","name":"toAddress","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"fillETH2BSCSwap","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"bep20Addr","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"swapBSC2ETH","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"}]
BSCSwapAgentImpl合约bytecode
0x608060405234801561001057600080fd5b50611db3806100206000396000f3fe608060405260043610620000f25760003560e01c806360b810f1116200008b5780638da5cb5b11620000615780638da5cb5b146200039b578063be0ace6914620003b3578063e307b93114620003ea578063f2fde38b146200043757620000f2565b806360b810f1146200033457806366fec65c146200036b578063715018a6146200038357620000f2565b806334e1990711620000cd57806334e19907146200025f578063358394d8146200028f5780634e2dc7f114620002dc57806354cf2aeb146200030a57620000f2565b80630344165a14620000f75780631ba3b150146200012b57806332bd6e31146200016e575b600080fd5b3480156200010457600080fd5b506200010f6200046e565b604080516001600160a01b039092168252519081900360200190f35b6200015a600480360360408110156200014357600080fd5b506001600160a01b0381351690602001356200047d565b604080519115158252519081900360200190f35b3480156200017b57600080fd5b506200010f600480360360a08110156200019457600080fd5b8135916001600160a01b0360208201351691810190606081016040820135640100000000811115620001c557600080fd5b820183602082011115620001d857600080fd5b80359060200191846001830284011164010000000083111715620001fb57600080fd5b9193909290916020810190356401000000008111156200021a57600080fd5b8201836020820111156200022d57600080fd5b803590602001918460018302840111640100000000831117156200025057600080fd5b91935091503560ff1662000717565b3480156200026c57600080fd5b506200028d600480360360208110156200028557600080fd5b503562000a24565b005b3480156200029c57600080fd5b506200028d60048036036080811015620002b557600080fd5b506001600160a01b0381358116916020810135916040820135811691606001351662000a85565b348015620002e957600080fd5b506200015a600480360360208110156200030257600080fd5b503562000b75565b3480156200031757600080fd5b506200032262000b8a565b60408051918252519081900360200190f35b3480156200034157600080fd5b506200010f600480360360208110156200035a57600080fd5b50356001600160a01b031662000b90565b3480156200037857600080fd5b506200010f62000bab565b3480156200039057600080fd5b506200028d62000bba565b348015620003a857600080fd5b506200010f62000c60565b348015620003c057600080fd5b506200010f60048036036020811015620003d957600080fd5b50356001600160a01b031662000c6f565b348015620003f757600080fd5b506200015a600480360360808110156200041057600080fd5b508035906001600160a01b0360208201358116916040810135909116906060013562000c8a565b3480156200044457600080fd5b506200028d600480360360208110156200045d57600080fd5b50356001600160a01b031662000e9f565b6005546001600160a01b031681565b60006200048a3362000f9e565b15620004dd576040805162461bcd60e51b815260206004820152601f60248201527f636f6e7472616374206973206e6f7420616c6c6f77656420746f207377617000604482015290519081900360640190fd5b33321462000532576040805162461bcd60e51b815260206004820152601c60248201527f6e6f2070726f787920636f6e747261637420697320616c6c6f77656400000000604482015290519081900360640190fd5b6001600160a01b038084166000908152600260205260409020541680620005a0576040805162461bcd60e51b815260206004820152601b60248201527f6e6f2073776170207061697220666f72207468697320746f6b656e0000000000604482015290519081900360640190fd5b6007543414620005ec576040805162461bcd60e51b81526020600482015260126024820152711cddd85c08199959481b9bdd08195c5d585b60721b604482015290519081900360640190fd5b620006096001600160a01b03851633308663ffffffff62000fa416565b836001600160a01b03166342966c68846040518263ffffffff1660e01b815260040180828152602001915050602060405180830381600087803b1580156200065057600080fd5b505af115801562000665573d6000803e3d6000fd5b505050506040513d60208110156200067c57600080fd5b50503415620006c1576004546040516001600160a01b03909116903480156108fc02916000818181858888f19350505050158015620006bf573d6000803e3d6000fd5b505b60408051848152346020820152815133926001600160a01b0385811693908916927f49c08ff11118922c1e8298915531eff9ef6f8b39b44b3e9952b75d47e1d0cdd0929181900390910190a45060019392505050565b60006200072362001006565b6004546001600160a01b0390811691161462000775576040805162461bcd60e51b8152602060048201819052602482015260008051602062001d34833981519152604482015290519081900360640190fd5b6001600160a01b038781166000908152600160205260409020541615620007da576040805162461bcd60e51b8152602060048201526014602482015273323ab83634b1b0ba32b21039bbb0b8103830b4b960611b604482015290519081900360640190fd5b6006546005546040516000926001600160a01b03908116921690620007ff90620012f8565b6001600160a01b03928316815291166020820152606060408083018290526000918301829052519182900360a0019190f08015801562000843573d6000803e3d6000fd5b50604051631de7d79760e31b815260ff851660448201526000606482018190526001608483018190523060a4840181905260c06004850190815260c485018c905294955085946001600160a01b0386169463ef3ebcb8948e948e948e948e948e94939291908190602481019060e4018b8b80828437600083820152601f01601f191690910184810383528981526020019050898980828437600081840152601f19601f8201169050808301925050509a5050505050505050505050600060405180830381600087803b1580156200091957600080fd5b505af11580156200092e573d6000803e3d6000fd5b5050506001600160a01b03808b16600081815260016020908152604080832080549588166001600160a01b03199687168117909155808452600283529281902080549095168417909455835160ff8a1694810194909452606080855284018a9052919350918d917fcc0314763eabceb74cd3d30ae785c09bfe4e204af2088b3bfcdbbe5082133db5918b918b918f918f918d918190810160808201888880828437600083820152601f01601f191690910184810383528681526020019050868680828437600083820152604051601f909101601f1916909201829003995090975050505050505050a49998505050505050505050565b62000a2e62001006565b6004546001600160a01b0390811691161462000a80576040805162461bcd60e51b8152602060048201819052602482015260008051602062001d34833981519152604482015290519081900360640190fd5b600755565b600054610100900460ff168062000aa1575062000aa16200100a565b8062000ab0575060005460ff16155b62000aed5760405162461bcd60e51b815260040180806020018281038252602e81526020018062001d06602e913960400191505060405180910390fd5b600054610100900460ff1615801562000b19576000805460ff1961ff0019909116610100171660011790555b600680546001600160a01b038088166001600160a01b0319928316179092556007869055600480548684169083161790556005805492851692909116919091179055801562000b6e576000805461ff00191690555b5050505050565b60036020526000908152604090205460ff1681565b60075481565b6001602052600090815260409020546001600160a01b031681565b6006546001600160a01b031681565b62000bc462001006565b6004546001600160a01b0390811691161462000c16576040805162461bcd60e51b8152602060048201819052602482015260008051602062001d34833981519152604482015290519081900360640190fd5b6004546040516000916001600160a01b0316907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0908390a3600480546001600160a01b0319169055565b6004546001600160a01b031681565b6002602052600090815260409020546001600160a01b031681565b600062000c9662001006565b6004546001600160a01b0390811691161462000ce8576040805162461bcd60e51b8152602060048201819052602482015260008051602062001d34833981519152604482015290519081900360640190fd5b60008581526003602052604090205460ff161562000d45576040805162461bcd60e51b81526020600482015260156024820152746574682074782066696c6c656420616c726561647960581b604482015290519081900360640190fd5b6001600160a01b03808516600090815260016020526040902054168062000db3576040805162461bcd60e51b815260206004820152601b60248201527f6e6f2073776170207061697220666f72207468697320746f6b656e0000000000604482015290519081900360640190fd5b6000868152600360209081526040808320805460ff191660011790558051635b91d9a760e11b8152600481018790526001600160a01b03888116602483015291519185169363b723b34e9360448084019491939192918390030190829087803b15801562000e2057600080fd5b505af115801562000e35573d6000803e3d6000fd5b505050506040513d602081101562000e4c57600080fd5b50506040805184815290516001600160a01b03808716928992918516917f3bebd9a738291e69898b5dbfadb6329b4b09fc648bdef68762928e521463abd99181900360200190a450600195945050505050565b62000ea962001006565b6004546001600160a01b0390811691161462000efb576040805162461bcd60e51b8152602060048201819052602482015260008051602062001d34833981519152604482015290519081900360640190fd5b6001600160a01b03811662000f425760405162461bcd60e51b815260040180806020018281038252602681526020018062001cba6026913960400191505060405180910390fd5b6004546040516001600160a01b038084169216907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a3600480546001600160a01b0319166001600160a01b0392909216919091179055565b3b151590565b604080516001600160a01b0380861660248301528416604482015260648082018490528251808303909101815260849091019091526020810180516001600160e01b03166323b872dd60e01b1790526200100090859062001010565b50505050565b3390565b303b1590565b606062001067826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316620010cc9092919063ffffffff16565b805190915015620010c7578080602001905160208110156200108857600080fd5b5051620010c75760405162461bcd60e51b815260040180806020018281038252602a81526020018062001d54602a913960400191505060405180910390fd5b505050565b6060620010dd8484600085620010e7565b90505b9392505050565b6060824710156200112a5760405162461bcd60e51b815260040180806020018281038252602681526020018062001ce06026913960400191505060405180910390fd5b620011358562000f9e565b62001187576040805162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015290519081900360640190fd5b60006060866001600160a01b031685876040518082805190602001908083835b60208310620011c85780518252601f199092019160209182019101620011a7565b6001836020036101000a03801982511681845116808217855250505050505090500191505060006040518083038185875af1925050503d80600081146200122c576040519150601f19603f3d011682016040523d82523d6000602084013e62001231565b606091505b5091509150620012438282866200124e565b979650505050505050565b606083156200125f575081620010e0565b825115620012705782518084602001fd5b8160405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015620012bc578181015183820152602001620012a2565b50505050905090810190601f168015620012ea5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b6109b380620013078339019056fe608060405234801561001057600080fd5b506040516109b33803806109b38339818101604052606081101561003357600080fd5b8151602083015160408085018051915193959294830192918464010000000082111561005e57600080fd5b90830190602082018581111561007357600080fd5b825164010000000081118282018810171561008d57600080fd5b82525081516020918201929091019080838360005b838110156100ba5781810151838201526020016100a2565b50505050905090810190601f1680156100e75780820380516001836020036101000a031916815260200191505b5060408181527f656970313936372e70726f78792e696d706c656d656e746174696f6e0000000082525190819003601c0190208693508592508491508390829060008051602061095d8339815191526000199091011461014357fe5b610155826001600160e01b0361027a16565b80511561020d576000826001600160a01b0316826040518082805190602001908083835b602083106101985780518252601f199092019160209182019101610179565b6001836020036101000a038019825116818451168082178552505050505050905001915050600060405180830381855af49150503d80600081146101f8576040519150601f19603f3d011682016040523d82523d6000602084013e6101fd565b606091505b505090508061020b57600080fd5b505b5050604080517f656970313936372e70726f78792e61646d696e000000000000000000000000008152905190819003601301902060008051602061093d8339815191526000199091011461025d57fe5b61026f826001600160e01b036102da16565b5050505050506102f2565b61028d816102ec60201b61054e1760201c565b6102c85760405162461bcd60e51b815260040180806020018281038252603681526020018061097d6036913960400191505060405180910390fd5b60008051602061095d83398151915255565b60008051602061093d83398151915255565b3b151590565b61063c806103016000396000f3fe60806040526004361061004e5760003560e01c80633659cfe6146100655780634f1ef286146100985780635c60da1b146101185780638f28397014610149578063f851a4401461017c5761005d565b3661005d5761005b610191565b005b61005b610191565b34801561007157600080fd5b5061005b6004803603602081101561008857600080fd5b50356001600160a01b03166101ab565b61005b600480360360408110156100ae57600080fd5b6001600160a01b0382351691908101906040810160208201356401000000008111156100d957600080fd5b8201836020820111156100eb57600080fd5b8035906020019184600183028401116401000000008311171561010d57600080fd5b5090925090506101e5565b34801561012457600080fd5b5061012d610292565b604080516001600160a01b039092168252519081900360200190f35b34801561015557600080fd5b5061005b6004803603602081101561016c57600080fd5b50356001600160a01b03166102cf565b34801561018857600080fd5b5061012d610389565b6101996103b4565b6101a96101a4610414565b610439565b565b6101b361045d565b6001600160a01b0316336001600160a01b031614156101da576101d581610482565b6101e2565b6101e2610191565b50565b6101ed61045d565b6001600160a01b0316336001600160a01b031614156102855761020f83610482565b6000836001600160a01b031683836040518083838082843760405192019450600093509091505080830381855af49150503d806000811461026c576040519150601f19603f3d011682016040523d82523d6000602084013e610271565b606091505b505090508061027f57600080fd5b5061028d565b61028d610191565b505050565b600061029c61045d565b6001600160a01b0316336001600160a01b031614156102c4576102bd610414565b90506102cc565b6102cc610191565b90565b6102d761045d565b6001600160a01b0316336001600160a01b031614156101da576001600160a01b0381166103355760405162461bcd60e51b815260040180806020018281038252603a815260200180610555603a913960400191505060405180910390fd5b7f7e644d79422f17c01e4894b5f4f588d331ebfa28653d42ae832dc59e38c9798f61035e61045d565b604080516001600160a01b03928316815291841660208301528051918290030190a16101d5816104c2565b600061039361045d565b6001600160a01b0316336001600160a01b031614156102c4576102bd61045d565b6103bc61045d565b6001600160a01b0316336001600160a01b0316141561040c5760405162461bcd60e51b81526004018080602001828103825260428152602001806105c56042913960600191505060405180910390fd5b6101a96101a9565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5490565b3660008037600080366000845af43d6000803e808015610458573d6000f35b3d6000fd5b7fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d61035490565b61048b816104e6565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b7fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d610355565b6104ef8161054e565b61052a5760405162461bcd60e51b815260040180806020018281038252603681526020018061058f6036913960400191505060405180910390fd5b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc55565b3b15159056fe5472616e73706172656e745570677261646561626c6550726f78793a206e65772061646d696e20697320746865207a65726f20616464726573735570677261646561626c6550726f78793a206e657720696d706c656d656e746174696f6e206973206e6f74206120636f6e74726163745472616e73706172656e745570677261646561626c6550726f78793a2061646d696e2063616e6e6f742066616c6c6261636b20746f2070726f787920746172676574a2646970667358221220d51b9a3c2a10700d22ce61992f1afda59614179c27719ea9d498bfc20e22c93d64736f6c63430006040033b53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5570677261646561626c6550726f78793a206e657720696d706c656d656e746174696f6e206973206e6f74206120636f6e74726163744f776e61626c653a206e6577206f776e657220697320746865207a65726f2061646472657373416464726573733a20696e73756666696369656e742062616c616e636520666f722063616c6c496e697469616c697a61626c653a20636f6e747261637420697320616c726561647920696e697469616c697a65644f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65725361666545524332303a204552433230206f7065726174696f6e20646964206e6f742073756363656564a2646970667358221220c84fdff4cabd7d2277fe8f8ac5de02e6b150109b949e9a549407a80a0a4b3db664736f6c63430006040033

部署后地址:0xc5612639db6250a9e44A7d846fe60adCE1f65E53

BSCSwapAgentUpgradeableProxy 合约abi

[{"inputs":[{"internalType":"address","name":"logic","type":"address"},{"internalType":"address","name":"admin","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"previousAdmin","type":"address"},{"indexed":false,"internalType":"address","name":"newAdmin","type":"address"}],"name":"AdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address","name":"admin_","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newAdmin","type":"address"}],"name":"changeAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"implementation","outputs":[{"internalType":"address","name":"implementation_","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"stateMutability":"payable","type":"function"},{"stateMutability":"payable","type":"receive"}]

BSCSwapAgentUpgradeableProxy 合约bytecode

0x608060405234801561001057600080fd5b506040516109b33803806109b38339818101604052606081101561003357600080fd5b8151602083015160408085018051915193959294830192918464010000000082111561005e57600080fd5b90830190602082018581111561007357600080fd5b825164010000000081118282018810171561008d57600080fd5b82525081516020918201929091019080838360005b838110156100ba5781810151838201526020016100a2565b50505050905090810190601f1680156100e75780820380516001836020036101000a031916815260200191505b5060408181527f656970313936372e70726f78792e696d706c656d656e746174696f6e0000000082525190819003601c0190208693508592508491508390829060008051602061095d8339815191526000199091011461014357fe5b610155826001600160e01b0361027a16565b80511561020d576000826001600160a01b0316826040518082805190602001908083835b602083106101985780518252601f199092019160209182019101610179565b6001836020036101000a038019825116818451168082178552505050505050905001915050600060405180830381855af49150503d80600081146101f8576040519150601f19603f3d011682016040523d82523d6000602084013e6101fd565b606091505b505090508061020b57600080fd5b505b5050604080517f656970313936372e70726f78792e61646d696e000000000000000000000000008152905190819003601301902060008051602061093d8339815191526000199091011461025d57fe5b61026f826001600160e01b036102da16565b5050505050506102f2565b61028d816102ec60201b61054e1760201c565b6102c85760405162461bcd60e51b815260040180806020018281038252603681526020018061097d6036913960400191505060405180910390fd5b60008051602061095d83398151915255565b60008051602061093d83398151915255565b3b151590565b61063c806103016000396000f3fe60806040526004361061004e5760003560e01c80633659cfe6146100655780634f1ef286146100985780635c60da1b146101185780638f28397014610149578063f851a4401461017c5761005d565b3661005d5761005b610191565b005b61005b610191565b34801561007157600080fd5b5061005b6004803603602081101561008857600080fd5b50356001600160a01b03166101ab565b61005b600480360360408110156100ae57600080fd5b6001600160a01b0382351691908101906040810160208201356401000000008111156100d957600080fd5b8201836020820111156100eb57600080fd5b8035906020019184600183028401116401000000008311171561010d57600080fd5b5090925090506101e5565b34801561012457600080fd5b5061012d610292565b604080516001600160a01b039092168252519081900360200190f35b34801561015557600080fd5b5061005b6004803603602081101561016c57600080fd5b50356001600160a01b03166102cf565b34801561018857600080fd5b5061012d610389565b6101996103b4565b6101a96101a4610414565b610439565b565b6101b361045d565b6001600160a01b0316336001600160a01b031614156101da576101d581610482565b6101e2565b6101e2610191565b50565b6101ed61045d565b6001600160a01b0316336001600160a01b031614156102855761020f83610482565b6000836001600160a01b031683836040518083838082843760405192019450600093509091505080830381855af49150503d806000811461026c576040519150601f19603f3d011682016040523d82523d6000602084013e610271565b606091505b505090508061027f57600080fd5b5061028d565b61028d610191565b505050565b600061029c61045d565b6001600160a01b0316336001600160a01b031614156102c4576102bd610414565b90506102cc565b6102cc610191565b90565b6102d761045d565b6001600160a01b0316336001600160a01b031614156101da576001600160a01b0381166103355760405162461bcd60e51b815260040180806020018281038252603a815260200180610555603a913960400191505060405180910390fd5b7f7e644d79422f17c01e4894b5f4f588d331ebfa28653d42ae832dc59e38c9798f61035e61045d565b604080516001600160a01b03928316815291841660208301528051918290030190a16101d5816104c2565b600061039361045d565b6001600160a01b0316336001600160a01b031614156102c4576102bd61045d565b6103bc61045d565b6001600160a01b0316336001600160a01b0316141561040c5760405162461bcd60e51b81526004018080602001828103825260428152602001806105c56042913960600191505060405180910390fd5b6101a96101a9565b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5490565b3660008037600080366000845af43d6000803e808015610458573d6000f35b3d6000fd5b7fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d61035490565b61048b816104e6565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b7fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d610355565b6104ef8161054e565b61052a5760405162461bcd60e51b815260040180806020018281038252603681526020018061058f6036913960400191505060405180910390fd5b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc55565b3b15159056fe5472616e73706172656e745570677261646561626c6550726f78793a206e65772061646d696e20697320746865207a65726f20616464726573735570677261646561626c6550726f78793a206e657720696d706c656d656e746174696f6e206973206e6f74206120636f6e74726163745472616e73706172656e745570677261646561626c6550726f78793a2061646d696e2063616e6e6f742066616c6c6261636b20746f2070726f787920746172676574a264697066735822122090c377aaf4226453bc11a4d3569b56498c596cfd128747512a8e6687b2339d8d64736f6c63430006040033b53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5570677261646561626c6550726f78793a206e657720696d706c656d656e746174696f6e206973206e6f74206120636f6e7472616374

此时计算_data如下

web3js.eth.abi.encodeFunctionCall({
    name: 'initialize',
    type: 'function',
    inputs: [{
        type: 'address',
        name: 'bep20Impl'
    },{
        type: 'uint256',
        name: 'fee'
    },{
        type: 'address',
        name: 'ownerAddr'
    },{
        type: 'address',
        name: 'bep20ProxyAdminAddr'
    }]
}, ['0x41941cC749d561A13A3a8295f355033D23fDc060',100000000, '0xbb93FcC2bB90D46255c43DD398A1A372E12bE6Aa','0x48F155527f25EB1d4cb2aa32b7e84692AA0025C0']);

得到

0x358394d800000000000000000000000041941cc749d561a13a3a8295f355033d23fdc0600000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000bb93fcc2bb90d46255c43dd398a1a372e12be6aa00000000000000000000000048f155527f25eb1d4cb2aa32b7e84692aa0025c0

最终三个参数为

  • _logic0xc5612639db6250a9e44A7d846fe60adCE1f65E53
  • Admin_0xF8329Ea42489C2164F44A81DEb3bE22c2C456109
  • _data0x358394d800000000000000000000000041941cc749d561a13a3a8295f355033d23fdc0600000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000bb93fcc2bb90d46255c43dd398a1a372e12be6aa00000000000000000000000048f155527f25eb1d4cb2aa32b7e84692aa0025c0

部署后的地址:0x2dd3DC6ec3ddC15d05C3719EB329a7E64e00Aad5

BSC合约管理地址

升级管理地址:0xF8329Ea42489C2164F44A81DEb3bE22c2C456109
逻辑合约管理地址:0xbb93FcC2bB90D46255c43DD398A1A372E12bE6Aa
Bep20合约管理地址:0x48F155527f25EB1d4cb2aa32b7e84692AA0025C0

Swap程序部署

编译程序

git clone https://github.com/binance-chain/bsc-eth-swap.git
make build

编译完,服务程序会生成在bsc-eth-swap\build\swap-backend

config配置

由于没有文档,还是看代码吧,先查看demo config config\config.json,以及config解析的代码
从代码角度完善配置参数以及参数作用。

{
  "key_manager_config": {
    "key_type": "local_private_key",
    "aws_region": "",
    "aws_secret_name": "",

    "local_hmac_key": "",
    "local_bsc_private_key": "",
    "local_eth_private_key": "",
    "local_admin_api_key":"",
    "local_admin_secret_key":""
  },
  "db_config": {
    "dialect": "sqlite3",
    "db_path": "./build/test.db"
  },
  "chain_config": {
    "balance_monitor_interval": 60,

    "bsc_observer_fetch_interval": 1,
    "bsc_start_height": 6010146,
    "bsc_provider": "https://ropsten.infura.io/v3/c4f8f662c565463e8e3285708cb41823",
    "bsc_confirm_num": 2,
    "bsc_swap_agent_addr":"0x2dd3DC6ec3ddC15d05C3719EB329a7E64e00Aad5",
    "bsc_explorer_url": "https://ropsten.etherscan.io/tx",
    "bsc_max_track_retry": 60,
    "bsc_alert_threshold": "1000000000000000000",
    "bsc_wait_milli_sec_between_swaps": 100,

    "eth_observer_fetch_interval": 10,
    "eth_start_height": 8018001,
    "eth_provider": "https://goerli.infura.io/v3/87aba2e668f9410498cdf74bc7a35467",
    "eth_confirm_num": 1,
    "eth_swap_agent_addr": "0x31366023017888A1ef48ca60cbB5de26aCBe6b02",
    "eth_explorer_url": "https://goerli.etherscan.io/tx",
    "eth_max_track_retry": 600,
    "eth_alert_threshold": "1000000000000000000",
    "eth_wait_milli_sec_between_swaps": 200
  },
  "log_config": {
    "level": "INFO",
    "filename": "build/test.log",
    "max_file_size_in_mb": 100,
    "max_backups_of_log_files": 1,
    "max_age_to_retain_log_files_in_days": 30,
    "use_console_logger": false,
    "use_file_logger": true,
    "compress": false
  },
  "alert_config": {
    "telegram_bot_id": "",
    "telegram_chat_id": "",
    "block_update_timeout": 10
  },
  "admin_config": {
    "listen_addr": ":8000"
  }
}

启动程序

./build/swap-backend --config-type local --config-path config/config.json

unknown block

由于现有项目不支持 EIP-1559,导致解析 block head错误,block hash无效,报 unknown block

支持EIP-1559

更新 go-ethereum模块
go get github.com/ethereum/go-ethereum@v1.10.8

需要重新生成\bsc-eth-swap\abi\*.goabi.go文件
需要准备合约编译工具环境

首次需要安装solc
sudo pip3 install solc-select
sudo solc-select install 0.6.4 //安装指定版本solc
solc-select use 0.6.4

查看solc安装位置

whereis solc
/usr/local/bin/solc /home/surou/.local/bin/solc

添加到环境变量,或者移动到默认/usr/bin

sudo cp /usr/local/bin/solc  /usr/bin

查看版本solc版本

solc --version
solc, the solidity compiler commandline interface
Version: 0.6.4+commit.1dca32f3.Linux.g++
首次需要安装protobuf
sudo apt install protobuf-compiler

编译go-ethereum

git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum/
make all

最终生成在go-ethereum\build\bin\abigen
复制到全局

sudo cp ./build/bin/abigen /usr/bin/

重新生成abi和bin

git clone https://github.com/binance-chain/eth-bsc-swap-contracts.git
cd eth-bsc-swap-contracts/
npm run flatten //合并依赖为单文件
cd eth-bsc-swap-contracts/contracts/flattened
BSCSwapAgentImpl
solc --bin ./BSCSwapAgentImpl.sol > BSCSwapAgent.bin //生成文件中只保留BSCSwapAgentImpl独立内容
solc --abi ./BSCSwapAgentImpl.sol > BSCSwapAgent.abi //生成文件中只保留BSCSwapAgentImpl独立内容
abigen --bin=BSCSwapAgent.bin --abi=BSCSwapAgent.abi --pkg=BSCSwapAgent --out=BSCSwapAgent.go
ETHSwapAgentImpl
solc --bin ./ETHSwapAgentImpl.sol > ETHSwapAgent.bin //生成文件中只保留ETHSwapAgentImpl独立内容
solc --abi ./ETHSwapAgentImpl.sol > ETHSwapAgent.abi //生成文件中只保留ETHSwapAgentImpl独立内容
abigen --bin=ETHSwapAgent.bin --abi=ETHSwapAgent.abi --pkg=ETHSwapAgent --out=ETHSwapAgent.go

回到 \bsc-eth-swap\abi目录,将上面生成的BSCSwapAgent.go,BSCSwapAgent.abi,ETHSwapAgent.abi,ETHSwapAgent.go 文件覆盖过来
然后把 BSCSwapAgent.goETHSwapAgent.go中的package改为abi

ERC20
cd eth-bsc-swap-contracts/
sudo npm install -g truffle-flattener
truffle-flattener node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol > contracts/flattened/ERC20.sol

同上将contracts/flattened/ERC20.sol 复制到 \bsc-eth-swap\abi目录中

cd `bsc-eth-swap/abi`
solc --bin ./ERC20.sol > ERC20.bin // 删除多余注释
solc --abi ./ERC20.sol > ERC20.abi // 删除多余注释
abigen --bin=ERC20.bin --abi=ERC20.abi --pkg=ERC20 --out=ERC20.go

修改后的代码已提交

git clone https://github.com/bcskill/bsc-eth-swap.git

常见问题

  • Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: 0x0NaN
    把逻辑合约重新部署下,用新地址再试下,或者确认下逻辑合约初始化函数参数,是否与encodeFunctionCall参数一致
  • admin cannot fallback to proxy target
    不能拿升级管理权限地址操作逻辑合约,逻辑合约管理权限地址不能与升级管理权限地址相同

注意

  1. encodeFunctionCall执行时,参数要与函数方法参数顺序一致

在线工具

https://www.sojson.com/yasuoyihang.html

使用OpenZeppelin编写可升级合约

为了方便社区新手入门,本文尽可能的详细

准备合约编译环境

测试系统:WSL ubuntu 20.04
node: v10.19.0
npm: 6.14.4

创建环境工程

mkdir myContract
cd myContract
sudo npm i -g truffle
truffle init

此时目录下会生成truffle工程相关文件

contracts  migrations  test  truffle-config.js

编写测试合约

需要部署三个合约,分别是

  1. Parms(逻辑合约)
  2. ProxyAdmin(管理合约)
  3. TransparentUpgradeProxy(代理合约,DAPP直接交互的合约地址)

Params合约代码

myContract/contract/Parms.sol文件,并输入

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract Params is Initializable,OwnableUpgradeable {
    function initialize()public initializer{
        __Context_init_unchained();
        __Ownable_init_unchained();
    }
    mapping(string => uint256) private uint256Params;

    event Uint256ParamSetted(string indexed _key,uint256 _value);

    function SetUint256Param(string memory _key,uint256 _value) external onlyOwner{
        uint256Params[_key] = _value;
        emit Uint256ParamSetted(_key,_value);
    }


    function GetUint256Param(string memory _key)public view returns(uint256){
        return uint256Params[_key];
    }
}

ProxyAdmin合约代码

myContract/contract/ProxyAdmin.sol文件,并输入

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

/**
 * @dev This is an auxiliary contract meant to be assigned as the admin of a {TransparentUpgradeableProxy}. For an
 * explanation of why you would want to use this see the documentation for {TransparentUpgradeableProxy}.
 */
contract ProxyAdmin is Ownable {
    /**
     * @dev Returns the current implementation of `proxy`.
     *
     * Requirements:
     *
     * - This contract must be the admin of `proxy`.
     */
    function getProxyImplementation(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
        // We need to manually run the static call since the getter cannot be flagged as view
        // bytes4(keccak256("implementation()")) == 0x5c60da1b
        (bool success, bytes memory returndata) = address(proxy).staticcall(hex"5c60da1b");
        require(success);
        return abi.decode(returndata, (address));
    }

    /**
     * @dev Returns the current admin of `proxy`.
     *
     * Requirements:
     *
     * - This contract must be the admin of `proxy`.
     */
    function getProxyAdmin(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
        // We need to manually run the static call since the getter cannot be flagged as view
        // bytes4(keccak256("admin()")) == 0xf851a440
        (bool success, bytes memory returndata) = address(proxy).staticcall(hex"f851a440");
        require(success);
        return abi.decode(returndata, (address));
    }

    /**
     * @dev Changes the admin of `proxy` to `newAdmin`.
     *
     * Requirements:
     *
     * - This contract must be the current admin of `proxy`.
     */
    function changeProxyAdmin(TransparentUpgradeableProxy proxy, address newAdmin) public virtual onlyOwner {
        proxy.changeAdmin(newAdmin);
    }

    /**
     * @dev Upgrades `proxy` to `implementation`. See {TransparentUpgradeableProxy-upgradeTo}.
     *
     * Requirements:
     *
     * - This contract must be the admin of `proxy`.
     */
    function upgrade(TransparentUpgradeableProxy proxy, address implementation) public virtual onlyOwner {
        proxy.upgradeTo(implementation);
    }

    /**
     * @dev Upgrades `proxy` to `implementation` and calls a function on the new implementation. See
     * {TransparentUpgradeableProxy-upgradeToAndCall}.
     *
     * Requirements:
     *
     * - This contract must be the admin of `proxy`.
     */
    function upgradeAndCall(
        TransparentUpgradeableProxy proxy,
        address implementation,
        bytes memory data
    ) public payable virtual onlyOwner {
        proxy.upgradeToAndCall{value: msg.value}(implementation, data);
    }
}

TransparentUpgradeableProxy 代理合约代码

myContract/contract/TransparentUpgradeProxy.sol文件,并输入

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

/**
 * @dev This contract implements a proxy that is upgradeable by an admin.
 *
 * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
 * clashing], which can potentially be used in an attack, this contract uses the
 * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
 * things that go hand in hand:
 *
 * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
 * that call matches one of the admin functions exposed by the proxy itself.
 * 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the
 * implementation. If the admin tries to call a function on the implementation it will fail with an error that says
 * "admin cannot fallback to proxy target".
 *
 * These properties mean that the admin account can only be used for admin actions like upgrading the proxy or changing
 * the admin, so it's best if it's a dedicated account that is not used for anything else. This will avoid headaches due
 * to sudden errors when trying to call a function from the proxy implementation.
 *
 * Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way,
 * you should think of the `ProxyAdmin` instance as the real administrative interface of your proxy.
 */
contract TransparentUpgradeableProxy is ERC1967Proxy {
    /**
     * @dev Initializes an upgradeable proxy managed by `_admin`, backed by the implementation at `_logic`, and
     * optionally initialized with `_data` as explained in {ERC1967Proxy-constructor}.
     */
    constructor(
        address _logic,
        address admin_,
        bytes memory _data
    ) payable ERC1967Proxy(_logic, _data) {
        assert(_ADMIN_SLOT == bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1));
        _changeAdmin(admin_);
    }

    /**
     * @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin.
     */
    modifier ifAdmin() {
        if (msg.sender == _getAdmin()) {
            _;
        } else {
            _fallback();
        }
    }

    /**
     * @dev Returns the current admin.
     *
     * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}.
     *
     * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
     * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
     * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
     */
    function admin() external ifAdmin returns (address admin_) {
        admin_ = _getAdmin();
    }

    /**
     * @dev Returns the current implementation.
     *
     * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}.
     *
     * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
     * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
     * `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
     */
    function implementation() external ifAdmin returns (address implementation_) {
        implementation_ = _implementation();
    }

    /**
     * @dev Changes the admin of the proxy.
     *
     * Emits an {AdminChanged} event.
     *
     * NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}.
     */
    function changeAdmin(address newAdmin) external virtual ifAdmin {
        _changeAdmin(newAdmin);
    }

    /**
     * @dev Upgrade the implementation of the proxy.
     *
     * NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}.
     */
    function upgradeTo(address newImplementation) external ifAdmin {
        _upgradeToAndCall(newImplementation, bytes(""), false);
    }

    /**
     * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
     * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
     * proxied contract.
     *
     * NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}.
     */
    function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin {
        _upgradeToAndCall(newImplementation, data, true);
    }

    /**
     * @dev Returns the current admin.
     */
    function _admin() internal view virtual returns (address) {
        return _getAdmin();
    }

    /**
     * @dev Makes sure the admin cannot access the fallback function. See {Proxy-_beforeFallback}.
     */
    function _beforeFallback() internal virtual override {
        require(msg.sender != _getAdmin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target");
        super._beforeFallback();
    }
}

修改truffle solc版本为0.8.0

打开truffle-config.js,修改solc版本

compilers: {
    solc: {
      version: "^0.8.0"
    ...

编译合约

首次需要安装依赖

npm i --save @openzeppelin/contracts-upgradeable
npm i --save @openzeppelin/contracts

编译合约

truffle complite

部署合约

等待完成后,会在build\contracts找到对应的编译后的合约
如何使用编译后的文件进行合约部署,参考《MyEtherWallet使用》

Params合约部署完的合约地址:0x808189EB5932Cfe710A849752CDb54F5fb1b85DA
ProxyAdmin合约部署完的合约地址:0x9b05f1F378B52011215464f4Ad4666BC80B7bcA5

部署TransparentUpgradeableProxy时,
LOGIC:逻辑合约地址,这里为 0x808189EB5932Cfe710A849752CDb54F5fb1b85DA
ADMIN
:管理合约地址,这里为 0x9b05f1F378B52011215464f4Ad4666BC80B7bcA5
_DATA:逻辑合约初始化方法调用数据,这里为0x8129fc1c(只调用initialize方法,initialize方法没有入参,如果有参数也是支持的)

0x8129fc1c由以下获得

web3js.eth.abi.encodeFunctionCall({
    name: 'initialize',
    type: 'function',
    inputs: []
}, []);
>0x8129fc1c

部署后的地址是:0x95e19C3609DE02291840CE9093c75e233cF2Cf08

此时用MyEtherWallet执行合约地址:0x95e19C3609DE02291840CE9093c75e233cF2Cf08,使用Params合约的abi执行
调用SetUint256Param,设置_key=K,_VALUE=1,并通过GetUint256Param进行验证
此时完成了基本的通过TransparentUpgradeableProxy代理合约调用Params逻辑合约的过程。

下面开始演示通过ProxyAdmin合约将Params逻辑合约升级

升级Params逻辑合约

修改Params合约,并部署

function GetUint256Param(string memory _key)public view returns(uint256){
    uint256 v = uint256Params[_key];
    return v+1;
}

新部署后的地址为0xBf72f7C1e39B76D7A5b702E16251Fdd132Cd6618

调用ProxyAdmin进行升级

ProxyAdmin提供两个方法进行升级

  1. upgrade,需要传入proxy地址,新的逻辑实现地址
  2. upgradeAndCall,需要传入roxy地址,新的逻辑实现地址,初始化调用数据

本例中,由于数据是保存在代理合约中,这份数据已经初始化过了,不需要再初始化,所以调用upgrade方法即可,参数如下:

  • proxy: 0x95e19C3609DE02291840CE9093c75e233cF2Cf08
  • implementation: 0xBf72f7C1e39B76D7A5b702E16251Fdd132Cd6618

至此,合约升级完毕。调用GetUint256Param方法进行验证,获得_key=K的value为2,合约升级成功。

参考

https://docs.openzeppelin.com/contracts/3.x/api/proxy
https://www.cnblogs.com/cqvoip/p/15033402.html
https://docs.openzeppelin.com/upgrades-plugins/1.x/

https://medium.com/@kenschiller/making-ethereum-smart-contracts-upgradable-aa16a4256d32