-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from DevelBlockchain/devel
Devel
- Loading branch information
Showing
125 changed files
with
11,881 additions
and
12,079 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ name: 🚀 Test And Build | |
|
||
on: | ||
push: | ||
branches: [ main, devel ] | ||
|
||
jobs: | ||
test-and-build: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Oops, something went wrong.