Skip to content

Commit

Permalink
Merge pull request #109 from tonkeeper/staking-actions
Browse files Browse the repository at this point in the history
Add DepositStake and RecoverStake actions
  • Loading branch information
mr-tron authored Jun 15, 2023
2 parents 7f1c9f4 + bfef94c commit 8d0c97c
Show file tree
Hide file tree
Showing 14 changed files with 774 additions and 15 deletions.
42 changes: 42 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@
"ContractDeploy": {
"$ref": "#/components/schemas/ContractDeployAction"
},
"DepositStake": {
"$ref": "#/components/schemas/DepositStakeAction"
},
"JettonTransfer": {
"$ref": "#/components/schemas/JettonTransferAction"
},
Expand All @@ -703,6 +706,9 @@
"NftPurchase": {
"$ref": "#/components/schemas/NftPurchaseAction"
},
"RecoverStake": {
"$ref": "#/components/schemas/RecoverStakeAction"
},
"SmartContractExec": {
"$ref": "#/components/schemas/SmartContractAction"
},
Expand Down Expand Up @@ -737,6 +743,8 @@
"UnSubscribe",
"AuctionBid",
"NftPurchase",
"DepositStake",
"RecoverStake",
"SmartContractExec",
"Unknown"
],
Expand Down Expand Up @@ -1189,6 +1197,23 @@
],
"type": "object"
},
"DepositStakeAction": {
"properties": {
"amount": {
"example": 1660050553,
"format": "int64",
"type": "integer"
},
"staker": {
"$ref": "#/components/schemas/AccountAddress"
}
},
"required": [
"amount",
"staker"
],
"type": "object"
},
"DnsExpiring": {
"properties": {
"items": {
Expand Down Expand Up @@ -2202,6 +2227,23 @@
],
"type": "object"
},
"RecoverStakeAction": {
"properties": {
"amount": {
"example": 1660050553,
"format": "int64",
"type": "integer"
},
"staker": {
"$ref": "#/components/schemas/AccountAddress"
}
},
"required": [
"amount",
"staker"
],
"type": "object"
},
"Refund": {
"properties": {
"origin": {
Expand Down
32 changes: 32 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,8 @@ components:
- UnSubscribe
- AuctionBid
- NftPurchase
- DepositStake
- RecoverStake
- SmartContractExec
- Unknown
status:
Expand All @@ -2626,6 +2628,10 @@ components:
$ref: '#/components/schemas/AuctionBidAction'
NftPurchase:
$ref: '#/components/schemas/NftPurchaseAction'
DepositStake:
$ref: '#/components/schemas/DepositStakeAction'
RecoverStake:
$ref: '#/components/schemas/RecoverStakeAction'
SmartContractExec:
$ref: '#/components/schemas/SmartContractAction'
simple_preview:
Expand Down Expand Up @@ -2808,6 +2814,32 @@ components:
bidder:
$ref: '#/components/schemas/AccountAddress'

DepositStakeAction:
type: object
required:
- amount
- staker
properties:
amount:
type: integer
format: int64
example: 1660050553
staker:
$ref: '#/components/schemas/AccountAddress'

RecoverStakeAction:
type: object
required:
- amount
- staker
properties:
amount:
type: integer
format: int64
example: 1660050553
staker:
$ref: '#/components/schemas/AccountAddress'

NftPurchaseAction:
type: object
required:
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/sourcegraph/conc v0.3.0
github.com/stretchr/testify v1.8.1
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269
github.com/tonkeeper/tongo v1.1.2-0.20230614170918-21867e935297
github.com/tonkeeper/tongo v1.1.2-0.20230614212647-93fc48518802
go.opentelemetry.io/otel v1.11.1
go.opentelemetry.io/otel/metric v0.33.0
go.opentelemetry.io/otel/trace v1.11.1
Expand Down Expand Up @@ -61,7 +61,6 @@ require (
github.com/valyala/fasttemplate v1.2.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
golang.org/x/exp v0.0.0-20230116083435-1de6713980de // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269 h1:roJPVpmFnUZX6YCUqzoD5+0+Z2ruuuXmxWKS+IH3O2s=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269/go.mod h1:VGp8QednbWkKHcpQVlWyO0XSqAA0cR6d9wEdrDmHbbA=
github.com/tonkeeper/tongo v1.1.2-0.20230613142834-d288ff73f9f6 h1:+j84fDe+Gol48eBFcYI5IqKiKjLVfZSalpzWRGf3rus=
github.com/tonkeeper/tongo v1.1.2-0.20230613142834-d288ff73f9f6/go.mod h1:LdOBjpUz6vLp1EdX3E0XLNks9YI5XMSqaQahfOMrBEY=
github.com/tonkeeper/tongo v1.1.2-0.20230614170918-21867e935297 h1:dH9HmZUn/l+93SeDrLHxzEqod+OdX8313gp/NR+WECE=
github.com/tonkeeper/tongo v1.1.2-0.20230614170918-21867e935297/go.mod h1:LdOBjpUz6vLp1EdX3E0XLNks9YI5XMSqaQahfOMrBEY=
github.com/tonkeeper/tongo v1.1.2-0.20230614212647-93fc48518802 h1:XG9ic1xSrs6a5qmrEZWQXRZ97X7SofJ16EhX1fbJ0os=
github.com/tonkeeper/tongo v1.1.2-0.20230614212647-93fc48518802/go.mod h1:LdOBjpUz6vLp1EdX3E0XLNks9YI5XMSqaQahfOMrBEY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
Expand Down Expand Up @@ -318,12 +316,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20221006183845-316c7553db56 h1:BrYbdKcCNjLyrN6aKqXy4hPw9qGI8IATkj4EWv9Q+kQ=
golang.org/x/exp v0.0.0-20221006183845-316c7553db56/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp v0.0.0-20230116083435-1de6713980de h1:DBWn//IJw30uYCgERoxCg84hWtA97F4wMiKOIh00Uf0=
golang.org/x/exp v0.0.0-20230116083435-1de6713980de/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
36 changes: 36 additions & 0 deletions pkg/api/event_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
jettonTransferMessageID = "jettonTransferAction"
smartContractMessageID = "smartContractExecAction"
subscriptionMessageID = "subscriptionAction"
depositStakeMessageID = "depositStakeAction"
recoverStakeMessageID = "recoverStakeAction"
)

func distinctAccounts(book addressBook, accounts ...*tongo.AccountID) []oas.AccountAddress {
Expand Down Expand Up @@ -266,6 +268,40 @@ func (h Handler) convertAction(ctx context.Context, a bath.Action, acceptLanguag
Seller: convertAccountAddress(a.NftPurchase.Seller, h.addressBook),
Buyer: convertAccountAddress(a.NftPurchase.Buyer, h.addressBook),
})
case bath.DepositStake:
value := utils.HumanFriendlyCoinsRepr(a.DepositStake.Amount)
action.DepositStake.SetTo(oas.DepositStakeAction{
Amount: a.DepositStake.Amount,
Staker: convertAccountAddress(a.DepositStake.Staker, h.addressBook),
})
action.SimplePreview = oas.ActionSimplePreview{
Name: "Deposit Stake",
Description: i18n.T(acceptLanguage.Value, i18n.C{
MessageID: depositStakeMessageID,
TemplateData: map[string]interface{}{
"Amount": value,
},
}),
Value: oas.NewOptString(value),
Accounts: distinctAccounts(h.addressBook, &a.DepositStake.Elector, &a.DepositStake.Staker),
}
case bath.RecoverStake:
value := utils.HumanFriendlyCoinsRepr(a.RecoverStake.Amount)
action.RecoverStake.SetTo(oas.RecoverStakeAction{
Amount: a.RecoverStake.Amount,
Staker: convertAccountAddress(a.RecoverStake.Staker, h.addressBook),
})
action.SimplePreview = oas.ActionSimplePreview{
Name: "Recover Stake",
Description: i18n.T(acceptLanguage.Value, i18n.C{
MessageID: recoverStakeMessageID,
TemplateData: map[string]interface{}{
"Amount": value,
},
}),
Value: oas.NewOptString(value),
Accounts: distinctAccounts(h.addressBook, &a.RecoverStake.Elector, &a.RecoverStake.Staker),
}
case bath.SmartContractExec:
op := "Call"
if a.SmartContractExec.Operation != "" {
Expand Down
16 changes: 16 additions & 0 deletions pkg/bath/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
ContractDeploy ActionType = "ContractDeploy"
Subscription ActionType = "Subscription"
UnSubscription ActionType = "UnSubscribe"
DepositStake ActionType = "DepositStake"
RecoverStake ActionType = "RecoverStake"
AuctionBid ActionType = "AuctionBid"
AuctionTgInitBid ActionType = "AuctionTgInitBid"

Expand Down Expand Up @@ -57,6 +59,8 @@ type (
Subscription *SubscriptionAction `json:",omitempty"`
UnSubscription *UnSubscriptionAction `json:",omitempty"`
AuctionBid *AuctionBidAction `json:",omitempty"`
DepositStake *DepositStakeAction `json:",omitempty"`
RecoverStake *RecoverStakeAction `json:",omitempty"`
Success bool
Type ActionType
}
Expand Down Expand Up @@ -91,6 +95,18 @@ type (
Price int64
}

DepositStakeAction struct {
Amount int64
Elector tongo.AccountID
Staker tongo.AccountID
}

RecoverStakeAction struct {
Amount int64
Elector tongo.AccountID
Staker tongo.AccountID
}

JettonTransferAction struct {
Comment *string
Jetton tongo.AccountID
Expand Down
118 changes: 118 additions & 0 deletions pkg/bath/staking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package bath

import (
"github.com/tonkeeper/opentonapi/pkg/blockchain/config"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/abi"
)

type BubbleDepositStake struct {
Staker tongo.AccountID
Amount int64
Success bool
}

func (ds BubbleDepositStake) ToAction() *Action {
return &Action{
DepositStake: &DepositStakeAction{
Amount: ds.Amount,
Elector: config.ElectorAddress(),
Staker: ds.Staker,
},
Success: ds.Success,
Type: DepositStake,
}
}

func FindDepositStake(bubble *Bubble) bool {
bubbleTx, ok := bubble.Info.(BubbleTx)
if !ok {
return false
}
if !bubbleTx.operation(abi.ElectorNewStakeMsgOp) {
return false
}
if bubbleTx.account.Address != config.ElectorAddress() {
return false
}
stake := BubbleDepositStake{
Amount: bubbleTx.inputAmount,
Staker: bubbleTx.inputFrom.Address,
}
newBubble := Bubble{
Accounts: bubble.Accounts,
ValueFlow: bubble.ValueFlow,
}
newBubble.Children = ProcessChildren(bubble.Children,
func(child *Bubble) *Merge {
confirmation, ok := child.Info.(BubbleTx)
if !ok {
return nil
}
if !confirmation.operation(abi.ElectorNewStakeConfirmationMsgOp) {
return nil
}
stake.Success = true
newBubble.ValueFlow.Merge(child.ValueFlow)
return &Merge{children: child.Children}
})
newBubble.Info = stake
*bubble = newBubble
return true
}

type BubbleRecoverStake struct {
Staker tongo.AccountID
Amount int64
Success bool
}

func (b BubbleRecoverStake) ToAction() *Action {
return &Action{
RecoverStake: &RecoverStakeAction{
Amount: b.Amount,
Elector: config.ElectorAddress(),
Staker: b.Staker,
},
Success: b.Success,
Type: RecoverStake,
}
}

func FindRecoverStake(bubble *Bubble) bool {
bubbleTx, ok := bubble.Info.(BubbleTx)
if !ok {
return false
}
if !bubbleTx.operation(abi.ElectorRecoverStakeRequestMsgOp) {
return false
}
if bubbleTx.account.Address != config.ElectorAddress() {
return false
}
recoverStake := BubbleRecoverStake{
Amount: bubbleTx.inputAmount,
Staker: bubbleTx.inputFrom.Address,
}
newBubble := Bubble{
Accounts: bubble.Accounts,
ValueFlow: bubble.ValueFlow,
}
newBubble.Children = ProcessChildren(bubble.Children,
func(child *Bubble) *Merge {
response, ok := child.Info.(BubbleTx)
if !ok {
return nil
}
if !response.operation(abi.ElectorRecoverStakeResponseMsgOp) {
return nil
}
recoverStake.Success = true
newBubble.ValueFlow.Merge(child.ValueFlow)
return &Merge{children: child.Children}
})
newBubble.Info = recoverStake
*bubble = newBubble
return true

}
2 changes: 2 additions & 0 deletions pkg/bath/straws.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var DefaultStraws = []Straw{
FindInitialSubscription,
FindExtendedSubscription,
FindNftPurchase,
FindDepositStake,
FindRecoverStake,
}

func FindNFTTransfer(bubble *Bubble) bool {
Expand Down
10 changes: 10 additions & 0 deletions pkg/blockchain/config/elector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

import (
"github.com/tonkeeper/tongo"
)

func ElectorAddress() tongo.AccountID {
// TODO: read from the blockchain config
return tongo.MustParseAccountID("Ef8zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzM0vF")
}
Loading

0 comments on commit 8d0c97c

Please sign in to comment.