Skip to content

Commit

Permalink
Merge pull request #194 from rsksmart/feature/GBI-1578
Browse files Browse the repository at this point in the history
Feature/GBI-1578 - Add gas fee
  • Loading branch information
MaximStanciu8 committed Jan 4, 2024
2 parents 5e137b1 + 0eb6e30 commit 3bcdb5a
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 61 deletions.
12 changes: 6 additions & 6 deletions contracts/LiquidityBridgeContractV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,10 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
if (callRegistry[quoteHash].success) {
refundAmount = min(
transferredAmount,
quote.value + quote.callFee
quote.value + quote.callFee + quote.gasFee
);
} else {
refundAmount = min(transferredAmount, quote.callFee);
refundAmount = min(transferredAmount, quote.callFee + quote.gasFee);
}
increaseBalance(quote.liquidityProviderRskAddress, refundAmount);

Expand Down Expand Up @@ -640,7 +640,7 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
bytes memory signature
) external payable {
require(isRegisteredForPegout(quote.lpRskAddress), "LBC037");
require(quote.value + quote.callFee + quote.productFeeAmount <= msg.value, "LBC063");
require(quote.value + quote.callFee + quote.productFeeAmount + quote.gasFee <= msg.value, "LBC063");
require(block.timestamp <= quote.depositDateLimit, "LBC065");
require(block.timestamp <= quote.expireDate, "LBC046");
require(block.number <= quote.expireBlock, "LBC047");
Expand Down Expand Up @@ -671,7 +671,7 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
"LBC041"
);

uint valueToTransfer = quote.value + quote.callFee + quote.productFeeAmount;
uint valueToTransfer = quote.value + quote.callFee + quote.productFeeAmount + quote.gasFee;
address addressToTransfer = quote.rskRefundAddress;

uint penalty = min(quote.penaltyFee, pegoutCollateral[quote.lpRskAddress]);
Expand Down Expand Up @@ -799,7 +799,7 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
"LBC054"
);
require(
quote.value + quote.callFee + quote.productFeeAmount >= minPegIn,
quote.value + quote.callFee + quote.productFeeAmount + quote.gasFee >= minPegIn,
"LBC055"
);
require(
Expand Down Expand Up @@ -889,7 +889,7 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
uint256 height
) private view returns (bool) {
// do not penalize if deposit amount is insufficient
if (amount > 0 && uint256(amount) < quote.value + quote.callFee + quote.productFeeAmount) {
if (amount > 0 && uint256(amount) < quote.value + quote.callFee + quote.productFeeAmount + quote.gasFee) {
return false;
}

Expand Down
10 changes: 7 additions & 3 deletions contracts/QuotesV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ library QuotesV2 {
uint16 depositConfirmations;
bool callOnRegister;
uint256 productFeeAmount;
uint256 gasFee;
}

struct PegOutQuote {
Expand All @@ -43,6 +44,7 @@ library QuotesV2 {
uint32 expireDate;
uint32 expireBlock;
uint256 productFeeAmount;
uint256 gasFee;
}

function encodeQuote(
Expand Down Expand Up @@ -90,7 +92,8 @@ library QuotesV2 {
quote.callTime,
quote.depositConfirmations,
quote.callOnRegister,
quote.productFeeAmount
quote.productFeeAmount,
quote.gasFee
);
}

Expand Down Expand Up @@ -124,7 +127,8 @@ library QuotesV2 {
quote.transferTime,
quote.expireDate,
quote.expireBlock,
quote.productFeeAmount
quote.productFeeAmount,
quote.gasFee
);
}

Expand All @@ -133,7 +137,7 @@ library QuotesV2 {
uint transferredAmount
) external pure {
uint agreedAmount = 0;
agreedAmount = quote.value + quote.callFee + quote.productFeeAmount;
agreedAmount = quote.value + quote.callFee + quote.productFeeAmount + quote.gasFee;


uint delta = agreedAmount / 10000;
Expand Down
2 changes: 1 addition & 1 deletion integration-test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function loadConfig() {
return JSON.parse(buffer.toString())
}
async function sendBtc({ toAddress, amountInBtc, rpc, data }) {
const outputs = [ { [toAddress]: amountInBtc } ]
const outputs = [ { [toAddress]: amountInBtc.toString() } ]
const fundOptions = { fee_rate: 25 }
if (data) {
outputs.push({ data })
Expand Down
40 changes: 30 additions & 10 deletions integration-test/pegin.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { expect } = require('chai')

const LiquidityBridgeContract = artifacts.require("LiquidityBridgeContract")
const LiquidityBridgeContract = artifacts.require("LiquidityBridgeContractV2")
const BtcUtils = artifacts.require("BtcUtils")
const bs58check = require('bs58check')
const bs58 = require('bs58')
Expand Down Expand Up @@ -29,8 +29,10 @@ describe('Flyover pegin process should', () => {
let lpAccount
let interval

let config

before(async () => {
const config = await loadConfig()
config = await loadConfig()
interval = config.pollingIntervalInSeconds * 1000
lpAccount = web3.eth.accounts.privateKeyToAccount(config.lpPrivateKey)
const userAccount = web3.eth.accounts.privateKeyToAccount(config.userPrivateKey)
Expand All @@ -47,7 +49,8 @@ describe('Flyover pegin process should', () => {
const timestamp = Math.floor(Date.now() / 1000)
const transferTime = 1800
const gasLimit = await web3.eth.estimateGas({ to: userAddress, data: '0x' })
const gasCost = await web3.eth.getGasPrice().then(price => price * gasLimit)
const gasPrice = await web3.eth.getGasPrice()
const cfuGasCost = gasPrice * gasLimit
const fedAddress = await web3.eth.call({
to: '0x0000000000000000000000000000000001000006',
data: web3.eth.abi.encodeFunctionSignature('getFederationAddress()')
Expand All @@ -58,25 +61,34 @@ describe('Flyover pegin process should', () => {
})
const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)

const productFeePercentage = await lbc.methods.productFeePercentage().call()
const value = BigInt(600000000000000000) // 0.6 eth
const productFee = (BigInt(productFeePercentage) * value) / BigInt(100)
const daoAddress = await lbc.methods.daoFeeCollectorAddress().call()
const daoGas = await web3.eth.estimateGas({ to: daoAddress, value: productFee.toString() })
const daoGasCost = gasPrice * daoGas

quote = {
fedBtcAddress: fedAddress,
lbcAddress: config.lbcAddress,
liquidityProviderRskAddress: lpAddress,
btcRefundAddress: userBtcAddress,
rskRefundAddress: userAddress,
liquidityProviderBtcAddress: lpBtcAddress,
callFee: BigInt(100000000000000 + gasCost), // fee is 0.0001 eth
callFee: BigInt(10000000000000000), // fee is 0.01 eth
penaltyFee: 1000000,
contractAddress: userAddress,
data: '0x',
gasLimit: gasLimit,
nonce: nonce,
value: BigInt(6000000000000000), // 0.006 eth
value: value,
agreementTimestamp: timestamp,
timeForDeposit: transferTime,
callTime: transferTime * 2,
depositConfirmations: 1,
callOnRegister: false
callOnRegister: false,
productFeeAmount: productFee,
gasFee: cfuGasCost + daoGasCost
}


Expand All @@ -98,10 +110,18 @@ describe('Flyover pegin process should', () => {

it('execute registerPegIn', async () => {
const derivationAddress = await getDervivationAddress({ quote, quoteHash, btcUtilsInstance: btcUtils, lbc})

const amountInSatoshi = (quote.value + quote.callFee) / BigInt(10**10)
const amountInBtc = Number(amountInSatoshi) / 10**8

const total = web3.utils.toBN(quote.value.toString())
.add(web3.utils.toBN(quote.callFee.toString()))
.add(web3.utils.toBN(quote.gasFee.toString()))
.add(web3.utils.toBN(quote.productFeeAmount.toString()));
const amountInBtc = web3.utils.fromWei(total, 'ether')

if (config.btc.walletPassphrase) {
await bitcoinRpc('walletpassphrase', {
passphrase: config.btc.walletPassphrase,
timeout: 60
})
}
const txHash = await sendBtc({ rpc: bitcoinRpc, amountInBtc, toAddress: derivationAddress })
const tx = await waitForBtcTransaction({ rpc: bitcoinRpc, hash: txHash, confirmations: quote.depositConfirmations, interval })

Expand Down
32 changes: 24 additions & 8 deletions integration-test/pegout.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { expect } = require('chai')

const LiquidityBridgeContract = artifacts.require("LiquidityBridgeContract")
const LiquidityBridgeContract = artifacts.require("LiquidityBridgeContractV2")
const bs58check = require('bs58check')
const pmtBuilder = require("@rsksmart/pmt-builder")

Expand Down Expand Up @@ -29,8 +29,10 @@ describe('Flyover pegout process should', () => {
let interval
let userBtcEncodedAddress

let config

before(async () => {
const config = await loadConfig()
config = await loadConfig()
interval = config.pollingIntervalInSeconds * 1000
lpAccount = web3.eth.accounts.privateKeyToAccount(config.lpPrivateKey)
userAccount = web3.eth.accounts.privateKeyToAccount(config.userPrivateKey)
Expand All @@ -46,30 +48,38 @@ describe('Flyover pegout process should', () => {

const timestamp = Math.floor(Date.now() / 1000)
const transferTime = 1800, expireDate = 3600
const gasLimit = await web3.eth.estimateGas({ to: userAddress, data: '0x' })
const gasCost = await web3.eth.getGasPrice().then(price => price * gasLimit)
const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
const height = await web3.eth.getBlock("latest").then(block => block.number);

const productFeePercentage = await lbc.methods.productFeePercentage().call()
const value = BigInt(600000000000000000) // 0.6 eth
const productFee = (BigInt(productFeePercentage) * value) / BigInt(100)
const daoAddress = await lbc.methods.daoFeeCollectorAddress().call()
const daoGas = await web3.eth.estimateGas({ to: daoAddress, value: productFee.toString() })
const gasPrice = await web3.eth.getGasPrice()
const daoGasCost = gasPrice * daoGas
const btcNetworkFee = 0.00006700 * 10**18

quote = {
lbcAddress: config.lbcAddress,
lpRskAddress: lpAddress,
btcRefundAddress: userBtcAddress,
rskRefundAddress: userAddress,
lpBtcAddress: lpBtcAddress,
callFee: BigInt(100000000000000 + gasCost), // fee is 0.0001 eth
callFee: BigInt(100000000000000), // fee is 0.0001 eth
penaltyFee: 1000000,
nonce: nonce,
deposityAddress: userBtcAddress,
gasLimit: gasLimit,
value: BigInt(6000000000000000), // 0.006 eth
agreementTimestamp: timestamp,
depositDateLimit: timestamp + transferTime,
depositConfirmations: 1,
transferConfirmations: 1,
transferTime: transferTime,
expireDate: timestamp + expireDate,
expireBlock: height + 50
expireBlock: height + 50,
productFeeAmount: productFee,
gasFee: daoGasCost + btcNetworkFee
}

quoteHash = await lbc.methods.hashPegoutQuote(quote).call()
Expand All @@ -79,7 +89,7 @@ describe('Flyover pegout process should', () => {
it('execute depositPegout', async () => {
const receipt = await sendFromAccount({
account: userAccount,
value: quote.callFee + quote.value,
value: quote.callFee + quote.value + quote.productFeeAmount + BigInt(quote.gasFee),
call: lbc.methods.depositPegout(quote, signedQuote.signature)
})
const parsedReceipt = decodeLogs({ abi: LiquidityBridgeContract.abi, receipt })
Expand All @@ -89,6 +99,12 @@ describe('Flyover pegout process should', () => {
it('execute refundPegOut', async () => {
const amountInSatoshi = (quote.value + quote.callFee) / BigInt(10**10)
const amountInBtc = Number(amountInSatoshi) / 10**8
if (config.btc.walletPassphrase) {
await bitcoinRpc('walletpassphrase', {
passphrase: config.btc.walletPassphrase,
timeout: 60
})
}
const txHash = await sendBtc({
rpc: bitcoinRpc,
amountInBtc,
Expand Down
3 changes: 2 additions & 1 deletion integration-test/test.config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"btc": {
"url": "http://127.0.0.1:5555/",
"user": "test",
"pass": "test"
"pass": "test",
"walletPassphrase": "pass"
}
}
2 changes: 1 addition & 1 deletion migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module.exports = async function (deployer, network) {
state.address = btcUtilsInstance.address;
});

minimumPegIn = 2;
minimumPegIn = 3;
}

let config = read();
Expand Down
Loading

0 comments on commit 3bcdb5a

Please sign in to comment.