Skip to content

Commit

Permalink
DAO-240: add multichain currency support (#120)
Browse files Browse the repository at this point in the history
* add multichain currency support
  • Loading branch information
yuetloo authored Sep 8, 2021
1 parent 8f34f3c commit e8b06f4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export const DEFAULT_ETH_NODE = 'wss://mainnet.eth.aragon.network/ws'
export const DEFAULT_CURRENCY = {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
7 changes: 5 additions & 2 deletions src/evaluator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Web3Utils from 'web3-utils'
import BN from 'bn.js'
import types from '../types'
import HelperManager from '../helpers/HelperManager'
import { DEFAULT_ETH_NODE } from '../defaults'
import { DEFAULT_ETH_NODE, DEFAULT_CURRENCY } from '../defaults'

/**
* A value coupled with a type
Expand Down Expand Up @@ -54,14 +54,15 @@ class TypedValue {
* @param {radspec/Bindings} bindings An object of bindings and their values
* @param {?Object} options An options object
* @param {?Object} options.availablehelpers Available helpers
* @param {?Object} options.currency Native currency of the blockchain
* @param {?Web3} options.eth Web3 instance (used over options.ethNode)
* @param {?string} options.ethNode The URL to an Ethereum node
* @param {?string} options.to The destination address for this expression's transaction
* @property {radspec/parser/AST} ast
* @property {radspec/Bindings} bindings
*/
export class Evaluator {
constructor (ast, bindings, { availableHelpers = {}, eth, ethNode, from, to, value = '0', data } = {}) {
constructor (ast, bindings, { availableHelpers = {}, eth, ethNode, from, to, value = '0', data, currency } = {}) {
this.ast = ast
this.bindings = bindings
this.eth = eth || new Eth(ethNode || DEFAULT_ETH_NODE)
Expand All @@ -70,6 +71,7 @@ export class Evaluator {
this.value = new TypedValue('uint', new BN(value))
this.data = data && new TypedValue('bytes', data)
this.helpers = new HelperManager(availableHelpers)
this.currency = currency || DEFAULT_CURRENCY
}

/**
Expand Down Expand Up @@ -358,6 +360,7 @@ export class Evaluator {
* @param {?Object} options An options object
* @param {?string} options.ethNode The URL to an Ethereum node
* @param {?string} options.to The destination address for this expression's transaction
* @param {?Object} options.currency The native currency of the blockchain
* @return {string}
*/
export function evaluate (ast, bindings, options) {
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/lib/token.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const ETH = '0x0000000000000000000000000000000000000000'
export const NON_TOKEN_ADDRESS = '0x0000000000000000000000000000000000000000'
export const ETH = NON_TOKEN_ADDRESS

/* eslint-disable key-spacing, quotes */
export const ERC20_SYMBOL_BYTES32_ABI = [
Expand Down
14 changes: 9 additions & 5 deletions src/helpers/tokenAmount.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import BN from 'bn.js'
import { toUtf8 } from 'web3-utils'
import { ERC20_SYMBOL_BYTES32_ABI, ERC20_SYMBOL_DECIMALS_ABI, ETH } from './lib/token'
import {
ERC20_SYMBOL_BYTES32_ABI,
ERC20_SYMBOL_DECIMALS_ABI,
NON_TOKEN_ADDRESS
} from './lib/token'
import { formatBN, tenPow } from './lib/formatBN'

export default (eth) =>
export default (eth, evaluator) =>
/**
* Format token amounts taking decimals into account
*
Expand All @@ -20,10 +24,10 @@ export default (eth) =>
let decimals
let symbol

if (tokenAddress === ETH) {
decimals = new BN(18)
if (tokenAddress === NON_TOKEN_ADDRESS) {
decimals = new BN(evaluator.currency.decimals)
if (showSymbol) {
symbol = 'ETH'
symbol = evaluator.currency.symbol
}
} else {
let token = new eth.Contract(ERC20_SYMBOL_DECIMALS_ABI, tokenAddress)
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { evaluateRaw } from './lib'
* @param {?Web3} options.eth Web3 instance (used over options.ethNode)
* @param {?string} options.ethNode The URL to an Ethereum node
* @param {?Object} options.userHelpers User defined helpers
* @param {?Object} options.currency Native currency of the blockchain
* @return {Promise<string>} The result of the evaluation
*/
function evaluate (source, call, { userHelpers = {}, ...options } = {}) {
Expand Down
9 changes: 4 additions & 5 deletions test/examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ const helperCases = [
bindings: { token: address('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'), balance: int('1000000000000000001') }
}, 'Balance: 1.000000000000000001 DAI'],
[{
source: 'Balance: `@tokenAmount(self.token(): address, balance)`',
bindings: { balance: int('10000000000000000000') },
options: { to: '0xD39902f046B5885D70e9E66594b65f84D4d1c952' }
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { balance: int('10000000000000000000'), token: address('0x960b236A07cf122663c4303350609A66A7B288C0') },
}, 'Balance: 10 ANT'],
[{
source: 'Ethereum launched `@formatDate(date)`',
Expand Down Expand Up @@ -760,10 +759,10 @@ const cases = [
options: { to: '0x960b236A07cf122663c4303350609A66A7B288C0' }
}, 'Burns the ANT balance of 0x0000000000000000000000000000000000000001 (balance is 0)'],
[{
source: 'Send ETH to the sale at block `((self.controller(): address).sale(): address).initialBlock(): uint` from `person`',
source: 'Send ETH to the sale at block `self.creationBlock(): uint` from `person`',
bindings: { person: address('0x0000000000000000000000000000000000000001') },
options: { to: '0x960b236A07cf122663c4303350609A66A7B288C0' }
}, 'Send ETH to the sale at block 3723000 from 0x0000000000000000000000000000000000000001'],
}, 'Send ETH to the sale at block 3711733 from 0x0000000000000000000000000000000000000001'],
[{
source: "Initialize Finance app for Vault at `_vault` with period length of `(_periodDuration - _periodDuration % 86400) / 86400` day`_periodDuration >= 172800 ? 's' : ' '`",
bindings: { _periodDuration: int(86400 * 2), _vault: address('0x960b236A07cf122663c4303350609A66A7B288C0') }
Expand Down

0 comments on commit e8b06f4

Please sign in to comment.