cic-go/net/erc20_demurrage_token.go

112 lines
3.5 KiB
Go
Raw Normal View History

package net
2022-05-06 13:19:44 +02:00
import (
"context"
"math/big"
2022-05-06 13:19:44 +02:00
"github.com/ethereum/go-ethereum/common"
"github.com/grassrootseconomics/cic-go/provider"
2022-05-06 13:19:44 +02:00
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
)
type DemurrageTokenInfo struct {
DemurrageAmount big.Int
DemurrageTimestamp big.Int
MinimumParticipantSpend big.Int
ResolutionFactor big.Int
PeriodStart big.Int
PeriodDuration big.Int
TaxLevel big.Int
ActualPeriod big.Int
RedistributionCount big.Int
IsDemurrageToken bool
2022-05-06 13:19:44 +02:00
}
func (c *CicNet) DemurrageTokenInfo(ctx context.Context, tokenAddress common.Address) (DemurrageTokenInfo, error) {
2022-05-06 13:19:44 +02:00
var (
demurrageAmount big.Int
demurrageTimestamp big.Int
minimumParticipantSpend big.Int
resolutionFactor big.Int
periodStart big.Int
periodDuration big.Int
taxLevel big.Int
actualPeriod big.Int
redistributionCount big.Int
2022-05-06 13:19:44 +02:00
)
err := c.provider.EthClient.CallCtx(
2022-05-06 13:19:44 +02:00
ctx,
eth.CallFunc(w3.MustNewFunc("demurrageAmount()", "uint128"), tokenAddress).Returns(&demurrageAmount),
eth.CallFunc(w3.MustNewFunc("demurrageTimestamp()", "uint256"), tokenAddress).Returns(&demurrageTimestamp),
eth.CallFunc(w3.MustNewFunc("minimumParticipantSpend()", "uint256"), tokenAddress).Returns(&minimumParticipantSpend),
eth.CallFunc(w3.MustNewFunc("resolutionFactor()", "uint256"), tokenAddress).Returns(&resolutionFactor),
eth.CallFunc(w3.MustNewFunc("periodStart()", "uint256"), tokenAddress).Returns(&periodStart),
eth.CallFunc(w3.MustNewFunc("periodDuration()", "uint256"), tokenAddress).Returns(&periodDuration),
eth.CallFunc(w3.MustNewFunc("taxLevel()", "uint256"), tokenAddress).Returns(&taxLevel),
eth.CallFunc(w3.MustNewFunc("actualPeriod()", "uint256"), tokenAddress).Returns(&actualPeriod),
eth.CallFunc(w3.MustNewFunc("redistributionCount()", "uint256"), tokenAddress).Returns(&redistributionCount),
2022-05-06 13:19:44 +02:00
)
2022-05-06 13:19:44 +02:00
if err != nil {
return DemurrageTokenInfo{}, err
2022-05-06 13:19:44 +02:00
}
return DemurrageTokenInfo{
DemurrageAmount: demurrageAmount,
DemurrageTimestamp: demurrageTimestamp,
MinimumParticipantSpend: minimumParticipantSpend,
ResolutionFactor: resolutionFactor,
PeriodStart: periodStart,
PeriodDuration: periodDuration,
TaxLevel: taxLevel,
ActualPeriod: actualPeriod,
RedistributionCount: redistributionCount,
2022-05-06 13:19:44 +02:00
}, nil
}
func (c *CicNet) BaseBalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) {
var balance big.Int
err := c.provider.EthClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("baseBalanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance),
)
if err != nil {
return big.Int{}, err
}
return balance, nil
}
func (c *CicNet) ChangePeriod(ctx context.Context, txData provider.WriteTx) (common.Hash, error) {
sig := w3.MustNewFunc("changePeriod()", "bool")
input, err := sig.EncodeArgs()
if err != nil {
return [32]byte{}, err
}
txHash, err := c.signAndCall(ctx, input, txData)
if err != nil {
return [32]byte{}, err
}
return txHash, nil
}
func (c *CicNet) ApplyDemurrageLimited(ctx context.Context, rounds int64, txData provider.WriteTx) (common.Hash, error) {
sig := w3.MustNewFunc("applyDemurrageLimited(uint256 _rounds)", "bool")
input, err := sig.EncodeArgs(big.NewInt(rounds))
if err != nil {
return [32]byte{}, err
}
txHash, err := c.signAndCall(ctx, input, txData)
if err != nil {
return [32]byte{}, err
}
return txHash, nil
}