2
2
module aptos_token ::token_transfers {
3
3
use std::signer;
4
4
use std::string::String ;
5
- use aptos_std::table_with_length::{Self , TableWithLength };
5
+ use std::error;
6
+ use aptos_std::table::{Self , Table };
6
7
use aptos_token::token::{Self , Token , TokenId };
7
8
9
+ const ETOKEN_OFFER_NOT_EXIST : u64 = 1 ;
10
+
8
11
struct TokenTransfers has key {
9
- pending_claims: TableWithLength <address , TableWithLength <TokenId , Token >>,
12
+ pending_claims: Table <TokenOfferId , Token >,
13
+ }
14
+
15
+ struct TokenOfferId has copy , drop , store {
16
+ to_addr: address ,
17
+ token_id: TokenId ,
10
18
}
11
19
12
20
fun initialize_token_transfers (account: &signer ) {
13
21
move_to (
14
22
account,
15
23
TokenTransfers {
16
- pending_claims: table_with_length ::new <address , TableWithLength < TokenId , Token > >(),
24
+ pending_claims: table ::new <TokenOfferId , Token >(),
17
25
}
18
26
)
19
27
}
20
28
29
+ fun create_token_offer_id (to_addr: address , token_id: TokenId ): TokenOfferId {
30
+ TokenOfferId {
31
+ to_addr,
32
+ token_id
33
+ }
34
+ }
35
+
21
36
public entry fun offer_script (
22
37
sender: signer ,
23
38
receiver: address ,
@@ -31,7 +46,6 @@ module aptos_token::token_transfers {
31
46
offer (&sender, receiver, token_id, amount);
32
47
}
33
48
34
- // Make an entry into pending transfers and extract from gallery
35
49
public fun offer (
36
50
sender: &signer ,
37
51
receiver: address ,
@@ -45,19 +59,14 @@ module aptos_token::token_transfers {
45
59
46
60
let pending_claims =
47
61
&mut borrow_global_mut <TokenTransfers >(sender_addr).pending_claims;
48
- if (!table_with_length::contains (pending_claims, receiver)) {
49
- table_with_length::add (pending_claims, receiver, table_with_length::new ())
50
- };
51
- let addr_pending_claims = table_with_length::borrow_mut (pending_claims, receiver);
52
-
62
+ let token_offer_id = create_token_offer_id (receiver, token_id);
53
63
let token = token::withdraw_token (sender, token_id, amount);
54
- let token_id = *token::token_id (&token);
55
- if (table_with_length::contains (addr_pending_claims, token_id)) {
56
- let dst_token = table_with_length::borrow_mut (addr_pending_claims, token_id);
57
- token::merge (dst_token, token)
64
+ if (!table::contains (pending_claims, token_offer_id)) {
65
+ table::add (pending_claims, token_offer_id, token);
58
66
} else {
59
- table_with_length::add (addr_pending_claims, token_id, token)
60
- }
67
+ let dst_token = table::borrow_mut (pending_claims, token_offer_id);
68
+ token::merge (dst_token, token);
69
+ };
61
70
}
62
71
63
72
public entry fun claim_script (
@@ -72,24 +81,17 @@ module aptos_token::token_transfers {
72
81
claim (&receiver, sender, token_id);
73
82
}
74
83
75
- // Pull from someone else's pending transfers and insert into our gallery
76
84
public fun claim (
77
85
receiver: &signer ,
78
86
sender: address ,
79
87
token_id: TokenId ,
80
88
) acquires TokenTransfers {
81
- let receiver_addr = signer ::address_of (receiver);
82
89
let pending_claims =
83
90
&mut borrow_global_mut <TokenTransfers >(sender).pending_claims;
84
- let pending_tokens = table_with_length::borrow_mut (pending_claims, receiver_addr);
85
- let token = table_with_length::remove (pending_tokens, token_id);
86
-
87
- if (table_with_length::length (pending_tokens) == 0 ) {
88
- let real_pending_claims = table_with_length::remove (pending_claims, receiver_addr);
89
- table_with_length::destroy_empty (real_pending_claims)
90
- };
91
-
92
- token::deposit_token (receiver, token)
91
+ let token_offer_id = create_token_offer_id (signer ::address_of (receiver), token_id);
92
+ assert !(table::contains (pending_claims, token_offer_id), error::not_found (ETOKEN_OFFER_NOT_EXIST ));
93
+ let tokens = table::remove (pending_claims, token_offer_id);
94
+ token::deposit_token (receiver, tokens);
93
95
}
94
96
95
97
public entry fun cancel_offer_script (
@@ -111,16 +113,10 @@ module aptos_token::token_transfers {
111
113
token_id: TokenId ,
112
114
) acquires TokenTransfers {
113
115
let sender_addr = signer ::address_of (sender);
116
+ let token_offer_id = create_token_offer_id (receiver, token_id);
114
117
let pending_claims =
115
118
&mut borrow_global_mut <TokenTransfers >(sender_addr).pending_claims;
116
- let pending_tokens = table_with_length::borrow_mut (pending_claims, receiver);
117
- let token = table_with_length::remove (pending_tokens, token_id);
118
-
119
- if (table_with_length::length (pending_tokens) == 0 ) {
120
- let real_pending_claims = table_with_length::remove (pending_claims, receiver);
121
- table_with_length::destroy_empty (real_pending_claims)
122
- };
123
-
119
+ let token = table::remove (pending_claims, token_offer_id);
124
120
token::deposit_token (sender, token)
125
121
}
126
122
@@ -134,6 +130,7 @@ module aptos_token::token_transfers {
134
130
offer (&creator, owner_addr, token_id, 1 );
135
131
claim (&owner, creator_addr, token_id);
136
132
133
+
137
134
offer (&owner, creator_addr, token_id, 1 );
138
135
cancel_offer (&owner, creator_addr, token_id);
139
136
}
@@ -153,13 +150,13 @@ module aptos_token::token_transfers {
153
150
aptos_framework::account ::create_account_for_test (owner1_addr);
154
151
155
152
offer (&creator, owner0_addr, token_id, 1 );
156
- assert !(table_with_length::length (&borrow_global <TokenTransfers >(creator_addr).pending_claims) == 1 , 1 );
157
153
offer (&creator, owner1_addr, token_id, 1 );
158
- assert !(table_with_length::length (&borrow_global <TokenTransfers >(creator_addr).pending_claims) == 2 , 2 );
154
+
155
+ assert !(token::balance_of (signer ::address_of (&creator), token_id) == 0 , 1 );
159
156
claim (&owner0, creator_addr, token_id);
160
- assert !(table_with_length:: length (& borrow_global < TokenTransfers >(creator_addr).pending_claims ) == 1 , 3 );
157
+ assert !(token:: balance_of ( signer :: address_of (&owner0), token_id ) == 1 , 1 );
161
158
claim (&owner1, creator_addr, token_id);
162
- assert !(table_with_length:: length (& borrow_global < TokenTransfers >(creator_addr).pending_claims ) == 0 , 4 );
159
+ assert !(token:: balance_of ( signer :: address_of (&owner1), token_id ) == 1 , 1 );
163
160
164
161
offer (&owner0, owner1_addr, token_id, 1 );
165
162
claim (&owner1, owner0_addr, token_id);
0 commit comments