Skip to content

Commit 7668a57

Browse files
cmickeybprakashngit
authored andcommitted
Enable token storage in asset wallet notebooks
Expose and use the necessary interfaces for tokens to be included in wallet notebooks. The big benefit of this is that all "ownership" functions can now be handled through the same interface whether they are fungible assets (like marbles) or non-fungible assets (like tokens). Needed to support the "get_balance" method in the token contract (we already support many of the other issuer methods through tokens). Added better support for the balance and transfer in the plugins. And... most notably, added an "issuer" reference in the context file. Context references turn out to be rather useful. This creates a reference to the specific token object in the collections file. So now there is a canonical reference point for accessing issuer functions. Signed-off-by: Mic Bowman <mic.bowman@intel.com>
1 parent 295984c commit 7668a57

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

exchange-contract/contracts/token_object.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ contract_method_reference_t contract_method_dispatch_table[] = {
9393
CONTRACT_METHOD(echo),
9494

9595
// object transfer, escrow & claim methods
96+
CONTRACT_METHOD2(get_balance,ww::exchange::token_object::get_balance),
9697
CONTRACT_METHOD2(transfer,ww::exchange::token_object::transfer),
9798
CONTRACT_METHOD2(escrow,ww::exchange::token_object::escrow),
9899
CONTRACT_METHOD2(escrow_attestation,ww::exchange::token_object::escrow_attestation),

exchange-contract/docs/notebooks/templates/token.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ def token_transfer(new_owner) :
135135
# free to change the file name as well. The default uses the asset name.
136136

137137
# %%
138+
# this adds a reference key to "issuer" which makes the wallets work with tokens
139+
context.set('issuer', '@{.token_object.' + token_name + '}')
140+
138141
contract_identifier = '{}_{}'.format(token_class, instance_identifier)
139-
contexts = ['asset_type', 'vetting', 'guardian', 'token_issuer', 'token_object']
142+
contexts = ['asset_type', 'issuer', 'vetting', 'guardian', 'token_issuer', 'token_object']
140143
contract_files = {
141144
'token' : token_context.get('save_file'),
142145
}

exchange-contract/exchange/contracts/token_object.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ bool ww::exchange::token_object::initialize(
139139
return rsp.success(true);
140140
}
141141

142+
// -----------------------------------------------------------------
143+
// METHOD: get_balance
144+
//
145+
// JSON PARAMETERS:
146+
// none
147+
//
148+
// RETURNS:
149+
// current number of assets assigned to the requestor
150+
// -----------------------------------------------------------------
151+
bool ww::exchange::token_object::get_balance(const Message& msg, const Environment& env, Response& rsp)
152+
{
153+
ASSERT_INITIALIZED(rsp);
154+
155+
int count = 0;
156+
std::string owner;
157+
158+
ASSERT_SUCCESS(rsp, ww::contract::base::get_owner(owner), "failed to retrieve owner");
159+
if (env.originator_id_ == owner)
160+
count = 1;
161+
162+
ww::value::Number balance(count);
163+
return rsp.value(balance, false);
164+
}
165+
142166
// -----------------------------------------------------------------
143167
// transfer
144168
// -----------------------------------------------------------------

exchange-contract/exchange/token_object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace token_object
6262
bool initialize(const Message& msg, const Environment& env, Response& rsp);
6363
6464
// the interface for these methods is copied from issuer contract
65+
bool get_balance(const Message& msg, const Environment& env, Response& rsp);
6566
bool transfer(const Message& msg, const Environment& env, Response& rsp);
6667
bool escrow(const Message& msg, const Environment& env, Response& rsp);
6768
bool escrow_attestation(const Message& msg, const Environment& env, Response& rsp);

exchange-contract/pdo/exchange/plugins/token_object.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
'op_get_asset_type_identifier',
4242
'op_get_issuer_authority',
4343
'op_get_authority',
44+
'op_get_balance',
4445
'op_transfer',
4546
'op_escrow',
4647
'op_release',
@@ -59,6 +60,7 @@
5960
op_get_asset_type_identifier = asset_type.op_get_asset_type_identifier
6061
op_get_issuer_authority = vetting.op_get_issuer_authority
6162
op_get_authority = issuer.op_get_authority
63+
op_get_balance = issuer.op_get_balance
6264
op_transfer = issuer.op_transfer
6365
op_escrow = issuer.op_escrow
6466
op_release = issuer.op_release
@@ -312,6 +314,13 @@ def invoke(cls, state, context, new_owner, **kwargs) :
312314
with open (keyfile, "r") as myfile:
313315
verifying_key = myfile.read()
314316

317+
# in case count was specified, it must be 1 and we must remove it since it is set explicitly
318+
# in the op invocation below
319+
if 'count' in kwargs :
320+
if kwargs['count'] != 1 :
321+
raise ValueError('unexpected count for token transfer')
322+
kwargs.pop('count')
323+
315324
session = pbuilder.SessionParameters(save_file=save_file)
316325
pcontract.invoke_contract_op(issuer.op_transfer, state, context, session, verifying_key, 1, **kwargs)
317326

@@ -372,6 +381,7 @@ def invoke(cls, state, context, message, **kwargs) :
372381
op_get_asset_type_identifier,
373382
op_get_issuer_authority,
374383
op_get_authority,
384+
op_get_balance,
375385
op_transfer,
376386
op_escrow,
377387
op_release,

0 commit comments

Comments
 (0)