Skip to content
Verified contractExactMIT

BarkcoinVN

Address0x3252b0f3ef94a2e6ec7596bc7c3044f2cf4c23f0
Balance

0 ETH

 

Transactions

49

 

Compiler

solc 0.8.20

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.20+commit.a1b79de6
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
24,876,116
Creation transaction
0x8d4585a8a8…ea86adeeb9

contracts/BarkcoinVN.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "./interfaces/IBarckcardVLP.sol";

interface IERC20 {
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    function transfer(address to, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
}

contract BarkcoinVN {

    struct BarkOrder {
        uint256 barkOrderId;
        uint256 bondOrderId;
        uint256 blockNumber;
        uint256 timestamp;
        address sourceAddress;
        address targetAddress;
        uint256 vnAmount;
        uint256 barkAmount;
        uint8 status; // 0: pending, 1: completed
    }

    uint256 private constant DECIMALS = 10**18;

    IERC20 public barkToken;
    IBarckcardVLP public vlpContract;
    address public admin;
    uint256 public ratio = 3125;

    uint256 public orderCount;
    mapping(uint256 => BarkOrder) public orders;
    mapping(address => uint256[]) public userOrderIds;
    uint256[] public allOrderIds;
    mapping(uint256 => bool) public bondOrderUsed;

    event OrderCreated(uint256 indexed barkOrderId, uint256 indexed bondOrderId, address sourceAddress, uint256 vnAmount, uint256 barkAmount);
    event OrdersClosed(uint256[] orderIds);
    event BarkWithdrawn(uint256[] orderIds, address to, uint256 totalAmount);
    event RatioUpdated(uint256 oldRatio, uint256 newRatio);
    event AdminChanged(address oldAdmin, address newAdmin);

    modifier onlyAdmin() {
        require(msg.sender == admin, "Not admin");
        _;
    }

    constructor(address _barkToken, address _vlpContract, address _admin) {
        require(_barkToken != address(0), "Invalid bark token");
        require(_vlpContract != address(0), "Invalid vlp contract");
        require(_admin != address(0), "Invalid admin");
        barkToken = IERC20(_barkToken);
        vlpContract = IBarckcardVLP(_vlpContract);
        admin = _admin;
    }

    function changeAdmin(address newAdmin) external onlyAdmin {
        require(newAdmin != address(0), "Invalid new admin");
        address old = admin;
        admin = newAdmin;
        emit AdminChanged(old, newAdmin);
    }

    function createOrder(uint256 bondOrderId) external {
        IBarckcardVLP.BondOrder memory vlpOrder = vlpContract.getOrder(bondOrderId);
        require(vlpOrder.status == 1, "VLP order not completed");
        require(!bondOrderUsed[bondOrderId], "Bond order already used");

        address targetAddress = vlpOrder.targetAddress;
        uint256 vlpAmount = vlpOrder.vlpAmount;

        // vnAmount = ceil(vlpAmount / 10) in whole tokens, stored in wei
        uint256 vnAmount = ((vlpAmount + 10 * DECIMALS - 1) / (10 * DECIMALS)) * DECIMALS;
        require(vnAmount > 0, "vnAmount must be positive");
        require(getUserPendingCount(msg.sender) < 10, "Too many pending orders");

        // barkAmount = vnAmount_whole * ratio, stored in wei
        uint256 barkAmount = (vnAmount / DECIMALS) * ratio * DECIMALS;

        barkToken.transferFrom(msg.sender, address(this), barkAmount);

        bondOrderUsed[bondOrderId] = true;
        orderCount++;
        uint256 barkOrderId = orderCount;

        orders[barkOrderId] = BarkOrder({
            barkOrderId: barkOrderId,
            bondOrderId: bondOrderId,
            blockNumber: block.number,
            timestamp: block.timestamp,
            sourceAddress: msg.sender,
            targetAddress: targetAddress,
            vnAmount: vnAmount,
            barkAmount: barkAmount,
            status: 0
        });

        userOrderIds[msg.sender].push(barkOrderId);
        allOrderIds.push(barkOrderId);

        emit OrderCreated(barkOrderId, bondOrderId, msg.sender, vnAmount, barkAmount);
    }

    function closeOrders(uint256[] calldata orderIds) external onlyAdmin {
        for (uint256 i = 0; i < orderIds.length; i++) {
            uint256 orderId = orderIds[i];
            require(orders[orderId].barkOrderId != 0, "Order does not exist");
            orders[orderId].status = 1;
        }
        emit OrdersClosed(orderIds);
    }

    function withdrawBark(uint256[] calldata orderIds, address to) external onlyAdmin {
        require(to != address(0), "Invalid to address");
        uint256 totalAmount = 0;
        for (uint256 i = 0; i < orderIds.length; i++) {
            uint256 orderId = orderIds[i];
            require(orders[orderId].barkOrderId != 0, "Order does not exist");
            require(orders[orderId].status == 1, "Order not closed");
            totalAmount += orders[orderId].barkAmount;
        }
        if (totalAmount > 0) {
            barkToken.transfer(to, totalAmount);
        }
        emit BarkWithdrawn(orderIds, to, totalAmount);
    }

    function setRatio(uint256 newRatio) external onlyAdmin {
        require(newRatio > 0, "Ratio must be positive");
        uint256 oldRatio = ratio;
        ratio = newRatio;
        emit RatioUpdated(oldRatio, newRatio);
    }

    function getOrder(uint256 barkOrderId) external view returns (BarkOrder memory) {
        return orders[barkOrderId];
    }

    function getUserOrders(address user, uint256 offset, uint256 limit) external view returns (uint256[] memory) {
        uint256[] storage ids = userOrderIds[user];
        uint256 total = ids.length;
        if (total == 0 || offset >= total) {
            return new uint256[](0);
        }
        uint256 startIdx = total - 1 - offset;
        uint256 count = limit;
        if (startIdx + 1 < limit) {
            count = startIdx + 1;
        }
        uint256[] memory result = new uint256[](count);
        for (uint256 i = 0; i < count; i++) {
            result[i] = ids[startIdx - i];
        }
        return result;
    }

    function getAllOrders(uint256 offset, uint256 limit) external view returns (uint256[] memory) {
        uint256 total = allOrderIds.length;
        if (total == 0 || offset >= total) {
            return new uint256[](0);
        }
        uint256 startIdx = total - 1 - offset;
        uint256 count = limit;
        if (startIdx + 1 < limit) {
            count = startIdx + 1;
        }
        uint256[] memory result = new uint256[](count);
        for (uint256 i = 0; i < count; i++) {
            result[i] = allOrderIds[startIdx - i];
        }
        return result;
    }

    function getUserPendingCount(address user) public view returns (uint256) {
        uint256[] storage ids = userOrderIds[user];
        uint256 count = 0;
        for (uint256 i = 0; i < ids.length; i++) {
            if (orders[ids[i]].status == 0) {
                count++;
            }
        }
        return count;
    }
}

contracts/interfaces/IBarckcardVLP.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IBarckcardVLP {
    struct BondOrder {
        uint256 bondOrderId;
        uint256 blockNumber;
        uint256 timestamp;
        address sourceAddress;
        address targetAddress;
        uint256[] nftIdArray;
        uint256 principalA;
        uint256 principalB;
        uint256 interestA;
        uint256 interestB;
        uint256 vlpAmount;
        uint8 status;
    }

    function getOrder(uint256 orderId) external view returns (BondOrder memory);
    function changeAdmin(address newAdmin) external;
    function closeOrders(uint256[] calldata orderIds) external;
    function withdrawNFTs(uint256[] calldata orderIds, address to) external;
}
Compiler settingsShow
{
  "viaIR": true,
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.