Skip to content

Commit

Permalink
Merge pull request #53 from DevelBlockchain/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
felipemarts authored Sep 11, 2024
2 parents 95e660c + 527ec3e commit ea8c4b5
Show file tree
Hide file tree
Showing 125 changed files with 11,881 additions and 12,079 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test_and_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: 🚀 Test And Build

on:
push:
branches: [ main, devel ]

jobs:
test-and-build:
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ TOKEN | A string of random characters for generating authentication encryption.
node dist/index.js -new-chain local
```

2. Run node:
2. Run node using new local chain (first time run):

```shell
node dist/index.js -log -chain local.json -start
```

3. Run node (without new chain flag, otherwise you'll receive "conflict zero block" error):

```shell
node dist/index.js -log local.json -start
```
23 changes: 23 additions & 0 deletions assets/DvP_stealthaddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import BywiseUtils from 'bywise-utils';

class DVP {

/**
* @external
* @param {address} contract
* @param {bigint} amount
* @param {address} swappedAddress
* @returns {boolean}
*/
giveMeBackHalfOfMyTokens(contract, amount, swappedAddress) {
const dvpContractAddress = BywiseUtils.getThisAddress();
const sender = BywiseUtils.getTxSender();

const token = BywiseUtils.getContract(contract, ['transferFrom', 'transfer']);
token.transferFrom(sender, dvpContractAddress, amount); // received payment
token.transfer(swappedAddress, (parseInt(amount) / 2).toFixed(0)); // send payment
return true;
}
}

BywiseUtils.exportContract(new DVP());
181 changes: 109 additions & 72 deletions assets/ERC20.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,170 @@
import BigNumber from 'bignumber.js';
import BywiseUtils, {StorageValue, StorageMap, StorageList } from 'bywise-utils.js';
import BywiseUtils, { StorageValue, StorageMap } from 'bywise-utils';

const TOKEN_NAME = "SimpleToken";
const TOKEN_SYMBOL = "TKN";
const INITIAL_AMOUNT = "5000";

function checkAmountIsInteger(value) {
if (! /^[0-9]{1,36}$/.test(value)) {
throw new Error(`invalid amount - ${value}`);
}
}

function isValidAddress(value) {
if (! /^(BWS1[MT][CU][0-9a-fA-F]{40}[0-9a-zA-Z]{0,64}[0-9a-fA-F]{3})|(BWS000000000000000000000000000000000000000000000)$/.test(value)) {
throw new Error(`invalid address - ${value}`);
}
}
const INITIAL_AMOUNT = 5000n;

class ERC20 {

_name;
_symbolToken;
_totalSupply = new StorageValue('0');
_balances = new StorageMap('0');
_totalSupply = new StorageValue(0n);
_balances = new StorageMap(0n);
_allowances = new StorageMap();

constructor(name, symbolToken) {
this._name = name;
this._symbolToken = symbolToken;

this._mint(BywiseUtils.getTxSender(), (new BigNumber(INITIAL_AMOUNT).multipliedBy(10 ** this.decimals())));
this._mint(BywiseUtils.getTxSender(), INITIAL_AMOUNT * (10n ** this.decimals()));
}

name() { // @view
/**
* @external
* @view
* @returns {string}
*/
name() {
return this._name;
}

symbol() { // @view
/**
* @external
* @view
* @returns {string}
*/
symbol() {
return this._symbolToken
}

totalSupply() { // @view
/**
* @external
* @view
* @returns {bigint}
*/
totalSupply() {
return this._totalSupply.get();
}

decimals() { // @view
return 18;
}

balanceOf(account) { // @view
isValidAddress(account);
return this._balances.getBigNumber(account);
}

allowance(owner, spender) { // @view
isValidAddress(owner);
isValidAddress(spender);
if (this._allowances.has(owner)) {
return this._allowances.getStorageMap(owner).getBigNumber(spender);
/**
* @external
* @view
* @returns {bigint}
*/
decimals() {
return 18n;
}

/**
* @external
* @view
* @param {address} account
* @returns {bigint}
*/
balanceOf(account) {
return this._balances.get(account);
}

/**
* @external
* @view
* @param {address} owner
* @param {address} spender
* @returns {bigint}
*/
allowance(owner, spender) {
const allowance = this._allowances.get(owner);
if (allowance !== null) {
return allowance.get(spender);
}
return new BigNumber('0');
return 0n;
}

/**
* @external
* @param {address} recipient
* @param {bigint} amount
* @returns {boolean}
*/
transfer(recipient, amount) {
isValidAddress(recipient);
checkAmountIsInteger(amount);

let sender = BywiseUtils.getTxSender();

this._transfer(sender, recipient, amount);
return true;
}

/**
* @external
* @param {address} from
* @param {address} to
* @param {bigint} amount
* @returns {boolean}
*/
transferFrom(from, to, amount) {
isValidAddress(from);
isValidAddress(to);
checkAmountIsInteger(amount);

let spender = BywiseUtils.getTxSender();

this._decreaseAllowance(spender, from, amount);
this._transfer(from, to, amount);
return true;
}

/**
* @external
* @param {address} spender
* @param {bigint} amount
* @returns {boolean}
*/
approve(spender, amount) {
isValidAddress(spender);
checkAmountIsInteger(amount);

let owner = BywiseUtils.getTxSender();

this._approve(spender, owner, amount);
return true;
}

_transfer(from, to, amount) { // @private
amount = new BigNumber(amount);
_transfer(from, to, amount) {
let fromBalance = this._balances.get(from);
if (amount > fromBalance) throw new Error('insuficient funds');
fromBalance -= amount;
this._balances.set(from, fromBalance);

let fromBalance = this._balances.getBigNumber(from);
if (amount.isGreaterThan(fromBalance)) throw new Error('insuficient funds');
this._balances.set(from, fromBalance.minus(amount))

let toBalance = this._balances.getBigNumber(to);
this._balances.set(to, toBalance.plus(amount))
let toBalance = this._balances.get(to);
toBalance += amount;
this._balances.set(to, toBalance)

BywiseUtils.emit("transfer", {
from: from,
to: to,
amount: amount.toString(),
});
}

_mint(recipient, amount) { // @private
isValidAddress(recipient);
checkAmountIsInteger(amount);
_mint(recipient, amount) {
let recipientBalance = this._balances.get(recipient);
recipientBalance += amount;
this._balances.set(recipient, recipientBalance);

amount = new BigNumber(amount);
let totalSupply = this._totalSupply.get();
totalSupply += amount;
this._totalSupply.set(totalSupply);

let recipientBalance = this._balances.getBigNumber(recipient);
this._balances.set(recipient, recipientBalance.plus(amount));
this._totalSupply.set(this._totalSupply.getBigNumber().plus(amount));
BywiseUtils.emit("mint", {
account: recipient,
amount: amount.toString(),
});
}

_approve(spender, owner, amount) { // @private
_approve(spender, owner, amount) {
if (!this._allowances.has(owner)) {
this._allowances.set(owner, new StorageMap('0'));
this._allowances.set(owner, new StorageMap(0n));
}
this._allowances.getStorageMap(owner).set(spender, amount);
this._allowances.get(owner).set(spender, amount);
}

_decreaseAllowance(spender, owner, amount) { // @private
amount = new BigNumber(amount);
const allowance = this.allowance(owner, spender);
if (amount.isGreaterThan(allowance)) throw new Error('decreased allowance below zero');

this._approve(spender, owner, allowance.minus(amount).toString())
_decreaseAllowance(spender, owner, amount) {
let allowance = this.allowance(owner, spender);
if (amount > allowance) throw new Error('decreased allowance below zero');
allowance -= amount;
this._approve(spender, owner, allowance)
}
}

BywiseUtils.exportContract(new ERC20(TOKEN_NAME, TOKEN_SYMBOL));
BywiseUtils.exportContract(new ERC20(TOKEN_NAME, TOKEN_SYMBOL));
Loading

0 comments on commit ea8c4b5

Please sign in to comment.