Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Apr 21, 2024
1 parent a031bd9 commit 6b37e86
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 191 deletions.
112 changes: 0 additions & 112 deletions packages/osmojs/starship/src/config.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/osmojs/starship/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './config';
export * from './utils';
121 changes: 43 additions & 78 deletions packages/osmojs/starship/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ export const calcShareOutAmount = (poolInfo, coinsNeeded) => {
return poolInfo.poolAssets
.map(({ token }, i) => {
const tokenInAmount = new BigNumber(coinsNeeded[i].amount);
const totalShare = new BigNumber(poolInfo.totalShares.amount);
const totalShareExp = totalShare.shiftedBy(-18);
const totalShare = new BigNumber(poolInfo.totalShares.amount).shiftedBy(-18);
const poolAssetAmount = new BigNumber(token.amount);

return tokenInAmount
.multipliedBy(totalShareExp)
.multipliedBy(totalShare)
.dividedBy(poolAssetAmount)
.shiftedBy(18)
.decimalPlaces(0, BigNumber.ROUND_HALF_UP)
Expand All @@ -25,89 +24,55 @@ export const calcShareOutAmount = (poolInfo, coinsNeeded) => {
};

export const waitUntil = (date, timeout = 90000) => {
return new Promise((resolve) => {
const delay = date.getTime() - Date.now();
if (delay > timeout) {
throw new Error('Timeout to wait until date');
}
setTimeout(resolve, delay + 3000);
});
};

export const transferIbcTokens = async (
fromChain,
toChain,
toAddress,
amount
) => {
const registry = Config.registry;
const { chainInfo, getCoin, getRpcEndpoint, creditFromFaucet } =
useChain(fromChain);
const denom = getCoin().base;

const { chainInfo: toChainInfo } = useChain(toChain);

const ibcInfos = registry.getChainIbcData(chainInfo.chain.chain_id);
const ibcInfo = ibcInfos.find(
(i) =>
i.chain_1.chain_name === chainInfo.chain.chain_id &&
i.chain_2.chain_name === toChainInfo.chain.chain_id
);

if (!ibcInfo) {
throw new Error('cannot find IBC info');
const delay = date.getTime() - Date.now();
if (delay > timeout) {
throw new Error('Timeout to wait until date');
}
return new Promise(resolve => setTimeout(resolve, delay + 3000));
};

const { port_id: sourcePort, channel_id: sourceChannel } =
ibcInfo.channels[0].chain_1;
export const transferIbcTokens = async (fromChain, toChain, toAddress, amount) => {
const fromChainData = useChain(fromChain);
const toChainData = useChain(toChain);
const ibcInfo = findIbcInfo(fromChainData.chainInfo, toChainData.chainInfo);

// Create temp address on fromChain that will transfer the funds
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
generateMnemonic(),
{ prefix: chainInfo.chain.bech32_prefix }
);
const wallet = await createTempWallet(fromChainData.chainInfo.chain.bech32_prefix);
const fromAddress = (await wallet.getAccounts())[0].address;

// Transfer funds to address from faucet
await creditFromFaucet(fromAddress);

// Create ibc client to transfer tokens
const fromClient = await getSigningIbcClient({
rpcEndpoint: getRpcEndpoint(),
signer: wallet
});

const currentTime = Math.floor(Date.now() / 1000);
const timeoutTime = currentTime + 300; // 5 minutes
await fromChainData.creditFromFaucet(fromAddress);

const fromClient = await setupIbcClient(fromChainData.getRpcEndpoint(), wallet);
const token = { denom: fromChainData.getCoin().base, amount };

const resp = await sendIbcTokens(fromClient, fromAddress, toAddress, token, ibcInfo, amount);

assertIsDeliverTxSuccess(resp);
return token;
};

const fee = {
amount: [
{
denom,
amount: '100000'
}
],
gas: '550000'
};
const findIbcInfo = (chainInfo, toChainInfo) => {
const registry = Config.registry;
const ibcInfos = registry.getChainIbcData(chainInfo.chain.chain_id);
const found = ibcInfos.find(
i => i.chain_1.chain_name === chainInfo.chain.chain_id &&
i.chain_2.chain_name === toChainInfo.chain.chain_id
);
if (!found) throw new Error('Cannot find IBC info');
return found;
};

const token = {
denom,
amount
};
const createTempWallet = async (bech32Prefix) => {
return DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), { prefix: bech32Prefix });
};

// send ibc tokens
const resp = await fromClient.sendIbcTokens(
fromAddress,
toAddress,
token,
sourcePort,
sourceChannel,
undefined,
timeoutTime,
fee
);
const setupIbcClient = async (rpcEndpoint, wallet) => {
return getSigningIbcClient({ rpcEndpoint, signer: wallet });
};

assertIsDeliverTxSuccess(resp);
const sendIbcTokens = async (client, fromAddress, toAddress, token, ibcInfo, amount) => {
const { port_id: sourcePort, channel_id: sourceChannel } = ibcInfo.channels[0].chain_1;
const timeoutTime = Math.floor(Date.now() / 1000) + 300; // 5 minutes
const fee = { amount: [{ denom: token.denom, amount: '100000' }], gas: '550000' };

return token;
return client.sendIbcTokens(fromAddress, toAddress, token, sourcePort, sourceChannel, undefined, timeoutTime, fee);
};

0 comments on commit 6b37e86

Please sign in to comment.