mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-18 04:40:03 +02:00
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package abi
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
eos "github.com/eoscanada/eos-go"
|
|
"github.com/eosswedenorg/thalos/app/cache"
|
|
)
|
|
|
|
// AbiManager handles an ABI cache that fetches the ABI from an API on cache miss.
|
|
type AbiManager struct {
|
|
cache *cache.Cache
|
|
api *eos.API
|
|
ctx context.Context
|
|
}
|
|
|
|
// Create a new ABI Manager
|
|
func NewAbiManager(cache *cache.Cache, api *eos.API) *AbiManager {
|
|
return &AbiManager{
|
|
cache: cache,
|
|
api: api,
|
|
ctx: context.Background(),
|
|
}
|
|
}
|
|
|
|
// Set or update an ABI in the cache.
|
|
func (mgr *AbiManager) SetAbi(account eos.AccountName, abi *eos.ABI) error {
|
|
return mgr.cache.Set(string(account), *abi, time.Hour)
|
|
}
|
|
|
|
// Get an ABI from the cache, on cache miss it is fetched from the
|
|
// API, gets cached and then returned to the user
|
|
func (mgr *AbiManager) GetAbi(account eos.AccountName) (*eos.ABI, error) {
|
|
var abi eos.ABI
|
|
if err := mgr.cache.Get(string(account), &abi); err != nil {
|
|
resp, err := mgr.api.GetABI(mgr.ctx, account)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("api: %s", err)
|
|
}
|
|
abi = resp.ABI
|
|
|
|
err = mgr.SetAbi(account, &abi)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cache: %s", err)
|
|
}
|
|
}
|
|
return &abi, nil
|
|
}
|