Skip to content

Commit

Permalink
feat: add bunch of snx helpers for trading queue
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Mucha <jakub.mucha@icloud.com>
  • Loading branch information
drptbl committed Dec 12, 2020
1 parent 3f8dd67 commit d7c0a64
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 13 deletions.
115 changes: 109 additions & 6 deletions commands/synthetix.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,126 @@
const { SynthetixJs } = require('synthetix-js');
const { synthetix } = require('@synthetixio/js');
const { getNetwork } = require('../helpers');
const metamask = require('./metamask');
const bytes32 = require('bytes32');
const sleep = require('util').promisify(setTimeout);

module.exports = {
settle: async ({ asset, privateKey }) => {
const assetAsBytes32 = bytes32({ input: asset });
settle: async ({ asset, walletAddress, privateKey }) => {
if (privateKey === undefined && process.env.PRIVATE_KEY) {
privateKey = process.env.PRIVATE_KEY;
}
if (walletAddress === undefined) {
walletAddress = metamask.walletAddress();
}
const assetAsBytes32 = bytes32({ input: asset });
const networkId = getNetwork().networkId;
const networkName = getNetwork().networkName;
const signer = new SynthetixJs.signers.PrivateKey(
undefined,
networkId,
`0x${privateKey}`,
);
const snxjs = new SynthetixJs({ signer, networkId });
const txn = await snxjs.Synthetix.settle(assetAsBytes32);
console.log(`Settle executed: ${txn.hash}`);
await txn.wait();
return txn.hash;

const maxEntriesInQueue = await getMaxEntriesInQueue(networkName);
const currentLengthOfEntries = await getLengthOfEntries(
networkName,
walletAddress,
assetAsBytes32,
);

if (currentLengthOfEntries >= maxEntriesInQueue - 3) {
const maxSecsLeftInWaitingPeriod = await getMaxSecsLeftInWaitingPeriod(
networkName,
walletAddress,
assetAsBytes32,
);

if (maxSecsLeftInWaitingPeriod > 0) {
console.log(
`We're currently in waiting period, waiting for ${maxSecsLeftInWaitingPeriod} seconds before settling..`,
);
await sleep(maxSecsLeftInWaitingPeriod * 1000);
const txHash = await sendSettleTx();
return txHash;
} else {
const txHash = await sendSettleTx();
return txHash;
}
} else {
console.log(
`Current length of entries in queue is ${currentLengthOfEntries}, no need to settle yet.`,
);
}

async function sendSettleTx() {
const txn = await snxjs.Synthetix.settle(assetAsBytes32);
console.log(`Settle executed: ${txn.hash}`);
await txn.wait();
return txn.hash;
}
},
checkWaitingPeriod: async (asset, walletAddress) => {
if (walletAddress === undefined) {
walletAddress = metamask.walletAddress();
}
const assetAsBytes32 = bytes32({ input: asset });
const networkName = getNetwork().networkName;
const maxSecsLeftInWaitingPeriod = await getMaxSecsLeftInWaitingPeriod(
networkName,
walletAddress,
assetAsBytes32,
);
if (maxSecsLeftInWaitingPeriod > 0) {
console.log(
`We're currently in waiting period, waiting for ${maxSecsLeftInWaitingPeriod} seconds..`,
);
await sleep(maxSecsLeftInWaitingPeriod * 1000);
return true;
} else {
return true;
}
},
};

async function getMaxEntriesInQueue(networkName) {
const snxjs = synthetix({ network: networkName });
const maxEntriesInQueue = await snxjs.contracts.ExchangeState.maxEntriesInQueue();
return Number(maxEntriesInQueue);
}

async function getLengthOfEntries(networkName, walletAddress, assetAsBytes32) {
const snxjs = synthetix({ network: networkName });
const getLengthOfEntries = await snxjs.contracts.ExchangeState.getLengthOfEntries(
walletAddress,
assetAsBytes32,
);
return Number(getLengthOfEntries);
}

// async function hasWaitingPeriodOrSettlementOwing(
// networkName,
// walletAddress,
// assetAsBytes32,
// ) {
// const snxjs = synthetix({ network: networkName });
// const hasWaitingPeriodOrSettlementOwing = await snxjs.contracts.Exchanger.hasWaitingPeriodOrSettlementOwing(
// walletAddress,
// assetAsBytes32,
// );
// return hasWaitingPeriodOrSettlementOwing;
// }

async function getMaxSecsLeftInWaitingPeriod(
networkName,
walletAddress,
assetAsBytes32,
) {
const snxjs = synthetix({ network: networkName });
const maxSecsLeftInWaitingPeriod = await snxjs.contracts.Exchanger.maxSecsLeftInWaitingPeriod(
walletAddress,
assetAsBytes32,
);
return Number(maxSecsLeftInWaitingPeriod);
}
17 changes: 14 additions & 3 deletions plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,27 @@ module.exports = (on, config) => {
return walletAddress;
},
fetchMetamaskWalletAddress: async () => {
return metamask.walletAddress;
return metamask.walletAddress();
},
setupMetamask: async ({ secretWords, network, password }) => {
await metamask.initialSetup({ secretWords, network, password });
return true;
},
snxExchangerSettle: async ({ asset, privateKey }) => {
const settled = await synthetix.settle({ asset, privateKey });
snxExchangerSettle: async ({ asset, walletAddress, privateKey }) => {
const settled = await synthetix.settle({
asset,
walletAddress,
privateKey,
});
return settled;
},
snxCheckWaitingPeriod: async ({ asset, walletAddress }) => {
const waitingPeriod = await synthetix.checkWaitingPeriod({
asset,
walletAddress,
});
return waitingPeriod;
},
getNetwork: () => {
const network = helpers.getNetwork();
return network;
Expand Down
12 changes: 10 additions & 2 deletions support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,23 @@ Cypress.Commands.add(

Cypress.Commands.add(
'snxExchangerSettle',
(asset, privateKey = process.env.PRIVATE_KEY) => {
(asset, walletAddress, privateKey = process.env.PRIVATE_KEY) => {
return cy.task(
'snxExchangerSettle',
{ asset, privateKey },
{ asset, walletAddress, privateKey },
{ timeout: 120000 },
);
},
);

Cypress.Commands.add('snxCheckWaitingPeriod', (asset, walletAddress) => {
return cy.task(
'snxCheckWaitingPeriod',
{ asset, walletAddress },
{ timeout: 30000 },
);
});

Cypress.Commands.add('getNetwork', () => {
return cy.task('getNetwork');
});
10 changes: 8 additions & 2 deletions support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,15 @@ declare namespace Cypress {
/**
* Execute settle on Exchanger contract
* @example
* cy.snxExchangerSettle('sETH', '123123123123123123...')
* cy.snxExchangerSettle('sETH', '0x...', '123123123123123123...')
*/
snxExchangerSettle(asset, privateKey): Chainable<Subject>;
snxExchangerSettle(asset, walletAddress, privateKey): Chainable<Subject>;
/**
* Check waiting period on Exchanger contract
* @example
* cy.snxCheckWaitingPeriod('sETH', '0x...')
*/
snxCheckWaitingPeriod(asset, walletAddress): Chainable<Subject>;
/**
* Get current network
* @example
Expand Down

0 comments on commit d7c0a64

Please sign in to comment.