Fix fee estimate for Stake mediator (#4)

This commit is contained in:
Gerardo Nardelli 2020-05-04 17:35:49 -03:00 committed by GitHub
parent bf15e1c296
commit a31f116ecd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 22 deletions

View File

@ -10,6 +10,7 @@
"start-local": "tsc -w"
},
"dependencies": {
"@burner-wallet/exchange": "^1.1.4",
"@poanet/tokenbridge-bw-exchange": "^0.0.3"
},
"devDependencies": {

View File

@ -0,0 +1,35 @@
export default [
{
constant: true,
inputs: [],
name: 'getFee',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [
{
name: '_value',
type: 'uint256'
}
],
name: 'calculateFee',
outputs: [
{
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
}
]

View File

@ -1,13 +1,63 @@
import { Mediator } from '@poanet/tokenbridge-bw-exchange'
import { Stake, xStake } from '../index'
import BN from 'bn.js'
import { EstimateReturn, ValueTypes } from '@burner-wallet/exchange'
import HomeStakeMediatorAbi from '../abis/HomeStakeMediatorAbi'
import { fromWei, toBN } from 'web3-utils'
// TODO set stake mediator addresses
const homeMediatorAddress = '0x'
const foreignMediatorAddress = '0x'
export default class StakeBridge extends Mediator {
constructor() {
constructor({
assetA = xStake.id,
assetABridge = homeMediatorAddress,
assetB = Stake.id,
assetBBridge = foreignMediatorAddress
}) {
super({
assetA: xStake.id,
assetABridge: '0x', // TODO set address
assetB: Stake.id,
assetBBridge: '0x' // TODO set address
assetA,
assetABridge,
assetB,
assetBBridge
})
}
async estimateAtoB(value: ValueTypes): Promise<EstimateReturn> {
const web3 = this.getExchange()
.getAsset(this.assetA)
.getWeb3()
const userAmount = this._getValue(value)
const contract = new web3.eth.Contract(HomeStakeMediatorAbi, this.assetABridge)
const { feeAmount, feePercentage } = await this.getFee(web3, contract, userAmount)
const finalAmount = toBN(userAmount).sub(feeAmount)
const estimateInfo = feeAmount.isZero()
? null
: `Estimation takes fee charges into consideration. Fee: ${feePercentage}%`
return {
estimate: finalAmount.toString(),
estimateInfo
}
}
async estimateBtoA(value: ValueTypes): Promise<EstimateReturn> {
return {
estimate: this._getValue(value),
estimateInfo: null
}
}
async getFee(web3: any, contract: any, value: any): Promise<{ feeAmount: BN; feePercentage: number }> {
const fee = toBN(await contract.methods.getFee().call())
const feePercentage = Number(fromWei(fee, 'ether')) * 100
const feeAmount = toBN(await contract.methods.calculateFee(value).call())
return {
feeAmount,
feePercentage
}
}
}

View File

@ -5,7 +5,7 @@
"dependencies": {
"@burner-wallet/assets": "^1.1.10",
"@burner-wallet/core": "^1.1.9",
"@burner-wallet/exchange": "^1.1.3",
"@burner-wallet/exchange": "^1.1.4",
"@burner-wallet/metamask-plugin": "^1.0.1",
"@burner-wallet/modern-ui": "^1.0.7",
"@poanet/tokenbridge-bw-exchange": "^0.0.3",

View File

@ -6,7 +6,8 @@ import { InfuraGateway, InjectedGateway } from '@burner-wallet/core/gateways'
import ModernUI from '@burner-wallet/modern-ui'
import Exchange from '@burner-wallet/exchange'
import MetamaskPlugin from '@burner-wallet/metamask-plugin'
import { ERC677Asset, Mediator, TokenBridgeGateway } from '@poanet/tokenbridge-bw-exchange'
import { ERC677Asset, TokenBridgeGateway } from '@poanet/tokenbridge-bw-exchange'
import { StakeBridge } from 'my-plugin'
const sStake = new ERC677Asset({
id: 'assetAtHome',
@ -28,7 +29,7 @@ const kStake = new ERC677Asset({
address: process.env.REACT_APP_FOREIGN_TOKEN_ADDRESS
})
const StakeBridgePair = new Mediator({
const StakeBridgePair = new StakeBridge({
assetA: sStake.id,
// @ts-ignore
assetABridge: process.env.REACT_APP_HOME_MEDIATOR_ADDRESS,
@ -43,9 +44,7 @@ const core = new BurnerCore({
assets: [sStake, kStake]
})
const exchange = new Exchange({
pairs: [StakeBridgePair]
})
const exchange = new Exchange([StakeBridgePair])
const BurnerWallet = () => <ModernUI title="Burner Wallet" core={core} plugins={[exchange, new MetamaskPlugin()]} />

View File

@ -5,7 +5,7 @@
"dependencies": {
"@burner-wallet/assets": "^1.1.10",
"@burner-wallet/core": "^1.1.9",
"@burner-wallet/exchange": "^1.1.3",
"@burner-wallet/exchange": "^1.1.4",
"@burner-wallet/metamask-plugin": "^1.0.1",
"@burner-wallet/modern-ui": "^1.0.7",
"@poanet/tokenbridge-bw-exchange": "^0.0.3",

View File

@ -1215,16 +1215,6 @@
web3 "^1.2.2"
web3-provider-engine "15.0.3"
"@burner-wallet/exchange@^1.1.3":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@burner-wallet/exchange/-/exchange-1.1.3.tgz#18a3d6ad74e6b755318a390aa7936416aa614f05"
integrity sha512-8eWVhT5/Om0JmilsaaIRo4jDbsiTxMH+wwLOjJfZ868i0qIQmdwyvdKqk7PBNz+vwjt+tkpE5XTqRafGcZtyAg==
dependencies:
"@burner-wallet/types" "^1.0.0"
"@types/react" "*"
"@types/styled-components" "4.1.8"
styled-components "^5.0.1"
"@burner-wallet/exchange@^1.1.4":
version "1.1.4"
resolved "https://registry.yarnpkg.com/@burner-wallet/exchange/-/exchange-1.1.4.tgz#8763d9444aaf0cbf715dcbe377819b08fa751d67"