(impl-trait .gas-oracle-trait.gas-oracle-trait)
(define-constant oracle-scaling-factor u1000000000000000000)
(define-constant err-unauthorized (err u4000))
(define-constant err-not-found (err u4001))
(define-constant err-already-initialized (err u4002))
(define-constant err-not-initialized (err u4003))
(define-data-var owner principal tx-sender)
(define-data-var this-chain-id (optional uint) none)
(define-data-var from-oracle-to-chain-scaling-factor uint u1000000000000)
(define-map chain-data
uint
{
price: uint,
gas-price: uint,
}
)
(define-public (init (this-chain-id_ uint))
(begin
(asserts! (is-none (var-get this-chain-id)) err-already-initialized)
(var-set this-chain-id (some this-chain-id_))
(only-owner)
)
)
(define-public (set-chain-data
(chain-id_ uint)
(new-price uint)
(new-gas-price uint)
)
(begin
(map-set chain-data chain-id_ {
price: new-price,
gas-price: new-gas-price,
})
(only-owner)
)
)
(define-public (set-price
(chain-id_ uint)
(new-price uint)
)
(begin
(match (map-get? chain-data chain-id_)
entry (map-set chain-data chain-id_ (merge entry { price: new-price }))
(map-set chain-data chain-id_ {
price: new-price,
gas-price: u0,
})
)
(only-owner)
)
)
(define-public (set-gas-price
(chain-id_ uint)
(new-gas-price uint)
)
(begin
(match (map-get? chain-data chain-id_)
entry (map-set chain-data chain-id_ (merge entry { gas-price: new-gas-price }))
(map-set chain-data chain-id_ {
price: u0,
gas-price: new-gas-price,
})
)
(only-owner)
)
)
(define-read-only (get-transaction-gas-cost-in-native-token
(other-chain-id uint)
(gas-amount uint)
)
(let (
(scaling-factor (var-get from-oracle-to-chain-scaling-factor))
(other-entry (unwrap! (map-get? chain-data other-chain-id) err-not-found))
(self-entry (unwrap!
(map-get? chain-data
(unwrap! (var-get this-chain-id) err-not-initialized)
)
err-not-found
))
(other-gas-price (get gas-price other-entry))
(other-price (get price other-entry))
(self-price (get price self-entry))
)
(ok (/ (* (* other-gas-price gas-amount) other-price)
(* self-price scaling-factor)
))
)
)
(define-read-only (get-transaction-gas-cost-in-usd
(other-chain-id uint)
(gas-amount uint)
)
(let (
(other-entry (unwrap! (map-get? chain-data other-chain-id) err-not-found))
(other-gas-price (get gas-price other-entry))
(other-price (get price other-entry))
)
(ok (/ (* (* other-gas-price gas-amount) other-price) oracle-scaling-factor))
)
)
(define-read-only (cross-rate (other-chain-id uint))
(let (
(other-entry (unwrap! (map-get? chain-data other-chain-id) err-not-found))
(self-entry (unwrap!
(map-get? chain-data
(unwrap! (var-get this-chain-id) err-not-initialized)
)
err-not-found
))
(other-price (get price other-entry))
(self-price (get price self-entry))
)
(ok (/ (* other-price oracle-scaling-factor) self-price))
)
)
(define-read-only (get-price (chain-id_ uint))
(match (map-get? chain-data chain-id_)
entry (ok (get price entry))
err-not-found
)
)
(define-read-only (get-gas-price (chain-id_ uint))
(match (map-get? chain-data chain-id_)
entry (ok (get gas-price entry))
err-not-found
)
)
(define-read-only (get-chain-data (chain-id_ uint))
(match (map-get? chain-data chain-id_)
entry (ok entry)
err-not-found
)
)
;; ========================= OWNABLE ==========================
(define-public (set-owner (new-owner principal))
(begin
(try! (only-owner))
(var-set owner new-owner)
(ok true)
)
)
(define-private (only-owner)
(if (is-eq contract-caller (var-get owner))
(ok true)
err-unauthorized
)
)