-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
277 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package bot | ||
|
||
import ( | ||
"context" | ||
"crypto/ecdsa" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"math/big" | ||
"path" | ||
"strconv" | ||
|
||
"github.com/chain4travel/camino-messenger-bot/config" | ||
"github.com/chain4travel/camino-messenger-bot/tests/e2e/conduit" | ||
"github.com/chain4travel/camino-messenger-bot/tests/e2e/node" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
var DefaultCMAccountOwnerFunds = big.NewInt(0).Mul(node.CAM, big.NewInt(100)) | ||
|
||
type Factory struct { | ||
dir string | ||
binPath string | ||
migrationsPath string | ||
networkClient *node.Client | ||
matrix *conduit.MatrixServer | ||
} | ||
|
||
// NewFactory creates a new bot factory. | ||
func NewFactory( | ||
e2eTmpDir string, | ||
binPath string, | ||
migrationsDir string, | ||
networkClient *node.Client, | ||
matrix *conduit.MatrixServer, | ||
) *Factory { | ||
return &Factory{ | ||
dir: path.Join(e2eTmpDir, "cmb"), | ||
binPath: binPath, | ||
migrationsPath: "file://" + migrationsDir, | ||
networkClient: networkClient, | ||
matrix: matrix, | ||
} | ||
} | ||
|
||
// CreateBot creates a new bot. | ||
func (f *Factory) CreateBot(ctx context.Context, port uint64, out io.Writer) (*Bot, chan error, error) { | ||
key, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to generate key: %w", err) | ||
} | ||
|
||
ownerAddr := crypto.PubkeyToAddress(key.PublicKey) | ||
|
||
cmAccountAddress, _, err := f.networkClient.CreateCMAccount(ctx, key) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to create CM account: %w", err) | ||
} | ||
|
||
if err := f.networkClient.Transfer(ctx, f.networkClient.PrefundedKeys()[0], ownerAddr, DefaultCMAccountOwnerFunds); err != nil { | ||
return nil, nil, fmt.Errorf("failed to transfer funds to cm account owner: %w", err) | ||
} | ||
|
||
if err := f.networkClient.AddBotToCMAccount(ctx, cmAccountAddress, key, ownerAddr); err != nil { | ||
return nil, nil, fmt.Errorf("failed to add bot to CM account: %w", err) | ||
} | ||
|
||
botDir := path.Join(f.dir, strconv.FormatUint(port, 10)) // TODO@ add some more suffix to make bots form different tests unique 100% | ||
|
||
return StartNewBot( | ||
ctx, | ||
botDir, | ||
f.binPath, | ||
config.UnparsedConfig{ | ||
DeveloperMode: true, | ||
BotKey: hex.EncodeToString(crypto.FromECDSA(key)), | ||
CMAccountAddress: cmAccountAddress.Hex(), | ||
ChainRPCURL: f.networkClient.ChainRPCURL(), | ||
BookingTokenAddress: f.networkClient.BookingTokenContractAddress().Hex(), | ||
NetworkFeeRecipientBotAddress: f.matrix.NetworkFeeRecipientBotAddress().Hex(), | ||
NetworkFeeRecipientCMAccountAddress: f.matrix.NetworkFeeRecipientCMAccountAddress().Hex(), | ||
ChequeExpirationTime: 3600 * 24 * 30 * 7, // 7 months | ||
MinChequeDurationUntilExpiration: 3600 * 24 * 30 * 6, // 6 months | ||
CashInPeriod: 3600, // 1h | ||
ResponseTimeout: 30000, // 30s | ||
PartnerPlugin: config.PartnerPluginConfig{ | ||
Enabled: false, | ||
Host: "", | ||
Unencrypted: true, | ||
}, | ||
RPCServer: config.RPCServerConfig{ | ||
Enabled: true, | ||
Port: port, | ||
Unencrypted: true, | ||
}, | ||
Matrix: config.UnparsedMatrixConfig{Host: f.matrix.Host().String()}, | ||
DB: config.UnparsedSQLiteDBConfig{ | ||
DBPath: path.Join(botDir, "db"), | ||
MigrationsPath: f.migrationsPath, | ||
}, | ||
Tracing: config.TracingConfig{Enabled: false}, | ||
}, | ||
out, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.