Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve setup scripts. #37

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.example

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"compile": "typechain-compiler --toolchain nightly-2023-08-10",
"compile:release": "typechain-compiler --release --toolchain nightly-2023-08-10",
"deploy:local": "source scripts/local_env && ts-node scripts/deploy.ts",
"example:local": "source scripts/local_env && ts-node scripts/example_setup.ts",
"createTokens:local": "source scripts/local_env && ts-node scripts/create_tokens.ts",
"addLiquidity:local": "source scripts/local_env && ts-node scripts/add_liquidity.ts",
deuszx marked this conversation as resolved.
Show resolved Hide resolved
"format": "prettier --write ."
},
"dependencies": {
Expand Down
91 changes: 91 additions & 0 deletions scripts/add_liquidity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api';
import { loadAddresses } from './utils';
import Token from '../types/contracts/psp22_token';
import Router from '../types/contracts/router_contract';
import Factory from '../types/contracts/factory_contract';
import { ONE_THOUSAND_STABLECOIN } from './constants';
import { addLiquidityNative } from './utils';
import { parseUnits } from './shared';

// Create a new instance of contract
const wsProvider = new WsProvider(process.env.WS_NODE);
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is not relevant, as the line merely connects to ws.

// Create a keyring instance
const keyring = new Keyring({ type: 'sr25519' });

async function main(): Promise<void> {
const api = await ApiPromise.create({ provider: wsProvider });
const deployer = keyring.addFromUri(process.env.AUTHORITY_SEED);

const {
routerAddress,
factoryAddress,
wnativeAddress,
dogeAddress,
usdcAddress,
usdtAddress,
} = loadAddresses();

const router = new Router(routerAddress, deployer, api);
const factory = new Factory(factoryAddress, deployer, api);

/// Create tokens

const doge = new Token(dogeAddress, deployer, api);
const usdc = new Token(usdcAddress, deployer, api);
const usdt = new Token(usdtAddress, deployer, api);

/// Add liquidity
const dogeAmount = parseUnits(1000, 18).toString();

await doge.tx.approve(router.address, dogeAmount);
console.log('approved 1000 DOGE to spend by router');
await addLiquidityNative(
router,
doge,
dogeAmount,
dogeAmount,
deployer.address,
);
console.log('added 1000 DOGE liquidity');
await usdc.tx.approve(router.address, ONE_THOUSAND_STABLECOIN);
console.log('approved 1000 USDC to spend by router');
await addLiquidityNative(
router,
usdc,
ONE_THOUSAND_STABLECOIN,
ONE_THOUSAND_STABLECOIN,
deployer.address,
);
console.log('added 1000 USDC liquidity');
await usdt.tx.approve(router.address, ONE_THOUSAND_STABLECOIN);
console.log('approved 1000 USDT to spend by router');
await addLiquidityNative(
router,
usdt,
ONE_THOUSAND_STABLECOIN,
ONE_THOUSAND_STABLECOIN,
deployer.address,
);
console.log('added 1000 USDT liquidity');

/// Query pair addresses
const {
value: { ok: dogeWAzeroAddress },
} = await factory.query.getPair(doge.address, wnativeAddress);
console.log('dogeWAzeroAddress', dogeWAzeroAddress);
const {
value: { ok: usdcWAzeroAddress },
} = await factory.query.getPair(usdc.address, wnativeAddress);
console.log('usdcWAzeroAddress', usdcWAzeroAddress);
const {
value: { ok: usdtWAzeroAddress },
} = await factory.query.getPair(usdt.address, wnativeAddress);
console.log('usdtWAzeroAddress', usdtWAzeroAddress);

await api.disconnect();
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
6 changes: 5 additions & 1 deletion scripts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { parseUnits } from './shared';

export const DEADLINE = '111111111111111111';
export const ONE_WAZERO = parseUnits(1, 12).toString();
export const ONE_STABLECOIN = parseUnits(100, 6).toString();
export const ONE_HUNDRED_WAZERO = parseUnits(100, 12).toString();
export const ONE_THOUSAND_WAZERO = parseUnits(1_000, 12).toString();
export const ONE_STABLECOIN = parseUnits(1, 6).toString();
export const ONE_HUNDRED_STABLECOIN = parseUnits(100, 6).toString();
export const ONE_THOUSAND_STABLECOIN = parseUnits(1_000, 6).toString();
export const TOTAL_SUPPLY = parseUnits(1_000_000, 18).toString();
export const STABLE_TOTAL_SUPPLY = parseUnits(1_000_000, 6).toString();
export const DUMMY_ADDRESS =
Expand Down
64 changes: 64 additions & 0 deletions scripts/create_tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api';
import { loadAddresses, storeAddresses } from './utils';
import Token_factory from '../types/constructors/psp22_token';
import * as token from './token';
import { TOTAL_SUPPLY, STABLE_TOTAL_SUPPLY } from './constants';

// Create a new instance of contract
const wsProvider = new WsProvider(process.env.WS_NODE);
// Create a keyring instance
const keyring = new Keyring({ type: 'sr25519' });

async function main(): Promise<void> {
const api = await ApiPromise.create({ provider: wsProvider });
const deployer = keyring.addFromUri(process.env.AUTHORITY_SEED);
const tokenCodeHash = await token.upload(api, deployer);
console.log('token code hash:', tokenCodeHash);

const addresses = loadAddresses();

const tokenFactory = new Token_factory(api, deployer);

/// Create tokens
const tokenInitGas = await token.estimateInit(api, deployer);
const { address: dogeAddress } = await tokenFactory.new(
TOTAL_SUPPLY,
'Doge Coin',
'DOGE',
18,
{ gasLimit: tokenInitGas },
);
console.log('doge coin address', dogeAddress);
const { address: usdcAddress } = await tokenFactory.new(
STABLE_TOTAL_SUPPLY,
'USD Coin',
'USDC',
6,
{ gasLimit: tokenInitGas },
);
console.log('usdc token address:', usdcAddress);
const { address: usdtAddress } = await tokenFactory.new(
STABLE_TOTAL_SUPPLY,
'Tether USD',
'USDT',
6,
{ gasLimit: tokenInitGas },
);
console.log('usdt token address:', usdtAddress);

storeAddresses({
...addresses,
dogeAddress,
usdcAddress,
usdtAddress,
});

console.log('psp22 token addresses stored');

await api.disconnect();
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
114 changes: 0 additions & 114 deletions scripts/example_setup.ts

This file was deleted.

3 changes: 3 additions & 0 deletions scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export type Addresses = {
factoryAddress: string;
wnativeAddress: string;
routerAddress: string;
dogeAddress?: string;
usdcAddress?: string;
usdtAddress?: string;
};
4 changes: 2 additions & 2 deletions scripts/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export async function estimateInit(
): Promise<WeightV2> {
return estimateContractInit(api, deployer, 'psp22_token.contract', [
TOTAL_SUPPLY,
'Apollo Token',
'APLO',
'Doge Coin',
'DOGE',
18,
]);
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Token from '../types/contracts/psp22_token';
import Router from '../types/contracts/router_contract';
import { ONE_WAZERO, DEADLINE } from './constants';
import { DEADLINE, ONE_THOUSAND_WAZERO } from './constants';
import { Addresses } from './shared';
import fs from 'fs';

Expand All @@ -24,11 +24,11 @@ export const addLiquidityNative = async (
token.address,
amountDesired,
amountMin,
ONE_WAZERO,
ONE_THOUSAND_WAZERO,
to,
DEADLINE,
{
value: ONE_WAZERO,
value: ONE_THOUSAND_WAZERO,
},
);
};
Expand Down