在不使用uniswap的情况下使用坚固性创建令牌的交换函数(与速率交换)
我是该领域的新手,只是在网上学习一些有关坚固性并创建简单合同的课程,我想询问我尝试但尚未成功的简单项目。 我想创建一个可以交换函数,该函数可以在我从ERC20创建的2个令牌之间进行交换,而无需使用uniswap或类似的内容,只需开发一个新函数,可以在令牌a和令牌B之间交换,例如1令牌A的速率A = 3令牌B,要求速率保存在映射中并首先获得配置,但是我真的不知道如何设置交换速率。 任何人都可以帮助我,谢谢大家。
存在掉期合同的代码,只是临时:
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 <0.9.0;
import "./Token.sol";
import "./safemath.sol";
contract SwapToken {
using SafeMath for uint;
Token public token1;
address public owner1;
Token public token2;
address public owner2;
uint public amount1;
uint public amount2;
uint public swapRate;
constructor(
address _token1,
address _owner1,
address _token2,
address _owner2,
uint _amount1
) {
token1 = Token(_token1);
owner1 = _owner1;
token2 = Token(_token2);
owner2 = _owner2;
swapRate = 3;
amount1 = _amount1;
amount2 = _amount1.mul(swapRate);
}
function swap() public {
require(msg.sender == owner1 || msg.sender == owner2, "Not authorized from owners");
require(
token1.allowance(owner1, address(this)) >= amount1,
"Token 1 allowance too low"
);
require(
token2.allowance(owner2, address(this)) >= amount2,
"Token 2 allowance too low"
);
_safeTransferFrom(token1, owner1, owner2, amount1);
_safeTransferFrom(token2, owner2, owner1, amount2);
}
function _safeTransferFrom(
Token token,
address sender,
address recipient,
uint amount
) private {
bool sent = token.transferFrom(sender, recipient, amount);
require(sent, "Token transfer failed");
}
}
I am newbie to this field, and just learning some courses online about solidity and creating a simple contract, I would like to ask about a simple project that I try but not successful yet.
I want to create a swap function that can swap between 2 tokens that I created from ERC20, WITHOUT using uniswap or something like that, just dev a new function that swap between token A and token B, for example with the rate of 1 token A = 3 token B, the rate was ask to save in a mapping and got config at first, but I dont really know how to setting up the rate for swapping.
Could anyone help me out with this, many thanks guys.
Existed Code of Swap contract, just temporary :
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 <0.9.0;
import "./Token.sol";
import "./safemath.sol";
contract SwapToken {
using SafeMath for uint;
Token public token1;
address public owner1;
Token public token2;
address public owner2;
uint public amount1;
uint public amount2;
uint public swapRate;
constructor(
address _token1,
address _owner1,
address _token2,
address _owner2,
uint _amount1
) {
token1 = Token(_token1);
owner1 = _owner1;
token2 = Token(_token2);
owner2 = _owner2;
swapRate = 3;
amount1 = _amount1;
amount2 = _amount1.mul(swapRate);
}
function swap() public {
require(msg.sender == owner1 || msg.sender == owner2, "Not authorized from owners");
require(
token1.allowance(owner1, address(this)) >= amount1,
"Token 1 allowance too low"
);
require(
token2.allowance(owner2, address(this)) >= amount2,
"Token 2 allowance too low"
);
_safeTransferFrom(token1, owner1, owner2, amount1);
_safeTransferFrom(token2, owner2, owner1, amount2);
}
function _safeTransferFrom(
Token token,
address sender,
address recipient,
uint amount
) private {
bool sent = token.transferFrom(sender, recipient, amount);
require(sent, "Token transfer failed");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们从一个一般的答案开始,我们将基于它,因为我们更好地了解这个问题。
我认为您不是要使用流动性池创建交换解决方案,而是想匹配想要交换代币的2个人(?)。
然后,可以在交换时,可以进行简单的方法
,然后检查速率并相应地计算。
Let's start with a general answer and we'll build on it as we understand the question better.
I think you are not trying to create a swapping solution with a liquidity pool, but instead, want to match 2 people(?) that want to swap tokens.
A very simplistic approach to it could be
Then, while swapping, check the rate and calculate accordingly.