Skip to content

Commit

Permalink
Merge pull request #9 from amp-buildpacks/bugfix/deploy
Browse files Browse the repository at this point in the history
Fix deploy failed issue
  • Loading branch information
wangeguo authored Jun 10, 2024
2 parents 2791854 + 50f7622 commit 4a94ddb
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
6 changes: 6 additions & 0 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ api = "0.8"
description = "Configure the rpc for Starknet deploy"
name = "BP_STARKNET_DEPLOY_RPC"

[[metadata.configurations]]
build = true
default = ""
description = "Configure the args for Starknet deploy"
name = "BP_STARKNET_DEPLOY_ARGS"

[[metadata.dependencies]]
id = "starkli-gnu"
name = "Starkli (GNU libc)"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/buildpacks/libcnb v1.30.3
github.com/paketo-buildpacks/libpak v1.70.0
github.com/mattn/go-shellwords v1.0.12
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ fi
ln -fs main linux/amd64/bin/build
ln -fs main linux/arm64/bin/build
ln -fs main linux/amd64/bin/detect
ln -fs main linux/arm64/bin/detect
ln -fs main linux/arm64/bin/detect
69 changes: 55 additions & 14 deletions starknet/starknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/buildpacks/libcnb"
"github.com/mattn/go-shellwords"
"github.com/paketo-buildpacks/libpak"
"github.com/paketo-buildpacks/libpak/bard"
"github.com/paketo-buildpacks/libpak/crush"
Expand All @@ -36,6 +37,11 @@ type Starknet struct {
Executor effect.Executor
}

const (
classHashFile = "/workspace/class_hash.txt"
compileDir = "/workspace/target/dev"
)

func NewStarknet(dependency libpak.BuildpackDependency, cache libpak.DependencyCache, configResolver libpak.ConfigurationResolver) Starknet {
contributor := libpak.NewDependencyLayerContributor(dependency, cache, libcnb.LayerTypes{
Cache: true,
Expand Down Expand Up @@ -92,6 +98,16 @@ func (r Starknet) Contribute(layer libcnb.Layer) (libcnb.Layer, error) {
layer.LaunchEnvironment.Default("STARKNET_PRIVATE_KEY", deployPrivateKey)
layer.LaunchEnvironment.Default("STARKNET_ACCOUNT", deployAccount)
layer.LaunchEnvironment.Default("STARKNET_RPC", deployRpc)

os.Setenv("STARKNET_PRIVATE_KEY", deployPrivateKey)
os.Setenv("STARKNET_ACCOUNT", deployAccount)
os.Setenv("STARKNET_RPC", deployRpc)

classHash, err := r.DeclareContract()
if err != nil {
return libcnb.Layer{}, err
}
layer.LaunchEnvironment.Default("STARKNET_CLASS_HASH", classHash)
return layer, nil
})
}
Expand Down Expand Up @@ -119,15 +135,22 @@ func (r Starknet) BuildProcessTypes(cr libpak.ConfigurationResolver, app libcnb.
return processes, fmt.Errorf("BP_STARKNET_DEPLOY_PRIVATE_KEY must be specified")
}

if classHash, err := r.DeclareContract(); err != nil {
deployWalletAddress, _ := r.configResolver.Resolve("BP_STARKNET_DEPLOY_WALLET_ADDRESS")
processes = append(processes, libcnb.Process{
Type: PlanEntryStarkli,
Command: PlanEntryStarkli,
Arguments: []string{"deploy", classHash, deployWalletAddress},
Default: true,
})
deployArgsRaw, _ := cr.Resolve("BP_STARKNET_DEPLOY_ARGS")
deployArgs, err := shellwords.Parse(deployArgsRaw)
if err != nil {
return processes, fmt.Errorf("unable to parse BP_STARKNET_DEPLOY_ARGS=%q\n%w", deployArgsRaw, err)
}

args := []string{"deploy", "--strk", "$STARKNET_CLASS_HASH"}
args = append(args, deployArgs...)
r.Logger.Bodyf("Deploying contract with args: %s", args)

processes = append(processes, libcnb.Process{
Type: PlanEntryStarkli,
Command: PlanEntryStarkli,
Arguments: args,
Default: true,
})
}
return processes, nil
}
Expand Down Expand Up @@ -155,7 +178,7 @@ func (r Starknet) InitializeWallet() (bool, error) {
deployRpc, _ := r.configResolver.Resolve("BP_STARKNET_DEPLOY_RPC")

accountDir := filepath.Dir(deployAccount)
r.Logger.Bodyf("Initializing deploy wallet and save to dir:", accountDir)
r.Logger.Bodyf("Initializing deploy wallet and save to dir: %s", accountDir)
os.MkdirAll(accountDir, os.ModePerm)

args := []string{
Expand All @@ -175,17 +198,35 @@ func (r Starknet) InitializeWallet() (bool, error) {
}

func (r Starknet) DeclareContract() (string, error) {
r.Logger.Bodyf("Declaring contract")
args := []string{
"declare",
"target/dev/*.contract_class.json",
file, err := r.ReadContractClass()
if err != nil {
return "", fmt.Errorf("unable to read contract class\n%w", err)
}

args := []string{
"class-hash",
filepath.Join(compileDir, file),
}
buf, err := r.Execute(PlanEntryStarkli, args)
if err != nil {
return "", fmt.Errorf("unable to declaring contract\n%w", err)
}

classHash := strings.TrimSpace(buf.String())
r.Logger.Bodyf("Writing class hash: %s to file: %s", classHash, classHashFile)
os.WriteFile(classHashFile, []byte(classHash), 0644)
return classHash, nil
}

func (r Starknet) ReadContractClass() (string, error) {
files, err := os.ReadDir(compileDir)
if err != nil {
return "", fmt.Errorf("unable to read contract class\n%w", err)
}

for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".contract_class.json") {
return file.Name(), nil
}
}
return "", fmt.Errorf("unable to find contract class")
}

0 comments on commit 4a94ddb

Please sign in to comment.