以太坊网络可以理解为一个软件基础设施,系统I34-开发I633-搭建53I9,在这个网络上可以部署运行各种智能合约,智能合约有各种各样的功能、应用场景。运行以太坊客户端的设备就构成了以太坊网络中的节点。
Uniswap V3也是在以太坊上部署运行的智能合约。
1、路由(Route):路由也是一个智能合约,用于管理token兑换。
2、池(Pool):池也是一个智能合约,用于存储两种以太坊代币,并在这两种代币之间进行交换。
3、工厂合约(Factory):工厂合约是用来创建池的智能合约。
4、ABI:(应用程序二进制接口)是一种规范,用于描述智能合约如何与外部世界互相通信。它指定了智能合约的函数名称,参数类型和返回值类型,以及如何编码和解码数据,并确定了智能合约的外部接口。可以理解为要调用某个接口,就必须按照接口约定的标准调用,而ABI中记录的就是约定的一系列标准。
一旦智能合约在以太坊上部署,就有一个地址。
/* jshint esversion: 7 */
let ContractV3SwapRouterV2 = "``0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45``"
function computePoolPrice(decimals0, decimals1, sqrtPriceX96) {
``[decimals0, decimals1, sqrtPriceX96] = [decimals0, decimals1, sqrtPriceX96].map(BigInt);
``const TWO = BigInt(2);
``const TEN = BigInt(10);
``const SIX_TENTH = BigInt(1000000);
``const Q192 = (TWO ** BigInt(96)) ** TWO;
``return (
``Number((sqrtPriceX96 ** TWO * TEN ** decimals0 * SIX_TENTH) / (Q192 * TEN ** decimals1)) /
``Number(SIX_TENTH)
``);
}
function toAmount(s, decimals) {
``return Number((BigDecimal(BigInt(s))/BigDecimal(Math.pow(10, decimals))).toString())
}
function toInnerAmount(n, decimals) {
``return (BigDecimal(n)*BigDecimal(Math.pow(10,decimals))).toFixed(0)
}
$.NewUniswapV3 = function(e) {
``e = e || exchange
``if (e.GetName() !== 'Web3') {
``panic("``only support Web3 exchange``")
``}
``let self = {
``tokenInfo: {},
``walletAddress: e.IO("``address``"),
``pool: {}
``}
``// register
``e.IO("``abi``", ContractV3Factory, ABI_Factory)
``e.IO("``abi``", ContractV3SwapRouterV2, ABI_Route)
``self.addToken = function(name, address) {
``let ret = e.IO("``api``", address, "``decimals``")
``if (!ret) {
``throw "``get token decimals failed``"
``}
``let decimals = Number(ret)
``self.tokenInfo[name] = {
``name: name,
``decimals: decimals,
``address: address
``}
``}
``self.waitMined = function(tx) {
``while (true) {
``Sleep(1000)
``let info = e.IO("``api``", "``eth``", "``eth_getTransactionReceipt", tx)
``if
(info && info.gasUsed) {
``return
true
``}
``Log(``'Transaction not yet mined'``, tx)
``}