-
Notifications
You must be signed in to change notification settings - Fork 3
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 #36 from provable-things/adds-allowance-check
feat(peg-ins): <- adds allowance check to that
- Loading branch information
Showing
7 changed files
with
3,891 additions
and
3,248 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
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,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 } |
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,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 } |
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,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 } |
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
Oops, something went wrong.