增加一个erc20换取ceg的合约
This commit is contained in:
parent
dc971a4d5c
commit
7e836a135c
16610
build/contracts/BETokenMall.json
Normal file
16610
build/contracts/BETokenMall.json
Normal file
File diff suppressed because one or more lines are too long
4080
build/contracts/ICurrency.json
Normal file
4080
build/contracts/ICurrency.json
Normal file
File diff suppressed because one or more lines are too long
108
contracts/market/BETokenMall.sol
Normal file
108
contracts/market/BETokenMall.sol
Normal file
@ -0,0 +1,108 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity 0.8.10;
|
||||
|
||||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
|
||||
import "../utils/UInt.sol";
|
||||
import "./MallBase.sol";
|
||||
|
||||
interface ICurrency is IERC20 {
|
||||
function decimals() external view returns (uint256);
|
||||
}
|
||||
|
||||
contract BETokenMall is MallBase, ReentrancyGuard {
|
||||
using SafeERC20 for IERC20;
|
||||
using UInt for uint256;
|
||||
uint256 constant ROUND = 1000000;
|
||||
address public tokenAddress;
|
||||
address public seller;
|
||||
// currency => price
|
||||
mapping(address => uint256) public prices;
|
||||
|
||||
// Events
|
||||
event BuyTransaction(
|
||||
address indexed buyer,
|
||||
address tokenAddress,
|
||||
address currency,
|
||||
uint256 amount
|
||||
);
|
||||
event TokenAddressUpdated(address tokenAddress);
|
||||
event SellerUpdated(address seller);
|
||||
event UpdateTokenPrice(address currency, uint256 price, uint256 pricePre);
|
||||
|
||||
/**
|
||||
* @dev Constructor
|
||||
*/
|
||||
constructor(address _tokenAddress, address _seller) {
|
||||
tokenAddress = _tokenAddress;
|
||||
seller = _seller;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Update token address
|
||||
*/
|
||||
function updateTokenAddress(address _tokenAddress) external onlyOwner {
|
||||
tokenAddress = _tokenAddress;
|
||||
emit TokenAddressUpdated(_tokenAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Update seller address
|
||||
*/
|
||||
function updateSeller(address _seller) external onlyOwner {
|
||||
seller = _seller;
|
||||
emit SellerUpdated(_seller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Update token price
|
||||
*/
|
||||
function updateTokenPrice(
|
||||
address currency,
|
||||
uint256 price
|
||||
) external onlyOwner {
|
||||
uint256 pricePre = prices[currency];
|
||||
prices[currency] = price;
|
||||
emit UpdateTokenPrice(currency, price, pricePre);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Buy token
|
||||
*/
|
||||
function buyToken(address currency, uint256 amount) external nonReentrant {
|
||||
require(amount > 0, "BETokenMall: invalid amount");
|
||||
require(erc20Supported[currency], "BETokenMall: invalid payment method");
|
||||
require(prices[currency] > 0, "BETokenMall: invalid token price");
|
||||
// calc currency amount
|
||||
uint256 currencyDecimal = ICurrency(currency).decimals();
|
||||
uint256 tokenAmount = (prices[currency] *
|
||||
amount *
|
||||
(10 ** (18 - currencyDecimal))) / ROUND;
|
||||
address buyer = _msgSender();
|
||||
require(
|
||||
IERC20(tokenAddress).balanceOf(seller) >= tokenAmount,
|
||||
"BETokenMall: seller doesn't have enough token to sell this item"
|
||||
);
|
||||
require(
|
||||
IERC20(tokenAddress).allowance(seller, address(this)) >= tokenAmount,
|
||||
"BETokenMall: seller doesn't approve enough token to sell this item"
|
||||
);
|
||||
// Check payment approval and buyer balance
|
||||
require(
|
||||
IERC20(currency).balanceOf(buyer) >= amount,
|
||||
"BETokenMall: buyer doesn't have enough token to buy this item"
|
||||
);
|
||||
require(
|
||||
IERC20(currency).allowance(buyer, address(this)) >= amount,
|
||||
"BETokenMall: buyer doesn't approve enough token to buy this item"
|
||||
);
|
||||
|
||||
// Transfer payment to seller
|
||||
IERC20(currency).safeTransferFrom(buyer, feeToAddress, amount);
|
||||
// Transfer token to buyer
|
||||
IERC20(tokenAddress).safeTransferFrom(seller, buyer, tokenAmount);
|
||||
// emit buy event
|
||||
emit BuyTransaction(buyer, tokenAddress, currency, amount);
|
||||
}
|
||||
}
|
@ -2,6 +2,9 @@ const BENftMarket = artifacts.require("market/BENftMarket");
|
||||
const BENftMall = artifacts.require("market/BENftMall");
|
||||
const GameItemMarket = artifacts.require("market/GameItemMarket");
|
||||
const GameItemMall = artifacts.require("market/GameItemMall");
|
||||
const FT = artifacts.require("tokens/erc20/FT");
|
||||
const BETokenMall = artifacts.require("market/BETokenMall");
|
||||
const config = require("../config/config");
|
||||
const base = require("../scripts/base");
|
||||
|
||||
module.exports = async function (deployer, network, accounts) {
|
||||
@ -18,16 +21,40 @@ module.exports = async function (deployer, network, accounts) {
|
||||
// network,
|
||||
// });
|
||||
|
||||
await deployer.deploy(BENftMall);
|
||||
const nftMallInstance = await BENftMall.deployed();
|
||||
if (nftMallInstance) {
|
||||
console.log("BENftMall successfully deployed.");
|
||||
// await deployer.deploy(BENftMall);
|
||||
// const nftMallInstance = await BENftMall.deployed();
|
||||
// if (nftMallInstance) {
|
||||
// console.log("BENftMall successfully deployed.");
|
||||
// }
|
||||
// base.updateArray({
|
||||
// name: "BENftMall",
|
||||
// type: "logic",
|
||||
// json: "assets/contracts/BENftMall.json",
|
||||
// address: nftMallInstance.address,
|
||||
// network,
|
||||
// });
|
||||
let cfgs = base.loadData({ network });
|
||||
const cegInstance = await FT.at(cfgs.find((c) => c.name === "CEG").address);
|
||||
await deployer.deploy(
|
||||
BETokenMall,
|
||||
cegInstance.address,
|
||||
config.market.feeToAddress
|
||||
);
|
||||
const tokenMallInstance = await BETokenMall.deployed();
|
||||
if (tokenMallInstance) {
|
||||
console.log("BETokenMall successfully deployed.");
|
||||
}
|
||||
await tokenMallInstance.setFeeToAddress(config.market.feeToAddress);
|
||||
const usdcInstance = await FT.at(
|
||||
cfgs.find((c) => c.name === "BEUSDC").address
|
||||
);
|
||||
await tokenMallInstance.addERC20Support(usdcInstance.address);
|
||||
await tokenMallInstance.updateTokenPrice(usdcInstance.address, "10000000");
|
||||
base.updateArray({
|
||||
name: "BENftMall",
|
||||
name: "BETokenMall",
|
||||
type: "logic",
|
||||
json: "assets/contracts/BENftMall.json",
|
||||
address: nftMallInstance.address,
|
||||
json: "assets/contracts/BETokenMall.json",
|
||||
address: tokenMallInstance.address,
|
||||
network,
|
||||
});
|
||||
|
||||
|
@ -99,7 +99,7 @@
|
||||
"name": "BEMultiSigWallet",
|
||||
"type": "logic",
|
||||
"json": "assets/contracts/BEMultiSigWallet.json",
|
||||
"address": "0xE68F149daF2F314d9960c08496D8701BC7671850"
|
||||
"address": "0x2419c58F3542E69c3f10dfe6C1Bb1fBd4D54Db5A"
|
||||
},
|
||||
{
|
||||
"name": "TestBadge",
|
||||
@ -118,5 +118,11 @@
|
||||
"type": "erc20",
|
||||
"json": "assets/contracts/FT.json",
|
||||
"address": "0x944D0A8463B2c955F90F7252bBb99A3395087155"
|
||||
},
|
||||
{
|
||||
"name": "BETokenMall",
|
||||
"type": "logic",
|
||||
"json": "assets/contracts/BETokenMall.json",
|
||||
"address": "0x9110Bc91942E559eebBF19B91cdAf67eEee3F93D"
|
||||
}
|
||||
]
|
||||
|
Loading…
x
Reference in New Issue
Block a user