Skip to content

Commit

Permalink
feat: add more code for transfer bundle and check if token is already…
Browse files Browse the repository at this point in the history
… claimed
  • Loading branch information
wm798222 committed Feb 19, 2021
1 parent 4f37463 commit 4dbdbb8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions server/models/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Session{
throw new Error("Can not start transaction in transaction");
}
this.thx = await knex.transaction();
console.log('HEREbbba');
}

async commitTransaction(){
Expand Down
22 changes: 19 additions & 3 deletions server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ class Wallet{
*/
async transfer(sender, receiver, tokens, claimBoolean){
// await this.checkDeduct(sender, receiver);
// check is claim is already set to true, if so, we cannot transfer this token.
if (claim === true) {
console.log("token is claimed, cannot be transfered");
throw new HttpError(403, `The token ${uuid} is claimed, cannot be transfered`);
}

//check tokens belong to sender
for(const token of tokens){
if(!await token.belongsTo(sender)){
Expand Down Expand Up @@ -544,7 +550,7 @@ class Wallet{
}
}

async transferBundle(sender, receiver, bundleSize){
async transferBundle(sender, receiver, bundleSize, claimBoolean){
//check has enough tokens to sender
const tokenCount = await this.tokenService.countTokenByWallet(sender);
expect(tokenCount).number();
Expand All @@ -570,13 +576,23 @@ class Wallet{
bundle: {
bundleSize: bundleSize,
}
}
},
//TODO: boolean for claim
claim: claimBoolean,
});
log.debug("now, deal with tokens");
const tokens = await this.tokenService.getTokensByBundle(sender, bundleSize)



for(let token of tokens){
await token.completeTransfer(transfer);
// check is claim is already set to true, if so, we cannot transfer this token.
if (token.claim === true) {
console.log("token is claimed, cannot be transfered");
throw new HttpError(403, `The token ${uuid} is claimed, cannot be transfered`);
} else {
await token.completeTransfer(transfer);
}
}
return transfer;
}else{
Expand Down
21 changes: 21 additions & 0 deletions server/models/Wallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,27 @@ describe("Wallet", () => {
fn1.restore();
});

it.only("Claim set to true, should success", async () => {
const fn0 = sinon.stub(Token.prototype, "belongsTo").resolves(true);
// const fn1 = sinon.stub(TransferRepository.prototype, "create").resolves({
// id: 1,
// state: Transfer.STATE.completed,

// });
const fn1 = sinon.stub(wallet, "hasTrust").resolves(true);
const sender = new Wallet(1, session);
const receiver = new Wallet(2, session);
const token = new Token({
id: 1,
uuid: "uu",
transfer_pending: false,
}, session);
const transfer = await wallet.transfer(sender, receiver, [token], true);
expect(transfer).property("state").eq(Transfer.STATE.completed);
fn0.restore();
fn1.restore();
});

it("don't have trust, sender under control, should return transfer with pending state", async () => {
sinon.stub(Wallet.prototype, "isDeduct").resolves(false);
const fn3 = sinon.stub(Wallet.prototype, "hasControlOver");
Expand Down
10 changes: 9 additions & 1 deletion server/routes/transferRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,25 @@ transferRouter.post(
}),
})
);
console.log('HERE0');
const session = new Session();

//begin transaction
try{
console.log('HERE00');
await session.beginTransaction();
console.log('HERE000');
const walletService = new WalletService(session);
const walletLogin = await walletService.getById(res.locals.wallet_id);

console.log('HERE001');
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;
//TODO: put the claim boolean into each tokens
if(req.body.tokens){
const tokens = [];
const tokenService = new TokenService(session);
Expand All @@ -73,8 +79,10 @@ transferRouter.post(
}else{
//Case 2: with trust, bundle transfer
// TODO: get only transferrable tokens
result = await walletLogin.transferBundle(walletSender, walletReceiver, req.body.bundle.bundle_size);
console.log('HERE2');
result = await walletLogin.transferBundle(walletSender, walletReceiver, req.body.bundle.bundle_size, claim);
}
// console.log('HERE3');
const transferService = new TransferService(session);
result = await transferService.convertToResponse(result);
if(result.state === Transfer.STATE.completed){
Expand Down
5 changes: 5 additions & 0 deletions server/routes/transferRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("transferRouter", () => {
}

beforeEach(() => {
sinon.stub(Session.prototype);
sinon.stub(ApiKeyService.prototype, "check");
sinon.stub(JWTService.prototype, "verify").returns({
id: walletLogin.id,
Expand Down Expand Up @@ -210,6 +211,7 @@ describe("transferRouter", () => {
id: 1,
state: Transfer.STATE.completed,
});

const res = await request(app)
.post('/')
.send({
Expand All @@ -223,6 +225,9 @@ describe("transferRouter", () => {
expect(res).property('statusCode').eq(201);
});

//check what's in the db, after transfer claim
// test transfer, in wallet.spec.js

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 4dbdbb8

Please sign in to comment.