Tài liệu SolanaCơ sở hạ tầng dưới dạng mãThư Viện Chuẩn

Hàm Chuẩn

Các hàm này có sẵn trong tất cả các Runbook như một phần của thư viện chuẩn.

Toán Tử

Các toán tử toán học và so sánh cơ bản:

// Addition
output "sum" {
value = 1 + 2 // or std::add(1, 2)
}
// Subtraction
output "diff" {
value = 5 - 3 // or std::minus(5, 3)
}
// Multiplication
output "product" {
value = 4 * 3 // or std::multiply(4, 3)
}
// Division
output "quotient" {
value = 10 / 2 // or std::div(10, 2)
}
// Modulo
output "remainder" {
value = 10 % 3 // or std::modulo(10, 3)
}

Hàm Băm

// SHA256 hash
output "hash" {
value = std::sha256("hello world")
}
// Keccak256 hash
output "keccak" {
value = std::keccak256("hello world")
}

Hàm Hex

// Encode string's raw bytes to hex (returns string with 0x prefix)
output "hex" {
value = std::encode_hex("hello, world")
}
// > hex: 0x68656c6c6f2c20776f726c64
// Decode from hex (returns buffer)
output "decoded" {
value = std::decode_hex("0x68656c6c6f2c20776f726c64")
}
// > decoded: 0x68656c6c6f2c20776f726c64

Hàm Base58

// Encode to base58 (accepts buffer or hex string)
output "base58" {
value = std::encode_base58("0x68656c6c6f")
}
// > base58: Cn8eVZg
// Decode from base58 (returns buffer)
output "decoded" {
value = std::decode_base58("Cn8eVZg")
}
// > decoded: 0x68656c6c6f

Hàm Base64

// Encode to base64 (accepts buffer or hex string)
output "encoded" {
value = std::encode_base64("0x48656c6c6f20776f726c6421")
}
// > encoded: SGVsbG8gd29ybGQh
// Decode from base64 (returns buffer)
output "decoded" {
value = std::decode_base64("SGVsbG8gd29ybGQh")
}
// > decoded: 0x48656c6c6f20776f726c6421

Hàm JSON

// Query JSON with jq syntax
output "message" {
value = std::jq("{ \"message\": \"Hello world!\" }", ".message")
}

Hàm List

// Get element at index
output "entry" {
value = std::index(['a', 'b', 'c'], 1) // Returns 'b'
}

Hàm Khẳng Định

// Assert equality
output "check_eq" {
value = std::assert_eq(action.example.result, 1)
}
// Assert not equal
output "check_ne" {
value = std::assert_ne(action.example.result, 0)
}
// Assert greater than / greater than or equal
output "check_gt" {
value = std::assert_gt(action.example.result, 0)
}
// Assert less than / less than or equal
output "check_lt" {
value = std::assert_lt(action.example.result, 100)
}

Is this page helpful?

Mục lục

Chỉnh sửa trang