From 4b180ea285e40fe77802e93d06c182d72e7a79d3 Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Thu, 1 Dec 2022 13:07:22 +0000 Subject: [PATCH] refactor: move abis to system_init * closes #31 --- cmd/init_system.go | 8 ++++++++ internal/tasker/task/system.go | 7 +------ internal/tasker/task/transfer.go | 7 +------ internal/tasker/types.go | 2 ++ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/init_system.go b/cmd/init_system.go index 3de05c6..a329170 100644 --- a/cmd/init_system.go +++ b/cmd/init_system.go @@ -13,6 +13,7 @@ import ( func initSystemContainer() *tasker.SystemContainer { return &tasker.SystemContainer{ + Abis: initAbis(), 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")), @@ -27,6 +28,13 @@ func initSystemContainer() *tasker.SystemContainer { } } +func initAbis() map[string]*w3.Func { + return map[string]*w3.Func{ + "mint": w3.MustNewFunc("mint(address,uint256)", ""), + "transfer": w3.MustNewFunc("transfer(address,uint256)", "bool"), + } +} + func initSystemKey() *ecdsa.PrivateKey { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() diff --git a/internal/tasker/task/system.go b/internal/tasker/task/system.go index c388a49..682fbec 100644 --- a/internal/tasker/task/system.go +++ b/internal/tasker/task/system.go @@ -147,12 +147,7 @@ func GiftTokenProcessor( return err } - abi, err := w3.NewFunc("mint(address,uint256)", "") - if err != nil { - return err - } - - input, err := abi.EncodeArgs(publicKey, system.GiftableTokenValue) + input, err := system.Abis["mint"].EncodeArgs(publicKey, system.GiftableTokenValue) if err != nil { return fmt.Errorf("ABI encode failed %v: %w", err, asynq.SkipRetry) } diff --git a/internal/tasker/task/transfer.go b/internal/tasker/task/transfer.go index 799dc7c..333a558 100644 --- a/internal/tasker/task/transfer.go +++ b/internal/tasker/task/transfer.go @@ -55,12 +55,7 @@ func TransferToken( return err } - abi, err := w3.NewFunc("transfer(address,uint256)", "bool") - if err != nil { - return err - } - - input, err := abi.EncodeArgs(w3.A(p.To), parseTransferValue(p.Amount, system.TokenDecimals)) + input, err := system.Abis["transfer"].EncodeArgs(w3.A(p.To), parseTransferValue(p.Amount, system.TokenDecimals)) if err != nil { return fmt.Errorf("ABI encode failed %v: %w", err, asynq.SkipRetry) } diff --git a/internal/tasker/types.go b/internal/tasker/types.go index 28175c5..9f39ae7 100644 --- a/internal/tasker/types.go +++ b/internal/tasker/types.go @@ -7,6 +7,7 @@ import ( "time" "github.com/celo-org/celo-blockchain/common" + "github.com/grassrootseconomics/w3-celo-patch" ) type ( @@ -15,6 +16,7 @@ type ( ) type SystemContainer struct { + Abis map[string]*w3.Func GasRefillThreshold *big.Int GasRefillValue *big.Int GiftableGasValue *big.Int