[#16] ir: Use mint and burn methods in balance contract

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-01 14:16:16 +03:00
parent 27fcf2cc1d
commit 58cb90966a
2 changed files with 45 additions and 17 deletions

View file

@ -22,11 +22,20 @@ type (
Amount int64 // in Fixed16 Amount int64 // in Fixed16
Until uint64 // epochs Until uint64 // epochs
} }
// MintBurnParams for Mint and Burn invocations.
MintBurnParams struct {
ScriptHash []byte
Amount int64 // in Fixed16
Comment []byte
}
) )
const ( const (
transferXMethod = "transferX" transferXMethod = "transferX"
lockMethod = "lock" lockMethod = "lock"
mintMethod = "mint"
burnMethod = "burn"
) )
// TransferBalanceX invokes transferX method. // TransferBalanceX invokes transferX method.
@ -43,6 +52,32 @@ func TransferBalanceX(cli *client.Client, con util.Uint160, p *TransferXParams)
) )
} }
// Mint assets in contract.
func Mint(cli *client.Client, con util.Uint160, p *MintBurnParams) error {
if cli == nil {
return client.ErrNilClient
}
return cli.Invoke(con, extraFee, mintMethod,
p.ScriptHash,
p.Amount,
p.Comment,
)
}
// Burn minted assets.
func Burn(cli *client.Client, con util.Uint160, p *MintBurnParams) error {
if cli == nil {
return client.ErrNilClient
}
return cli.Invoke(con, extraFee, burnMethod,
p.ScriptHash,
p.Amount,
p.Comment,
)
}
// LockAsset invokes Lock method. // LockAsset invokes Lock method.
func LockAsset(cli *client.Client, con util.Uint160, p *LockParams) error { func LockAsset(cli *client.Client, con util.Uint160, p *LockParams) error {
if cli == nil { if cli == nil {

View file

@ -15,11 +15,6 @@ const (
lockAccountLifetime uint64 = 20 lockAccountLifetime uint64 = 20
) )
var (
// fedReserveAddr is a special account in balance contract.
fedReserveAddr = []byte{0x0F, 0xED}
)
// Process deposit event by invoking balance contract and sending native // Process deposit event by invoking balance contract and sending native
// gas in morph chain. // gas in morph chain.
func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) { func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) {
@ -29,12 +24,11 @@ func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) {
} }
// send transferX to balance contract // send transferX to balance contract
err := invoke.TransferBalanceX(np.morphClient, np.balanceContract, err := invoke.Mint(np.morphClient, np.balanceContract,
&invoke.TransferXParams{ &invoke.MintBurnParams{
Sender: fedReserveAddr, ScriptHash: deposit.To().BytesBE(),
Receiver: deposit.To().BytesBE(), Amount: deposit.Amount() * 1_0000_0000, // from Fixed8 to Fixed16
Amount: deposit.Amount() * 1_0000_0000, // from Fixed8 to Fixed16 Comment: append([]byte(txLogPrefix), deposit.ID()...),
Comment: append([]byte(txLogPrefix), deposit.ID()...),
}) })
if err != nil { if err != nil {
np.log.Error("can't transfer assets to balance contract", zap.Error(err)) np.log.Error("can't transfer assets to balance contract", zap.Error(err))
@ -91,12 +85,11 @@ func (np *Processor) processCheque(cheque *neofsEvent.Cheque) {
return return
} }
err := invoke.TransferBalanceX(np.morphClient, np.balanceContract, err := invoke.Burn(np.morphClient, np.balanceContract,
&invoke.TransferXParams{ &invoke.MintBurnParams{
Sender: cheque.LockAccount().BytesBE(), ScriptHash: cheque.LockAccount().BytesBE(),
Receiver: fedReserveAddr, Amount: cheque.Amount() * 1_0000_0000, // from Fixed8 to Fixed16
Amount: cheque.Amount() * 1_0000_0000, // from Fixed8 to Fixed16 Comment: append([]byte(txLogPrefix), cheque.ID()...),
Comment: append([]byte(txLogPrefix), cheque.ID()...),
}) })
if err != nil { if err != nil {
np.log.Error("can't transfer assets to fed contract", zap.Error(err)) np.log.Error("can't transfer assets to fed contract", zap.Error(err))