Skip to content

Commit

Permalink
rename and update integration test protocol-xlm-eth-scenario-1.js
Browse files Browse the repository at this point in the history
pull stellar base reserve from the latest ledger rather then hard code it
integrate latest ethereum-htlc
  • Loading branch information
Chris Hatch committed Jan 23, 2018
1 parent 355a214 commit 94d6fbb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,30 @@ const eBuyerAddr = web3.eth.accounts[5]
/*
* Stellar Accounts
*/
const sBuyerKP = sdk.Keypair.random()
const sSellerKP = sdk.Keypair.random()
const sBuyerKP = sdk.Keypair.random()

/*
* Trade definition
* Hashlock preimage and hash for the trade
*/
const {secret: preImageStr, hash: hashXStr} = newSecretHashPair()

/*
* Trade definition
*/
const initialTrade = {
initialSide: Protocol.TradeSide.STELLAR,
timelock: Date.now() + 120,
commitment: hashXStr.substring(2),
commitment: hashXStr.substring(2), // slice off prefix '0x'
stellar: {
token: 'XLM',
amount: 200.0,
amount: 100.0,
depositor: sSellerKP.publicKey(),
withdrawer: sBuyerKP.publicKey(),
},
ethereum: {
token: 'ETH',
amount: 0.01,
amount: 0.05,
depositor: eSellerAddr,
withdrawer: eBuyerAddr,
},
Expand Down Expand Up @@ -80,7 +83,7 @@ const log = msg => console.info(`INFO: ${msg}`)

const main = async () => {
/*
* Party 1 initiates trade setting up the Stellar holding account (2.2)
* Party 1 initiates trade setting up the Stellar holding account (2.3)
*/
const config1 = new Config(configParty1)
let trade1 = new Trade(initialTrade)
Expand Down Expand Up @@ -108,14 +111,14 @@ const main = async () => {
log(`party2 imported and checked the trade status`)

/*
* Party 2 generates the refund tx for Party 1 (2.3)
* Party 2 generates the refund tx envelope for Party 1 (2.4)
*/
trade2.stellar.refundTx = await protocol2.stellarRefundTx()
expect(await protocol2.status()).toEqual(Protocol.Status.STELLAR_REFUND_TX)
log(`refund tx for party 1 created: [${trade2.stellar.refundTx}]`)

/*
* Party 1 receives the refund tx and deposits XLM into holding account (2.4)
* Party 1 receives the refund tx then deposits XLM into holding account (2.5)
*/
trade1.stellar.refundTx = trade2.stellar.refundTx // party 2 sends tx to party 1
expect(await protocol1.status()).toEqual(Protocol.Status.STELLAR_REFUND_TX)
Expand All @@ -127,7 +130,7 @@ const main = async () => {
log(`party1 deposited XLM`)

/*
* Party 2 creates the HTLC and deposits ETH (2.5)
* Party 2 creates the HTLC and deposits ETH (2.6)
*/
const htlcId = await protocol2.ethereumPrepare()
log(`htlc created: ${htlcId}`)
Expand All @@ -143,7 +146,7 @@ const main = async () => {
expect(await protocol2.status()).toEqual(Protocol.Status.ETHEREUM_WITHDRAW)

/*
* Party 2 withdraws the XLM revealing the preimage (3.1)
* Party 2 withdraws the XLM with the revealed preimage (3.2)
*/
// TODO: pull the preimage from the events log ...
// for now cheat and just plug it in ..
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "xcat",
"version": "0.0.2",
"description": "Cross chain atomic trades protocol and api for trades between Stellar and Ethereum",
"description":
"Cross chain atomic trades protocol and api for trades between Stellar and Ethereum",
"repository": "chatch/xcat",
"license": "GPL-3.0",
"author": {
Expand All @@ -18,7 +19,8 @@
"pretest": "eslint src --fix",
"test": "jest src",
"test-watch": "jest src --watch",
"test-protocol": "node_modules/babel-cli/bin/babel-node.js integration-test/protocol-scenario1.js"
"test-protocol":
"node_modules/babel-cli/bin/babel-node.js integration-test/protocol-xlm-eth-scenario-1.js"
},
"keywords": [
"crosschain",
Expand All @@ -31,7 +33,7 @@
"dependencies": {
"ajv": "^5.2.2",
"bluebird": "^3.5.0",
"ethereum-htlc": "^0.0.4",
"ethereum-htlc": "^0.0.5",
"human-readable-ids": "^1.0.3",
"lodash": "^4.17.4",
"stellar-sdk": "0.7.3",
Expand All @@ -49,13 +51,9 @@
"jest-cli": "^21.0.0",
"nsp": "^2.6.3"
},
"files": [
"lib"
],
"files": ["lib"],
"jest": {
"roots": [
"src/"
],
"roots": ["src/"],
"testEnvironment": "node",
"transform": {
"^.+\\.jsx?$": "babel-jest"
Expand Down
10 changes: 6 additions & 4 deletions src/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ const validateSetup = (web3, htlcContractObj, htlcContractAddr) => {

if (htlcBytecode !== htlcContractObj.deployedBytecode)
throw new Error(
`Wrong code deployed at HashedTimelock deployment address ` +
`Wrong code deployed at HashedTimelock deployment address. The ` +
`deployed contract should match the bytecode in the ethereum-htlc ` +
`module abi file. ` +
`[${htlcContractAddr}]\n\nExpected:[\n` +
`[${htlcContractObj.deployedBytecode}]\n\nGot:[\n${htlcBytecode}\n]\n`
`[${htlcContractObj.deployedBytecode}]\n\nGot:[\n${htlcBytecode}]\n`
)
}

Expand Down Expand Up @@ -116,7 +118,7 @@ class Ethereum {
}

/**
* Try find a trade contract given contrace details. Looks up LogNewContract
* Try find a trade contract given contrace details. Looks up LogHTLCNew
* events to discover a matching contract.
*
* @return Promise with contractId of matching contract
Expand All @@ -129,7 +131,7 @@ class Ethereum {
hashlock: hashlock,
timelock: timelock,
}
const event = this.htlcWeb3.LogNewContract(filter)
const event = this.htlcWeb3.LogHTLCNew(filter)
event.get = Promise.promisify(event.get)
return event
.get()
Expand Down
14 changes: 12 additions & 2 deletions src/stellar.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Stellar {
constructor(sdk, network) {
this.sdk = sdk
this.server = initServer(sdk, network)
this.getBaseReserve().then(reserve => (this.baseReserve = reserve)) // TODO: move this to a static initaliser
}

async createHoldingAccount(
Expand All @@ -85,8 +86,8 @@ class Stellar {
tb.addOperation(
this.sdk.Operation.createAccount({
destination: newAccKeypair.publicKey(),
startingBalance: '40', // +20 base; +10 hash(x) signer; +10 buyer signer
}) // TODO: don't use 10 .. pull value of base from latest ledger
startingBalance: String(4 * this.baseReserve), // 2 base + 1 hash(x) signer +1 buyer signer
})
)

// Op2: Add buyer as signer on holding account
Expand Down Expand Up @@ -290,6 +291,15 @@ class Stellar {
return this.server.loadAccount(publicKey)
}

async getBaseReserve() {
const {records} = await this.server
.ledgers()
.order('desc')
.limit(1)
.call()
return Number(records[0].base_reserve)
}

async getBalance(publicKey) {
return this.loadAccount(publicKey).then(accRec => {
const xlmBalance = Number(
Expand Down

0 comments on commit 94d6fbb

Please sign in to comment.