简体   繁体   English

使用 ERC-20 代币在 Solidity 合约中设置价格

[英]Set price in Solidity contract using ERC-20 token

I have a ERC721 contract and I have one problem, I'm trying to set the price in another currency like UNI or SUSHI but the problem is that I don't know how to change it, I don't know a lot about contracts, here is the code.我有一份ERC721合同,但我有一个问题,我正在尝试以另一种货币(如UNISUSHI )设置价格,但问题是我不知道如何更改它,我对合同了解不多,这里是代码。

I was wondering if it is possible to make the following UNI work:我想知道是否可以使以下UNI工作:

uint256 public constant NFT_PRICE = 1 ether;

I try to do something like this我尝试做这样的事情

uint256 public constant NFT_PRICE = 1 UNI;

But this doesn't work但这不起作用

Ethereum natively knows only about Ether payments.以太坊本身只知道Ether币支付。

For a token payment, you need to study ERC-20 standard and its transferFrom() functionality to support ERC-20 token payments in your Solidity smart contracts.对于代币支付,您需要研究 ERC-20 标准及其transferFrom()功能,以支持您的 Solidity 智能合约中的 ERC-20 代币支付。

For ERC-777 tokens you can have an incoming payment handler .对于 ERC-777 代币,您可以使用收款处理程序

Well, maybe this answer is a bit late, if you want to set a price in anything that is not ether you will need to use an erc20, the uniswap token is an erc20, now if you want to have a fixed amount of that token (remember that contracts are not upgradable) you probably do something like this uniToken.transferFrom(msg.sender,to,amount);好吧,也许这个答案有点晚了,如果你想为任何非以太币设定价格,你需要使用 erc20,uniswap 代币是 erc20,现在如果你想拥有固定数量的代币(记住合约是不可升级的)你可能会做这样的事情uniToken.transferFrom(msg.sender,to,amount); remember that tokens can also have decimals and could be different from the ether decimals, if you want to use a non fixed amount you will need a chainlink oracle to provide you the price, as an example if you want to set an amount of 10 dollars in Uni you will need an oracle that returns you that value and then you can do the calculations and other things, other thing you need to know, in solidity the expression x ether is just sugar syntax, what it does is return an uint 256 of x * 10^18, because that's how decimals are handled in tokens and ether, so if you want 0.5 ethers you can do this 0.5 ether or this 5 * 10^17 or this 500000000000000000 and all represent the exact same number, that's a way to dont't loose presition with decimals, most tokens and ether use 18 decimals but there are some that doesn't, so is important to check that请记住,代币也可以有小数,并且可能与以太小数不同,如果您想使用非固定金额,您将需要一个链环 oracle 来为您提供价格,例如,如果您想设置 10 美元的金额在 Uni 中,您将需要一个 oracle 来返回该值,然后您可以进行计算和其他事情,您需要知道的其他事情,在solidity 中,表达式x ether只是糖语法,它的作用是返回一个 uint 256 x * 10^18,因为这是在令牌和以太币中处理小数的方式,所以如果你想要 0.5 以太币,你可以使用0.5 ether币或5 * 10^17500000000000000000 ,它们都代表完全相同的数字,这是一种方式为了不使用小数,大多数代币和以太币都使用 18 位小数,但有些不是,所以检查一下很重要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM