github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/pricing/maker.go (about)

     1  package pricing
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/articulate"
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/call"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    13  )
    14  
    15  var (
    16  	makerMedianizer = base.HexToAddress("0x729d19f657bd0614b4985cf1d82531c67569197b")
    17  	makerDeployment = base.Blknum(3684349)
    18  )
    19  
    20  func priceUsdMaker(conn *rpc.Connection, statement *types.Statement) (price base.Float, source string, err error) {
    21  	if statement.BlockNumber <= makerDeployment {
    22  		msg := fmt.Sprintf("Block %d is prior to deployment (%d) of Maker. No fallback pricing method", statement.BlockNumber, makerDeployment)
    23  		logger.TestLog(true, msg)
    24  		return 0.0, "eth-not-priced-pre-maker", nil
    25  	}
    26  
    27  	msg := fmt.Sprintf("Block %d is prior to deployment (%d) of Uniswap V2. Falling back to Maker (%s)", statement.BlockNumber, uniswapFactoryV2_deployed, makerMedianizer)
    28  	logger.TestLog(true, msg)
    29  	theCall := "peek()"
    30  
    31  	contractCall, _, err := call.NewContractCall(conn, makerMedianizer, theCall)
    32  	if err != nil {
    33  		wrapped := fmt.Errorf("the --call value provided (%s) was not found: %s", theCall, err)
    34  		return 0.0, "not-priced", wrapped
    35  	}
    36  
    37  	contractCall.BlockNumber = statement.BlockNumber
    38  	artFunc := func(str string, function *types.Function) error {
    39  		return articulate.ArticulateFunction(function, "", str[2:])
    40  	}
    41  	result, err := contractCall.Call(artFunc)
    42  	if err != nil {
    43  		return 0.0, "not-priced", err
    44  	}
    45  
    46  	divisor := new(base.Wei)
    47  	divisor.SetString("1000000000000000000", 10)
    48  
    49  	// TODO: Since Dawid fixed the articulate code, we should use the value at results["val_1"] instead of this
    50  	//       hacky string manipulation
    51  	trimmed := strings.TrimPrefix(string(result.ReturnedBytes), "0x")
    52  	trimmed = trimmed[:64]
    53  	int0 := new(base.Wei)
    54  	int0.SetString(trimmed, 16)
    55  	int0 = int0.Mul(int0, new(base.Wei).SetInt64(100000))
    56  	int1 := new(base.Wei).Quo(int0, divisor)
    57  
    58  	bigPrice := new(base.Ether).SetWei(int1)
    59  	bigPrice = bigPrice.Quo(bigPrice, new(base.Ether).SetInt64(100000))
    60  	price = base.Float(bigPrice.Float64())
    61  	source = "maker"
    62  	r := priceDebugger{
    63  		address:     statement.AssetAddr,
    64  		symbol:      statement.AssetSymbol,
    65  		blockNumber: statement.BlockNumber,
    66  		source1:     makerMedianizer,
    67  		theCall1:    theCall,
    68  		source2:     base.ZeroAddr,
    69  		theCall2:    "0x" + trimmed,
    70  		first:       base.ZeroAddr,
    71  		second:      base.ZeroAddr,
    72  		reversed:    false,
    73  		int0:        int0,
    74  		int1:        int1,
    75  		bigPrice:    bigPrice,
    76  		price:       price,
    77  		source:      source,
    78  	}
    79  	r.report("using Maker")
    80  
    81  	return
    82  }