Skip to content

Commit

Permalink
Multiple smart contract support
Browse files Browse the repository at this point in the history
  • Loading branch information
janezpodhostnik committed Oct 28, 2020
1 parent 6aa98c0 commit dda6277
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 74 deletions.
9 changes: 5 additions & 4 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import (

// An Account is an account on the Flow network.
type Account struct {
Address Address
Balance uint64
Code []byte
Keys []*AccountKey
Address Address
Balance uint64
Code []byte
Keys []*AccountKey
Contracts map[string][]byte
}

// AccountKeyWeightThreshold is the total key weight required to authorize access to an account.
Expand Down
6 changes: 3 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,10 @@ type EventRangeQuery struct {

// BlockEvents are the events that occurred in a specific block.
type BlockEvents struct {
BlockID flow.Identifier
Height uint64
BlockID flow.Identifier
Height uint64
BlockTimestamp time.Time
Events []flow.Event
Events []flow.Event
}

// GetEventsForHeightRange retrieves events for all sealed blocks between the start and end block
Expand Down
18 changes: 10 additions & 8 deletions client/convert/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func AccountToMessage(a flow.Account) *entities.Account {
}

return &entities.Account{
Address: a.Address.Bytes(),
Balance: a.Balance,
Code: a.Code,
Keys: accountKeys,
Address: a.Address.Bytes(),
Balance: a.Balance,
Code: a.Code,
Keys: accountKeys,
Contracts: a.Contracts,
}
}

Expand All @@ -65,10 +66,11 @@ func MessageToAccount(m *entities.Account) (flow.Account, error) {
}

return flow.Account{
Address: flow.BytesToAddress(m.GetAddress()),
Balance: m.GetBalance(),
Code: m.GetCode(),
Keys: accountKeys,
Address: flow.BytesToAddress(m.GetAddress()),
Balance: m.GetBalance(),
Code: m.GetCode(),
Keys: accountKeys,
Contracts: m.GetContracts(),
}, nil
}

Expand Down
6 changes: 5 additions & 1 deletion examples/deploy_contract/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ func DeployContractDemo() {

// Deploy the Great NFT contract
nftCode := examples.ReadFile(GreatTokenContractFile)
deployContractTx := templates.CreateAccount(nil, nftCode, myAddress)
deployContractTx := templates.CreateAccount(nil,
[]templates.Contract{{
Name: "GreatToken",
Source: nftCode,
}}, myAddress)

deployContractTx.SetProposalKey(myAddress, myAcctKey.Index, myAcctKey.SequenceNumber)
// we can set the same reference block id. We shouldn't be to far away from it
Expand Down
18 changes: 8 additions & 10 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ import (
)

// ReadFile reads a file from the file system.
func ReadFile(path string) []byte {
func ReadFile(path string) string {
contents, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return contents
return string(contents)
}

const configPath = "./flow.json"
Expand Down Expand Up @@ -133,7 +133,6 @@ func RandomAccount(flowClient *client.Client) (flow.Address, *flow.AccountKey, c
account := CreateAccount(
flowClient,
[]*flow.AccountKey{accountKey},
nil,
)

signer := crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
Expand All @@ -147,18 +146,13 @@ func GetReferenceBlockId(flowClient *client.Client) flow.Identifier {
return block.ID
}

func DeployContract(flowClient *client.Client, code []byte) flow.Address {
account := CreateAccount(flowClient, nil, code)
return account.Address
}

func CreateAccount(flowClient *client.Client, publicKeys []*flow.AccountKey, code []byte) *flow.Account {
func CreateAccountWithContracts(flowClient *client.Client, publicKeys []*flow.AccountKey, contracts []templates.Contract) *flow.Account {
ctx := context.Background()

serviceAcctAddr, serviceAcctKey, serviceSigner := ServiceAccount(flowClient)
referenceBlockID := GetReferenceBlockId(flowClient)

createAccountTx := templates.CreateAccount(publicKeys, code, serviceAcctAddr)
createAccountTx := templates.CreateAccount(publicKeys, contracts, serviceAcctAddr)
createAccountTx.
SetProposalKey(serviceAcctAddr, serviceAcctKey.Index, serviceAcctKey.SequenceNumber).
SetReferenceBlockID(referenceBlockID).
Expand All @@ -184,6 +178,10 @@ func CreateAccount(flowClient *client.Client, publicKeys []*flow.AccountKey, cod
return account
}

func CreateAccount(flowClient *client.Client, publicKeys []*flow.AccountKey) *flow.Account {
return CreateAccountWithContracts(flowClient, publicKeys, nil)
}

func Handle(err error) {
if err != nil {
fmt.Println("err:", err.Error())
Expand Down
11 changes: 8 additions & 3 deletions examples/query_events/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main
import (
"context"
"fmt"
"github.com/onflow/flow-go-sdk/templates"

"google.golang.org/grpc"

Expand Down Expand Up @@ -53,7 +54,11 @@ func QueryEventsDemo() {
}
`

contractAddr := examples.DeployContract(flowClient, []byte(contract))
contractAccount := examples.CreateAccountWithContracts(flowClient,
nil, []templates.Contract{{
Name: "EventDemo",
Source: contract,
}})

// Send a tx that emits the event in the deployed contract
script := fmt.Sprintf(`
Expand All @@ -64,7 +69,7 @@ func QueryEventsDemo() {
EventDemo.add(x: 2, y: 3)
}
}
`, contractAddr.Hex())
`, contractAccount.Address.Hex())

referenceBlockID := examples.GetReferenceBlockId(flowClient)
runScriptTx := flow.NewTransaction().
Expand Down Expand Up @@ -103,7 +108,7 @@ func QueryEventsDemo() {
// 2
// Query for our custom event by type
results, err = flowClient.GetEventsForHeightRange(ctx, client.EventRangeQuery{
Type: fmt.Sprintf("A.%s.EventDemo.Add", contractAddr.Hex()),
Type: fmt.Sprintf("AC.%s.EventDemo.EventDemo.Add", contractAccount.Address.Hex()),
StartHeight: 0,
EndHeight: 100,
})
Expand Down
4 changes: 2 additions & 2 deletions examples/transaction_signing/multi_party/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func MultiPartySingleSignatureDemo() {

key3Signer := crypto.NewInMemorySigner(privateKey3, key3.HashAlgo)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1}, nil)
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3}, nil)
account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1})
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3})
referenceBlockID := examples.GetReferenceBlockId(flowClient)

tx := flow.NewTransaction().
Expand Down
4 changes: 2 additions & 2 deletions examples/transaction_signing/multi_party_multisig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func MultiPartyMultiSignatureDemo() {

key4Signer := crypto.NewInMemorySigner(privateKey4, key4.HashAlgo)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1, key2}, nil)
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3, key4}, nil)
account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1, key2})
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3, key4})
referenceBlockID := examples.GetReferenceBlockId(flowClient)

tx := flow.NewTransaction().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func MultiPartySingleSignatureDemo() {

key3Signer := crypto.NewInMemorySigner(privateKey3, key3.HashAlgo)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1}, nil)
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3}, nil)
account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1})
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3})

referenceBlockID := examples.GetReferenceBlockId(flowClient)
tx := flow.NewTransaction().
Expand Down
2 changes: 1 addition & 1 deletion examples/transaction_signing/single_party/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func SinglePartySingleSignatureDemo() {

key1Signer := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1}, nil)
account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1})

referenceBlockID := examples.GetReferenceBlockId(flowClient)
tx := flow.NewTransaction().
Expand Down
2 changes: 1 addition & 1 deletion examples/transaction_signing/single_party_multisig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func SinglePartyMultiSignatureDemo() {

key2Signer := crypto.NewInMemorySigner(privateKey2, key2.HashAlgo)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1, key2}, nil)
account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1, key2})

referenceBlockID := examples.GetReferenceBlockId(flowClient)
tx := flow.NewTransaction().
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6
github.com/ethereum/go-ethereum v1.9.9
github.com/golang/protobuf v1.4.2
github.com/onflow/cadence v0.9.1
github.com/onflow/flow/protobuf/go/flow v0.1.7
github.com/onflow/cadence v0.10.0
github.com/onflow/flow/protobuf/go/flow v0.1.8
github.com/pkg/errors v0.8.1
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.5.1
Expand All @@ -17,3 +17,5 @@ require (
google.golang.org/genproto v0.0.0-20200831141814-d751682dd103
google.golang.org/grpc v1.31.1
)

replace github.com/fxamacker/cbor/v2 => github.com/turbolent/cbor/v2 v2.2.1-0.20200911003300-cac23af49154
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fxamacker/cbor/v2 v2.2.0 h1:6eXqdDDe588rSYAi1HfZKbx6YYQO4mxQ9eC6xYpU/JQ=
github.com/fxamacker/cbor/v2 v2.2.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -219,10 +217,10 @@ github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/onflow/cadence v0.9.1 h1:ZgkR/e8P2vLDo9+QBVucfx8O/OKMiEwlUAzcJOIuqjc=
github.com/onflow/cadence v0.9.1/go.mod h1:R43uGnqQsTcnFf1fMPCcMjDPplBIzbR5XkFZRF2OH3Y=
github.com/onflow/flow/protobuf/go/flow v0.1.7 h1:BaZVc1XWkI1vx/+qvdkXnKjsWk4UFRBAg7vvfQ/u5Pk=
github.com/onflow/flow/protobuf/go/flow v0.1.7/go.mod h1:kRugbzZjwQqvevJhrnnCFMJZNmoSJmxlKt6hTGXZojM=
github.com/onflow/cadence v0.10.0 h1:mYv5Chgk44mPKKXCRP+jnYaS6yBXOlpBOYUfI1q1L6w=
github.com/onflow/cadence v0.10.0/go.mod h1:ORAnWydDsrefAUazeD1g+l7vjNwEuJAcZ7bMz1KnSbg=
github.com/onflow/flow/protobuf/go/flow v0.1.8 h1:jBR8aXEL0MOh3gVJmCr0KYXmtG3JUBhzADonKkYE6oI=
github.com/onflow/flow/protobuf/go/flow v0.1.8/go.mod h1:kRugbzZjwQqvevJhrnnCFMJZNmoSJmxlKt6hTGXZojM=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand Down Expand Up @@ -273,6 +271,8 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d h1:gZZadD8H+fF+n9CmNhYL1Y0dJB+kLOmKd7FbPJLeGHs=
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA=
github.com/turbolent/cbor/v2 v2.2.1-0.20200911003300-cac23af49154 h1:CSkqhj5tW/xAO4hdGtLsHXJczcpPsWtatCn7Y03scrU=
github.com/turbolent/cbor/v2 v2.2.1-0.20200911003300-cac23af49154/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees=
Expand Down
Loading

0 comments on commit dda6277

Please sign in to comment.