文章/教程 知道 solana 合约方法名,如何计算出 anchor 的 Discriminator

spike · 2025年05月26日 · 12 次阅读

如果有 idl 文件可以直接从 idl 中查到某个方法对应的 Discriminator 如果没有 idl 但是可以在 solscan 上看到相关交易调用的合约方法名,可以用下面这个 golang 函数计算 Discriminator 项目用其他语言的话,找 chatgpt 翻译一下

package utils

import (
    "crypto/sha256"
    "fmt"
)

// namespace 一般是"global", name 就是你想调用的方法名,和合约保持一致
func GetDiscriminator(namespace string, name string) []byte {
    preimage := fmt.Sprintf("%s:%s", namespace, name)
    hash := sha256.Sum256([]byte(preimage))
    return hash[:8]
}

https://yimingwow.github.io/talks/2025-05-26-talk-4

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号