智能合约(Smart contract)是依托计算机在网络空间运行的合约,它以信息化方式传播、验证或执行合同,由计算机读取、执行,具备自助的特点。而区块链的去中心化,数据的防篡改,决定了智能合约更加适合于在区块链上来实现
Artificial intelligence technology is one of the core technologies in the era of intelligent industry.AI technology includes machine learning,deep learning,natural language processing,computer vision,etc.The application of these technologies enables the machine to learn,understand and judge independently,and can help industrial enterprises achieve automated,intelligent and efficient production and management.
ERC20代币兑换ERC20代币的函数入口和ERC20代币兑换ETH的函数入口类似,不同的是ERC20代币兑换ERC20代币的函数入口在调用进行兑换的业务函数前会先用创建自身的工厂合约实现的getExchange函数来获取目标代币所在的兑换合约地址,然后再向目标兑换合约地址发送兑换请求,将在本合约兑换得到的ETH兑换成目标代币。
函数入口调用到的函数就两种,分别是根据输入代币计算输出代币的tokenToTokenInput,以及根据输出代币计算输入代币的tokenToTokenOutput。
下面先看tokenToTokenInput。
private
def tokenToTokenInput(tokens_sold:uint256,min_tokens_bought:uint256,min_eth_bought:uint256(wei),deadline:timestamp,buyer:address,recipient:address,exchange_addr:address)->uint256:开发功能I35系统7O98详细O7I8
assert(deadline>=block.timestamp and tokens_sold>0)and(min_tokens_bought>0 and min_eth_bought>0)
assert exchange_addr!=self and exchange_addr!=ZERO_ADDRESS
token_reserve:uint256=self.token.balanceOf(self)#获得支付代币的储备量
#用getInputPrice计算所能兑换到的ETH
eth_bought:uint256=self.getInputPrice(tokens_sold,token_reserve,as_unitless_number(self.balance))
wei_bought:uint256(wei)=as_wei_value(eth_bought,'wei')#将单位转换成wei
assert wei_bought>=min_eth_bought
assert self.token.transferFrom(buyer,self,tokens_sold)#收取支付代币
#调用目标兑换合约地址的ethToTokenTransferInput函数,将ETH兑换成目标代币
tokens_bought:uint256=Exchange(exchange_addr).ethToTokenTransferInput(min_tokens_bought,deadline,recipient,value=wei_bought)
log.EthPurchase(buyer,tokens_sold,wei_bought)
return tokens_bought