Skip to content

Commit

Permalink
Add test creating a transaction with constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Oct 7, 2024
1 parent 4f8d004 commit e86bd25
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@ import (

var testSalt = sha256.Sum256([]byte("a1"))

func GetHelloWorldContract() []byte {
contractFile := path.Join(GetCurrentDirectory(), "../../../../../wasms/test_hello_world.wasm")
func getTestContract(name string) []byte {
contractFile := path.Join(GetCurrentDirectory(), "../../../../../wasms/test_"+name+".wasm")
ret, err := os.ReadFile(contractFile)
if err != nil {
str := fmt.Sprintf(
"unable to read test_hello_world.wasm (%v) please run `make build-test-wasms` at the project root directory",
err)
"unable to read %s.wasm (%v) please run `make build-test-wasms` at the project root directory",
name,
err,
)
panic(str)
}
return ret
}

func GetHelloWorldContract() []byte {
return getTestContract("hello_world")
}

func GetNoArgConstructorContract() []byte {
return getTestContract("no_arg_constructor")
}

func CreateInvokeHostOperation(sourceAccount string, contractID xdr.Hash, method string, args ...xdr.ScVal) *txnbuild.InvokeHostFunction {
return &txnbuild.InvokeHostFunction{
HostFunction: xdr.HostFunction{
Expand Down Expand Up @@ -116,3 +126,37 @@ func createCreateContractOperation(sourceAccount string, salt xdr.Uint256, contr
SourceAccount: sourceAccount,
}
}

func CreateCreateNoArgConstructorContractOperation(sourceAccount string) *txnbuild.InvokeHostFunction {
contractHash := xdr.Hash(sha256.Sum256(GetNoArgConstructorContract()))
salt := xdr.Uint256(testSalt)
return createCreateContractV2Operation(sourceAccount, salt, contractHash)
}

func createCreateContractV2Operation(sourceAccount string, salt xdr.Uint256, contractHash xdr.Hash) *txnbuild.InvokeHostFunction {
sourceAccountID := xdr.MustAddress(sourceAccount)
return &txnbuild.InvokeHostFunction{
HostFunction: xdr.HostFunction{
Type: xdr.HostFunctionTypeHostFunctionTypeCreateContractV2,
CreateContractV2: &xdr.CreateContractArgsV2{
ContractIdPreimage: xdr.ContractIdPreimage{
Type: xdr.ContractIdPreimageTypeContractIdPreimageFromAddress,
FromAddress: &xdr.ContractIdPreimageFromAddress{
Address: xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeAccount,
AccountId: &sourceAccountID,
},
Salt: salt,
},
},
Executable: xdr.ContractExecutable{
Type: xdr.ContractExecutableTypeContractExecutableWasm,
WasmHash: &contractHash,
},
ConstructorArgs: nil,
},
},
Auth: []xdr.SorobanAuthorizationEntry{},
SourceAccount: sourceAccount,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,11 @@ func (i *Test) UploadHelloWorldContract() (methods.GetTransactionResponse, xdr.H
return i.uploadContract(contractBinary)
}

func (i *Test) UploadNoArgConstructorContract() (methods.GetTransactionResponse, xdr.Hash) {
contractBinary := GetNoArgConstructorContract()
return i.uploadContract(contractBinary)
}

func (i *Test) uploadContract(contractBinary []byte) (methods.GetTransactionResponse, xdr.Hash) {
contractHash := xdr.Hash(sha256.Sum256(contractBinary))
op := CreateUploadWasmOperation(i.MasterAccount().GetAccountID(), contractBinary)
Expand Down
20 changes: 20 additions & 0 deletions cmd/soroban-rpc/internal/integrationtest/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,23 @@ func TestSendTransactionFailedInvalidXDR(t *testing.T) {
require.Equal(t, "invalid_xdr", jsonRPCErr.Message)
require.Equal(t, jrpc2.InvalidParams, jsonRPCErr.Code)
}

func TestContractCreationWithConstructor(t *testing.T) {
test := infrastructure.NewTest(t, nil)

test.UploadNoArgConstructorContract()

client := test.GetRPCLient()

params := infrastructure.PreflightTransactionParams(t, client,
infrastructure.CreateTransactionParams(
test.MasterAccount(),
infrastructure.CreateCreateNoArgConstructorContractOperation(test.MasterAccount().GetAccountID()),
),
)

tx, err := txnbuild.NewTransaction(params)
require.NoError(t, err)
infrastructure.SendSuccessfulTransaction(t, client, test.MasterKey(), tx)

}

Check failure on line 224 in cmd/soroban-rpc/internal/integrationtest/transaction_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary trailing newline (whitespace)
Binary file added wasms/test_no_arg_constructor.wasm
Binary file not shown.

0 comments on commit e86bd25

Please sign in to comment.