mirror of
https://github.com/alexgo-io/alex-v1.git
synced 2026-01-12 14:33:42 +08:00
stx20-bridge_test (#760)
This commit is contained in:
committed by
GitHub
parent
069d8e9da7
commit
29d7f440a3
@@ -1428,6 +1428,7 @@ depends_on = [ ]
|
||||
[contracts.auto-alex-v3-endpoint]
|
||||
path = "contracts/auto-token/auto-alex-v3-endpoint.clar"
|
||||
depends_on = [ "trait-sip-010", "age000-governance-token", "alex-reserve-pool" ]
|
||||
|
||||
[contracts.stx20-bridge-endpoint]
|
||||
path = "contracts/bridge/stx20-bridge-endpoint.clar"
|
||||
clarity_version = 2
|
||||
@@ -1438,11 +1439,17 @@ path = "contracts/bridge/stx20-bridge-registry.clar"
|
||||
clarity_version = 2
|
||||
epoch = 2.4
|
||||
|
||||
[contracts.stx20-stxs]
|
||||
path = "contracts/token/stx20-stxs.clar"
|
||||
clarity_version = 2
|
||||
epoch = 2.4
|
||||
|
||||
[contracts.token-aewbtc]
|
||||
path = "contracts/token/token-aewbtc.clar"
|
||||
depends_on = [ "trait-ownable", "trait-sip-010" ]
|
||||
clarity_version = 2
|
||||
epoch = 2.4
|
||||
|
||||
[contracts.token-waewbtc]
|
||||
path = "contracts/wrapped-token/token-waewbtc.clar"
|
||||
depends_on = [ "trait-ownable", "trait-sip-010" ]
|
||||
|
||||
@@ -8,9 +8,6 @@
|
||||
|
||||
(define-constant ONE_8 u100000000)
|
||||
|
||||
(define-data-var contract-owner principal tx-sender)
|
||||
(define-map approved-operators principal bool)
|
||||
|
||||
(define-read-only (get-approved-operator-or-default (operator principal))
|
||||
(contract-call? .stx20-bridge-registry get-approved-operator-or-default operator))
|
||||
|
||||
@@ -25,16 +22,6 @@
|
||||
|
||||
;; governance calls
|
||||
|
||||
(define-public (set-contract-owner (owner principal))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (var-set contract-owner owner))))
|
||||
|
||||
(define-public (set-approved-operator (operator principal) (approved bool))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (map-set approved-operators operator approved))))
|
||||
|
||||
;; privileged calls
|
||||
|
||||
(define-public (finalize-peg-in (tx { txid: (buff 32), from: principal, to: principal, ticker: (string-ascii 8), amount: uint }) (token-trait <ft-trait>))
|
||||
@@ -52,14 +39,10 @@
|
||||
(let (
|
||||
(sender tx-sender)
|
||||
(ticker (try! (get-token-to-ticker-or-fail (contract-of token-trait))))
|
||||
(memo (unwrap-panic (to-consensus-buff? (concat "t" (concat ticker (unwrap! (as-max-len? (int-to-ascii (/ amount ONE_8)) u20) err-amount-exceeds-max-len)))))))
|
||||
(memo (concat "t" (concat ticker (unwrap! (as-max-len? (int-to-ascii (/ amount ONE_8)) u20) err-amount-exceeds-max-len)))))
|
||||
(as-contract (try! (contract-call? token-trait burn-fixed amount sender)))
|
||||
(as-contract (stx-transfer-memo? u1 tx-sender sender memo))))
|
||||
|
||||
;; internal calls
|
||||
|
||||
(define-private (check-is-owner)
|
||||
(ok (asserts! (is-eq tx-sender (var-get contract-owner)) err-not-authorised)))
|
||||
|
||||
(define-private (check-is-approved)
|
||||
(ok (asserts! (or (get-approved-operator-or-default tx-sender) (is-ok (check-is-owner))) err-not-authorised)))
|
||||
(try! (stx-transfer? u1 tx-sender (as-contract tx-sender)))
|
||||
(as-contract (try! (stx-transfer-memo? u1 tx-sender sender (unwrap-panic (to-consensus-buff? memo)))))
|
||||
(ok memo)))
|
||||
|
||||
;; internal calls
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
(define-data-var contract-owner principal tx-sender)
|
||||
(define-map approved-operators principal bool)
|
||||
(define-map approved-contracts principal bool)
|
||||
|
||||
(define-map tx-sent { txid: (buff 32), from: principal, to: principal, ticker: (string-ascii 8), amount: uint } bool)
|
||||
(define-map ticker-to-tokens (string-ascii 8) principal)
|
||||
@@ -18,6 +19,9 @@
|
||||
(define-read-only (get-approved-operator-or-default (operator principal))
|
||||
(default-to false (map-get? approved-operators operator)))
|
||||
|
||||
(define-read-only (get-approved-contract-or-default (contract principal))
|
||||
(default-to false (map-get? approved-contracts contract)))
|
||||
|
||||
(define-read-only (get-ticker-to-token-or-fail (ticker (string-ascii 8)))
|
||||
(ok (unwrap! (map-get? ticker-to-tokens ticker) err-invalid-token-or-ticker)))
|
||||
|
||||
@@ -39,6 +43,11 @@
|
||||
(try! (check-is-owner))
|
||||
(ok (map-set approved-operators operator approved))))
|
||||
|
||||
(define-public (set-approved-contract (contract principal) (approved bool))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (map-set approved-contracts contract approved))))
|
||||
|
||||
(define-public (set-ticker-to-token (ticker (string-ascii 8)) (token principal))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
@@ -61,4 +70,4 @@
|
||||
(ok (asserts! (is-eq tx-sender (var-get contract-owner)) err-not-authorised)))
|
||||
|
||||
(define-private (check-is-approved)
|
||||
(ok (asserts! (or (get-approved-operator-or-default tx-sender) (is-ok (check-is-owner))) err-not-authorised)))
|
||||
(ok (asserts! (or (get-approved-contract-or-default tx-sender) (is-ok (check-is-owner))) err-not-authorised)))
|
||||
228
clarity/contracts/token/stx20-stxs.clar
Normal file
228
clarity/contracts/token/stx20-stxs.clar
Normal file
@@ -0,0 +1,228 @@
|
||||
(define-constant ERR-NOT-AUTHORIZED (err u1000))
|
||||
|
||||
(define-constant ONE_8 u100000000)
|
||||
|
||||
;; -- token implementation
|
||||
|
||||
(define-fungible-token stx20-stxs)
|
||||
|
||||
(define-data-var contract-owner principal tx-sender)
|
||||
(define-data-var token-name (string-ascii 32) "STXS (STX20)")
|
||||
(define-data-var token-symbol (string-ascii 32) "STXS")
|
||||
(define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/stx20-stxs.json"))
|
||||
|
||||
(define-data-var token-decimals uint u8)
|
||||
|
||||
(define-map approved-contracts principal bool)
|
||||
|
||||
;; read-only calls
|
||||
|
||||
(define-read-only (get-contract-owner)
|
||||
(ok (var-get contract-owner))
|
||||
)
|
||||
|
||||
(define-read-only (get-name)
|
||||
(ok (var-get token-name))
|
||||
)
|
||||
|
||||
(define-read-only (get-symbol)
|
||||
(ok (var-get token-symbol))
|
||||
)
|
||||
|
||||
(define-read-only (get-decimals)
|
||||
(ok (var-get token-decimals))
|
||||
)
|
||||
|
||||
(define-read-only (get-balance (who principal))
|
||||
(ok (ft-get-balance stx20-stxs who))
|
||||
)
|
||||
|
||||
(define-read-only (get-total-supply)
|
||||
(ok (ft-get-supply stx20-stxs))
|
||||
)
|
||||
|
||||
(define-read-only (get-token-uri)
|
||||
(ok (var-get token-uri))
|
||||
)
|
||||
|
||||
;; @desc fixed-to-decimals
|
||||
;; @params amount
|
||||
;; @returns uint
|
||||
(define-read-only (fixed-to-decimals (amount uint))
|
||||
(/ (* amount (pow-decimals)) ONE_8)
|
||||
)
|
||||
|
||||
;; @desc get-total-supply-fixed
|
||||
;; @params token-id
|
||||
;; @returns (response uint)
|
||||
(define-read-only (get-total-supply-fixed)
|
||||
(ok (decimals-to-fixed (unwrap-panic (get-total-supply))))
|
||||
)
|
||||
|
||||
;; @desc get-balance-fixed
|
||||
;; @params token-id
|
||||
;; @params who
|
||||
;; @returns (response uint)
|
||||
(define-read-only (get-balance-fixed (account principal))
|
||||
(ok (decimals-to-fixed (unwrap-panic (get-balance account))))
|
||||
)
|
||||
|
||||
;; governance calls
|
||||
|
||||
(define-public (set-contract-owner (owner principal))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (var-set contract-owner owner))
|
||||
)
|
||||
)
|
||||
|
||||
(define-public (set-name (new-name (string-ascii 32)))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (var-set token-name new-name))
|
||||
)
|
||||
)
|
||||
|
||||
(define-public (set-symbol (new-symbol (string-ascii 10)))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (var-set token-symbol new-symbol))
|
||||
)
|
||||
)
|
||||
|
||||
(define-public (set-decimals (new-decimals uint))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (var-set token-decimals new-decimals))
|
||||
)
|
||||
)
|
||||
|
||||
(define-public (set-token-uri (new-uri (optional (string-utf8 256))))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (var-set token-uri new-uri))
|
||||
)
|
||||
)
|
||||
|
||||
(define-public (add-approved-contract (new-approved-contract principal))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (map-set approved-contracts new-approved-contract true))
|
||||
)
|
||||
)
|
||||
|
||||
(define-public (set-approved-contract (owner principal) (approved bool))
|
||||
(begin
|
||||
(try! (check-is-owner))
|
||||
(ok (map-set approved-contracts owner approved))
|
||||
)
|
||||
)
|
||||
|
||||
;; priviliged calls
|
||||
|
||||
;; @desc mint
|
||||
;; @restricted ContractOwner/Approved Contract
|
||||
;; @params token-id
|
||||
;; @params amount
|
||||
;; @params recipient
|
||||
;; @returns (response bool)
|
||||
(define-public (mint (amount uint) (recipient principal))
|
||||
(begin
|
||||
(asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED)
|
||||
(ft-mint? stx20-stxs amount recipient)
|
||||
)
|
||||
)
|
||||
|
||||
;; @desc burn
|
||||
;; @restricted ContractOwner/Approved Contract
|
||||
;; @params token-id
|
||||
;; @params amount
|
||||
;; @params sender
|
||||
;; @returns (response bool)
|
||||
(define-public (burn (amount uint) (sender principal))
|
||||
(begin
|
||||
(asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED)
|
||||
(ft-burn? stx20-stxs amount sender)
|
||||
)
|
||||
)
|
||||
|
||||
;; @desc mint-fixed
|
||||
;; @params token-id
|
||||
;; @params amount
|
||||
;; @params recipient
|
||||
;; @returns (response bool)
|
||||
(define-public (mint-fixed (amount uint) (recipient principal))
|
||||
(mint (fixed-to-decimals amount) recipient)
|
||||
)
|
||||
|
||||
;; @desc burn-fixed
|
||||
;; @params token-id
|
||||
;; @params amount
|
||||
;; @params sender
|
||||
;; @returns (response bool)
|
||||
(define-public (burn-fixed (amount uint) (sender principal))
|
||||
(burn (fixed-to-decimals amount) sender)
|
||||
)
|
||||
|
||||
(define-public (mint-fixed-many (recipients (list 200 { amount: uint, to: principal})))
|
||||
(fold mint-many-iter recipients (ok true))
|
||||
)
|
||||
|
||||
;; public calls
|
||||
|
||||
(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
|
||||
(begin
|
||||
(asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED)
|
||||
(try! (ft-transfer? stx20-stxs amount sender recipient))
|
||||
(match memo to-print (print to-print) 0x)
|
||||
(ok true)
|
||||
)
|
||||
)
|
||||
|
||||
;; @desc transfer-fixed
|
||||
;; @params token-id
|
||||
;; @params amount
|
||||
;; @params sender
|
||||
;; @params recipient
|
||||
;; @returns (response bool)
|
||||
(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
|
||||
(transfer (fixed-to-decimals amount) sender recipient memo)
|
||||
)
|
||||
|
||||
(define-public (transfer-fixed-many (recipients (list 200 { amount: uint, to: principal})))
|
||||
(fold transfer-many-iter recipients (ok true))
|
||||
)
|
||||
|
||||
;; private calls
|
||||
|
||||
(define-private (check-is-owner)
|
||||
(ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))
|
||||
)
|
||||
|
||||
(define-private (check-is-approved)
|
||||
(ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))
|
||||
)
|
||||
|
||||
;; @desc decimals-to-fixed
|
||||
;; @params amount
|
||||
;; @returns uint
|
||||
(define-private (decimals-to-fixed (amount uint))
|
||||
(/ (* amount ONE_8) (pow-decimals))
|
||||
)
|
||||
|
||||
;; @desc pow-decimals
|
||||
;; @returns uint
|
||||
(define-private (pow-decimals)
|
||||
(pow u10 (unwrap-panic (get-decimals)))
|
||||
)
|
||||
|
||||
(define-private (mint-many-iter (recipient { amount: uint, to: principal }) (previous-response (response bool uint)))
|
||||
(match previous-response prev-ok (mint-fixed (get amount recipient) (get to recipient)) prev-err previous-response)
|
||||
)
|
||||
|
||||
(define-private (transfer-many-iter (recipient { amount: uint, to: principal }) (previous-response (response bool uint)))
|
||||
(match previous-response prev-ok (transfer-fixed (get amount recipient) tx-sender (get to recipient) none) prev-err previous-response)
|
||||
)
|
||||
|
||||
;; contract initialisation
|
||||
;; (set-contract-owner .executor-dao)
|
||||
190
clarity/tests/stx20-bridge_test.ts
Normal file
190
clarity/tests/stx20-bridge_test.ts
Normal file
@@ -0,0 +1,190 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/ban-types */
|
||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||
import {
|
||||
Account,
|
||||
Chain,
|
||||
Clarinet,
|
||||
Tx,
|
||||
types,
|
||||
} from 'https://deno.land/x/clarinet@v1.2.0/index.ts';
|
||||
|
||||
const contractNames = {
|
||||
endpoint: 'stx20-bridge-endpoint',
|
||||
registry: 'stx20-bridge-registry',
|
||||
stxs: 'stx20-stxs',
|
||||
wstx: 'token-wstx',
|
||||
};
|
||||
|
||||
const uintCV = types.uint;
|
||||
const principalCV = types.principal;
|
||||
const noneCV = types.none;
|
||||
const someCV = types.some;
|
||||
const bufferCV = types.buff;
|
||||
const tupleCV = types.tuple;
|
||||
const boolCV = types.bool;
|
||||
const stringAsciiCV = types.ascii;
|
||||
|
||||
Clarinet.test({
|
||||
name: 'stx20-bridge works',
|
||||
fn(chain: Chain, accounts: Map<string, Account>) {
|
||||
const deployer = accounts.get('deployer')!;
|
||||
const operator = accounts.get('wallet_1')!;
|
||||
const user = accounts.get('wallet_4')!;
|
||||
|
||||
let responses = chain.mineBlock([
|
||||
Tx.contractCall(
|
||||
contractNames.stxs,
|
||||
'set-approved-contract',
|
||||
[
|
||||
principalCV(deployer.address + '.' + contractNames.endpoint),
|
||||
boolCV(true),
|
||||
],
|
||||
deployer.address,
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.registry,
|
||||
'set-approved-contract',
|
||||
[
|
||||
principalCV(deployer.address + '.' + contractNames.endpoint),
|
||||
boolCV(true),
|
||||
],
|
||||
deployer.address,
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.registry,
|
||||
'set-ticker-to-token',
|
||||
[
|
||||
stringAsciiCV('STXS'),
|
||||
principalCV(deployer.address + '.' + contractNames.stxs)
|
||||
],
|
||||
deployer.address,
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.registry,
|
||||
'set-approved-operator',
|
||||
[
|
||||
principalCV(operator.address),
|
||||
boolCV(true),
|
||||
],
|
||||
deployer.address,
|
||||
)
|
||||
]);
|
||||
responses.receipts.map((e: any) => e.result.expectOk());
|
||||
|
||||
responses = chain.mineBlock([
|
||||
Tx.contractCall(
|
||||
contractNames.endpoint,
|
||||
'finalize-peg-in',
|
||||
[
|
||||
tupleCV({
|
||||
txid: bufferCV('0x00'),
|
||||
from: principalCV(user.address),
|
||||
to: principalCV(deployer.address + '.' + contractNames.endpoint),
|
||||
ticker: stringAsciiCV('STXS'),
|
||||
amount: uintCV(1),
|
||||
}),
|
||||
principalCV(deployer.address + '.' + contractNames.stxs),
|
||||
],
|
||||
user.address,
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.endpoint,
|
||||
'finalize-peg-in',
|
||||
[
|
||||
tupleCV({
|
||||
txid: bufferCV('0x00'),
|
||||
from: principalCV(user.address),
|
||||
to: principalCV(operator.address),
|
||||
ticker: stringAsciiCV('STXS'),
|
||||
amount: uintCV(1),
|
||||
}),
|
||||
principalCV(deployer.address + '.' + contractNames.stxs),
|
||||
],
|
||||
operator.address,
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.endpoint,
|
||||
'finalize-peg-in',
|
||||
[
|
||||
tupleCV({
|
||||
txid: bufferCV('0x00'),
|
||||
from: principalCV(user.address),
|
||||
to: principalCV(deployer.address + '.' + contractNames.endpoint),
|
||||
ticker: stringAsciiCV('STXS'),
|
||||
amount: uintCV(1),
|
||||
}),
|
||||
principalCV(deployer.address + '.' + contractNames.wstx),
|
||||
],
|
||||
operator.address,
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.endpoint,
|
||||
'finalize-peg-in',
|
||||
[
|
||||
tupleCV({
|
||||
txid: bufferCV('0x00'),
|
||||
from: principalCV(user.address),
|
||||
to: principalCV(deployer.address + '.' + contractNames.endpoint),
|
||||
ticker: stringAsciiCV('STXS'),
|
||||
amount: uintCV(1),
|
||||
}),
|
||||
principalCV(deployer.address + '.' + contractNames.stxs),
|
||||
],
|
||||
operator.address,
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.endpoint,
|
||||
'finalize-peg-in',
|
||||
[
|
||||
tupleCV({
|
||||
txid: bufferCV('0x00'),
|
||||
from: principalCV(user.address),
|
||||
to: principalCV(deployer.address + '.' + contractNames.endpoint),
|
||||
ticker: stringAsciiCV('STXS'),
|
||||
amount: uintCV(1),
|
||||
}),
|
||||
principalCV(deployer.address + '.' + contractNames.stxs),
|
||||
],
|
||||
operator.address,
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.endpoint,
|
||||
'finalize-peg-out',
|
||||
[
|
||||
principalCV(deployer.address + '.' + contractNames.stxs),
|
||||
uintCV(2e8)
|
||||
],
|
||||
user.address
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.endpoint,
|
||||
'finalize-peg-out',
|
||||
[
|
||||
principalCV(deployer.address + '.' + contractNames.wstx),
|
||||
uintCV(1e8)
|
||||
],
|
||||
user.address
|
||||
),
|
||||
Tx.contractCall(
|
||||
contractNames.endpoint,
|
||||
'finalize-peg-out',
|
||||
[
|
||||
principalCV(deployer.address + '.' + contractNames.stxs),
|
||||
uintCV(1e8)
|
||||
],
|
||||
user.address
|
||||
),
|
||||
]);
|
||||
responses.receipts[0].result.expectErr(1000);
|
||||
responses.receipts[1].result.expectErr(1002);
|
||||
responses.receipts[2].result.expectErr(1001);
|
||||
responses.receipts[3].result.expectOk();
|
||||
responses.receipts[4].result.expectErr(1003);
|
||||
responses.receipts[5].result.expectErr(1);
|
||||
responses.receipts[6].result.expectErr(1001);
|
||||
responses.receipts[7].result.expectOk("tSTXS1");
|
||||
console.log(responses.receipts[3].events);
|
||||
console.log(responses.receipts[7].events);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user