WT_Token과 NWT_Token을 교환 해주는 컨트렉트
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.10;
import "../node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract TokenSwap{
IERC20 public token1; // stable coin
IERC20 public token2; // 유동성 코인
address public owner2; // server address
constructor (address _token1, address _token2, address serverAddress) public {
token1 = IERC20(_token1);
token2 = IERC20(_token2);
owner2 = serverAddress;
}
function swap(uint256 _amount, address user, address spender, address _contract1, address _contract2) public {
uint256 tokenAmount = _amount * 1e18;
//_exchangeToken(user, spender, _contract1);
//_exchangeToken(owner2, spender, _contract2);
require(msg.sender == owner2, "Not authorized");
require(token1.allowance(user, address(this)) >= tokenAmount, "Token 1 allowance too low");
require(token2.allowance(owner2, address(this)) >= tokenAmount/5, "Token 2 allowance too low");
_safeTransferFrom(token1, user, owner2, tokenAmount);
_safeTransferFrom(token2, owner2, user, tokenAmount/5);
}
{*/ function _exchangeToken(address owner,address spender, address _contract)public returns(uint){
(bool data, ) = _contract.call(abi.encodeWithSignature("approveToken(address,address)",owner,spender));
if(data == true){
return 1;
}
}
/*}
function _safeTransferFrom(IERC20 token, address sender, address recipient, uint256 amount) private {
bool sent = token.transferFrom(sender, recipient, amount);
require(sent, "Token transfer failed");
}
}
IERC20 public token1; // stable coin
IERC20 public token2; // 유동성 코인
address public owner2; // server address
constructor (address _token1, address _token2, address serverAddress) public {
token1 = IERC20(_token1);
token2 = IERC20(_token2);
owner2 = serverAddress;
}
function swap
function swap(uint256 _amount, address user, address spender, address _contract1, address _contract2) public {
uint256 tokenAmount = _amount * 1e18;
require(msg.sender == owner2, "Not authorized");
require(token1.allowance(user, address(this)) >= tokenAmount, "Token 1 allowance too low");
- token1에 해당하는 컨트렉트에서 allowance를 실행하여 user가 이 컨트렉트에게 양도 설정한 토큰의 양을 확인
require(token2.allowance(owner2, address(this)) >= tokenAmount/5, "Token 2 allowance too low");
- token2에 해당하는 컨트렉트에서 allowance를 실행하여 user가 이 컨트렉트에게 양도 설정한 토큰의 양을 확인
- tokenAmount/5 => 우리 플랫폼은 WT와 NWT의 비율을 1:5로 하기에 이렇게 설정하여 체크
- 추후 거래소 가격 추적으로 비율 조정 가능하게 수정할 필요가 있음
- _safeTransferFrom 함수 실행
_safeTransferFrom(token1, user, owner2, tokenAmount);
- user가 owner2 = serverAddress에게 tokenAmount만큼 전송
_safeTransferFrom(token2, owner2, user, tokenAmount/5);
- owner2 = serverAddress가 user에게 tokenAmount/5 ( 1:5 ) 의 비율 만큼 전송
}
function_safeTransferFrom
function _safeTransferFrom(IERC20 token, address sender, address recipient, uint256 amount) private {
bool sent = token.transferFrom(sender, recipient, amount);
- token => 위에서 token1, token2를 받고 각 컨트렉트에서 transferFrom을 실행해준다
require(sent, "Token transfer failed");
- bool sent : 아무 값이 없으면 false기 때문에 "Token transfer failed"
}
'WATTO 프로젝트 > 컨트렉트' 카테고리의 다른 글
NFT 경매 (0) | 2022.02.13 |
---|---|
NFT 컨트렉트 (0) | 2022.02.13 |
WT_Token (0) | 2022.02.12 |
NWT_Token (0) | 2022.02.12 |
사용 컨트렉트 소개 (0) | 2022.02.12 |
댓글