Skip to content

Commit

Permalink
完成了ERC20所有接口调用
Browse files Browse the repository at this point in the history
  • Loading branch information
AnFengDe committed Jun 16, 2019
1 parent fccf732 commit 42731c3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
4 changes: 4 additions & 0 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const MOAC_ERC20_DECIMALS = 18;
const MOAC_ERC20_ADDRESS = "0x9bd4810a407812042f938d2f69f673843301cfa6";
const MOAC_SPENDER_ADDRESS = "0x09344477fDc71748216a7b8BbE7F2013B893DeF8";
const MOCK_ERC20_TX_SIGN = "0xf86a0c808504a817c80083030d40949bd4810a407812042f938d2f69f673843301cfa68081aa808081e9a040f366cac1b80976aadc9da0f5a36449f0023b54e95d093ae23b89a83bf2625aa071a8f3967bf19afebf6aea82a4cfd25e9936f25331755e253b78dae83d91e9f6"
const MOCK_ERC20_APPROVE_HASH = "0xf8ae0c808504a817c80083030d40949bd4810a407812042f938d2f69f673843301cfa680b844095ea7b30000000000000000000000000000000000000000000000000000000000000NaN000000000000000000000000000000000000000000000000006a94d74f430000808081eaa02f916405809da3b01409a36a0b3bf1bbb1a8e2d5b7bff765a9dc4bd55d43ee95a06cb0b9e5c1a596124d2d07fcf497022b5218d897abb68fa3cf3ef0c0ddd0077b"
const MOCK_ERC20_TRANSFERFROM_HASH = "0xf86a0c808504a817c80083030d40949bd4810a407812042f938d2f69f673843301cfa68081aa808081e9a040f366cac1b80976aadc9da0f5a36449f0023b54e95d093ae23b89a83bf2625aa071a8f3967bf19afebf6aea82a4cfd25e9936f25331755e253b78dae83d91e9f6"

module.exports = {
MOAC_ADDRESS,
Expand All @@ -51,6 +53,8 @@ module.exports = {
MOAC_SPENDER_ADDRESS,
MOAC_ERC20_ADDRESS,
MOCK_ERC20_TX_SIGN,
MOCK_ERC20_APPROVE_HASH,
MOCK_ERC20_TRANSFERFROM_HASH,
MOCK_NODE,
MOCK_NONCE,
MOCK_GAS,
Expand Down
70 changes: 55 additions & 15 deletions test/erc20.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,65 @@ describe('test ERC20', function () {
stub.returns(18);
let spy = sandbox.spy(inst, "sendRawSignedTransaction");
let hash = await inst.approve(config.MOAC_SECRET, config.MOAC_SPENDER_ADDRESS, config.MOCK_DEPOSIT_VALUE);
// expect(spy.args[0][0]).to.equal(config.MOCK_ERC20_TX_SIGN);
expect(spy.args[0][0]).to.equal(config.MOCK_ERC20_APPROVE_HASH);
expect(hash).to.equal(config.MOCK_HASH)
})

// it('amount is invalid', function () {
// expect(() => inst.transfer(config.MOAC_SECRET, config.MOAC_TO_ADDRESS, 0)).throw(`0 is invalid amount.`);
// })
it('amount is invalid', function () {
expect(() => inst.approve(config.MOAC_SECRET, config.MOAC_SPENDER_ADDRESS, 0)).throw(`0 is invalid amount.`);
})

it('moac secret is invalid', function () {
expect(() => inst.approve(config.MOAC_SECRET.substring(1), config.MOAC_SPENDER_ADDRESS, config.MOCK_DEPOSIT_VALUE)).throw(`${config.MOAC_SECRET.substring(1)} is invalid moac secret.`)
})

it('approve in error', function (done) {
let stub = sandbox.stub(inst._chain3.mc, "getTransactionCount");
stub.yields(new Error('request nonce in error'), null);
inst.approve(config.MOAC_SECRET, config.MOAC_SPENDER_ADDRESS, config.MOCK_DEPOSIT_VALUE).catch(error => {
expect(error.message).to.equal('request nonce in error')
done()
})
})

// it('moac secret is invalid', function () {
// expect(() => inst.transfer(config.MOAC_SECRET.substring(1), config.MOAC_TO_ADDRESS, config.MOCK_DEPOSIT_VALUE)).throw(`${config.MOAC_SECRET.substring(1)} is invalid moac secret.`)
// })
it('get allowance', function () {
let stub = sandbox.stub(inst._instance, "allowance");
stub.returns(new BigNumber(config.MOCK_DEPOSIT_VALUE));

// it('transfer in error', function (done) {
// let stub = sandbox.stub(inst._chain3.mc, "getTransactionCount");
// stub.yields(new Error('request nonce in error'), null);
// inst.transfer(config.MOAC_SECRET, config.MOAC_TO_ADDRESS, config.MOCK_DEPOSIT_VALUE).catch(error => {
// expect(error.message).to.equal('request nonce in error')
// done()
// })
// })
let amount = inst.allowance(config.MOAC_ADDRESS, config.MOAC_SPENDER_ADDRESS);
expect(amount.toNumber()).to.equal(config.MOCK_DEPOSIT_VALUE)
})

it('transferFrom successfully', async function () {
let stub = sandbox.stub(inst._chain3.mc, "getGasPrice");
stub.yields(null, config.MOCK_GAS);
stub = sandbox.stub(inst._chain3.mc, "getTransactionCount");
stub.yields(null, config.MOCK_NONCE);
stub = sandbox.stub(inst._chain3.currentProvider, "sendAsync");
stub.yields(null, {
jsonrpc: '2.0',
id: 1536822829875,
result: {}
})
stub = sandbox.stub(inst._chain3.mc, "sendRawTransaction");
stub.yields(null, config.MOCK_HASH);
stub = sandbox.stub(inst._instance.transferFrom, "getData");
stub.returns("0xaa")
stub = sandbox.stub(inst._instance, "decimals");
stub.returns(18);
let spy = sandbox.spy(inst, "sendRawSignedTransaction");
let hash = await inst.transferFrom(config.MOAC_SECRET, config.MOAC_SPENDER_ADDRESS, config.MOAC_ADDRESS, config.MOCK_DEPOSIT_VALUE);
expect(spy.args[0][0]).to.equal(config.MOCK_ERC20_TRANSFERFROM_HASH);
expect(hash).to.equal(config.MOCK_HASH)
})

it('transferFrom in error', function (done) {
let stub = sandbox.stub(inst._chain3.mc, "getTransactionCount");
stub.yields(new Error('request nonce in error'), null);
inst.transferFrom(config.MOAC_SECRET, config.MOAC_SPENDER_ADDRESS, config.MOAC_ADDRESS, config.MOCK_DEPOSIT_VALUE).catch(error => {
expect(error.message).to.equal('request nonce in error')
done()
})
})
})
});

0 comments on commit 42731c3

Please sign in to comment.