Skip to content

Commit cf1f669

Browse files
committed
merge commit
2 parents 52d9599 + ffe0aad commit cf1f669

File tree

161 files changed

+8899
-5520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+8899
-5520
lines changed

package-lock.json

Lines changed: 2028 additions & 3610 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitcoin-computer",
3-
"version": "0.21.0-beta.0",
3+
"version": "0.22.0-beta.0",
44
"description": "Lightweight Smart Contracts for Bitcoin and Litecoin",
55
"contributors": [
66
"Clemens Ley",
@@ -38,6 +38,7 @@
3838
"@endo/static-module-record": "^1.0.4"
3939
},
4040
"devDependencies": {
41+
"@types/dotenv": "^8.2.3",
4142
"@types/node": "^20.11.21",
4243
"edit-json-file": "^1.8.0",
4344
"eslint": "^8.57.0",

packages/TBC20/build/src/token.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export declare class Token extends Contract {
44
symbol: string;
55
_owners: string[];
66
constructor(to: string, amount: number, name: string, symbol?: string);
7-
transfer(to: string, amount?: number): Token | null;
7+
transfer(to: string, amount?: number): Token | undefined;
8+
burn(): void;
9+
merge(tokens: Token[]): void;
810
}
911
export interface ITBC20 {
1012
deploy(): Promise<string>;
@@ -13,7 +15,7 @@ export interface ITBC20 {
1315
balanceOf(publicKey: string, root: string): Promise<number>;
1416
transfer(to: string, amount: number, root: string): Promise<void>;
1517
}
16-
export declare class TBC20 implements ITBC20 {
18+
export declare class TokenHelper implements ITBC20 {
1719
name: string;
1820
symbol: string;
1921
computer: any;

packages/TBC20/build/src/token.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@ export class Token extends Contract {
55
transfer(to, amount) {
66
if (typeof amount === 'undefined') {
77
this._owners = [to];
8-
return null;
8+
return undefined;
99
}
1010
if (this.amount >= amount) {
1111
this.amount -= amount;
1212
return new Token(to, amount, this.name, this.symbol);
1313
}
1414
throw new Error('Insufficient funds');
1515
}
16+
burn() {
17+
this.amount = 0;
18+
}
19+
merge(tokens) {
20+
let total = 0;
21+
tokens.forEach((token) => {
22+
total += token.amount;
23+
token.burn();
24+
});
25+
this.amount += total;
26+
}
1627
}
17-
export class TBC20 {
28+
export class TokenHelper {
1829
constructor(computer, mod) {
1930
this.computer = computer;
2031
this.mod = mod;

packages/TBC20/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"prettier": "prettier --write \"src/**/*.{js,jsx,ts,tsx,css,scss,md}\"",
2929
"types": "tsc",
3030
"test": "mocha --config .mocharc.json",
31-
"test-and-show": "npm run test 2>&1 | tee tbc20-test.log && open tbc20-test.log"
31+
"test-and-show": "npm run test 2>&1 | tee TBC20-test.log && open TBC20-test.log"
3232
},
3333
"author": "Clemens Ley <clemens@bitcoincomputer.io>",
3434
"license": "Apache-2.0",

packages/TBC20/src/token.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-disable max-classes-per-file */
2+
export class Token extends Contract {
3+
constructor(to, amount, name, symbol = '') {
4+
super({ _owners: [to], amount, name, symbol });
5+
}
6+
transfer(to, amount) {
7+
if (typeof amount === 'undefined') {
8+
// Send entire amount
9+
this._owners = [to];
10+
return undefined;
11+
}
12+
if (this.amount >= amount) {
13+
// Send partial amount in a new object
14+
this.amount -= amount;
15+
return new Token(to, amount, this.name, this.symbol);
16+
}
17+
throw new Error('Insufficient funds');
18+
}
19+
burn() {
20+
this.amount = 0;
21+
}
22+
merge(tokens) {
23+
let total = 0;
24+
tokens.forEach((token) => {
25+
total += token.amount;
26+
token.burn();
27+
});
28+
this.amount += total;
29+
}
30+
}
31+
export class TokenHelper {
32+
constructor(computer, mod) {
33+
this.computer = computer;
34+
this.mod = mod;
35+
}
36+
async deploy() {
37+
this.mod = await this.computer.deploy(`export ${Token}`);
38+
return this.mod;
39+
}
40+
async mint(publicKey, amount, name, symbol) {
41+
const args = [publicKey, amount, name, symbol];
42+
const token = await this.computer.new(Token, args, this.mod);
43+
return token._root;
44+
}
45+
async totalSupply(root) {
46+
const rootBag = await this.computer.sync(root);
47+
return rootBag.amount;
48+
}
49+
async getBags(publicKey, root) {
50+
const revs = await this.computer.query({ publicKey, mod: this.mod });
51+
const bags = await Promise.all(revs.map(async (rev) => this.computer.sync(rev)));
52+
return bags.flatMap((bag) => (bag._root === root ? [bag] : []));
53+
}
54+
async balanceOf(publicKey, root) {
55+
if (typeof root === 'undefined')
56+
throw new Error('Please pass a root into balanceOf.');
57+
const bags = await this.getBags(publicKey, root);
58+
return bags.reduce((prev, curr) => prev + curr.amount, 0);
59+
}
60+
async transfer(to, amount, root) {
61+
let _amount = amount;
62+
const owner = this.computer.getPublicKey();
63+
const bags = await this.getBags(owner, root);
64+
const results = [];
65+
while (_amount > 0 && bags.length > 0) {
66+
const [bag] = bags.splice(0, 1);
67+
const available = Math.min(_amount, bag.amount);
68+
// eslint-disable-next-line no-await-in-loop
69+
results.push(await bag.transfer(to, available));
70+
_amount -= available;
71+
}
72+
if (_amount > 0)
73+
throw new Error('Could not send entire amount');
74+
await Promise.all(results);
75+
}
76+
}

packages/TBC20/src/token.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,32 @@ export class Token extends Contract {
1010
super({ _owners: [to], amount, name, symbol })
1111
}
1212

13-
transfer(to: string, amount?: number): Token | null {
14-
// Send entire amount
13+
transfer(to: string, amount?: number): Token | undefined {
1514
if (typeof amount === 'undefined') {
15+
// Send entire amount
1616
this._owners = [to]
17-
return null
17+
return undefined
1818
}
19-
// Send partial amount in a new object
2019
if (this.amount >= amount) {
20+
// Send partial amount in a new object
2121
this.amount -= amount
2222
return new Token(to, amount, this.name, this.symbol)
2323
}
24-
2524
throw new Error('Insufficient funds')
2625
}
26+
27+
burn() {
28+
this.amount = 0
29+
}
30+
31+
merge(tokens: Token[]) {
32+
let total = 0
33+
tokens.forEach((token) => {
34+
total += token.amount
35+
token.burn()
36+
})
37+
this.amount += total
38+
}
2739
}
2840

2941
export interface ITBC20 {
@@ -34,7 +46,7 @@ export interface ITBC20 {
3446
transfer(to: string, amount: number, root: string): Promise<void>
3547
}
3648

37-
export class TBC20 implements ITBC20 {
49+
export class TokenHelper implements ITBC20 {
3850
name: string
3951
symbol: string
4052
computer: any

0 commit comments

Comments
 (0)