|  | 
|  | 1 | +package balancemgr | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | + | 
|  | 6 | +	"golang.org/x/xerrors" | 
|  | 7 | + | 
|  | 8 | +	"github.com/filecoin-project/go-address" | 
|  | 9 | +	"github.com/filecoin-project/go-state-types/abi" | 
|  | 10 | +	"github.com/filecoin-project/go-state-types/big" | 
|  | 11 | + | 
|  | 12 | +	"github.com/filecoin-project/curio/harmony/harmonydb" | 
|  | 13 | +	"github.com/filecoin-project/curio/harmony/harmonytask" | 
|  | 14 | + | 
|  | 15 | +	"github.com/filecoin-project/lotus/api" | 
|  | 16 | +	"github.com/filecoin-project/lotus/chain/actors" | 
|  | 17 | +	"github.com/filecoin-project/lotus/chain/actors/builtin/market" | 
|  | 18 | +	"github.com/filecoin-project/lotus/chain/types" | 
|  | 19 | +) | 
|  | 20 | + | 
|  | 21 | +func (b *BalanceMgrTask) adderF05(ctx context.Context, taskFunc harmonytask.AddTaskFunc, addr *balanceManagerAddress) error { | 
|  | 22 | +	marketBalance, err := b.chain.StateMarketBalance(ctx, addr.SubjectAddress, types.EmptyTSK) | 
|  | 23 | +	if err != nil { | 
|  | 24 | +		return xerrors.Errorf("getting market balance: %w", err) | 
|  | 25 | +	} | 
|  | 26 | + | 
|  | 27 | +	sourceBalance, err := b.chain.StateGetActor(ctx, addr.SubjectAddress, types.EmptyTSK) | 
|  | 28 | +	if err != nil { | 
|  | 29 | +		return xerrors.Errorf("getting source balance: %w", err) | 
|  | 30 | +	} | 
|  | 31 | + | 
|  | 32 | +	addr.SubjectBalance = big.Sub(marketBalance.Escrow, marketBalance.Locked) | 
|  | 33 | +	addr.SecondBalance = sourceBalance.Balance | 
|  | 34 | + | 
|  | 35 | +	var shouldCreateTask bool | 
|  | 36 | +	switch addr.ActionType { | 
|  | 37 | +	case "requester": | 
|  | 38 | +		shouldCreateTask = addr.SubjectBalance.LessThan(addr.LowWatermarkFilBalance) | 
|  | 39 | +	} | 
|  | 40 | + | 
|  | 41 | +	if shouldCreateTask { | 
|  | 42 | +		taskFunc(func(taskID harmonytask.TaskID, tx *harmonydb.Tx) (shouldCommit bool, seriousError error) { | 
|  | 43 | +			// check that address.ID has active_task_id = null, set the task ID, set last_ to null | 
|  | 44 | +			n, err := tx.Exec(` | 
|  | 45 | +				UPDATE balance_manager_addresses | 
|  | 46 | +				SET active_task_id = $1, last_msg_cid = NULL, last_msg_sent_at = NULL, last_msg_landed_at = NULL | 
|  | 47 | +				WHERE id = $2 AND active_task_id IS NULL AND (last_msg_cid IS NULL OR last_msg_landed_at IS NOT NULL) | 
|  | 48 | +			`, taskID, addr.ID) | 
|  | 49 | +			if err != nil { | 
|  | 50 | +				return false, xerrors.Errorf("updating balance manager address: %w", err) | 
|  | 51 | +			} | 
|  | 52 | + | 
|  | 53 | +			return n > 0, nil | 
|  | 54 | +		}) | 
|  | 55 | +	} | 
|  | 56 | +	return nil | 
|  | 57 | +} | 
|  | 58 | + | 
|  | 59 | +func (b *BalanceMgrTask) doF05(ctx context.Context, taskID harmonytask.TaskID, addr *balanceManagerAddress) (bool, error) { | 
|  | 60 | +	log.Infow("balancemgr f05 Do", | 
|  | 61 | +		"id", addr.ID, | 
|  | 62 | +		"subject", addr.SubjectAddress, | 
|  | 63 | +		"low", types.FIL(addr.LowWatermarkFilBalance), | 
|  | 64 | +		"high", types.FIL(addr.HighWatermarkFilBalance)) | 
|  | 65 | + | 
|  | 66 | +	marketBalance, err := b.chain.StateMarketBalance(ctx, addr.SubjectAddress, types.EmptyTSK) | 
|  | 67 | +	if err != nil { | 
|  | 68 | +		return false, xerrors.Errorf("getting market balance: %w", err) | 
|  | 69 | +	} | 
|  | 70 | + | 
|  | 71 | +	sourceBalance, err := b.chain.StateGetActor(ctx, addr.SubjectAddress, types.EmptyTSK) | 
|  | 72 | +	if err != nil { | 
|  | 73 | +		return false, xerrors.Errorf("getting source balance: %w", err) | 
|  | 74 | +	} | 
|  | 75 | + | 
|  | 76 | +	addr.SubjectBalance = big.Sub(marketBalance.Escrow, marketBalance.Locked) | 
|  | 77 | +	addr.SecondBalance = types.BigInt(sourceBalance.Balance) | 
|  | 78 | + | 
|  | 79 | +	// calculate amount to send (based on latest chain balance) | 
|  | 80 | +	var amount types.BigInt | 
|  | 81 | +	var to address.Address | 
|  | 82 | +	var shouldSend bool | 
|  | 83 | + | 
|  | 84 | +	if addr.ActionType != "requester" { | 
|  | 85 | +		return false, xerrors.Errorf("action type is not requester: %s", addr.ActionType) | 
|  | 86 | +	} | 
|  | 87 | + | 
|  | 88 | +	if addr.SubjectBalance.LessThan(addr.LowWatermarkFilBalance) { | 
|  | 89 | +		amount = types.BigSub(addr.HighWatermarkFilBalance, addr.SubjectBalance) | 
|  | 90 | +		to = addr.SubjectAddress | 
|  | 91 | +		shouldSend = true | 
|  | 92 | +	} | 
|  | 93 | + | 
|  | 94 | +	if !shouldSend { | 
|  | 95 | +		log.Infow("balance within watermarks, no action needed", | 
|  | 96 | +			"subject", addr.SubjectAddress, | 
|  | 97 | +			"balance", types.FIL(addr.SubjectBalance), | 
|  | 98 | +			"low", types.FIL(addr.LowWatermarkFilBalance), | 
|  | 99 | +			"high", types.FIL(addr.HighWatermarkFilBalance)) | 
|  | 100 | + | 
|  | 101 | +		_, err = b.db.Exec(ctx, ` | 
|  | 102 | +			UPDATE balance_manager_addresses  | 
|  | 103 | +			SET active_task_id = NULL, last_action = NOW() | 
|  | 104 | +			WHERE id = $1 | 
|  | 105 | +		`, addr.ID) | 
|  | 106 | +		if err != nil { | 
|  | 107 | +			return false, xerrors.Errorf("clearing task id: %w", err) | 
|  | 108 | +		} | 
|  | 109 | +		return true, nil | 
|  | 110 | +	} | 
|  | 111 | + | 
|  | 112 | +	params, err := actors.SerializeParams(&addr.SubjectAddress) | 
|  | 113 | +	if err != nil { | 
|  | 114 | +		return false, xerrors.Errorf("failed to serialize miner address: %w", err) | 
|  | 115 | +	} | 
|  | 116 | + | 
|  | 117 | +	maxfee, err := types.ParseFIL("0.05 FIL") | 
|  | 118 | +	if err != nil { | 
|  | 119 | +		return false, xerrors.Errorf("failed to parse max fee: %w", err) | 
|  | 120 | +	} | 
|  | 121 | + | 
|  | 122 | +	msp := &api.MessageSendSpec{ | 
|  | 123 | +		MaxFee: abi.TokenAmount(maxfee), | 
|  | 124 | +	} | 
|  | 125 | + | 
|  | 126 | +	msg := &types.Message{ | 
|  | 127 | +		To:     market.Address, | 
|  | 128 | +		From:   addr.SecondAddress, | 
|  | 129 | +		Value:  amount, | 
|  | 130 | +		Method: market.Methods.AddBalance, | 
|  | 131 | +		Params: params, | 
|  | 132 | +	} | 
|  | 133 | + | 
|  | 134 | +	msgCid, err := b.sender.Send(ctx, msg, msp, "balancemgr-f05") | 
|  | 135 | +	if err != nil { | 
|  | 136 | +		return false, xerrors.Errorf("failed to send message: %w", err) | 
|  | 137 | +	} | 
|  | 138 | + | 
|  | 139 | +	_, err = b.db.Exec(ctx, `INSERT INTO message_waits (signed_message_cid) VALUES ($1)`, msgCid) | 
|  | 140 | +	if err != nil { | 
|  | 141 | +		return false, xerrors.Errorf("inserting into message_waits: %w", err) | 
|  | 142 | +	} | 
|  | 143 | + | 
|  | 144 | +	_, err = b.db.Exec(ctx, ` | 
|  | 145 | +		UPDATE balance_manager_addresses  | 
|  | 146 | +		SET last_msg_cid = $2,  | 
|  | 147 | +		    last_msg_sent_at = NOW(),  | 
|  | 148 | +		    last_msg_landed_at = NULL, | 
|  | 149 | +			active_task_id = NULL | 
|  | 150 | +		WHERE id = $1 | 
|  | 151 | +	`, addr.ID, msgCid.String()) | 
|  | 152 | +	if err != nil { | 
|  | 153 | +		return false, xerrors.Errorf("updating message cid: %w", err) | 
|  | 154 | +	} | 
|  | 155 | + | 
|  | 156 | +	log.Infow("sent balance management message", | 
|  | 157 | +		"from", addr.SecondAddress, | 
|  | 158 | +		"to", to, | 
|  | 159 | +		"subjectType", "f05", | 
|  | 160 | +		"amount", types.FIL(amount), | 
|  | 161 | +		"msgCid", msgCid, | 
|  | 162 | +		"actionType", addr.ActionType) | 
|  | 163 | + | 
|  | 164 | +	return true, nil | 
|  | 165 | +} | 
0 commit comments