Module:Blockchain

From Remilia Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Blockchain/doc

-- Module:Blockchain
-- Templates for blockchain explorer links (Etherscan, Basescan, etc.)
-- Used for NFT collections and smart contract documentation

local p = {}

-- Helper: get frame arguments
local function getArgs(frame)
    local args = {}
    local parent = frame:getParent()
    for k, v in pairs(parent.args) do
        if v ~= '' then args[k] = v end
    end
    for k, v in pairs(frame.args) do
        if v ~= '' then args[k] = v end
    end
    return args
end

-- Explorer configurations
local explorers = {
    etherscan = {
        name = 'Etherscan',
        url = 'https://etherscan.io',
        chain = 'Ethereum'
    },
    basescan = {
        name = 'Basescan',
        url = 'https://basescan.org',
        chain = 'Base'
    },
    polygonscan = {
        name = 'Polygonscan',
        url = 'https://polygonscan.com',
        chain = 'Polygon'
    },
    arbiscan = {
        name = 'Arbiscan',
        url = 'https://arbiscan.io',
        chain = 'Arbitrum'
    },
    optimistic = {
        name = 'Optimistic Etherscan',
        url = 'https://optimistic.etherscan.io',
        chain = 'Optimism'
    },
    bscscan = {
        name = 'BscScan',
        url = 'https://bscscan.com',
        chain = 'BNB Chain'
    }
}

-- Known Remilia contract addresses for quick reference
local knownContracts = {
    ['milady'] = {
        address = '0x5Af0D9827E0c53E4799BB226655A1de152A425a5',
        name = 'Milady Maker'
    },
    ['remilio'] = {
        address = '0xD3D9ddd0CF0A5F0BFB8f7fcEAe075DF687eAEBaB',
        name = 'Remilio Babies'
    },
    ['bonkler'] = {
        address = '0xABFaE8A54e6817F57F9De7796044E9a60e61ad67',
        name = 'Bonkler'
    }
}

-- Generic explorer link
-- Parameters: address (required), text (optional), explorer (optional, default: etherscan)
function p.explorer(frame)
    local args = getArgs(frame)
    local address = args.address or args[1] or ''
    local text = args.text or args[2]
    local explorerKey = args.explorer and string.lower(args.explorer) or 'etherscan'

    -- Check for known contract shorthand
    local known = knownContracts[string.lower(address)]
    if known then
        address = known.address
        text = text or known.name
    end

    if address == '' then
        return '<span class="error">Blockchain explorer: address is required</span>'
    end

    local explorer = explorers[explorerKey] or explorers.etherscan
    text = text or explorer.name

    local url = explorer.url .. '/address/' .. address

    local html = mw.html.create('span')
        :addClass('blockchain-link')
        :addClass('blockchain-' .. explorerKey)

    local link = '[' .. url .. ' ' .. text .. ']'
    html:wikitext(link)

    return tostring(html)
end

-- Etherscan link (shortcut for explorer with etherscan)
function p.etherscan(frame)
    local args = getArgs(frame)
    local address = args[1] or args.address or ''
    local text = args[2] or args.text or 'Etherscan'

    -- Check for known contract shorthand
    local known = knownContracts[string.lower(address)]
    if known then
        address = known.address
        if text == 'Etherscan' then
            text = known.name .. ' on Etherscan'
        end
    end

    if address == '' then
        return '<span class="error">Etherscan: contract address is required</span>'
    end

    local url = 'https://etherscan.io/address/' .. address
    local link = '[' .. url .. ' ' .. text .. ']'

    return '<span class="blockchain-link blockchain-etherscan">' .. link .. '</span>'
end

-- OpenSea collection link
function p.opensea(frame)
    local args = getArgs(frame)
    local slug = args[1] or args.slug or ''
    local text = args[2] or args.text or 'OpenSea'

    if slug == '' then
        return '<span class="error">OpenSea: collection slug is required</span>'
    end

    local url = 'https://opensea.io/collection/' .. slug
    local link = '[' .. url .. ' ' .. text .. ']'

    return '<span class="blockchain-link blockchain-opensea">' .. link .. '</span>'
end

-- Transaction link
function p.tx(frame)
    local args = getArgs(frame)
    local hash = args[1] or args.hash or ''
    local text = args[2] or args.text
    local explorerKey = args.explorer and string.lower(args.explorer) or 'etherscan'

    if hash == '' then
        return '<span class="error">Transaction: hash is required</span>'
    end

    local explorer = explorers[explorerKey] or explorers.etherscan
    text = text or 'View transaction'

    local url = explorer.url .. '/tx/' .. hash
    local link = '[' .. url .. ' ' .. text .. ']'

    return '<span class="blockchain-link blockchain-tx">' .. link .. '</span>'
end

-- Token/NFT link (for specific token IDs)
function p.token(frame)
    local args = getArgs(frame)
    local address = args[1] or args.address or ''
    local tokenId = args[2] or args.token or args.id or ''
    local text = args[3] or args.text
    local explorerKey = args.explorer and string.lower(args.explorer) or 'etherscan'

    if address == '' or tokenId == '' then
        return '<span class="error">Token: address and token ID are required</span>'
    end

    local explorer = explorers[explorerKey] or explorers.etherscan
    text = text or 'Token #' .. tokenId

    local url = explorer.url .. '/nft/' .. address .. '/' .. tokenId
    local link = '[' .. url .. ' ' .. text .. ']'

    return '<span class="blockchain-link blockchain-token">' .. link .. '</span>'
end

return p