mirror of
https://github.com/alexgo-io/alex-v1.git
synced 2026-01-12 22:21:10 +08:00
amm-swap-pool-helper (#767)
* amm-swap-pool-helper * amm-swap-pool-helper
This commit is contained in:
committed by
GitHub
parent
0b44065e0d
commit
1100d50431
@@ -1561,3 +1561,9 @@ path = "contracts/wrapped-token/token-wwif.clar"
|
||||
depends_on = [ "trait-ownable", "trait-sip-010" ]
|
||||
clarity_version = 2
|
||||
epoch = 2.4
|
||||
|
||||
[contracts.amm-swap-pool-helper]
|
||||
path = "contracts/helpers/amm-swap-pool-helper.clar"
|
||||
depends_on = [ ]
|
||||
clarity_version = 2
|
||||
epoch = 2.4
|
||||
|
||||
126
clarity/contracts/helpers/amm-swap-pool-helper.clar
Normal file
126
clarity/contracts/helpers/amm-swap-pool-helper.clar
Normal file
@@ -0,0 +1,126 @@
|
||||
(use-trait ft-trait .trait-sip-010.sip-010-trait)
|
||||
|
||||
(define-constant err-not-authorised (err u1000))
|
||||
(define-constant err-token-mismatch (err u1001))
|
||||
(define-constant err-token-not-approved (err u1002))
|
||||
(define-constant err-insufficient-balance (err u1003))
|
||||
(define-constant err-request-not-found (err u1004))
|
||||
(define-constant err-request-not-approved (err u1005))
|
||||
|
||||
(define-constant MAX_UINT u340282366920938463463374607431768211455)
|
||||
|
||||
(define-constant PENDING 0x00)
|
||||
(define-constant APPROVED 0x01)
|
||||
(define-constant REJECTED 0x02)
|
||||
(define-constant FINALIZED 0x03)
|
||||
|
||||
(define-data-var contract-owner principal tx-sender)
|
||||
(define-map approved-operators principal bool)
|
||||
|
||||
(define-map approved-tokens principal { approved: bool, min-x: uint })
|
||||
|
||||
(define-data-var request-nonce uint u0)
|
||||
(define-map requests uint {
|
||||
requested-by: principal, requested-at: uint,
|
||||
token-x: principal, token-y: principal, factor: uint,
|
||||
bal-x: uint, bal-y: uint,
|
||||
fee-rate-x: uint, fee-rate-y: uint,
|
||||
max-in-ratio: uint, max-out-ratio: uint,
|
||||
threshold-x: uint, threshold-y: uint,
|
||||
oracle-enabled: bool, oracle-average: uint,
|
||||
start-block: uint,
|
||||
memo: (buff 256),
|
||||
status: (buff 1), status-memo: (buff 256)
|
||||
})
|
||||
|
||||
;; read-only calls
|
||||
|
||||
(define-read-only (get-approved-tokens-or-default (token principal))
|
||||
(default-to { approved: false, min-x: MAX_UINT } (map-get? approved-tokens token)))
|
||||
|
||||
(define-read-only (get-approved-operator-default (operator principal))
|
||||
(default-to false (map-get? approved-operators operator)))
|
||||
|
||||
(define-read-only (get-request-or-fail (request-id uint))
|
||||
(ok (unwrap! (map-get? requests request-id) err-request-not-found)))
|
||||
|
||||
;; public calls
|
||||
|
||||
(define-public (request-create
|
||||
(request-details {
|
||||
token-x: principal, token-y: principal, factor: uint,
|
||||
bal-x: uint, bal-y: uint,
|
||||
fee-rate-x: uint, fee-rate-y: uint,
|
||||
max-in-ratio: uint, max-out-ratio: uint,
|
||||
threshold-x: uint, threshold-y: uint,
|
||||
oracle-enabled: bool, oracle-average: uint,
|
||||
start-block: uint,
|
||||
memo: (buff 256) }) (token-x-trait <ft-trait>))
|
||||
(let (
|
||||
(next-nonce (+ (var-get request-nonce) u1))
|
||||
(token-details (get-approved-tokens-or-default (get token-x request-details)))
|
||||
(updated-request-details (merge request-details { requested-by: tx-sender, requested-at: block-height, status: PENDING, status-memo: 0x00 })))
|
||||
(asserts! (is-eq (get token-x request-details) (contract-of token-x-trait)) err-token-mismatch)
|
||||
(asserts! (get approved token-details) err-token-not-approved)
|
||||
(asserts! (>= (get bal-x request-details) (get min-x token-details)) err-insufficient-balance)
|
||||
(try! (contract-call? token-x-trait transfer-fixed (get bal-x request-details) tx-sender (as-contract tx-sender) none))
|
||||
(map-set requests next-nonce updated-request-details)
|
||||
(var-set request-nonce next-nonce)
|
||||
(print { notification: "request-create", payload: updated-request-details })
|
||||
(ok true)))
|
||||
|
||||
(define-public (finalize-request (request-id uint) (token-x-trait <ft-trait>) (token-y-trait <ft-trait>))
|
||||
(let (
|
||||
(request-details (try! (get-request-or-fail request-id)))
|
||||
(updated-request-details (merge request-details { requested-by: tx-sender, status: FINALIZED })))
|
||||
(asserts! (is-eq (get requested-by request-details) tx-sender) err-not-authorised)
|
||||
(asserts! (is-eq (get status request-details) APPROVED) err-request-not-approved)
|
||||
(asserts! (is-eq (get token-x request-details) (contract-of token-x-trait)) err-token-mismatch)
|
||||
(asserts! (is-eq (get token-y request-details) (contract-of token-y-trait)) err-token-mismatch)
|
||||
(as-contract (try! (contract-call? token-x-trait transfer-fixed (get bal-x request-details) tx-sender (get requested-by request-details) none)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 create-pool token-x-trait token-y-trait (get factor request-details) (get requested-by request-details) (get bal-x request-details) (get bal-y request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-fee-rate-x (get token-x request-details) (get token-y request-details) (get factor request-details) (get fee-rate-x request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-fee-rate-y (get token-x request-details) (get token-y request-details) (get factor request-details) (get fee-rate-y request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-max-in-ratio (get token-x request-details) (get token-y request-details) (get factor request-details) (get max-in-ratio request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-max-out-ratio (get token-x request-details) (get token-y request-details) (get factor request-details) (get max-out-ratio request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-threshold-x (get token-x request-details) (get token-y request-details) (get factor request-details) (get threshold-x request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-threshold-y (get token-x request-details) (get token-y request-details) (get factor request-details) (get threshold-y request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-oracle-enabled (get token-x request-details) (get token-y request-details) (get factor request-details) (get oracle-enabled request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-oracle-average (get token-x request-details) (get token-y request-details) (get factor request-details) (get oracle-average request-details)))
|
||||
(try! (contract-call? .amm-swap-pool-v1-1 set-start-block (get token-x request-details) (get token-y request-details) (get factor request-details) (get start-block request-details)))
|
||||
(map-set requests request-id updated-request-details)
|
||||
(print { notification: "finalize-request", payload: updated-request-details })
|
||||
(ok true)))
|
||||
|
||||
;; priviliged calls
|
||||
|
||||
(define-public (approve-request (request-id uint) (approved bool) (token-x-trait <ft-trait>) (wrapped-token-y principal) (memo (buff 256)))
|
||||
(let (
|
||||
(request-details (try! (get-request-or-fail request-id)))
|
||||
(updated-request-details (merge request-details { token-y: wrapped-token-y, status: (if approved APPROVED REJECTED), status-memo: memo })))
|
||||
(try! (check-is-approved))
|
||||
(asserts! (is-eq (get token-x request-details) (contract-of token-x-trait)) err-token-mismatch)
|
||||
(and (not approved) (as-contract (try! (contract-call? token-x-trait transfer-fixed (get bal-x request-details) tx-sender (get requested-by request-details) none))))
|
||||
(map-set requests request-id updated-request-details)
|
||||
(print { notification: "approve-request", payload: updated-request-details })
|
||||
(ok true)))
|
||||
|
||||
;; governance calls
|
||||
|
||||
(define-public (set-owner (owner principal))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (var-set contract-owner owner))))
|
||||
|
||||
(define-public (approve-operator (operator principal) (approved bool))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (map-set approved-operators operator approved))))
|
||||
|
||||
;; private calls
|
||||
|
||||
(define-private (check-is-approved)
|
||||
(ok (asserts! (or (get-approved-operator-default tx-sender) (is-ok (check-is-owner))) err-not-authorised)))
|
||||
|
||||
(define-private (check-is-owner)
|
||||
(ok (asserts! (is-eq tx-sender (var-get contract-owner)) err-not-authorised)))
|
||||
@@ -113,7 +113,7 @@ Clarinet.test({
|
||||
Tx.contractCall('auto-alex-v3-endpoint', 'revoke-redeem', [types.uint(3)], wallet_3.address),
|
||||
Tx.contractCall('auto-alex-v3-endpoint', 'claim-and-stake', [types.uint(redeem_cycle)], deployer.address)
|
||||
]);
|
||||
console.log(block.receipts[0].events);
|
||||
// console.log(block.receipts[0].events);
|
||||
block.receipts.forEach(e => { e.result.expectOk() });
|
||||
|
||||
block = chain.mineBlock([
|
||||
@@ -125,7 +125,7 @@ Clarinet.test({
|
||||
block = chain.mineBlock([
|
||||
Tx.contractCall('auto-alex-v3-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address),
|
||||
]);
|
||||
console.log(block.receipts[0].events);
|
||||
// console.log(block.receipts[0].events);
|
||||
block.receipts.forEach(e => { e.result.expectOk() });
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user