Skip to content

Commit

Permalink
enable erc20 (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingweicb authored Nov 4, 2022
1 parent bfb7f66 commit c58608d
Show file tree
Hide file tree
Showing 23 changed files with 7,015 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/actions/geth/scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ geth \
--http.port 8545 \
--http.vhosts '*' \
--http.api 'personal,eth,net,web3,txpool,miner,debug' \
--networkid '12345' \
--networkid '1' \
--mine \
--miner.etherbase '0x4cdBd835fE18BD93ccA39A262Cff72dbAC99E24F' \
--miner.gasprice 0 \
Expand Down
6,519 changes: 6,519 additions & 0 deletions .github/actions/geth/truffle/build/contracts/USDC.json

Large diffs are not rendered by default.

Empty file.
60 changes: 60 additions & 0 deletions .github/actions/geth/truffle/contracts/USDC.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.17;

contract USDC {

event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);

string public constant name = "USDC";
string public constant symbol = "USDC";
uint8 public constant decimals = 6;

mapping(address => uint256) balances;

mapping(address => mapping (address => uint256)) allowed;

uint256 totalSupply_;

constructor() {
totalSupply_ = 888888888888888;
balances[msg.sender] = totalSupply_;
}

function totalSupply() public view returns (uint256) {
return totalSupply_;
}

function balanceOf(address tokenOwner) public view returns (uint) {
return balances[tokenOwner];
}

function transfer(address receiver, uint numTokens) public returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] -= numTokens;
balances[receiver] += numTokens;
emit Transfer(msg.sender, receiver, numTokens);
return true;
}

function approve(address delegate, uint numTokens) public returns (bool) {
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}

function allowance(address owner, address delegate) public view returns (uint) {
return allowed[owner][delegate];
}

function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
require(numTokens <= balances[owner]);
require(numTokens <= allowed[owner][msg.sender]);

balances[owner] -= numTokens;
allowed[owner][msg.sender] -= numTokens;
balances[buyer] += numTokens;
emit Transfer(owner, buyer, numTokens);
return true;
}
}
Empty file.
4 changes: 4 additions & 0 deletions .github/actions/geth/truffle/migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var USDC = artifacts.require("./USDC.sol");
module.exports = function(deployer) {
deployer.deploy(USDC);
};
Empty file.
158 changes: 158 additions & 0 deletions .github/actions/geth/truffle/truffle-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* Use this file to configure your truffle project. It's seeded with some
* common settings for different networks and features like migrations,
* compilation, and testing. Uncomment the ones you need or modify
* them to suit your project as necessary.
*
* More information about configuration can be found at:
*
* https://trufflesuite.com/docs/truffle/reference/configuration
*
* Hands-off deployment with Infura
* --------------------------------
*
* Do you have a complex application that requires lots of transactions to deploy?
* Use this approach to make deployment a breeze 🏖️:
*
* Infura deployment needs a wallet provider (like @truffle/hdwallet-provider)
* to sign transactions before they're sent to a remote public node.
* Infura accounts are available for free at 🔍: https://infura.io/register
*
* You'll need a mnemonic - the twelve word phrase the wallet uses to generate
* public/private key pairs. You can store your secrets 🤐 in a .env file.
* In your project root, run `$ npm install dotenv`.
* Create .env (which should be .gitignored) and declare your MNEMONIC
* and Infura PROJECT_ID variables inside.
* For example, your .env file will have the following structure:
*
* MNEMONIC = <Your 12 phrase mnemonic>
* PROJECT_ID = <Your Infura project id>
*
* Deployment with Truffle Dashboard (Recommended for best security practice)
* --------------------------------------------------------------------------
*
* Are you concerned about security and minimizing rekt status 🤔?
* Use this method for best security:
*
* Truffle Dashboard lets you review transactions in detail, and leverages
* MetaMask for signing, so there's no need to copy-paste your mnemonic.
* More details can be found at 🔎:
*
* https://trufflesuite.com/docs/truffle/getting-started/using-the-truffle-dashboard/
*/

// require('dotenv').config();
// const { MNEMONIC, PROJECT_ID } = process.env;

// const HDWalletProvider = require('@truffle/hdwallet-provider');

module.exports = {
/**
* Networks define how you connect to your ethereum client and let you set the
* defaults web3 uses to send transactions. If you don't specify one truffle
* will spin up a managed Ganache instance for you on port 9545 when you
* run `develop` or `test`. You can ask a truffle command to use a specific
* network from the command line, e.g
*
* $ truffle test --network <network-name>
*/
// rpc: {
// host:"localhost",
// port:8546
// },
networks: {
// Useful for testing. The `development` name is special - truffle uses it by default
// if it's defined here and no other network is specified at the command line.
// You should run a client (like ganache, geth, or parity) in a separate terminal
// tab if you use this network and you must also set the `host`, `port` and `network_id`
// options below to some value.
//


// networks: {
// development: {
// host: "localhost", //our network is running on localhost
// port: 8546, // port where your blockchain is running
// network_id: "*",
// from: "0x4cdBd835fE18BD93ccA39A262Cff72dbAC99E24F", // use the account-id generated during the setup process
// gas: 20000000
// }
// }
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8546, // Standard Ethereum port (default: none)
network_id: 1, // Any network (default: none)
gas: 7000000, // Gas sent with each transaction (default: ~6700000)
gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
from: "0x4cdBd835fE18BD93ccA39A262Cff72dbAC99E24F", // Account to send transactions from (default: accounts[0])
},
//
// An additional network, but with some advanced options…
// advanced: {
// port: 8777, // Custom port
// network_id: 1342, // Custom network
// gas: 20000000, // Gas sent with each transaction (default: ~6700000)
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
// from: "0x4cdBd835fE18BD93ccA39A262Cff72dbAC99E24F", // Account to send transactions from (default: accounts[0])
// websocket: true // Enable EventEmitter interface for web3 (default: false)
// }
//
// Useful for deploying to a public network.
// Note: It's important to wrap the provider as a function to ensure truffle uses a new provider every time.
// goerli: {
// provider: () => new HDWalletProvider(MNEMONIC, `https://goerli.infura.io/v3/${PROJECT_ID}`),
// network_id: 5, // Goerli's id
// confirmations: 2, // # of confirmations to wait between deployments. (default: 0)
// timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
// skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
// },
//
// Useful for private networks
// private: {
// provider: () => new HDWalletProvider(MNEMONIC, `https://network.io`),
// network_id: 2111, // This network is yours, in the cloud.
// production: true // Treats this network as if it was a public net. (default: false)
// }
},

// Set default mocha options here, use special reporters, etc.
mocha: {
// timeout: 100000
},

// Configure your compilers
compilers: {
solc: {
version: "0.8.17" // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
// settings: { // See the solidity docs for advice about optimization and evmVersion
// optimizer: {
// enabled: false,
// runs: 200
// },
// evmVersion: "byzantium"
// }
}
}

// Truffle DB is currently disabled by default; to enable it, change enabled:
// false to enabled: true. The default storage location can also be
// overridden by specifying the adapter settings, as shown in the commented code below.
//
// NOTE: It is not possible to migrate your contracts to truffle DB and you should
// make a backup of your artifacts to a safe location before enabling this feature.
//
// After you backed up your artifacts you can utilize db by running migrate as follows:
// $ truffle migrate --reset --compile-all
//
// db: {
// enabled: false,
// host: "127.0.0.1",
// adapter: {
// name: "indexeddb",
// settings: {
// directory: ".db"
// }
// }
// }
};
2 changes: 1 addition & 1 deletion .github/scripts/construction.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# downloading cli
curl -sSfL https://raw.githubusercontent.com/coinbase/rosetta-cli/master/scripts/install.sh | sh -s
curl -sSfL https://raw.githubusercontent.com/coinbase/rosetta-cli/master/scripts/install.sh | sh -s v0.10.0

echo "start check:construction"
./bin/rosetta-cli --configuration-file examples/ethereum/rosetta-cli-conf/devnet/config.json check:construction
21 changes: 21 additions & 0 deletions .github/scripts/contract_infos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from web3 import Web3
from web3.middleware import geth_poa_middleware


web3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8546"))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)

print("latest block", web3.eth.block_number)

abi= [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":"false","inputs":[{"indexed":"true","internalType":"address","name":"tokenOwner","type":"address"},{"indexed":"true","internalType":"address","name":"spender","type":"address"},{"indexed":"false","internalType":"uint256","name":"tokens","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":"false","inputs":[{"indexed":"true","internalType":"address","name":"from","type":"address"},{"indexed":"true","internalType":"address","name":"to","type":"address"},{"indexed":"false","internalType":"uint256","name":"tokens","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"delegate","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"delegate","type":"address"},{"internalType":"uint256","name":"numTokens","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenOwner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"numTokens","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"buyer","type":"address"},{"internalType":"uint256","name":"numTokens","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
address = '0x62F3712A8A2bF3482F9Aa42F2C8296CF50774DDD'
contract= web3.eth.contract(address=address, abi=abi)

seedAddress="0x4cdBd835fE18BD93ccA39A262Cff72dbAC99E24F"
targetAddress="0x622Fbe99b3A378FAC736bf29d7e23B85E18816eB"

print("symbol is ", contract.functions.symbol().call())
print("decimal is ", contract.functions.decimals().call())
print("address is ", contract.address)
print("seedAddress balance is ", contract.functions.balanceOf(seedAddress).call())
print("targetAddress balance is ", contract.functions.balanceOf(targetAddress).call())
20 changes: 20 additions & 0 deletions .github/scripts/init_erc20.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

echo "update apt-get"
apt-get update

echo "install nodejs"
apt-get install -y nodejs

echo "check npm version"
npm -v

echo "install truffle"
npm install -g truffle

echo "install solc"
npm install -g solc

cd .github/actions/geth/truffle
echo "deploy the contract"
truffle migrate
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ env:
jobs:
Rosetta-Validation:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v3

Expand All @@ -35,6 +36,14 @@ jobs:
python -m pip install --upgrade pip
pip install web3
- name: deploy erc20 USDC
run: .github/scripts/init_erc20.sh
shell: bash

- name: Get erc20 infos
run: python .github/scripts/contract_infos.py
shell: bash

- name: Populate transactions
run: python .github/scripts/populate_txns.py
shell: bash
Expand Down Expand Up @@ -97,6 +106,3 @@ jobs:
with:
version: latest
- run: make salus



4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ func (ec *SDKClient) Balance(
Args: []interface{}{account.Address, blockNum},
Result: &nonce,
},
{ Method: "eth_getCode",
Args: []interface{}{account.Address, blockNum},
{Method: "eth_getCode",
Args: []interface{}{account.Address, blockNum},
Result: &code,
},
}
Expand Down
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestTraceBlockByHash(t *testing.T) {
RPCClient: rpcClient,
traceSemaphore: semaphore.NewWeighted(100),
rosettaConfig: configuration.RosettaConfig{
TraceType: configuration.OpenEthereumTrace,
TraceType: configuration.OpenEthereumTrace,
TracePrefix: "eth",
},
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestOpenEthTraceAPI_1Txn(t *testing.T) {
RPCClient: rpcClient,
traceSemaphore: semaphore.NewWeighted(100),
rosettaConfig: configuration.RosettaConfig{
TraceType: configuration.OpenEthereumTrace,
TraceType: configuration.OpenEthereumTrace,
TracePrefix: "eth",
},
}
Expand Down
8 changes: 4 additions & 4 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ type RosettaConfig struct {
Currency *RosettaTypes.Currency

// TracePrefix is the prefix appended to trace RPC calls
TracePrefix string
TracePrefix string

// IngestionMode indicates if blockchain ingestion mode
IngestionMode string
IngestionMode string

// IndexUnknownTokens determines whether we parse unknown ERC20 tokens
IndexUnknownTokens bool

// FilterToken determines whether we using our token whitelist
FilterTokens bool
FilterTokens bool

// TokenWhiteList is a list of ERC20 tokens we only support
TokenWhiteList []Token
TokenWhiteList []Token
}

type Token struct {
Expand Down
4 changes: 2 additions & 2 deletions examples/ethereum/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ func LoadConfiguration() (*configuration.Configuration, error) {
Symbol: "ETH",
Decimals: 18,
},
TracePrefix: "",
FilterTokens: tokenFilterValue,
TracePrefix: "",
FilterTokens: tokenFilterValue,
TokenWhiteList: payload,
}

Expand Down
Loading

0 comments on commit c58608d

Please sign in to comment.