mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-04-13 22:38:00 +08:00
get-owner -> get-asset-owner, get-balance -> get-token-balance
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
(define-map tokens ((account principal)) ((balance int)))
|
||||
(define (my-get-balance (account principal))
|
||||
(define (get-balance (account principal))
|
||||
(default-to 0 (get balance (fetch-entry tokens (tuple (account account))))))
|
||||
|
||||
(define (token-credit! (account principal) (amount int))
|
||||
(if (<= amount 0)
|
||||
(err "must move positive balance")
|
||||
(let ((current-amount (my-get-balance account)))
|
||||
(let ((current-amount (get-balance account)))
|
||||
(begin
|
||||
(set-entry! tokens (tuple (account account))
|
||||
(tuple (balance (+ amount current-amount))))
|
||||
(ok amount)))))
|
||||
|
||||
(define-public (token-transfer (to principal) (amount int))
|
||||
(let ((balance (my-get-balance tx-sender)))
|
||||
(let ((balance (get-balance tx-sender)))
|
||||
(if (or (> amount balance) (<= amount 0))
|
||||
(err "must transfer positive balance and possess funds")
|
||||
(begin
|
||||
@@ -21,7 +21,7 @@
|
||||
(token-credit! to amount)))))
|
||||
|
||||
(define-public (mint! (amount int))
|
||||
(let ((balance (my-get-balance tx-sender)))
|
||||
(let ((balance (get-balance tx-sender)))
|
||||
(token-credit! tx-sender amount)))
|
||||
|
||||
(token-credit! 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR 10000)
|
||||
|
||||
@@ -31,21 +31,21 @@ fn test_simple_read_only_violations() {
|
||||
(begin
|
||||
(set-entry! tokens (tuple (account tx-sender)) (tuple (balance 10)))
|
||||
tx-sender))
|
||||
(define-read-only (get-balance)
|
||||
(define-read-only (get-token-balance)
|
||||
(fetch-entry tokens ((account (update-balance-and-get-tx-sender)))))",
|
||||
"(define-map tokens ((account principal)) ((balance int)))
|
||||
(define (update-balance-and-get-tx-sender)
|
||||
(begin
|
||||
(set-entry! tokens (tuple (account tx-sender)) (tuple (balance 10)))
|
||||
(tuple (account tx-sender))))
|
||||
(define-read-only (get-balance)
|
||||
(define-read-only (get-token-balance)
|
||||
(fetch-entry tokens (update-balance-and-get-tx-sender)))",
|
||||
"(define-map tokens ((account principal)) ((balance int)))
|
||||
(define (update-balance-and-get-tx-sender)
|
||||
(begin
|
||||
(set-entry! tokens (tuple (account tx-sender)) (tuple (balance 10)))
|
||||
tx-sender))
|
||||
(define-read-only (get-balance)
|
||||
(define-read-only (get-token-balance)
|
||||
(fetch-entry tokens ((account (update-balance-and-get-tx-sender)))))",
|
||||
"(define-map tokens ((account principal)) ((balance int)))
|
||||
(define-read-only (not-reading-only)
|
||||
@@ -70,7 +70,7 @@ fn test_simple_read_only_violations() {
|
||||
fn test_contract_call_read_only_violations() {
|
||||
let contract1 =
|
||||
"(define-map tokens ((account principal)) ((balance int)))
|
||||
(define-read-only (get-balance)
|
||||
(define-read-only (get-token-balance)
|
||||
(get balance (fetch-entry tokens (tuple (account tx-sender))) ))
|
||||
(define-public (mint)
|
||||
(begin
|
||||
@@ -82,7 +82,7 @@ fn test_contract_call_read_only_violations() {
|
||||
(contract-call! contract1 mint))";
|
||||
let ok_caller =
|
||||
"(define-read-only (is-reading-only)
|
||||
(eq? 0 (expects! (contract-call! contract1 get-balance) 'false)))";
|
||||
(eq? 0 (expects! (contract-call! contract1 get-token-balance) 'false)))";
|
||||
|
||||
let mut contract1 = parse(contract1).unwrap();
|
||||
let mut bad_caller = parse(bad_caller).unwrap();
|
||||
|
||||
@@ -5,9 +5,9 @@ use vm::checker::{AnalysisDatabase, AnalysisDatabaseConnection};
|
||||
|
||||
const FIRST_CLASS_TOKENS: &str = "(define-token stackaroos)
|
||||
(define-asset stacka-nfts (buff 10))
|
||||
(get-owner stacka-nfts \"1234567890\" )
|
||||
(define-read-only (my-get-balance (account principal))
|
||||
(get-balance stackaroos account))
|
||||
(get-asset-owner stacka-nfts \"1234567890\" )
|
||||
(define-read-only (my-get-token-balance (account principal))
|
||||
(get-token-balance stackaroos account))
|
||||
(define-public (my-token-transfer (to principal) (amount int))
|
||||
(transfer-token! stackaroos amount tx-sender to))
|
||||
(define-public (faucet)
|
||||
@@ -55,7 +55,7 @@ const ASSET_NAMES: &str =
|
||||
(expects! (fetch-entry preorder-map
|
||||
(tuple (name-hash (hash160 (xor name salt))))) (err 5)))
|
||||
(name-entry
|
||||
(get-owner names name)))
|
||||
(get-asset-owner names name)))
|
||||
(if (and
|
||||
(is-none? name-entry)
|
||||
;; preorder must have paid enough
|
||||
@@ -90,13 +90,13 @@ fn test_names_tokens_contracts() {
|
||||
fn test_bad_asset_usage() {
|
||||
use vm::checker::type_check;
|
||||
|
||||
let bad_scripts = ["(get-balance stackoos tx-sender)",
|
||||
"(get-balance 1234 tx-sender)",
|
||||
"(get-balance stackaroos 100)",
|
||||
"(get-owner 1234 \"abc\")",
|
||||
"(get-owner stackoos \"abc\")",
|
||||
"(get-owner stacka-nfts 1234 )",
|
||||
"(get-owner stacka-nfts \"123456789012345\" )",
|
||||
let bad_scripts = ["(get-token-balance stackoos tx-sender)",
|
||||
"(get-token-balance 1234 tx-sender)",
|
||||
"(get-token-balance stackaroos 100)",
|
||||
"(get-asset-owner 1234 \"abc\")",
|
||||
"(get-asset-owner stackoos \"abc\")",
|
||||
"(get-asset-owner stacka-nfts 1234 )",
|
||||
"(get-asset-owner stacka-nfts \"123456789012345\" )",
|
||||
"(mint-asset! 1234 \"abc\" tx-sender)",
|
||||
"(mint-asset! stackoos \"abc\" tx-sender)",
|
||||
"(mint-asset! stacka-nfts 1234 tx-sender)",
|
||||
|
||||
@@ -7,7 +7,7 @@ use vm::checker::{AnalysisDatabase, AnalysisDatabaseConnection};
|
||||
|
||||
const SIMPLE_TOKENS: &str =
|
||||
"(define-map tokens ((account principal)) ((balance int)))
|
||||
(define-read-only (my-get-balance (account principal))
|
||||
(define-read-only (my-get-token-balance (account principal))
|
||||
(let ((balance
|
||||
(get balance (fetch-entry tokens (tuple (account account))))))
|
||||
(default-to 0 balance)))
|
||||
@@ -15,13 +15,13 @@ const SIMPLE_TOKENS: &str =
|
||||
(define (token-credit! (account principal) (amount int))
|
||||
(if (<= amount 0)
|
||||
(err 1)
|
||||
(let ((current-amount (my-get-balance account)))
|
||||
(let ((current-amount (my-get-token-balance account)))
|
||||
(begin
|
||||
(set-entry! tokens (tuple (account account))
|
||||
(tuple (balance (+ amount current-amount))))
|
||||
(ok 0)))))
|
||||
(define-public (token-transfer (to principal) (amount int))
|
||||
(let ((balance (my-get-balance tx-sender)))
|
||||
(let ((balance (my-get-token-balance tx-sender)))
|
||||
(if (or (> amount balance) (<= amount 0))
|
||||
(err 2)
|
||||
(begin
|
||||
@@ -439,7 +439,7 @@ fn test_bad_map_usage() {
|
||||
use vm::checker::type_check;
|
||||
let bad_fetch =
|
||||
"(define-map tokens ((account principal)) ((balance int)))
|
||||
(define (my-get-balance (account int))
|
||||
(define (my-get-token-balance (account int))
|
||||
(let ((balance
|
||||
(get balance (fetch-entry tokens (tuple (account account))))))
|
||||
balance))";
|
||||
@@ -497,40 +497,40 @@ fn test_expects() {
|
||||
use vm::checker::type_check;
|
||||
let okay =
|
||||
"(define-map tokens ((id int)) ((balance int)))
|
||||
(define (my-get-balance)
|
||||
(define (my-get-token-balance)
|
||||
(let ((balance (expects!
|
||||
(get balance (fetch-entry tokens (tuple (id 0))))
|
||||
0)))
|
||||
(+ 0 balance)))
|
||||
(define (my-get-balance-2)
|
||||
(define (my-get-token-balance-2)
|
||||
(let ((balance
|
||||
(get balance (expects! (fetch-entry tokens (tuple (id 0))) 0))
|
||||
))
|
||||
(+ 0 balance)))
|
||||
(define (my-get-balance-3)
|
||||
(define (my-get-token-balance-3)
|
||||
(let ((balance
|
||||
(expects! (get balance (fetch-entry tokens (tuple (id 0))))
|
||||
(err 'false))))
|
||||
(ok balance)))
|
||||
(define (my-get-balance-4)
|
||||
(expects! (my-get-balance-3) 0))
|
||||
(define (my-get-token-balance-4)
|
||||
(expects! (my-get-token-balance-3) 0))
|
||||
|
||||
(define (t-1)
|
||||
(err 3))
|
||||
(define (my-get-balance-5)
|
||||
(define (my-get-token-balance-5)
|
||||
(expects-err! (t-1) 0))
|
||||
|
||||
(+ (my-get-balance) (my-get-balance-2) (my-get-balance-5))";
|
||||
(+ (my-get-token-balance) (my-get-token-balance-2) (my-get-token-balance-5))";
|
||||
|
||||
let bad_return_types_tests = [
|
||||
"(define-map tokens ((id int)) ((balance int)))
|
||||
(define (my-get-balance)
|
||||
(define (my-get-token-balance)
|
||||
(let ((balance (expects!
|
||||
(get balance (fetch-entry tokens (tuple (id 0))))
|
||||
'false)))
|
||||
(+ 0 balance)))",
|
||||
"(define-map tokens ((id int)) ((balance int)))
|
||||
(define (my-get-balance)
|
||||
(define (my-get-token-balance)
|
||||
(let ((balance (expects!
|
||||
(get balance (fetch-entry tokens (tuple (id 0))))
|
||||
(err 1))))
|
||||
|
||||
@@ -663,7 +663,7 @@ identifiers are _unique_ identifiers.
|
||||
Like other kinds of definition statements, `define-asset` may only be used at the top level of a smart contract
|
||||
definition (i.e., you cannot put a define statement in the middle of a function body).
|
||||
|
||||
Assets defined using `define-asset` may be used in `asset-transfer!`, `mint-asset!`, and `get-owner` functions",
|
||||
Assets defined using `define-asset` may be used in `asset-transfer!`, `mint-asset!`, and `get-asset-owner` functions",
|
||||
example: "
|
||||
(define-asset names (buff 50))
|
||||
"
|
||||
@@ -821,30 +821,30 @@ Otherwise, on successfuly mint, it returns `(ok 'true)`.
|
||||
};
|
||||
|
||||
const GET_OWNER: SpecialAPI = SpecialAPI {
|
||||
name: "get-owner",
|
||||
name: "get-asset-owner",
|
||||
input_type: "AssetName, A",
|
||||
output_type: "(optional principal)",
|
||||
signature: "(get-owner asset-class asset-identifier)",
|
||||
description: "`get-owner` returns the owner of an asset, identified by `asset-identifier`, or `none` if the asset does not exist.
|
||||
signature: "(get-asset-owner asset-class asset-identifier)",
|
||||
description: "`get-asset-owner` returns the owner of an asset, identified by `asset-identifier`, or `none` if the asset does not exist.
|
||||
The asset type must have been defined using `define-asset`, and the supplied `asset-identifier` must be of the same type specified in
|
||||
that definition.",
|
||||
example: "
|
||||
(define-asset stackaroo (buff 40))
|
||||
(get-owner stackaroo \"Roo\")
|
||||
(get-asset-owner stackaroo \"Roo\")
|
||||
"
|
||||
};
|
||||
|
||||
|
||||
const GET_BALANCE: SpecialAPI = SpecialAPI {
|
||||
name: "get-balance",
|
||||
name: "get-token-balance",
|
||||
input_type: "TokenName, principal",
|
||||
output_type: "int",
|
||||
signature: "(get-balance token-name principal)",
|
||||
description: "`get-balance` returns `token-name` balance of the principal `principal`.
|
||||
signature: "(get-token-balance token-name principal)",
|
||||
description: "`get-token-balance` returns `token-name` balance of the principal `principal`.
|
||||
The token type must have been defined using `define-token`.",
|
||||
example: "
|
||||
(define-token stackaroos)
|
||||
(get-balance stackaroos tx-sender)
|
||||
(get-token-balance stackaroos tx-sender)
|
||||
"
|
||||
};
|
||||
|
||||
|
||||
@@ -133,8 +133,8 @@ impl NativeFunctions {
|
||||
"is-ok?" => Some(IsOkay),
|
||||
"is-none?" => Some(IsNone),
|
||||
"filter" => Some(Filter),
|
||||
"get-balance" => Some(GetTokenBalance),
|
||||
"get-owner" => Some(GetAssetOwner),
|
||||
"get-token-balance" => Some(GetTokenBalance),
|
||||
"get-asset-owner" => Some(GetAssetOwner),
|
||||
"transfer-token!" => Some(TransferToken),
|
||||
"transfer-asset!" => Some(TransferAsset),
|
||||
"mint-asset!" => Some(MintAsset),
|
||||
|
||||
@@ -16,8 +16,8 @@ fn symbols_from_values(mut vec: Vec<Value>) -> Vec<SymbolicExpression> {
|
||||
}
|
||||
|
||||
const FIRST_CLASS_TOKENS: &str = "(define-token stackaroos)
|
||||
(define-read-only (my-get-balance (account principal))
|
||||
(get-balance stackaroos account))
|
||||
(define-read-only (my-get-token-balance (account principal))
|
||||
(get-token-balance stackaroos account))
|
||||
(define-public (my-token-transfer (to principal) (amount int))
|
||||
(transfer-token! stackaroos amount tx-sender to))
|
||||
(define-public (faucet)
|
||||
@@ -87,7 +87,7 @@ const ASSET_NAMES: &str =
|
||||
(expects! (fetch-entry preorder-map
|
||||
(tuple (name-hash (hash160 (xor name salt))))) (err 5)))
|
||||
(name-entry
|
||||
(get-owner names name)))
|
||||
(get-asset-owner names name)))
|
||||
(if (and
|
||||
(is-none? name-entry)
|
||||
;; preorder must have paid enough
|
||||
@@ -196,7 +196,7 @@ fn test_simple_token_system() {
|
||||
assert_eq!(asset_map.to_table().len(), 0);
|
||||
|
||||
let (result, asset_map) = execute_transaction(&mut conn,
|
||||
p1.clone(), "tokens", "my-get-balance", &symbols_from_values(vec![p1.clone()])).unwrap();
|
||||
p1.clone(), "tokens", "my-get-token-balance", &symbols_from_values(vec![p1.clone()])).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
@@ -204,7 +204,7 @@ fn test_simple_token_system() {
|
||||
assert_eq!(asset_map.to_table().len(), 0);
|
||||
|
||||
let (result, asset_map) = execute_transaction(&mut conn,
|
||||
p1.clone(), "tokens", "my-get-balance", &symbols_from_values(vec![p2.clone()])).unwrap();
|
||||
p1.clone(), "tokens", "my-get-token-balance", &symbols_from_values(vec![p2.clone()])).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
@@ -234,7 +234,7 @@ fn test_simple_token_system() {
|
||||
assert_eq!(asset_map[&contract_principal][&token_identifier], AssetMapEntry::Token(1));
|
||||
|
||||
let (result, asset_map) = execute_transaction(&mut conn,
|
||||
p1.clone(), "tokens", "my-get-balance", &symbols_from_values(vec![p1.clone()])).unwrap();
|
||||
p1.clone(), "tokens", "my-get-token-balance", &symbols_from_values(vec![p1.clone()])).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
@@ -331,7 +331,7 @@ fn test_simple_naming_system() {
|
||||
let mut env = owned_env.get_exec_environment(None);
|
||||
assert_eq!(
|
||||
env.eval_read_only("names",
|
||||
"(get-owner names 1)").unwrap(),
|
||||
"(get-asset-owner names 1)").unwrap(),
|
||||
Value::some(p2.clone()));
|
||||
}
|
||||
|
||||
|
||||
@@ -34,20 +34,20 @@ const FACTORIAL_CONTRACT: &str = "(define-map factorials ((id int)) ((current in
|
||||
(init-factorial 8008 5))";
|
||||
|
||||
const SIMPLE_TOKENS: &str = "(define-map tokens ((account principal)) ((balance int)))
|
||||
(define-read-only (my-get-balance (account principal))
|
||||
(define-read-only (my-get-token-balance (account principal))
|
||||
(default-to 0 (get balance (fetch-entry tokens (tuple (account account))))))
|
||||
(define-read-only (explode (account principal))
|
||||
(delete-entry! tokens (tuple (account account))))
|
||||
(define (token-credit! (account principal) (amount int))
|
||||
(if (<= amount 0)
|
||||
(err \"must be positive\")
|
||||
(let ((current-amount (my-get-balance account)))
|
||||
(let ((current-amount (my-get-token-balance account)))
|
||||
(begin
|
||||
(set-entry! tokens (tuple (account account))
|
||||
(tuple (balance (+ amount current-amount))))
|
||||
(ok 0)))))
|
||||
(define-public (token-transfer (to principal) (amount int))
|
||||
(let ((balance (my-get-balance tx-sender)))
|
||||
(let ((balance (my-get-token-balance tx-sender)))
|
||||
(if (or (> amount balance) (<= amount 0))
|
||||
(err \"not enough balance\")
|
||||
(begin
|
||||
@@ -165,11 +165,11 @@ fn test_simple_token_system() {
|
||||
|
||||
assert_eq!(
|
||||
env.eval_read_only("tokens",
|
||||
"(my-get-balance 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)").unwrap(),
|
||||
"(my-get-token-balance 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)").unwrap(),
|
||||
Value::Int(1000));
|
||||
assert_eq!(
|
||||
env.eval_read_only("tokens",
|
||||
"(my-get-balance 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G)").unwrap(),
|
||||
"(my-get-token-balance 'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G)").unwrap(),
|
||||
Value::Int(9200));
|
||||
assert!(is_committed(&
|
||||
env.execute_contract("tokens", "faucet", &vec![]).unwrap()));
|
||||
@@ -182,7 +182,7 @@ fn test_simple_token_system() {
|
||||
|
||||
assert_eq!(
|
||||
env.eval_read_only("tokens",
|
||||
"(my-get-balance 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)").unwrap(),
|
||||
"(my-get-token-balance 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)").unwrap(),
|
||||
Value::Int(1003));
|
||||
assert!(!is_committed(&
|
||||
env.execute_contract("tokens", "mint-after", &symbols_from_values(vec![Value::Int(25)])).unwrap()));
|
||||
@@ -196,10 +196,10 @@ fn test_simple_token_system() {
|
||||
|
||||
assert_eq!(
|
||||
env.eval_read_only("tokens",
|
||||
"(my-get-balance 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)").unwrap(),
|
||||
"(my-get-token-balance 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)").unwrap(),
|
||||
Value::Int(1004));
|
||||
assert_eq!(
|
||||
env.execute_contract("tokens", "my-get-balance", &symbols_from_values(vec![p1.clone()])).unwrap(),
|
||||
env.execute_contract("tokens", "my-get-token-balance", &symbols_from_values(vec![p1.clone()])).unwrap(),
|
||||
Value::Int(1004));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user