Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer verifier Sui changes #3

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 94 additions & 15 deletions node/cmd/transfer-verifier/transfer-verifier-sui-structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -95,7 +95,7 @@ type ObjectChange struct {
// - ensure that the coin types match
func (o ObjectChange) ValidateTypeInformation(expectedPackageId string) (success bool) {
// AI generated regex
re := regexp.MustCompile(`0x2::dynamic_field::Field<([^:]+)::token_registry::Key<([^>]+)>, ([^:]+)::([^<]+)<([^>]+)>>`)
re := regexp.MustCompile(`^0x2::dynamic_field::Field<([^:]+)::token_registry::Key<([^>]+)>, ([^:]+)::([^<]+)<([^>]+)>>$`)
matches := re.FindStringSubmatch(o.ObjectType)

if len(matches) == 6 {
Expand Down Expand Up @@ -134,33 +134,33 @@ type SuiTryMultiGetPastObjectsResponse struct {
}

// Gets the balance difference of the two result objects.
func (r SuiTryMultiGetPastObjectsResponse) GetBalanceDiff() (int, error) {
func (r SuiTryMultiGetPastObjectsResponse) GetBalanceDiff() (*big.Int, error) {

if len(r.Result) != 2 {
return 0, fmt.Errorf("incorrect number of results received")
return big.NewInt(0), fmt.Errorf("incorrect number of results received")
}

// Determine if the asset is wrapped or native
isWrapped, err := r.Result[0].IsWrapped()
if err != nil {
return 0, fmt.Errorf("error in checking if object is wrapped: %w", err)
return big.NewInt(0), fmt.Errorf("error in checking if object is wrapped: %w", err)
}

// TODO: Should we check that the other asset is also wrapped?
newBalance, err := r.Result[0].GetVersionBalance(isWrapped)
if err != nil {
return 0, fmt.Errorf("error in getting new balance: %w", err)
return big.NewInt(0), fmt.Errorf("error in getting new balance: %w", err)
}

oldBalance, err := r.Result[1].GetVersionBalance(isWrapped)
if err != nil {
return 0, fmt.Errorf("error in getting old balance: %w", err)
return big.NewInt(0), fmt.Errorf("error in getting old balance: %w", err)
}

difference := newBalance - oldBalance
difference := newBalance.Sub(newBalance, oldBalance)
// If the asset is wrapped, it means that the balance was burned, so the difference should be negative.
if isWrapped {
difference = -difference
difference = difference.Mul(difference, big.NewInt(-1))
}

return difference, nil
Expand Down Expand Up @@ -212,6 +212,48 @@ func (r SuiTryMultiGetPastObjectsResponse) GetTokenChain() (uint16, error) {
return chain0, nil
}

func (r SuiTryMultiGetPastObjectsResponse) GetObjectId() (string, error) {
objectId, err := r.Result[0].GetObjectId()
if err != nil {
return "", fmt.Errorf("could not get object id")
}

return objectId, nil
}

func (r SuiTryMultiGetPastObjectsResponse) GetVersion() (string, error) {
version, err := r.Result[0].GetVersion()
if err != nil {
return "", fmt.Errorf("could not get object id")
}

return version, nil
}

func (r SuiTryMultiGetPastObjectsResponse) GetPreviousVersion() (string, error) {
previousVersion, err := r.Result[1].GetVersion()
if err != nil {
return "", fmt.Errorf("could not get object id")
}

return previousVersion, nil
}

func (r SuiTryMultiGetPastObjectsResponse) GetObjectType() (string, error) {
type0, err0 := r.Result[0].GetObjectType()
type1, err1 := r.Result[1].GetObjectType()

if err0 != nil {
return "", fmt.Errorf("error in getting token chain: %w", err0)
} else if err1 != nil {
return "", fmt.Errorf("error in getting token chain: %w", err1)
} else if type0 != type1 {
return "", fmt.Errorf("token chain ids do not match")
}

return type0, nil
}

// The result object for suix_tryMultiGetPastObjects.
type SuiTryMultiGetPastObjectsResult struct {
Status string `json:"status"`
Expand All @@ -231,9 +273,10 @@ func (r SuiTryMultiGetPastObjectsResult) IsWrapped() (bool, error) {
}

// Get the balance of the result object.
func (r SuiTryMultiGetPastObjectsResult) GetVersionBalance(isWrapped bool) (int, error) {
func (r SuiTryMultiGetPastObjectsResult) GetVersionBalance(isWrapped bool) (*big.Int, error) {

var path string
supplyInt := big.NewInt(0)

// The path to use for a native asset
pathNative := "content.fields.value.fields.custody"
Expand All @@ -250,13 +293,13 @@ func (r SuiTryMultiGetPastObjectsResult) GetVersionBalance(isWrapped bool) (int,
supply, err := extractFromJsonPath[string](*r.Details, path)

if err != nil {
return 0, fmt.Errorf("error in extracting wormhole balance: %w", err)
return supplyInt, fmt.Errorf("error in extracting wormhole balance: %w", err)
}

// TODO: This likely shouldn't be an int. It should be a big.Int?
supplyInt, err := strconv.Atoi(supply)
if err != nil {
return 0, fmt.Errorf("error converting supply to int: %w", err)
supplyInt, success := supplyInt.SetString(supply, 10)

if !success {
return supplyInt, fmt.Errorf("error converting supply to int: %w", err)
}

return supplyInt, nil
Expand Down Expand Up @@ -342,6 +385,42 @@ func (r SuiTryMultiGetPastObjectsResult) GetTokenChain() (uint16, error) {
return uint16(chain), nil
}

func (r SuiTryMultiGetPastObjectsResult) GetObjectId() (string, error) {
path := "objectId"

objectId, err := extractFromJsonPath[string](*r.Details, path)

if err != nil {
return "", fmt.Errorf("error in extracting objectId: %w", err)
}

return objectId, nil
}

func (r SuiTryMultiGetPastObjectsResult) GetVersion() (string, error) {
path := "version"

version, err := extractFromJsonPath[string](*r.Details, path)

if err != nil {
return "", fmt.Errorf("error in extracting version: %w", err)
}

return version, nil
}

func (r SuiTryMultiGetPastObjectsResult) GetObjectType() (string, error) {
path := "type"

version, err := extractFromJsonPath[string](*r.Details, path)

if err != nil {
return "", fmt.Errorf("error in extracting version: %w", err)
}

return version, nil
}

// Definition of the WormholeMessage event
type WormholeMessage struct {
ConsistencyLevel *uint8 `json:"consistency_level"`
Expand Down
18 changes: 9 additions & 9 deletions node/cmd/transfer-verifier/transfer-verifier-sui.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,15 @@ func processObjectUpdates(objectChanges []ObjectChange, suiApiConnection SuiApiI
continue
}

normalized := normalize(big.NewInt(int64(balanceDiff)), decimals)
normalized := normalize(balanceDiff, decimals)

// Add the key if it does not exist yet
key := fmt.Sprintf(KEY_FORMAT, address, chain)
if _, exists := transferredIntoBridge[key]; !exists {
transferredIntoBridge[key] = big.NewInt(0)
}

// Add the normalized amount to the transferredIntoBridge map
transferredIntoBridge[key] = new(big.Int).Add(transferredIntoBridge[key], normalized)
// Intentionally use 'Set' instead of 'Add' because there should only be a single objectChange per token
var amount big.Int
transferredIntoBridge[key] = amount.Set(normalized)

// Increment the number of changes processed
numChangesProcessed++
Expand All @@ -292,7 +291,7 @@ func processObjectUpdates(objectChanges []ObjectChange, suiApiConnection SuiApiI
return transferredIntoBridge, numChangesProcessed, nil
}

func processDigest(digest string, suiApiConnection SuiApiInterface, logger *zap.Logger) error {
func processDigest(digest string, suiApiConnection SuiApiInterface, logger *zap.Logger) (uint, error) {
// Get the transaction block
txBlock, err := suiApiConnection.GetTransactionBlock(digest)

Expand All @@ -313,19 +312,20 @@ func processDigest(digest string, suiApiConnection SuiApiInterface, logger *zap.
// TODO: Using `Warn` for testing purposes. Update to Fatal? when ready to go into PR.
// TODO: Revisit error handling here.
for key, amountOut := range requestedOutOfBridge {

if _, exists := transferredIntoBridge[key]; !exists {
logger.Warn("transfer-out request for tokens that were never deposited",
zap.String("tokenAddress", key))
// TODO: Is it better to return or continue here?
return errors.New("transfer-out request for tokens that were never deposited")
return 0, errors.New("transfer-out request for tokens that were never deposited")
// continue
}

amountIn := transferredIntoBridge[key]

if amountOut.Cmp(amountIn) > 0 {
logger.Warn("requested amount out is larger than amount in")
return errors.New("requested amount out is larger than amount in")
return 0, errors.New("requested amount out is larger than amount in")
}

keyParts := strings.Split(key, "-")
Expand All @@ -338,7 +338,7 @@ func processDigest(digest string, suiApiConnection SuiApiInterface, logger *zap.

logger.Info("Digest processed", zap.String("txDigest", digest), zap.Int("numEventsProcessed", numEventsProcessed), zap.Int("numChangesProcessed", numChangesProcessed))

return nil
return uint(numEventsProcessed), nil
}

type SuiApiResponse interface {
Expand Down
Loading
Loading