Skip to content

Commit

Permalink
feat: inital commit for #152 transfer claim feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wm798222 committed Jan 22, 2021
1 parent 38f6622 commit 4f37463
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
7 changes: 6 additions & 1 deletion server/models/Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,23 @@ class Token{
* To transfer this token according the transfer object
*/
async completeTransfer(transfer){
log.debug("Token complet transfer");

log.debug("Token complete transfer");
await this.tokenRepository.update({
id: this._id,
transfer_pending: false,
transfer_pending_id: transfer.id,
entity_id: transfer.destination_entity_id,
//TODO: add a boolean for claim.
claim: transfer.claim,
});
await this.transactionRepository.create({
token_id: this._id,
transfer_id: transfer.id,
source_entity_id: transfer.source_entity_id,
destination_entity_id: transfer.destination_entity_id,
//TODO: add a boolean for claim.
claim: transfer.claim,
});
}

Expand Down
7 changes: 6 additions & 1 deletion server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class Wallet{
/*
* Transfer some tokens from the sender to receiver
*/
async transfer(sender, receiver, tokens){
async transfer(sender, receiver, tokens, claimBoolean){
// await this.checkDeduct(sender, receiver);
//check tokens belong to sender
for(const token of tokens){
Expand Down Expand Up @@ -487,6 +487,8 @@ class Wallet{
parameters: {
tokens: tokensUUID,
},
// TODO: add boolean for claim in transferRepository
claim: claimBoolean,
});
log.debug("now, deal with tokens");
for(let token of tokens){
Expand Down Expand Up @@ -569,6 +571,7 @@ class Wallet{
bundleSize: bundleSize,
}
}
//TODO: boolean for claim
});
log.debug("now, deal with tokens");
const tokens = await this.tokenService.getTokensByBundle(sender, bundleSize)
Expand Down Expand Up @@ -699,12 +702,14 @@ class Wallet{
}
for(let token of tokens){
expect(token).defined();
//TODO: claim
await token.completeTransfer(transfer);
}
}else{
log.debug("transfer tokens");
const tokens = await this.tokenService.getTokensByPendingTransferId(transfer.id);
for(let token of tokens){
//TODO: claim
await token.completeTransfer(transfer);
}
}
Expand Down
11 changes: 10 additions & 1 deletion server/routes/transferRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ transferRouter.post(
Joi.string(),
Joi.number().min(1).max(32)
).required(),
// TODO: add boolean for claim, but default to false.
claim: Joi.boolean(),
}),
otherwise: Joi.object({
bundle: Joi.object({
Expand All @@ -54,6 +56,8 @@ transferRouter.post(

const walletSender = await walletService.getByIdOrName(req.body.sender_wallet);
const walletReceiver = await walletService.getByIdOrName(req.body.receiver_wallet);
// check if this transfer is a claim (claim == not transferrrable tokens)
const claim = req.body.claim;

let result;
if(req.body.tokens){
Expand All @@ -63,8 +67,12 @@ transferRouter.post(
const token = await tokenService.getByUUID(uuid);
tokens.push(token);
}
result = await walletLogin.transfer(walletSender, walletReceiver, tokens);
//Case 1: with trust, token transfer
console.log('HERE1');
result = await walletLogin.transfer(walletSender, walletReceiver, tokens, claim);
}else{
//Case 2: with trust, bundle transfer
// TODO: get only transferrable tokens
result = await walletLogin.transferBundle(walletSender, walletReceiver, req.body.bundle.bundle_size);
}
const transferService = new TransferService(session);
Expand Down Expand Up @@ -109,6 +117,7 @@ transferRouter.post('/:transfer_id/accept',
await session.beginTransaction();
const walletService = new WalletService(session);
const walletLogin = await walletService.getById(res.locals.wallet_id);
//TODO: claim
const transferJson = await walletLogin.acceptTransfer(req.params.transfer_id);
const transferService = new TransferService(session);
const transferJson2 = await transferService.convertToResponse(transferJson);
Expand Down
38 changes: 38 additions & 0 deletions server/routes/transferRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,44 @@ describe("transferRouter", () => {
expect(res).property('statusCode').eq(201);
});

//TODO: test for case 1: with trust relationship, tokens specified
it.only('claim transfer with existing trust relationship, and specified tokens', async () => {
sinon
.stub(WalletService.prototype, 'getById')
.resolves(new Wallet(1));
sinon
.stub(WalletService.prototype, 'getByIdOrName')
.onFirstCall()
.resolves(new Wallet(1))
.onSecondCall()
.resolves(new Wallet(2));
sinon.stub(TokenService.prototype, 'getByUUID').resolves(
new Token({
id: 1,
entity_id: 1,
}),
);
sinon.stub(Wallet.prototype, 'transfer').resolves({
id: 1,
state: Transfer.STATE.completed,
});
sinon.stub(TransferService.prototype, 'convertToResponse').resolves({
id: 1,
state: Transfer.STATE.completed,
});
const res = await request(app)
.post('/')
.send({
tokens: ['1'],
sender_wallet: 1,
receiver_wallet: 2,
claim: true,
});

console.log(res);
expect(res).property('statusCode').eq(201);
});

it("all parameters fine, but no trust relationship, should return 202", async () => {
sinon.stub(WalletService.prototype, "getByIdOrName").resolves(new Wallet(1));
sinon.stub(WalletService.prototype, "getById").resolves({
Expand Down

0 comments on commit 4f37463

Please sign in to comment.