BCSkill (Block chain skill )
区块链中文技术社区

只讨论区块链底层技术
遵守一切相关法律政策!

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

以太坊交易池架构设计

当前以太坊公链的平均每秒能处理30到40笔交易,因此以太坊一旦出现火热的DAPP时,极易出现交易拥堵。

偏底的交易处理速度永远无法同现有的中心化服务相比。当网络中出现大量交易排队时,矿工是如何选择并管理这些交易的呢?答案在本篇所介绍的以太坊交易池中,如果你对交易还不特别熟悉,则请先阅读 [以太坊交易]({{< ref "part1/transaction.md" >}})。

交易处理流程

当你通过以太坊钱包,发送一笔转账交易给张三时。这笔交易是如何进入网络,最终被矿工打包到区块中呢?

下图是一笔交易从出生到交易进入区块的关键流程。

transaction-life

首先,用户可通过以太坊钱包或者其他调用以太坊节点API (eth_sendRawTransaction等)发送交易到一个运行中的以太坊 geth 节点。

此时,因为交易时通过节点的API接收,因此此交易被视为一笔来自本地(local)(图中用红球表示),在经过一系列校验和处理后。交易成功进入交易池,随后向已连接的邻近节点发送此交易。

当邻近节点,如矿工节点从邻近节点接收到此交易时,在进入交易池之前。会将交易标记为来自远方(remote)的交易(图中用绿球表示)。也需要经过校验和处理后,进入矿工节点的交易池,等待矿工打包到区块中。

如果邻近节点,不是矿工,也无妨。因为任何节点会默认将接受到得合法交易及时发送给邻近节点。得益于P2P网络,一笔交易平均在6s内扩散到整个以太坊公链网络的各个节点中。

A-Distributed-P2P-Network-with-Elements-of-Blockchain-and-Cryptocurrency

进入以太坊交易池的交易被区分本地还是远方的目的是因为,节点对待local的交易和remote的交易有所差异。简单地说是 local 交易优先级高于 remote 交易。

以太坊交易池设计

前面并未交易池处理细节,这里将详细讲解以太坊交易池处理一笔交易时的完整过程。在讲解前,你还还有先了解以太坊交易池的设计模型。 从2014年到现在,以太坊的交易池一直在不断优化中,从未停止。从这里也说明,交易池不仅仅重要,还需要高性能。

下图是以太坊交易池的主要设计模块,分别是交易池配置、实时的区块链状态、交易管理容器、本地交易存储和新交易事件。

ethereum-tx-pool-desgin

各个模块相互影响,其中最重要的的交易管理。这也是需要我们重点介绍的部分。

交易池配置

交易池配置不多,但每项配置均直接影响交易池对交易的处理行为。配置信息由 TxPoolConfig 所定义,各项信息如下:

// core/tx_pool.go:125
type TxPoolConfig struct {
   Locals    []common.Address
   NoLocals  bool
   Journal   string
   Rejournal time.Duration
   PriceLimit uint64
   PriceBump  uint64
   AccountSlots uint64
   GlobalSlots  uint64
   AccountQueue uint64
   GlobalQueue  uint64
   Lifetime time.Duration
}
  • Locals: 定义了一组视为local交易的账户地址。任何来自此清单的交易均被视为 local 交易。
  • NoLocals: 是否禁止local交易处理。默认为 fasle,允许 local 交易。如果禁止,则来自 local 的交易均视为 remote 交易处理。
  • Journal: 存储local交易记录的文件名,默认是 ./transactions.rlp
  • Rejournal:定期将local交易存储文件中的时间间隔。默认为每小时一次。
  • PriceLimit: remote交易进入交易池的最低 Price 要求。此设置对 local 交易无效。默认值1。
  • PriceBump:替换交易时所要求的价格上调涨幅比例最低要求。任何低于要求的替换交易均被拒绝。
  • AccountSlots: 当交易池中可执行交易(是已在等待矿工打包的交易)量超标时,允许每个账户可以保留在交易池最低交易数。默认值是 16 笔。
  • GlobalSlots: 交易池中所允许的可执行交易量上限,高于上限时将释放部分交易。默认是 4096 笔交易。
  • AccountQueue:交易池中单个账户非可执行交易上限,默认是64笔。
  • GlobalQueue: 交易池中所有非可执行交易上限,默认1024 笔。
  • Lifetime: 允许 remote 的非可执行交易可在交易池存活的最长时间。交易池每分钟检查一次,一旦发现有超期的remote 账户,则移除该账户下的所有非可执行交易。默认为3小时。

上面配置中,包含两个重要概念可执行交易非可执行交易。可执行交易是指从交易池中择优选出的一部分交易可以被执行,打包到区块中。非可执行交易则相反,任何刚进入交易池的交易均属于非可执行状态,在某一个时刻才会提升为可执行状态。

一个节点如何自定义上述交易配置呢?以太坊 geth 节点允许在启动节点时,通过参数修改配置。可修改的交易池配置参数如下(通过 geth -h 查看):

TRANSACTION POOL OPTIONS:
  --txpool.locals value        Comma separated accounts to treat as locals (no flush, priority inclusion)
  --txpool.nolocals            Disables price exemptions for locally submitted transactions
  --txpool.journal value       Disk journal for local transaction to survive node restarts (default: "transactions.rlp")
  --txpool.rejournal value     Time interval to regenerate the local transaction journal (default: 1h0m0s)
  --txpool.pricelimit value    Minimum gas price limit to enforce for acceptance into the pool (default: 1)
  --txpool.pricebump value     Price bump percentage to replace an already existing transaction (default: 10)
  --txpool.accountslots value  Minimum number of executable transaction slots guaranteed per account (default: 16)
  --txpool.globalslots value   Maximum number of executable transaction slots for all accounts (default: 4096)
  --txpool.accountqueue value  Maximum number of non-executable transaction slots permitted per account (default: 64)
  --txpool.globalqueue value   Maximum number of non-executable transaction slots for all accounts (default: 1024)
  --txpool.lifetime value      Maximum amount of time non-executable transaction are queued (default: 3h0m0s)

链状态

所有进入交易池的交易均需要被校验,最基本的是校验账户余额是否足够支付交易执行。或者 交易 nonce 是否合法。在交易池中维护的最新的区块StateDB。当交易池接收到新区块信号时,将立即重置 statedb。

在交易池启动后,将订阅链的区块头事件:

//core/tx_pool.go:274
pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh)

并开始监听新事件:

//core/tx_pool.go:305
for {
   select {
   // Handle ChainHeadEvent
   case ev := <-pool.chainHeadCh:
      if ev.Block != nil {
         pool.mu.Lock()
         if pool.chainconfig.IsHomestead(ev.Block.Number()) {
            pool.homestead = true
         }
         pool.reset(head.Header(), ev.Block.Header())
         head = ev.Block

         pool.mu.Unlock()
      }
  //...
  }
}

接收到事件后,将执行 func (pool *TxPool) reset(oldHead, newHead *types.Header)方法更新 state和处理交易。核心是将交易池中已经不符合要求的交易删除并更新整理交易,这里不展开描述,有兴趣的话,可以到微信群中交流。

本地交易

在交易池中将交易标记为 local 的有多种用途:

  1. 在本地磁盘存储已发送的交易。这样,本地交易不会丢失,重启节点时可以重新加载到交易池,实时广播出去。
  2. 可以作为外部程序和以太坊沟通的一个渠道。外部程序只需要监听文件内容变化,则可以获得交易清单。
  3. local交易可优先于 remote 交易。对交易量的限制等操作,不影响 local 下的账户和交易。

对应本地交易存储,在启动交易池时根据配置开启本地交易存储能力:

//core/tx_pool.go:264
if !config.NoLocals && config.Journal != "" {
        pool.journal = newTxJournal(config.Journal)
        if err := pool.journal.load(pool.AddLocals); err != nil {
            log.Warn("Failed to load transaction journal", "err", err)
        }
    //...
}

并从磁盘中加载已有交易到交易池。在新的local 交易进入交易池时,将被实时写入 journal 文件。

// core/tx_pool.go:757
func (pool *TxPool) journalTx(from common.Address, tx *types.Transaction) {
   if pool.journal == nil || !pool.locals.contains(from) {
      return
   }
   if err := pool.journal.insert(tx); err != nil {
      log.Warn("Failed to journal local transaction", "err", err)
   }
}

从上可看到,只有属于 local 账户的交易才会被记录。你又没有注意到,如果仅仅是这样的话,journal 文件是否会跟随本地交易而无限增长?答案是否定的,虽然无法实时从journal中移除交易。但是支持定期更新journal文件。

journal 并不是保存所有的本地交易以及历史,他仅仅是存储当前交易池中存在的本地交易。因此交易池会定期对 journal 文件执行 rotate,将交易池中的本地交易写入journal文件,并丢弃旧数据。

journal := time.NewTicker(pool.config.Rejournal)
//...
//core/tx_pool.go:353
case <-journal.C:
            if pool.journal != nil {
                pool.mu.Lock()
                if err := pool.journal.rotate(pool.local()); err != nil {
                    log.Warn("Failed to rotate local tx journal", "err", err)
                }
                pool.mu.Unlock()
            }
}

新交易信号

文章开头,有提到进入交易池的交易将被广播到网络中。这是依赖于交易池支持外部订阅新交易事件信号。任何订阅此事件的子模块,在交易池出现新的可执行交易时,均可实时接受到此事件通知,并获得新交易信息。

需要注意的是并非所有进入交易池的交易均被通知外部,而是只有交易从非可执行状态变成可执行状态后才会发送信号。

//core/tx_pool.go:705
go pool.txFeed.Send(NewTxsEvent{types.Transactions{tx}})
//core/tx_pool.go:1022
go pool.txFeed.Send(NewTxsEvent{promoted})

在交易池中,有两处地方才会执行发送信号。一是交易时用于替换已经存在的可执行交易时。二是有新的一批交易从非可执行状态提升到可执行状态后。

外部只需要订阅SubscribeNewTxsEvent(ch chan<- NewTxsEvent)新可执行交易事件,则可实时接受交易。在 geth 中网络层将订阅交易事件,以便实时广播。

//eth/handler.go:213
pm.txsCh = make(chan core.NewTxsEvent, txChanSize)
pm.txsSub = pm.txpool.SubscribeNewTxsEvent(pm.txsCh)
//eth/handler.go:781
func (pm *ProtocolManager) txBroadcastLoop() {
   for {
      select {
      case event := <-pm.txsCh:
         pm.BroadcastTxs(event.Txs)
      //...
   }
}

另外是矿工实时订阅交易,以便将交易打包到区块中。

//miner/worker.go:207
worker.txsSub = eth.TxPool().SubscribeNewTxsEvent(worker.txsCh)
//miner/worker.go:462
txs := make(map[common.Address]types.Transactions)
for _, tx := range ev.Txs {
        acc, _ := types.Sender(w.current.signer, tx)
    txs[acc] = append(txs[acc], tx)
}
txset := types.NewTransactionsByPriceAndNonce(w.current.signer, txs)
w.commitTransactions(txset, coinbase, nil)

交易管理

最核心的部分则是交易池对交易的管理机制。以太坊将交易按状态分为两部分:可执行交易和非可执行交易。分别记录在pending容器中和 queue 容器中。

ethereum-tx-pool-txManager

如上图所示,交易池先采用一个 txLookup (内部为map)跟踪所有交易。同时将交易根据本地优先,价格优先原则将交易划分为两部分 queue 和 pending。而这两部交易则按账户分别跟踪。

那么在交易在进入交易池进行管理的细节有是如何的呢?等我下一篇文章详细介绍以太坊交易池交易管理。

转载自:https://learnblockchain.cn/books/geth/part2/txpool/txpool.html