Skip to content

Commit

Permalink
neotest: Add 'LoadContract' method
Browse files Browse the repository at this point in the history
Signed-off-by: Chubru <[email protected]>
  • Loading branch information
Chubru committed Dec 20, 2024
1 parent 6d20772 commit a0475a4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/neotest/compile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package neotest

import (
"encoding/json"
"io"
"os"
"testing"

"github.com/nspcc-dev/neo-go/cli/smartcontract"
Expand Down Expand Up @@ -90,3 +92,37 @@ func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath s
contracts[cacheKey] = c
return c
}

// ReadNEF loads a contract from the specified NEF and manifest files.
func ReadNEF(t testing.TB, sender util.Uint160, nefPath, manifestPath string) *Contract {
cacheKey := sender.StringLE() + "|" + nefPath + "|" + manifestPath
if c, ok := contracts[cacheKey]; ok {
return c
}

nefBytes, err := os.ReadFile(nefPath)
require.NoError(t, err)

ne, err := nef.FileFromBytes(nefBytes)
require.NoError(t, err)

manifestBytes, err := os.ReadFile(manifestPath)
require.NoError(t, err)

m := new(manifest.Manifest)
err = json.Unmarshal(manifestBytes, m)
require.NoError(t, err)

hash := state.CreateContractHash(sender, ne.Checksum, m.Name)
err = m.IsValid(hash, true)
require.NoError(t, err)

c := &Contract{
Hash: hash,
NEF: &ne,
Manifest: m,
}

contracts[cacheKey] = c
return c
}

0 comments on commit a0475a4

Please sign in to comment.