Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15,328 changes: 7,231 additions & 8,097 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/TBC20/build/src/token.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export declare class Token extends Contract {
symbol: string;
_owners: string[];
constructor(to: string, amount: number, name: string, symbol?: string);
transfer(to: string, amount?: number): Token | null;
transfer(to: string, amount?: number): Token | undefined;
burn(): void;
merge(tokens: Token[]): void;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/TBC20/build/src/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ export class Token extends Contract {
transfer(to, amount) {
if (typeof amount === 'undefined') {
this._owners = [to];
return undefined;
}
else if (this.amount >= amount) {
if (this.amount >= amount) {
this.amount -= amount;
return new Token(to, amount, this.name, this.symbol);
}
else {
throw new Error('Insufficient funds');
}
throw new Error('Insufficient funds');
}
burn() {
this.amount = 0;
Expand Down
76 changes: 76 additions & 0 deletions packages/TBC20/src/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable max-classes-per-file */
export class Token extends Contract {
constructor(to, amount, name, symbol = '') {
super({ _owners: [to], amount, name, symbol });
}
transfer(to, amount) {
if (typeof amount === 'undefined') {
// Send entire amount
this._owners = [to];
return undefined;
}
if (this.amount >= amount) {
// Send partial amount in a new object
this.amount -= amount;
return new Token(to, amount, this.name, this.symbol);
}
throw new Error('Insufficient funds');
}
burn() {
this.amount = 0;
}
merge(tokens) {
let total = 0;
tokens.forEach((token) => {
total += token.amount;
token.burn();
});
this.amount += total;
}
}
export class TokenHelper {
constructor(computer, mod) {
this.computer = computer;
this.mod = mod;
}
async deploy() {
this.mod = await this.computer.deploy(`export ${Token}`);
return this.mod;
}
async mint(publicKey, amount, name, symbol) {
const args = [publicKey, amount, name, symbol];
const token = await this.computer.new(Token, args, this.mod);
return token._root;
}
async totalSupply(root) {
const rootBag = await this.computer.sync(root);
return rootBag.amount;
}
async getBags(publicKey, root) {
const revs = await this.computer.query({ publicKey, mod: this.mod });
const bags = await Promise.all(revs.map(async (rev) => this.computer.sync(rev)));
return bags.flatMap((bag) => (bag._root === root ? [bag] : []));
}
async balanceOf(publicKey, root) {
if (typeof root === 'undefined')
throw new Error('Please pass a root into balanceOf.');
const bags = await this.getBags(publicKey, root);
return bags.reduce((prev, curr) => prev + curr.amount, 0);
}
async transfer(to, amount, root) {
let _amount = amount;
const owner = this.computer.getPublicKey();
const bags = await this.getBags(owner, root);
const results = [];
while (_amount > 0 && bags.length > 0) {
const [bag] = bags.splice(0, 1);
const available = Math.min(_amount, bag.amount);
// eslint-disable-next-line no-await-in-loop
results.push(await bag.transfer(to, available));
_amount -= available;
}
if (_amount > 0)
throw new Error('Could not send entire amount');
await Promise.all(results);
}
}
240 changes: 240 additions & 0 deletions packages/TBC20/test/token.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/* eslint-disable no-unused-expressions */
import { expect } from 'chai';
import { Computer } from '@bitcoin-computer/lib';
import dotenv from 'dotenv';
import { Token, TokenHelper } from '../src/token';
dotenv.config({ path: '../node/.env' });
const url = process.env.BCN_URL;
const chain = process.env.BCN_CHAIN;
const network = process.env.BCN_NETWORK;
function sleep(delay) {
return new Promise((resolve) => {
setTimeout(resolve, delay);
});
}
const sender = new Computer({ url, chain, network });
const receiver = new Computer({ url, chain, network });
before(async () => {
await sender.faucet(1e8);
await receiver.faucet(1e8);
});
describe('Token', async () => {
let token1;
describe('mint', async () => {
it('Sender mints 3 tokens', async () => {
token1 = await sender.new(Token, [sender.getPublicKey(), 3, 'test']);
});
it('The meta data should be set', async () => {
expect(token1.amount).to.eq(3);
expect(token1._owners).deep.equal([sender.getPublicKey()]);
expect(token1.name).to.eq('test');
expect(token1.symbol).to.eq('');
expect(token1._id).to.be.a('string');
expect(token1._rev).to.be.a('string');
expect(token1._root).to.be.a('string');
});
});
describe('transfer, merge and burn', () => {
let token2;
let token2After;
it('Sender transfers 1 token to Receiver', async () => {
token2 = (await token1.transfer(receiver.getPublicKey(), 1));
});
it('The meta data of token should be set correctly', () => {
expect(token1.amount).to.eq(2);
expect(token1._owners).deep.equal([sender.getPublicKey()]);
expect(token1.name).to.eq('test');
expect(token1.symbol).to.eq('');
expect(token1._id).to.be.a('string');
expect(token1._rev).to.be.a('string');
expect(token1._root).to.be.a('string');
});
it('The meta data of newToken should be set correctly', () => {
expect(token2.amount).to.eq(1);
expect(token2._owners).deep.equal([receiver.getPublicKey()]);
expect(token2.name).to.eq('test');
expect(token2.symbol).to.eq('');
expect(token2._id).to.be.a('string');
expect(token2._rev).to.be.a('string');
expect(token2._root).to.be.a('string');
});
it('computer.query should return the tokens', async () => {
const senderRevs = await sender.query({ publicKey: sender.getPublicKey() });
expect(senderRevs.length).eq(1);
const senderToken = (await sender.sync(senderRevs[0]));
expect(senderToken.amount).eq(2);
const receiverRevs = await receiver.query({ publicKey: receiver.getPublicKey() });
expect(receiverRevs.length).eq(1);
const receiverTokens = (await receiver.sync(receiverRevs[0]));
expect(receiverTokens.amount).eq(1);
});
it('Receiver send token2 back to sender', async () => {
// Here we would like to call
//
// await token2.transfer(sender.getPublicKey())
//
// However, the wallet associated with of token2 is sender as token2 was created by
// token1 which was created by sender. As token2 is owned by receiver the transaction
// would be rejected as sender cannot spend an output owned by receiver.
//
// We could execute the call by creating an object associated with receiver.
//
// const token2Receiver = (await receiver.sync(token2._rev)) as any
// await token2Receiver.transfer(sender.getPublicKey())
//
// Alternatively we can use encodeCall
const { tx, effect } = await receiver.encodeCall({
target: token2,
property: 'transfer',
args: [sender.getPublicKey()]
});
await receiver.broadcast(tx);
const { env } = effect;
const { __bc__ } = env;
token2After = __bc__;
expect(token1._owners).deep.eq([sender.getPublicKey()]);
expect(token2After._owners).deep.eq([sender.getPublicKey()]);
expect(token2._id).eq(token2After._id);
expect(token2._rev).not.eq(token2After._rev);
expect(await sender.query({ ids: [token1._id] })).deep.eq([token1._rev]);
expect(await sender.query({ ids: [token2._id] })).deep.eq([token2After._rev]);
});
it('Sender merges their two tokens', async () => {
await token1.merge([token2After]);
expect(token1.amount).eq(3);
expect(token2After.amount).eq(0);
});
it('Should burn a token', async () => {
await token1.burn();
expect(token1.amount).eq(0);
await token2.burn();
expect(token2.amount).eq(0);
});
it('Should update the revisions correctly', async () => {
const computer = new Computer();
await computer.faucet(2e8);
const t1 = await computer.new(Token, [computer.getPublicKey(), 3, 'test']);
const rev1 = t1._rev;
const t2 = await t1.transfer(computer.getPublicKey(), 1);
expect(t1._rev).not.eq(rev1);
const rev2 = t2._rev;
await t2.transfer(computer.getPublicKey());
expect(t2._rev).not.eq(rev2);
});
});
});
describe('TokenHelper', () => {
describe('mint', () => {
const tokenHelper = new TokenHelper(sender);
let root;
it('Should create the tokenHelper object', async () => {
const publicKey = tokenHelper.computer.getPublicKey();
root = await tokenHelper.mint(publicKey, 200, 'test', 'TST');
expect(root).not.to.be.undefined;
expect(typeof root).to.eq('string');
expect(root.length).to.be.greaterThan(64);
});
it('Should mint a root token', async () => {
const rootToken = await sender.sync(root);
expect(rootToken).not.to.be.undefined;
expect(rootToken._id).to.eq(root);
expect(rootToken._rev).to.eq(root);
expect(rootToken._root).to.eq(root);
expect(rootToken.amount).to.eq(200);
expect(rootToken.name).to.eq('test');
expect(rootToken.symbol).to.eq('TST');
});
});
describe('totalSupply', () => {
it('Should return the supply of tokens', async () => {
const tokenHelper = new TokenHelper(sender);
const publicKey = tokenHelper.computer.getPublicKey();
const root = await tokenHelper.mint(publicKey, 200, 'test', 'TST');
const supply = await tokenHelper.totalSupply(root);
expect(supply).to.eq(200);
});
});
describe('balanceOf', () => {
it('Should throw an error if the root is not set', async () => {
const publicKeyString = sender.getPublicKey();
const tokenHelper = new TokenHelper(sender);
expect(tokenHelper).not.to.be.undefined;
try {
await tokenHelper.balanceOf(publicKeyString, undefined);
expect(true).to.eq(false);
}
catch (err) {
expect(err.message).to.eq('Please pass a root into balanceOf.');
}
});
it('Should compute the balance', async () => {
const tokenHelper = new TokenHelper(sender);
const publicKey = tokenHelper.computer.getPublicKey();
const root = await tokenHelper.mint(publicKey, 200, 'test', 'TST');
await sleep(200);
const res = await tokenHelper.balanceOf(publicKey, root);
expect(res).to.eq(200);
});
});
describe('transfer', () => {
it('Should transfer a token', async () => {
const computer2 = new Computer();
const tokenHelper = new TokenHelper(sender);
const publicKey = tokenHelper.computer.getPublicKey();
const root = await tokenHelper.mint(publicKey, 200, 'test', 'TST');
await sleep(200);
await tokenHelper.transfer(computer2.getPublicKey(), 20, root);
await sleep(200);
const res = await tokenHelper.balanceOf(publicKey, root);
expect(res).to.eq(180);
});
it('Should transfer random amounts to different people', async () => {
const computer2 = new Computer();
const computer3 = new Computer();
const tokenHelper = new TokenHelper(sender);
const publicKey = tokenHelper.computer.getPublicKey();
const root = await tokenHelper.mint(publicKey, 200, 'multiple', 'MULT');
const amount2 = Math.floor(Math.random() * 100);
const amount3 = Math.floor(Math.random() * 100);
await sleep(200);
await tokenHelper.transfer(computer2.getPublicKey(), amount2, root);
await sleep(200);
await tokenHelper.transfer(computer3.getPublicKey(), amount3, root);
await sleep(200);
const res = await tokenHelper.balanceOf(publicKey, root);
expect(res).to.eq(200 - amount2 - amount3);
const res2 = await tokenHelper.balanceOf(computer2.getPublicKey(), root);
expect(res2).to.eq(amount2);
const res3 = await tokenHelper.balanceOf(computer3.getPublicKey(), root);
expect(res3).to.eq(amount3);
});
it('Should fail if the amount is greater than the balance', async () => {
const computer2 = new Computer();
const tokenHelper = new TokenHelper(sender);
const publicKey = tokenHelper.computer.getPublicKey();
const root = await tokenHelper.mint(publicKey, 200, 'test', 'TST');
await sleep(200);
try {
await tokenHelper.transfer(computer2.getPublicKey(), 201, root);
expect(true).to.eq('false');
}
catch (err) {
expect(err.message).to.eq('Could not send entire amount');
}
it('Should fail if the amount is greater than the balance', async () => {
const computer3 = new Computer({ url, chain, network });
const tbc20 = new TokenHelper(sender);
const pKey = tbc20.computer.getPublicKey();
const r = await tbc20.mint(pKey, 200, 'test', 'TST');
await sleep(200);
try {
await tbc20.transfer(computer3.getPublicKey(), 201, r);
expect(true).to.eq('false');
}
catch (err) {
expect(err.message).to.eq('Could not send entire amount');
}
});
});
});
});
2 changes: 1 addition & 1 deletion packages/chat/src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export function Chat({ chatId }: { chatId: string }) {
})
setMessages(messagesData)
showLoader(false)
} catch (error) {
} catch {
showLoader(false)
showSnackBar("Not a valid Chat", false)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/chat/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function NotLoggedMenu() {
localStorage.setItem("NETWORK", network)
setDropDownLabel(formatChainAndNetwork(chain, network))
window.location.href = "/"
} catch (err) {
} catch {
showSnackBar("Error setting chain and network", false)
Modal.get(modalId).show()
}
Expand Down
Loading