Skip to content

Commit

Permalink
initial commit, implement the avl.Tree struct changes, throw out stri…
Browse files Browse the repository at this point in the history
…ng concat and add special strucures
  • Loading branch information
matijamarjanovic committed Dec 17, 2024
1 parent 273fb27 commit 9353eb5
Showing 1 changed file with 81 additions and 16 deletions.
97 changes: 81 additions & 16 deletions examples/gno.land/p/demo/grc/grc1155/basic_grc1155_token.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,72 @@ import (
"gno.land/p/demo/ufmt"
)

// balanceKey represents the composite key for token balances
type balanceKey struct {
addr std.Address
tid TokenID
}

// Compare implements comparison for AVL tree operations
func (k balanceKey) Compare(other interface{}) int {
o := other.(balanceKey)
// First compare addresses
if k.addr < o.addr {
return -1
}
if k.addr > o.addr {
return 1
}
// If addresses are equal, compare token IDs
if k.tid < o.tid {
return -1
}
if k.tid > o.tid {
return 1
}
return 0
}

// String implements string representation for the key
func (k balanceKey) String() string {
return string(k.tid) + ":" + k.addr.String()
}

// approvalKey represents the composite key for operator approvals
type approvalKey struct {
owner std.Address
operator std.Address
}

// Compare implements comparison for AVL tree operations
func (k approvalKey) Compare(other interface{}) int {
o := other.(approvalKey)
// First compare owners
if k.owner < o.owner {
return -1
}
if k.owner > o.owner {
return 1
}
// If owners are equal, compare operators
if k.operator < o.operator {
return -1
}
if k.operator > o.operator {
return 1
}
return 0
}

// String implements string representation for the key
func (k approvalKey) String() string {
return k.owner.String() + ":" + k.operator.String()
}

type basicGRC1155Token struct {
uri string
balances avl.Tree // "TokenId:Address" -> uint64
operatorApprovals avl.Tree // "OwnerAddress:OperatorAddress" -> bool
balances avl.Tree // balanceKey -> uint64
operatorApprovals avl.Tree // approvalKey -> bool
}

var _ IGRC1155 = (*basicGRC1155Token)(nil)
Expand All @@ -32,7 +94,7 @@ func (s *basicGRC1155Token) BalanceOf(addr std.Address, tid TokenID) (uint64, er
return 0, ErrInvalidAddress
}

key := string(tid) + ":" + addr.String()
key := balanceKey{addr: addr, tid: tid}
balance, found := s.balances.Get(key)
if !found {
return 0, nil
Expand Down Expand Up @@ -72,7 +134,7 @@ func (s *basicGRC1155Token) IsApprovedForAll(owner, operator std.Address) bool {
if operator == owner {
return true
}
key := owner.String() + ":" + operator.String()
key := approvalKey{owner: owner, operator: operator}
_, found := s.operatorApprovals.Get(key)
if !found {
return false
Expand Down Expand Up @@ -201,7 +263,7 @@ func (s *basicGRC1155Token) setApprovalForAll(owner, operator std.Address, appro
return nil
}

key := owner.String() + ":" + operator.String()
key := approvalKey{owner: owner, operator: operator}
if approved {
s.operatorApprovals.Set(key, approved)
} else {
Expand Down Expand Up @@ -231,6 +293,10 @@ func (s *basicGRC1155Token) safeBatchTransferFrom(from, to std.Address, batch []
for i := 0; i < len(batch); i++ {
tid := batch[i]
amount := amounts[i]

fromKey := balanceKey{addr: from, tid: tid}
toKey := balanceKey{addr: to, tid: tid}

fromBalance, err := s.BalanceOf(from, tid)
if err != nil {
return err
Expand All @@ -245,14 +311,11 @@ func (s *basicGRC1155Token) safeBatchTransferFrom(from, to std.Address, batch []

fromBalance -= amount
toBalance += amount
fromBalanceKey := string(tid) + ":" + from.String()
toBalanceKey := string(tid) + ":" + to.String()
s.balances.Set(fromBalanceKey, fromBalance)
s.balances.Set(toBalanceKey, toBalance)
s.balances.Set(fromKey, fromBalance)
s.balances.Set(toKey, toBalance)
}

s.afterTokenTransfer(caller, from, to, batch, amounts)

return nil
}

Expand All @@ -271,17 +334,18 @@ func (s *basicGRC1155Token) mintBatch(to std.Address, batch []TokenID, amounts [
for i := 0; i < len(batch); i++ {
tid := batch[i]
amount := amounts[i]

key := balanceKey{addr: to, tid: tid}
toBalance, err := s.BalanceOf(to, tid)
if err != nil {
return err
}

toBalance += amount
toBalanceKey := string(tid) + ":" + to.String()
s.balances.Set(toBalanceKey, toBalance)
s.balances.Set(key, toBalance)
}

s.afterTokenTransfer(caller, zeroAddress, to, batch, amounts)

return nil
}

Expand All @@ -300,20 +364,21 @@ func (s *basicGRC1155Token) burnBatch(from std.Address, batch []TokenID, amounts
for i := 0; i < len(batch); i++ {
tid := batch[i]
amount := amounts[i]

key := balanceKey{addr: from, tid: tid}
fromBalance, err := s.BalanceOf(from, tid)
if err != nil {
return err
}
if fromBalance < amount {
return ErrBurnAmountExceedsBalance
}

fromBalance -= amount
fromBalanceKey := string(tid) + ":" + from.String()
s.balances.Set(fromBalanceKey, fromBalance)
s.balances.Set(key, fromBalance)
}

s.afterTokenTransfer(caller, from, zeroAddress, batch, amounts)

return nil
}

Expand Down

0 comments on commit 9353eb5

Please sign in to comment.