Skip to content

Commit 799cdc1

Browse files
authored
Add test creating a contract with constructor (#311)
1 parent 4f8d004 commit 799cdc1

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go

+50-4
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,28 @@ import (
1515

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

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

32+
func GetHelloWorldContract() []byte {
33+
return getTestContract("hello_world")
34+
}
35+
36+
func GetNoArgConstructorContract() []byte {
37+
return getTestContract("no_arg_constructor")
38+
}
39+
3040
func CreateInvokeHostOperation(sourceAccount string, contractID xdr.Hash, method string, args ...xdr.ScVal) *txnbuild.InvokeHostFunction {
3141
return &txnbuild.InvokeHostFunction{
3242
HostFunction: xdr.HostFunction{
@@ -116,3 +126,39 @@ func createCreateContractOperation(sourceAccount string, salt xdr.Uint256, contr
116126
SourceAccount: sourceAccount,
117127
}
118128
}
129+
130+
func CreateCreateNoArgConstructorContractOperation(sourceAccount string) *txnbuild.InvokeHostFunction {
131+
contractHash := xdr.Hash(sha256.Sum256(GetNoArgConstructorContract()))
132+
salt := xdr.Uint256(testSalt)
133+
return createCreateContractV2Operation(sourceAccount, salt, contractHash)
134+
}
135+
136+
func createCreateContractV2Operation(
137+
sourceAccount string, salt xdr.Uint256, contractHash xdr.Hash,
138+
) *txnbuild.InvokeHostFunction {
139+
sourceAccountID := xdr.MustAddress(sourceAccount)
140+
return &txnbuild.InvokeHostFunction{
141+
HostFunction: xdr.HostFunction{
142+
Type: xdr.HostFunctionTypeHostFunctionTypeCreateContractV2,
143+
CreateContractV2: &xdr.CreateContractArgsV2{
144+
ContractIdPreimage: xdr.ContractIdPreimage{
145+
Type: xdr.ContractIdPreimageTypeContractIdPreimageFromAddress,
146+
FromAddress: &xdr.ContractIdPreimageFromAddress{
147+
Address: xdr.ScAddress{
148+
Type: xdr.ScAddressTypeScAddressTypeAccount,
149+
AccountId: &sourceAccountID,
150+
},
151+
Salt: salt,
152+
},
153+
},
154+
Executable: xdr.ContractExecutable{
155+
Type: xdr.ContractExecutableTypeContractExecutableWasm,
156+
WasmHash: &contractHash,
157+
},
158+
ConstructorArgs: nil,
159+
},
160+
},
161+
Auth: []xdr.SorobanAuthorizationEntry{},
162+
SourceAccount: sourceAccount,
163+
}
164+
}

cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go

+5
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,11 @@ func (i *Test) UploadHelloWorldContract() (methods.GetTransactionResponse, xdr.H
652652
return i.uploadContract(contractBinary)
653653
}
654654

655+
func (i *Test) UploadNoArgConstructorContract() (methods.GetTransactionResponse, xdr.Hash) {
656+
contractBinary := GetNoArgConstructorContract()
657+
return i.uploadContract(contractBinary)
658+
}
659+
655660
func (i *Test) uploadContract(contractBinary []byte) (methods.GetTransactionResponse, xdr.Hash) {
656661
contractHash := xdr.Hash(sha256.Sum256(contractBinary))
657662
op := CreateUploadWasmOperation(i.MasterAccount().GetAccountID(), contractBinary)

cmd/soroban-rpc/internal/integrationtest/transaction_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,25 @@ func TestSendTransactionFailedInvalidXDR(t *testing.T) {
202202
require.Equal(t, "invalid_xdr", jsonRPCErr.Message)
203203
require.Equal(t, jrpc2.InvalidParams, jsonRPCErr.Code)
204204
}
205+
206+
func TestContractCreationWithConstructor(t *testing.T) {
207+
if infrastructure.GetCoreMaxSupportedProtocol() < 22 {
208+
t.Skip("Only test this for protocol >= 22")
209+
}
210+
test := infrastructure.NewTest(t, nil)
211+
212+
test.UploadNoArgConstructorContract()
213+
214+
client := test.GetRPCLient()
215+
216+
params := infrastructure.PreflightTransactionParams(t, client,
217+
infrastructure.CreateTransactionParams(
218+
test.MasterAccount(),
219+
infrastructure.CreateCreateNoArgConstructorContractOperation(test.MasterAccount().GetAccountID()),
220+
),
221+
)
222+
223+
tx, err := txnbuild.NewTransaction(params)
224+
require.NoError(t, err)
225+
infrastructure.SendSuccessfulTransaction(t, client, test.MasterKey(), tx)
226+
}

wasms/test_no_arg_constructor.wasm

656 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)