mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-10 01:06:46 +01:00
Mohammed Sohail
4d13a14dc2
Squashed commit of the following: commit05e1396121
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Wed Feb 15 10:03:44 2023 +0300 feat: add status types to dispatcher commit397cd78ca9
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Wed Feb 15 09:39:31 2023 +0300 deps: bump -> cic-celo-sdk commitf2ba079232
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Sun Feb 12 16:53:53 2023 +0300 snapshot: 12-ebening commit4f7909e4ee
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Sun Feb 12 12:50:43 2023 +0300 xnapshot: 12-02 commit773474cad9
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 9 14:23:37 2023 +0300 update: deps initializers commit8a0880fcfc
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 9 10:42:15 2023 +0300 wip: refactor taskers commit8676450122
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Fri Feb 3 12:29:27 2023 +0300 refactor: decouple sql queries, remove transfer * add inline docs * removed transfer taks in prep for re-write commitb4c09cd11a
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 2 12:29:43 2023 +0000 refactor: cmd/service/* and api
71 lines
2.9 KiB
Go
71 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"math/big"
|
|
"time"
|
|
|
|
eth_crypto "github.com/celo-org/celo-blockchain/crypto"
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
|
"github.com/grassrootseconomics/w3-celo-patch"
|
|
)
|
|
|
|
// Define common smart contrcat ABI's that can be injected into the system container.
|
|
// Any relevant function signature that will be used by the custodial system can be defined here.
|
|
func initAbis() map[string]*w3.Func {
|
|
return map[string]*w3.Func{
|
|
// Keccak hash -> 0x449a52f8
|
|
"mintTo": w3.MustNewFunc("mintTo(address, uint256)", "bool"),
|
|
// Keccak hash -> 0xa9059cbb
|
|
"transfer": w3.MustNewFunc("transfer(address,uint256)", "bool"),
|
|
// Keccak hash -> 0x23b872dd
|
|
"transferFrom": w3.MustNewFunc("transferFrom(address, address, uint256)", "bool"),
|
|
// Add to account index
|
|
"add": w3.MustNewFunc("add(address)", "bool"),
|
|
// giveTo gas refill
|
|
"giveTo": w3.MustNewFunc("giveTo(address)", "uint256"),
|
|
}
|
|
}
|
|
|
|
// Bootstrap the internal custodial system configs and system signer key.
|
|
// This container is passed down to individual tasker and API handlers.
|
|
func initSystemContainer(ctx context.Context, noncestore nonce.Noncestore) (*tasker.SystemContainer, error) {
|
|
// Some custodial system defaults loaded from the config file.
|
|
systemContainer := &tasker.SystemContainer{
|
|
Abis: initAbis(),
|
|
AccountIndexContract: w3.A(ko.MustString("system.account_index")),
|
|
GasFaucetContract: w3.A(ko.MustString("system.gas_faucet")),
|
|
GasRefillThreshold: big.NewInt(ko.MustInt64("system.gas_refill_threshold")),
|
|
GasRefillValue: big.NewInt(ko.MustInt64("system.gas_refill_value")),
|
|
GiftableGasValue: big.NewInt(ko.MustInt64("system.giftable_gas_value")),
|
|
GiftableToken: w3.A(ko.MustString("system.giftable_token_address")),
|
|
GiftableTokenValue: big.NewInt(ko.MustInt64("system.giftable_token_value")),
|
|
LockPrefix: ko.MustString("system.lock_prefix"),
|
|
LockTimeout: 1 * time.Second,
|
|
PublicKey: ko.MustString("system.public_key"),
|
|
TokenDecimals: ko.MustInt("system.token_decimals"),
|
|
TokenTransferGasLimit: uint64(ko.MustInt64("system.token_transfer_gas_limit")),
|
|
}
|
|
// Check if system signer account nonce is present.
|
|
// If not (first boot), we bootstrap it from the network.
|
|
currentSystemNonce, err := noncestore.Peek(ctx, ko.MustString("system.public_key"))
|
|
lo.Info("custodial: loaded (noncestore) system nonce", "nonce", currentSystemNonce)
|
|
if err == redis.Nil {
|
|
nonce, err := noncestore.SyncNetworkNonce(ctx, ko.MustString("system.public_key"))
|
|
lo.Info("custodial: syncing system nonce", "nonce", nonce)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
loadedPrivateKey, err := eth_crypto.HexToECDSA(ko.MustString("system.private_key"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
systemContainer.PrivateKey = loadedPrivateKey
|
|
|
|
return systemContainer, nil
|
|
}
|