-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(client:go): add batch create contract #689
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -233,6 +233,113 @@ func (s *Substrate) CreateNodeContract(identity Identity, node uint32, body stri | |
return s.GetContractWithHash(node, h) | ||
} | ||
|
||
// BatchCreateContractData struct for batch create contract | ||
type BatchCreateContractData struct { | ||
Node uint32 | ||
Body string | ||
Hash string | ||
PublicIPs uint32 | ||
SolutionProviderID *uint64 | ||
|
||
// for name contracts. if set the contract is assumed to be a name contract | ||
// and other fields are ignored | ||
Name string | ||
|
||
hexHash HexHash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field will be private, it doesn't start with upper case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that was intentional as it is generated internally and not provided by user, should it be public? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't return this object then no there is no use to it. |
||
} | ||
|
||
// BatchAllCreateContract creates a batch of contracts for deployments atomically. | ||
// transaction will rollback on error | ||
func (s *Substrate) BatchAllCreateContract(identity Identity, contractData []BatchCreateContractData) ([]uint64, error) { | ||
contracts, _, err := s.batchCreateContract(identity, contractData, "Utility.batch_all") | ||
return contracts, err | ||
} | ||
|
||
// BatchCreateContract creates a batch of contracts for deployments non-atomically. | ||
// on error returns the created contracts, the first failing contract index and the error message. | ||
func (s *Substrate) BatchCreateContract(identity Identity, contractData []BatchCreateContractData) ([]uint64, *int, error) { | ||
return s.batchCreateContract(identity, contractData, "Utility.batch") | ||
} | ||
|
||
func (s *Substrate) batchCreateContract(identity Identity, contractData []BatchCreateContractData, batchType string) ([]uint64, *int, error) { | ||
cl, meta, err := s.GetClient() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
calls := make([]types.Call, 0) | ||
for i, contract := range contractData { | ||
if contract.Name != "" { | ||
c, err := types.NewCall(meta, "SmartContractModule.create_name_contract", | ||
contract.Name, | ||
) | ||
if err != nil { | ||
return nil, nil, errors.Wrap(err, "failed to create call") | ||
} | ||
calls = append(calls, c) | ||
continue | ||
} | ||
|
||
var providerID types.OptionU64 | ||
if contract.SolutionProviderID != nil { | ||
providerID = types.NewOptionU64(types.U64(*contract.SolutionProviderID)) | ||
} | ||
|
||
h := NewHexHash(contract.Hash) | ||
c, err := types.NewCall(meta, "SmartContractModule.create_node_contract", | ||
contract.Node, h, contract.Body, contract.PublicIPs, providerID, | ||
) | ||
if err != nil { | ||
return nil, nil, errors.Wrap(err, "failed to create call") | ||
} | ||
|
||
calls = append(calls, c) | ||
contractData[i].hexHash = h | ||
} | ||
batchCall, err := types.NewCall(meta, batchType, calls) | ||
if err != nil { | ||
return nil, nil, errors.Wrap(err, "failed to create batch call") | ||
} | ||
|
||
resp, err := s.Call(cl, meta, identity, batchCall) | ||
if err != nil { | ||
return nil, nil, errors.Wrap(err, "failed to create contracts") | ||
} | ||
|
||
var failingContract *int | ||
if batchType == "Utility.batch" && resp.Events != nil && resp.Events.Utility_BatchInterrupted != nil { | ||
i := int(resp.Events.Utility_BatchInterrupted[0].Index) | ||
failingContract = &i | ||
} | ||
|
||
contracts := make([]uint64, 0) | ||
for i, contract := range contractData { | ||
if failingContract != nil && *failingContract == i { | ||
break | ||
} | ||
|
||
var contractID uint64 | ||
var err error | ||
|
||
if contract.Name != "" { | ||
contractID, err = s.GetContractIDByNameRegistration(contract.Name) | ||
} else { | ||
contractID, err = s.GetContractWithHash(contract.Node, contract.hexHash) | ||
} | ||
|
||
if err != nil { | ||
failingContract = &i | ||
return nil, failingContract, err | ||
} | ||
contracts = append(contracts, contractID) | ||
} | ||
|
||
if failingContract != nil { | ||
err = errors.New("failed to create contracts") | ||
} | ||
return contracts, failingContract, err | ||
} | ||
|
||
// CreateNameContract creates a contract for deployment | ||
func (s *Substrate) CreateNameContract(identity Identity, name string) (uint64, error) { | ||
cl, meta, err := s.GetClient() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add json tags please? We need them for the web3 proxy.