forked from vechain/token-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: auto validate read-only calls (vechain#161)
* chore: auto validate read-only calls * feat: auto add name, decimals and symbol * fix: validate provided info * revert readme * Update README.md * Update script.js * Update contract.js --------- Co-authored-by: libotony <[email protected]>
- Loading branch information
1 parent
b6a9721
commit d1174ee
Showing
18 changed files
with
305 additions
and
50 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,178 @@ | ||
const totalSupply = { | ||
constant: true, | ||
inputs: [], | ||
name: "totalSupply", | ||
outputs: [ | ||
{ | ||
name: "", | ||
type: "uint256", | ||
}, | ||
], | ||
payable: false, | ||
stateMutability: "view", | ||
type: "function", | ||
}; | ||
|
||
const balanceOf = { | ||
constant: true, | ||
inputs: [ | ||
{ | ||
name: "_owner", | ||
type: "address", | ||
}, | ||
], | ||
name: "balanceOf", | ||
outputs: [ | ||
{ | ||
name: "balance", | ||
type: "uint256", | ||
}, | ||
], | ||
payable: false, | ||
stateMutability: "view", | ||
type: "function", | ||
}; | ||
|
||
const allowance = { | ||
constant: true, | ||
inputs: [ | ||
{ | ||
name: "_owner", | ||
type: "address", | ||
}, | ||
{ | ||
name: "_spender", | ||
type: "address", | ||
}, | ||
], | ||
name: "allowance", | ||
outputs: [ | ||
{ | ||
name: "remaining", | ||
type: "uint256", | ||
}, | ||
], | ||
payable: false, | ||
stateMutability: "view", | ||
type: "function", | ||
}; | ||
|
||
const decimals = { | ||
constant: true, | ||
inputs: [], | ||
name: "decimals", | ||
outputs: [ | ||
{ | ||
name: "", | ||
type: "uint8", | ||
}, | ||
], | ||
payable: false, | ||
stateMutability: "view", | ||
type: "function", | ||
}; | ||
|
||
const name = { | ||
constant: true, | ||
inputs: [], | ||
name: "name", | ||
outputs: [ | ||
{ | ||
name: "", | ||
type: "string", | ||
}, | ||
], | ||
payable: false, | ||
stateMutability: "view", | ||
type: "function", | ||
}; | ||
|
||
const symbol = { | ||
constant: true, | ||
inputs: [], | ||
name: "symbol", | ||
outputs: [ | ||
{ | ||
name: "", | ||
type: "string", | ||
}, | ||
], | ||
payable: false, | ||
stateMutability: "view", | ||
type: "function", | ||
}; | ||
|
||
const transfer = { | ||
constant: false, | ||
inputs: [ | ||
{ | ||
name: "_to", | ||
type: "address", | ||
}, | ||
{ | ||
name: "_value", | ||
type: "uint256", | ||
}, | ||
], | ||
name: "transfer", | ||
outputs: [], | ||
payable: false, | ||
stateMutability: "nonpayable", | ||
type: "function", | ||
}; | ||
|
||
const transferFrom = { | ||
constant: false, | ||
inputs: [ | ||
{ | ||
name: "_from", | ||
type: "address", | ||
}, | ||
{ | ||
name: "_to", | ||
type: "address", | ||
}, | ||
{ | ||
name: "_value", | ||
type: "uint256", | ||
}, | ||
], | ||
name: "transferFrom", | ||
outputs: [], | ||
payable: false, | ||
stateMutability: "nonpayable", | ||
type: "function", | ||
}; | ||
|
||
const approve = { | ||
constant: false, | ||
inputs: [ | ||
{ | ||
name: "_spender", | ||
type: "address", | ||
}, | ||
{ | ||
name: "_value", | ||
type: "uint256", | ||
}, | ||
], | ||
name: "approve", | ||
outputs: [], | ||
payable: false, | ||
stateMutability: "nonpayable", | ||
type: "function", | ||
}; | ||
|
||
const abis = { | ||
totalSupply, | ||
balanceOf, | ||
allowance, | ||
decimals, | ||
name, | ||
symbol, | ||
transfer, | ||
transferFrom, | ||
approve, | ||
}; | ||
|
||
module.exports = abis; |
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,86 @@ | ||
const abis = require("./abis"); | ||
const { abi, address } = require("thor-devkit"); | ||
const axios = require("axios"); | ||
|
||
const totalSupplyAbi = new abi.Function(abis.totalSupply); | ||
const balanceOfAbi = new abi.Function(abis.balanceOf); | ||
const allowanceAbi = new abi.Function(abis.allowance); | ||
const decimalsAbi = new abi.Function(abis.decimals); | ||
const nameAbi = new abi.Function(abis.name); | ||
const symbolAbi = new abi.Function(abis.symbol); | ||
|
||
const verifyCalls = [ | ||
balanceOfAbi.encode("0xf077b491b355E64048cE21E3A6Fc4751eEeA77fa"), | ||
allowanceAbi.encode( | ||
"0xf077b491b355E64048cE21E3A6Fc4751eEeA77fa", | ||
"0x435933c8064b4Ae76bE665428e0307eF2cCFBD68", | ||
), | ||
]; | ||
const verifyContract = async (address, url) => { | ||
try { | ||
const clauses = verifyCalls.map((data) => ({ | ||
to: address, | ||
data, | ||
value: "0x0", | ||
})); | ||
|
||
const resp = await axios.post(`${url}/accounts/*`, { | ||
clauses, | ||
}); | ||
|
||
if (resp.data.some((r) => r.reverted)) { | ||
throw new Error("Some contract calls reverted"); | ||
} | ||
|
||
if (resp.data.length !== verifyCalls.length) { | ||
throw new Error("Invalid contract response"); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
throw new Error("unable to verify contract methods"); | ||
} | ||
}; | ||
|
||
const contractDetailsClauses = [ | ||
totalSupplyAbi.encode(), | ||
decimalsAbi.encode(), | ||
nameAbi.encode(), | ||
symbolAbi.encode(), | ||
]; | ||
|
||
const getContractDetails = async (address, url) => { | ||
const clauses = contractDetailsClauses.map((data) => ({ | ||
to: address, | ||
data, | ||
value: "0x0", | ||
})); | ||
|
||
const resp = await axios.post(`${url}/accounts/*`, { | ||
clauses, | ||
}); | ||
|
||
if (resp.data.some((r) => r.reverted)) { | ||
throw new Error("Some contract calls reverted"); | ||
} | ||
|
||
if (resp.data.length !== contractDetailsClauses.length) { | ||
throw new Error("Invalid contract response"); | ||
} | ||
|
||
const totalSupply = totalSupplyAbi.decode(resp.data[0].data); | ||
const decimals = decimalsAbi.decode(resp.data[1].data); | ||
const name = nameAbi.decode(resp.data[2].data); | ||
const symbol = symbolAbi.decode(resp.data[3].data); | ||
|
||
return { | ||
totalSupply: totalSupply["0"], | ||
decimals: parseInt(decimals["0"]), | ||
name: name["0"], | ||
symbol: symbol["0"], | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
verifyContract, | ||
getContractDetails, | ||
}; |
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
2 changes: 1 addition & 1 deletion
2
tokens/main/0x0ce6661b4ba86a0ea7ca2bd86a0de87b0b860f14/info.json
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
2 changes: 1 addition & 1 deletion
2
tokens/main/0x1b8ec6c2a45cca481da6f243df0d7a5744afc1f8/info.json
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
2 changes: 1 addition & 1 deletion
2
tokens/main/0x34109fc2a649965eecd953d31802c67dcc183d57/info.json
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
2 changes: 1 addition & 1 deletion
2
tokens/main/0x46209d5e5a49c1d403f4ee3a0a88c3a27e29e58d/info.json
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
2 changes: 1 addition & 1 deletion
2
tokens/main/0x5db3c8a942333f6468176a870db36eef120a34dc/info.json
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.