[#XX] rpcclient: Use actor/invoker providers in contract interfaces

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2024-01-24 11:11:42 +03:00
parent a7c45fdd0d
commit b1ea0864f4

View file

@ -17,6 +17,10 @@ type Invoker interface {
Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error)
}
type InvokerProvider interface {
GetInvoker() Invoker
}
// Actor is used by Contract to call state-changing methods.
type Actor interface {
Invoker
@ -29,66 +33,85 @@ type Actor interface {
SendRun(script []byte) (util.Uint256, uint32, error)
}
type ActorProvider interface {
GetActor() Actor
}
// ContractReader implements safe contract methods.
type ContractReader struct {
invoker Invoker
invokerProvider InvokerProvider
hash util.Uint160
}
// Contract implements all contract methods.
type Contract struct {
ContractReader
actor Actor
actorProvider ActorProvider
hash util.Uint160
}
type actorProviderDecorator struct {
actorProvider ActorProvider
}
func decorateActorProvider(actorProvider ActorProvider) *actorProviderDecorator{
return &actorProviderDecorator{
actorProvider: actorProvider,
}
}
func (a *actorProviderDecorator) GetInvoker() Invoker {
return a.actorProvider.GetActor()
}
// NewReader creates an instance of ContractReader using provided contract hash and the given Invoker.
func NewReader(invoker Invoker, hash util.Uint160) *ContractReader {
return &ContractReader{invoker, hash}
func NewReader(invokerProvider InvokerProvider, hash util.Uint160) *ContractReader {
return &ContractReader{invokerProvider, hash}
}
// New creates an instance of Contract using provided contract hash and the given Actor.
func New(actor Actor, hash util.Uint160) *Contract {
return &Contract{ContractReader{actor, hash}, actor, hash}
func New(actorProvider ActorProvider, hash util.Uint160) *Contract {
return &Contract{ContractReader{decorateActorProvider(actorProvider), hash}, actorProvider, hash}
}
// GetAdmin invokes `getAdmin` method of contract.
func (c *ContractReader) GetAdmin() (util.Uint160, error) {
return unwrap.Uint160(c.invoker.Call(c.hash, "getAdmin"))
return unwrap.Uint160(c.invokerProvider.GetInvoker().Call(c.hash, "getAdmin"))
}
// GetChain invokes `getChain` method of contract.
func (c *ContractReader) GetChain(entity *big.Int, entityName string, name []byte) ([]byte, error) {
return unwrap.Bytes(c.invoker.Call(c.hash, "getChain", entity, entityName, name))
return unwrap.Bytes(c.invokerProvider.GetInvoker().Call(c.hash, "getChain", entity, entityName, name))
}
// ListChains invokes `listChains` method of contract.
func (c *ContractReader) ListChains(namespace string, container string, name []byte) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.Call(c.hash, "listChains", namespace, container, name))
return unwrap.Array(c.invokerProvider.GetInvoker().Call(c.hash, "listChains", namespace, container, name))
}
// ListChainsByPrefix invokes `listChainsByPrefix` method of contract.
func (c *ContractReader) ListChainsByPrefix(entity *big.Int, entityName string, prefix []byte) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.Call(c.hash, "listChainsByPrefix", entity, entityName, prefix))
return unwrap.Array(c.invokerProvider.GetInvoker().Call(c.hash, "listChainsByPrefix", entity, entityName, prefix))
}
// Version invokes `version` method of contract.
func (c *ContractReader) Version() (*big.Int, error) {
return unwrap.BigInt(c.invoker.Call(c.hash, "version"))
return unwrap.BigInt(c.invokerProvider.GetInvoker().Call(c.hash, "version"))
}
// AddChain creates a transaction invoking `addChain` method of the contract.
// This transaction is signed and immediately sent to the network.
// The values returned are its hash, ValidUntilBlock value and error if any.
func (c *Contract) AddChain(entity *big.Int, entityName string, name []byte, chain []byte) (util.Uint256, uint32, error) {
return c.actor.SendCall(c.hash, "addChain", entity, entityName, name, chain)
return c.actorProvider.GetActor().SendCall(c.hash, "addChain", entity, entityName, name, chain)
}
// AddChainTransaction creates a transaction invoking `addChain` method of the contract.
// This transaction is signed, but not sent to the network, instead it's
// returned to the caller.
func (c *Contract) AddChainTransaction(entity *big.Int, entityName string, name []byte, chain []byte) (*transaction.Transaction, error) {
return c.actor.MakeCall(c.hash, "addChain", entity, entityName, name, chain)
return c.actorProvider.GetActor().MakeCall(c.hash, "addChain", entity, entityName, name, chain)
}
// AddChainUnsigned creates a transaction invoking `addChain` method of the contract.
@ -96,21 +119,21 @@ func (c *Contract) AddChainTransaction(entity *big.Int, entityName string, name
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
func (c *Contract) AddChainUnsigned(entity *big.Int, entityName string, name []byte, chain []byte) (*transaction.Transaction, error) {
return c.actor.MakeUnsignedCall(c.hash, "addChain", nil, entity, entityName, name, chain)
return c.actorProvider.GetActor().MakeUnsignedCall(c.hash, "addChain", nil, entity, entityName, name, chain)
}
// RemoveChain creates a transaction invoking `removeChain` method of the contract.
// This transaction is signed and immediately sent to the network.
// The values returned are its hash, ValidUntilBlock value and error if any.
func (c *Contract) RemoveChain(entity *big.Int, entityName string, name []byte) (util.Uint256, uint32, error) {
return c.actor.SendCall(c.hash, "removeChain", entity, entityName, name)
return c.actorProvider.GetActor().SendCall(c.hash, "removeChain", entity, entityName, name)
}
// RemoveChainTransaction creates a transaction invoking `removeChain` method of the contract.
// This transaction is signed, but not sent to the network, instead it's
// returned to the caller.
func (c *Contract) RemoveChainTransaction(entity *big.Int, entityName string, name []byte) (*transaction.Transaction, error) {
return c.actor.MakeCall(c.hash, "removeChain", entity, entityName, name)
return c.actorProvider.GetActor().MakeCall(c.hash, "removeChain", entity, entityName, name)
}
// RemoveChainUnsigned creates a transaction invoking `removeChain` method of the contract.
@ -118,21 +141,21 @@ func (c *Contract) RemoveChainTransaction(entity *big.Int, entityName string, na
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
func (c *Contract) RemoveChainUnsigned(entity *big.Int, entityName string, name []byte) (*transaction.Transaction, error) {
return c.actor.MakeUnsignedCall(c.hash, "removeChain", nil, entity, entityName, name)
return c.actorProvider.GetActor().MakeUnsignedCall(c.hash, "removeChain", nil, entity, entityName, name)
}
// RemoveChainsByPrefix creates a transaction invoking `removeChainsByPrefix` method of the contract.
// This transaction is signed and immediately sent to the network.
// The values returned are its hash, ValidUntilBlock value and error if any.
func (c *Contract) RemoveChainsByPrefix(entity *big.Int, entityName string, name []byte) (util.Uint256, uint32, error) {
return c.actor.SendCall(c.hash, "removeChainsByPrefix", entity, entityName, name)
return c.actorProvider.GetActor().SendCall(c.hash, "removeChainsByPrefix", entity, entityName, name)
}
// RemoveChainsByPrefixTransaction creates a transaction invoking `removeChainsByPrefix` method of the contract.
// This transaction is signed, but not sent to the network, instead it's
// returned to the caller.
func (c *Contract) RemoveChainsByPrefixTransaction(entity *big.Int, entityName string, name []byte) (*transaction.Transaction, error) {
return c.actor.MakeCall(c.hash, "removeChainsByPrefix", entity, entityName, name)
return c.actorProvider.GetActor().MakeCall(c.hash, "removeChainsByPrefix", entity, entityName, name)
}
// RemoveChainsByPrefixUnsigned creates a transaction invoking `removeChainsByPrefix` method of the contract.
@ -140,21 +163,21 @@ func (c *Contract) RemoveChainsByPrefixTransaction(entity *big.Int, entityName s
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
func (c *Contract) RemoveChainsByPrefixUnsigned(entity *big.Int, entityName string, name []byte) (*transaction.Transaction, error) {
return c.actor.MakeUnsignedCall(c.hash, "removeChainsByPrefix", nil, entity, entityName, name)
return c.actorProvider.GetActor().MakeUnsignedCall(c.hash, "removeChainsByPrefix", nil, entity, entityName, name)
}
// SetAdmin creates a transaction invoking `setAdmin` method of the contract.
// This transaction is signed and immediately sent to the network.
// The values returned are its hash, ValidUntilBlock value and error if any.
func (c *Contract) SetAdmin(addr util.Uint160) (util.Uint256, uint32, error) {
return c.actor.SendCall(c.hash, "setAdmin", addr)
return c.actorProvider.GetActor().SendCall(c.hash, "setAdmin", addr)
}
// SetAdminTransaction creates a transaction invoking `setAdmin` method of the contract.
// This transaction is signed, but not sent to the network, instead it's
// returned to the caller.
func (c *Contract) SetAdminTransaction(addr util.Uint160) (*transaction.Transaction, error) {
return c.actor.MakeCall(c.hash, "setAdmin", addr)
return c.actorProvider.GetActor().MakeCall(c.hash, "setAdmin", addr)
}
// SetAdminUnsigned creates a transaction invoking `setAdmin` method of the contract.
@ -162,5 +185,5 @@ func (c *Contract) SetAdminTransaction(addr util.Uint160) (*transaction.Transact
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
func (c *Contract) SetAdminUnsigned(addr util.Uint160) (*transaction.Transaction, error) {
return c.actor.MakeUnsignedCall(c.hash, "setAdmin", nil, addr)
return c.actorProvider.GetActor().MakeUnsignedCall(c.hash, "setAdmin", nil, addr)
}