Skip to content

Commit

Permalink
Merge pull request #36 from provable-things/adds-allowance-check
Browse files Browse the repository at this point in the history
feat(peg-ins): <- adds allowance check to that
  • Loading branch information
gskapka authored Jul 6, 2022
2 parents 6dbd8d9 + 048c505 commit 874351f
Show file tree
Hide file tree
Showing 7 changed files with 3,891 additions and 3,248 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ const main = _ => {
}
}

main().catch(_err => console.error('✘', _err.message))
main().catch(_err => console.error('✘', _err.message) || process.exit(1))
29 changes: 29 additions & 0 deletions lib/check-allowance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { BigNumber } = require('ethers')
const { logic } = require('ptokens-utils')
const { getTokenContract } = require('./get-token-contract')

const TIMEOUT_TIME = 3000

const checkAllowanceIsSufficient = (
_amount,
_spenderAddress,
_ownerAddress,
_tokenAddress,
_signer,
) =>
console.info(`✔ Checking spender ${_spenderAddress} has sufficient allowance for token ${_tokenAddress}...`) ||
logic.racePromise(
TIMEOUT_TIME,
getTokenContract(_tokenAddress, _signer).allowance,
[ _ownerAddress, _spenderAddress ],
)
.then(_allowance => {
if (_allowance.lt(BigNumber.from(_amount))) {
return Promise.reject(new Error(`Insufficient allowance to peg in! Got ${_allowance}, need ${_amount}!`))
} else {
console.info(`✔ Allowance of ${_allowance} is sufficient!`)
return
}
})

module.exports = { checkAllowanceIsSufficient }
25 changes: 18 additions & 7 deletions lib/check-endpoint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
const checkEndpoint = _provider =>
console.info('✔ Checking endpoint...') ||
_provider
.send('eth_blockNumber', [])
.then(_blockNumber =>
console.info(`✔ Endpoint working! Block height: ${parseInt(_blockNumber, 'hex')}`) || _provider
)
const checkEndpoint = _provider => {
console.info('✔ Checking endpoint...')
let timeoutReference
const TIMEOUT_TIME = 2000

const rejectAfterX = _x =>
/* eslint-disable-next-line no-return-assign */
new Promise((resolve, reject) => timeoutReference = setTimeout(reject, _x, new Error('Timed out!')))

// NOTE: We can't use `ptokens-utils` `racePromise` for this because `ethers` is a hymn to OOP style
// programming. And so when we pass the promise fxn we want (`_provider.send`), then the `racePromise`
// tries to call it, we lose all reference to `this` inside `_provider.send`, which then throws an error.

return Promise.race([ _provider.send('eth_blockNumber', []), rejectAfterX(TIMEOUT_TIME) ])
.then(_blockNumberHex => parseInt(_blockNumberHex, 'hex'))
.then(_blockNumber => console.info(`✔ Endpoint working! Block height: ${_blockNumber}`) || _provider)
.catch(_err => Promise.reject(new Error(`Endpoint error: ${_err.message}`)))
.finally(_ => clearTimeout(timeoutReference))
}

module.exports = { checkEndpoint }
20 changes: 20 additions & 0 deletions lib/get-token-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable-next-line no-shadow */
const ethers = require('ethers')

const TOKEN_ABI = [{
'inputs': [
{ 'internalType': 'address', 'name': 'holder', 'type': 'address' },
{ 'internalType': 'address', 'name': 'spender', 'type': 'address' }
],
'name': 'allowance',
'outputs': [ { 'internalType': 'uint256', 'name': '', 'type': 'uint256' } ],
'stateMutability': 'view',
'type': 'function'
}]

const getTokenContract = (_address, _signer) => {
console.info(`✔ Getting token contract @ '${_address}'...`)
return new ethers.Contract(_address, TOKEN_ABI, _signer)
}

module.exports = { getTokenContract }
32 changes: 24 additions & 8 deletions lib/peg-in.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { getVaultContract } = require('./get-vault-contract')
const { checkAllowanceIsSufficient } = require('./check-allowance')
const { callFxnInContractAndAwaitReceipt } = require('./contract-utils')

const pegIn = (
Expand All @@ -8,14 +9,29 @@ const pegIn = (
_destinationAddress,
_destinactionChainId,
_userData = '0x',
) => {
console.info('✔ Pegging in...')
return getVaultContract(_deployedContractAddress)
.then(callFxnInContractAndAwaitReceipt(
'pegIn(uint256,address,string,bytes,bytes4)',
[ _amount, _tokenAddress, _destinationAddress, _userData, _destinactionChainId ]
))
) =>
console.info('✔ Pegging in...') ||
getVaultContract(_deployedContractAddress)
.then(_vaultContact => Promise.all([ _vaultContact, _vaultContact.signer.getAddress() ]))
.then(([ _vaultContract, _ownerAddress ]) =>
Promise.all([
_vaultContract,
checkAllowanceIsSufficient(
_amount,
_deployedContractAddress, // NOTE: This is the "spender" address, the vault in this case!
_ownerAddress,
_tokenAddress,
_vaultContract.provider,
)
])
)
.then(([ _vaultContract ]) =>
callFxnInContractAndAwaitReceipt(
'pegIn(uint256,address,string,bytes,bytes4)',
[ _amount, _tokenAddress, _destinationAddress, _userData, _destinactionChainId ],
_vaultContract,
)
)
.then(_receipt => console.info('✔ Success! Transaction receipt:\n', _receipt))
}

module.exports = { pegIn }
Loading

0 comments on commit 874351f

Please sign in to comment.