Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
erokhinav committed Sep 18, 2024
1 parent 14f872c commit 3fc93dd
Show file tree
Hide file tree
Showing 40 changed files with 127 additions and 108 deletions.
32 changes: 14 additions & 18 deletions pkg/api/event_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func signedValue(value string, viewer *tongo.AccountID, source, destination tong
return value
}

func (h *Handler) convertActionTonTransfer(t *bath.TonTransferAction, acceptLanguage string, viewer *tongo.AccountID, actionDescription *string) (oas.OptTonTransferAction, oas.ActionSimplePreview) {
func (h *Handler) convertActionTonTransfer(t *bath.TonTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptTonTransferAction, oas.ActionSimplePreview) {

var action oas.OptTonTransferAction
action.SetTo(oas.TonTransferAction{
Expand All @@ -139,23 +139,19 @@ func (h *Handler) convertActionTonTransfer(t *bath.TonTransferAction, acceptLang
})
}
value := i18n.FormatTONs(t.Amount)
description := i18n.T(acceptLanguage, i18n.C{
DefaultMessage: &i18n.M{
ID: "tonTransferAction",
Other: "Transferring {{.Value}}",
},
TemplateData: i18n.Template{
"Value": value,
},
})
if actionDescription != nil {
description = *actionDescription
}
simplePreview := oas.ActionSimplePreview{
Name: "Ton Transfer",
Description: description,
Accounts: distinctAccounts(viewer, h.addressBook, &t.Sender, &t.Recipient),
Value: oas.NewOptString(value),
Name: "Ton Transfer",
Description: i18n.T(acceptLanguage, i18n.C{
DefaultMessage: &i18n.M{
ID: "tonTransferAction",
Other: "Transferring {{.Value}}",
},
TemplateData: i18n.Template{
"Value": value,
},
}),
Accounts: distinctAccounts(viewer, h.addressBook, &t.Sender, &t.Recipient),
Value: oas.NewOptString(value),
}
return action, simplePreview
}
Expand Down Expand Up @@ -436,7 +432,7 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
}
switch a.Type {
case bath.TonTransfer:
action.TonTransfer, action.SimplePreview = h.convertActionTonTransfer(a.TonTransfer, acceptLanguage.Value, viewer, a.Description)
action.TonTransfer, action.SimplePreview = h.convertActionTonTransfer(a.TonTransfer, acceptLanguage.Value, viewer)
case bath.NftItemTransfer:
action.NftItemTransfer, action.SimplePreview = h.convertActionNftTransfer(a.NftItemTransfer, acceptLanguage.Value, viewer)
case bath.JettonTransfer:
Expand Down
2 changes: 1 addition & 1 deletion pkg/bath/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type (
InscriptionTransfer *InscriptionTransferAction `json:",omitempty"`
Success bool
Type ActionType
Description *string
Error *string
BaseTransactions []ton.Bits256
}
TonTransferAction struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/bath/bath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ func TestFindActions(t *testing.T) {
WithStraws(straws),
WithInformationSource(source))
require.Nil(t, err)
actionsList = EnrichWithIntentions(trace, actionsList)
results := result{
Actions: actionsList.Actions,
}
Expand Down
54 changes: 38 additions & 16 deletions pkg/bath/intentions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ type OutMessage struct {
}

func EnrichWithIntentions(trace *core.Trace, actions *ActionsList) *ActionsList {
var outMessages []OutMessage
extractIntentions(trace, &outMessages)
outMessages, inMsgCount := extractIntentions(trace)
if len(outMessages) <= inMsgCount {
return actions
}
outMessages = removeMatchedIntentions(trace, &outMessages)
for _, outMsg := range outMessages {
newAction := createActionFromMessage(outMsg)
added := false
Expand All @@ -36,17 +39,40 @@ func EnrichWithIntentions(trace *core.Trace, actions *ActionsList) *ActionsList
return actions
}

func extractIntentions(trace *core.Trace, outMessages *[]OutMessage) {
*outMessages = append(*outMessages, getOutMessages(&trace.Transaction)...)
for _, child := range trace.Children {
for i, outMsg := range *outMessages {
if isMatch(outMsg, child.Transaction.InMsg) {
// remove this outgoing message
*outMessages = append((*outMessages)[:i], (*outMessages)[i+1:]...)
func extractIntentions(trace *core.Trace) ([]OutMessage, int) {
var outMessages []OutMessage
var inMsgCount int

var getIntentions func(*core.Trace)
getIntentions = func(trace *core.Trace) {
outMessages = append(outMessages, getOutMessages(&trace.Transaction)...)
for _, child := range trace.Children {
if child.InMsg != nil {
inMsgCount += 1
}
getIntentions(child)
}
}
getIntentions(trace)

return outMessages, inMsgCount
}

func removeMatchedIntentions(trace *core.Trace, intentions *[]OutMessage) []OutMessage {
var matchAndRemove func(*core.Trace)
matchAndRemove = func(trace *core.Trace) {
for _, child := range trace.Children {
for i, outMsg := range *intentions {
if isMatch(outMsg, child.Transaction.InMsg) {
// remove this outgoing message
*intentions = append((*intentions)[:i], (*intentions)[i+1:]...)
}
}
matchAndRemove(child)
}
extractIntentions(child, outMessages)
}
matchAndRemove(trace)
return *intentions
}

func isMatch(msgOut OutMessage, msgIn *core.Message) bool {
Expand Down Expand Up @@ -97,10 +123,6 @@ func compareMessageFields(msgOut OutMessage, msgIn *core.Message) bool {
return false
}

if msgOut.tx.Account != *msgIn.Source {
return false
}

if msgOut.mode < 128 && int64(msg.Value.Grams) != msgIn.Value {
return false
}
Expand Down Expand Up @@ -172,7 +194,7 @@ func createActionFromMessage(msgOut OutMessage) Action {
if msgOut.mode < 128 {
action.TonTransfer.Amount = int64(msgOut.messageRelaxed.MessageInternal.Value.Grams)
if msgOut.tx.EndBalance < action.TonTransfer.Amount {
action.Description = g.Pointer("Not enough balance")
action.Error = g.Pointer("Not enough balance")
}
}
case abi.NftTransferMsgBody:
Expand All @@ -195,7 +217,7 @@ func createActionFromMessage(msgOut OutMessage) Action {
if msgOut.mode < 128 {
action.TonTransfer.Amount = int64(msgOut.messageRelaxed.MessageInternal.Value.Grams)
if msgOut.tx.EndBalance < action.TonTransfer.Amount {
action.Description = g.Pointer("Not enough balance")
action.Error = g.Pointer("Not enough balance")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/bath/testdata/buy-nft-on-fragment.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"Success": true,
"Type": "NftPurchase",
"Description": null,
"Error": null,
"BaseTransactions": [
"268f46a593b64400a93eb17a0945629647fdd662fb00ff69b58327d10336022b",
"29c039520eeef18295d428e919b7b33bd472f50ab3e9713b767a4f5e5c55181e"
Expand All @@ -27,7 +27,7 @@
},
"Success": true,
"Type": "TonTransfer",
"Description": null,
"Error": null,
"BaseTransactions": [
"9fc8546b6f866685908c18b91e14d0fbafb39d6389403831b3ba63c3e60399e5"
]
Expand All @@ -43,7 +43,7 @@
},
"Success": true,
"Type": "TonTransfer",
"Description": null,
"Error": null,
"BaseTransactions": [
"fa4c9108bebce63aff79570fceccf99126996b1e8793fcbdcd56a0b8752a2ee1"
]
Expand Down
2 changes: 1 addition & 1 deletion pkg/bath/testdata/cut-jetton-transfer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"Success": true,
"Type": "JettonTransfer",
"Description": null,
"Error": null,
"BaseTransactions": [
"ae17905025e6920b8ca4f036c79bf3cdddeea052556f9d2b21c574670caf1d12",
"9187161025b3f6249b5d57b8a347bb6e7f82c364fb7f0efb3261f54604574e50",
Expand Down
2 changes: 1 addition & 1 deletion pkg/bath/testdata/dedust-swap-from-ton.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"Success": true,
"Type": "JettonSwap",
"Description": null,
"Error": null,
"BaseTransactions": [
"c85ebd3043603612af808ab874ac1d19d1a68725afc3c4a78589ed5e88372b21",
"bdef4c941f1f311c7de9badebc71464752c6599ae7cdcf514cd449726d750dd6",
Expand Down
4 changes: 2 additions & 2 deletions pkg/bath/testdata/dedust-swap-jettons.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"Success": true,
"Type": "JettonSwap",
"Description": null,
"Error": null,
"BaseTransactions": [
"c18eaf143061a879bdd81f4db08cf4e37ebaeeea03b350fcf33dce1fa486bb62",
"2e2cdf4736ba039f1e86faf195c6ac68835c030e150cbabcf96ce20514fbf7e4",
Expand All @@ -39,7 +39,7 @@
},
"Success": true,
"Type": "ContractDeploy",
"Description": null,
"Error": null,
"BaseTransactions": [
"2e2cdf4736ba039f1e86faf195c6ac68835c030e150cbabcf96ce20514fbf7e4"
]
Expand Down
2 changes: 1 addition & 1 deletion pkg/bath/testdata/dedust-swap-to-ton.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"Success": true,
"Type": "JettonSwap",
"Description": null,
"Error": null,
"BaseTransactions": [
"bba7985c6b547c2bce3a2979b41bf5b0c5bf5a399bcbee69a80572039147ce4d",
"93a65eccc2922a64274a090e52973ff9ddd26b86d6458ab2bed36ed3c051ec59",
Expand Down
4 changes: 2 additions & 2 deletions pkg/bath/testdata/deploy-contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"Success": true,
"Type": "TonTransfer",
"Description": null,
"Error": null,
"BaseTransactions": [
"4ac45cea7c469c2c98b73b064d1588313f8cde5b356267418302801d5e94b17b"
]
Expand All @@ -23,7 +23,7 @@
},
"Success": true,
"Type": "ContractDeploy",
"Description": null,
"Error": null,
"BaseTransactions": [
"657623f3db93397a3bb956e0e621d7a37e4ff27b80013a68f2dd91c8094b50e3"
]
Expand Down
6 changes: 3 additions & 3 deletions pkg/bath/testdata/deposit-liquid-staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"Success": true,
"Type": "DepositStake",
"Description": null,
"Error": null,
"BaseTransactions": [
"20bee52141d1af08d3631baba8e598518a5a98e7b7d82e6960c79cb4001ec910"
]
Expand All @@ -23,7 +23,7 @@
},
"Success": true,
"Type": "JettonMint",
"Description": null,
"Error": null,
"BaseTransactions": [
"2d77c21a86e5189a6b44bfed0c8c588505f770ff8bdcdfe1a3528512f330d9b1",
"242f3560ec3274abf5046ed7a867dfb7b37a97d86abb244f06211e809a27e6de",
Expand All @@ -38,7 +38,7 @@
},
"Success": true,
"Type": "ContractDeploy",
"Description": null,
"Error": null,
"BaseTransactions": [
"2d77c21a86e5189a6b44bfed0c8c588505f770ff8bdcdfe1a3528512f330d9b1"
]
Expand Down
8 changes: 4 additions & 4 deletions pkg/bath/testdata/disintar-nft-purchase.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"Success": true,
"Type": "NftPurchase",
"Description": null,
"Error": null,
"BaseTransactions": [
"bccf47a1994af80196b94000b1d3a7bd92a046ca0a8a26db690802457da1d5cf",
"3a30ad237eedde236d8aad4e1506e249d70fb6eb245c4d776ecb24df38d628d9",
Expand All @@ -28,7 +28,7 @@
},
"Success": true,
"Type": "TonTransfer",
"Description": null,
"Error": null,
"BaseTransactions": [
"5191af8acbf473017d2df1b164fd686f0d107278104597d89040104a7c63b9e7"
]
Expand All @@ -44,7 +44,7 @@
},
"Success": true,
"Type": "TonTransfer",
"Description": null,
"Error": null,
"BaseTransactions": [
"6f7fa225314b344d390ef9c1a451ea4c01b32528bf3341f215ad7787b6bc9694"
]
Expand All @@ -60,7 +60,7 @@
},
"Success": true,
"Type": "TonTransfer",
"Description": null,
"Error": null,
"BaseTransactions": [
"ec4e20e6047390f1202af08d2c4df336d8e843ea015d488832404898c54ffda2"
]
Expand Down
4 changes: 2 additions & 2 deletions pkg/bath/testdata/domain-renew.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"Success": true,
"Type": "DomainRenew",
"Description": null,
"Error": null,
"BaseTransactions": [
"bf33c4ee09a872f0831935adec38a0e411c6be83b87e47bb7bf7a843d5e06047"
]
Expand All @@ -22,7 +22,7 @@
},
"Success": true,
"Type": "SmartContractExec",
"Description": null,
"Error": null,
"BaseTransactions": [
"5123850dca7109412f695435a9a75618c001b70fad98d34d950b860116f37785"
]
Expand Down
4 changes: 2 additions & 2 deletions pkg/bath/testdata/encrypted-comment.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"Success": true,
"Type": "TonTransfer",
"Description": null,
"Error": null,
"BaseTransactions": [
"c1d98ee34e88a912d7eefd7d1401d612d33c74835343814ca99a318fb7138ea8"
]
Expand All @@ -26,7 +26,7 @@
},
"Success": true,
"Type": "ContractDeploy",
"Description": null,
"Error": null,
"BaseTransactions": [
"6f3e1f2c05df05345198a9d26456dcb51d4c78ce64ced56fb9976e92941211d3"
]
Expand Down
2 changes: 1 addition & 1 deletion pkg/bath/testdata/failed-dedust-swap.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"Success": false,
"Type": "TonTransfer",
"Description": "Not enough balance",
"Error": "Not enough balance",
"BaseTransactions": null
}
],
Expand Down
2 changes: 1 addition & 1 deletion pkg/bath/testdata/failed-simple-transfer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"Success": false,
"Type": "TonTransfer",
"Description": "Not enough balance",
"Error": "Not enough balance",
"BaseTransactions": null
}
],
Expand Down
Loading

0 comments on commit 3fc93dd

Please sign in to comment.