Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const StellarSdk = require('stellar-sdk')
- const LIVE_LOCKUP_0 = 1526425200; // May 15
- const LIVE_LOCKUP_90 = LIVE_LOCKUP_0 + (60 * 60 * 24) * 90; // Mon, 13 Aug 2018 23:00:00 +0000
- const LIVE_LOCKUP_180 = LIVE_LOCKUP_0 + (60 * 60 * 24) * 180; // Sun, 11 Nov 2018 23:00:00 +0000
- const TEST_LOCKUP_0 = 1531116000; // Mon, 09 Jul 2018 12:00:00 +0000
- const TEST_LOCKUP_90 = TEST_LOCKUP_0 + (60 * 60 * 24) * 5; // Fri, 14 Jul 2018 6:00:00 +0000
- const TEST_LOCKUP_180 = TEST_LOCKUP_0 + (60 * 60 * 24) * 10; // Wed, 19 Jul 2018 6:00:00 +0000
- // Queried from "https://s3.amazonaws.com/ficnetwork-public/ico.json" where we will update FIC coin distribution state.
- const ficDistributorAddress = "GAWLROE3Z6TKZ76VE5COZIO4PBI4YZ6STXGNRQF4HDRASKIXDMHJ7MDP";
- const Unlock = (ficAddress, lockupAddress, lockup) => async () => {
- const SUBMIT = async (te) => StellarApi.server.submitTransaction(te).catch((e)=>e).then((res)=>res)
- let lockupAccount;
- try {
- lockupAccount = await StellarApi.server.loadAccount(lockupAddress);
- lockupAccount._baseAccount.sequence.minus(2);
- } catch(e) {
- if(e instanceof StellarSdk.NotFoundError) throw new Error(`Already unlocked (${lockupAddress})`);
- throw e;
- }
- // console.log(`Lockup account ${lockupAddress} sequence & balance:`, lockupAccount.sequenceNumber(), lockupAccount.balances)
- let timebounds;
- switch(SettingFactory.getCurrentNetwork().networkPassphrase) {
- case(SettingFactory.NETWORKS.ficTest.networkPassphrase): {
- timebounds = {minTime: ({0: TEST_LOCKUP_0, 1: TEST_LOCKUP_90, 2: TEST_LOCKUP_180})[lockup], maxTime: Number.MAX_SAFE_INTEGER}
- break;
- }
- case(SettingFactory.NETWORKS.fic.networkPassphrase): {
- timebounds = {minTime: ({0: LIVE_LOCKUP_0, 1: LIVE_LOCKUP_90, 2: LIVE_LOCKUP_180})[lockup], maxTime: Number.MAX_SAFE_INTEGER}
- break;
- }
- default: {
- timebounds = undefined;
- break;
- }
- }
- const prete1 = new StellarSdk.TransactionBuilder(lockupAccount, { timebounds })
- .addOperation(StellarSdk.Operation.createAccount({
- source: lockupAddress,
- destination: ficAddress,
- startingBalance: '400',
- }))
- .build();
- const prete2 = new StellarSdk.TransactionBuilder(lockupAccount, { timebounds })
- .addOperation(StellarSdk.Operation.accountMerge({
- source: lockupAddress,
- destination: ficAddress,
- }))
- .build();
- const resultCreate = await SUBMIT(prete1); // Create user account ${ficAddress}
- const resultUnlock = await SUBMIT(prete2); // Merge into user account ${ficAddress}
- return {
- resultCreate,
- resultUnlock,
- }
- }
- const getFicTxs = async (userAddress) => {
- const directoryResponse = await fetch(`${StellarApi.server.serverURL}/accounts/${ficDistributorAddress}`);
- const directory = await directoryResponse.json();
- const lockupAddressMap = directory.data;
- const userLockups = Object.keys(lockupAddressMap).filter((key)=>key.slice(0,56) === userAddress);
- const responses = [];
- for(const key of userLockups) {
- const lockupAddress = Buffer.from(lockupAddressMap[key], 'base64').toString();
- const res = await fetch(`${StellarApi.server.serverURL}/accounts/${lockupAddress}/transactions?order=desc&limit=4`);
- const json = await res.json();
- const transactions = json._embedded.records;
- transactions.forEach((transaction) => transaction.envelope = new StellarSdk.Transaction(transaction.envelope_xdr));
- const creationTransation = transactions.find((tx)=>tx.source_account==ficDistributorAddress && tx.envelope.operations.find((op)=>op.type === 'createAccount'))
- const setupTransaction = transactions.find((tx)=>tx.source_account==ficDistributorAddress && tx.envelope.operations.find((op)=>op.type === 'setOptions'));
- const createTransaction = transactions.find((tx)=>tx.source_account==lockupAddress && tx.envelope.operations.find((op)=>op.type === 'createAccount'));
- const mergeTransaction = transactions.find((tx)=>tx.source_account==lockupAddress && tx.envelope.operations.find((op)=>op.type === 'accountMerge'));
- const lockupCode = creationTransation.envelope.memo.value.readInt8(31);
- const lockup = ({0: '0', 1:'90', 2:'180'})[lockupCode];
- const r = {
- lockupAddress,
- ficNonce: key.slice(57),
- ethAddress: creationTransation.envelope.memo.value.slice(0, 20).toString('hex'),
- lockup,
- amount: creationTransation.envelope.operations[0].startingBalance.split('.')[0], // Ghetto rounding to integers.
- ethTxHash: setupTransaction && setupTransaction.envelope.memo.value.toString('hex'),
- tx1: creationTransation && {timestamp: creationTransation.created_at, txId: creationTransation.id},
- tx2: setupTransaction && {timestamp: setupTransaction.created_at , txId: setupTransaction.id },
- tx3: createTransaction && {timestamp: createTransaction.created_at , txId: createTransaction.id },
- tx4: mergeTransaction && {timestamp: mergeTransaction.created_at , txId: mergeTransaction.id },
- unlock: Unlock(userAddress, lockupAddress, lockupCode),
- };
- responses.push(r);
- }
- return responses;
- }
- // Claim all of them! Note that claiming already unlocked FIC coins will give error;
- Promise.all(getFicTxs(myFicAddress).map((ficTx)=>unlock()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement