Skip to content

Commit

Permalink
governance abi support
Browse files Browse the repository at this point in the history
  • Loading branch information
RepoContributor committed Jun 7, 2021
1 parent ceda1e8 commit 92eebde
Show file tree
Hide file tree
Showing 29 changed files with 264 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/components/Balance.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import { asyncTimeout, formatBigInt, parseDecimal } from '../lib/utils'
import { showToast } from '../stores/toasts'
import deposit from '../lib/deposit'
import withdraw from '../lib/withdraw'
import deposit from '../lib/trading/deposit'
import withdraw from '../lib/trading/withdraw'
let input;
Expand Down
4 changes: 2 additions & 2 deletions src/components/NewOrder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import Button from './Button.svelte'
import { productToFigi } from '../lib/products'
import getProductInfo from '../lib/getProductInfo'
import submitOrder from '../lib/submitOrder'
import getProductInfo from '../lib/products/getProductInfo'
import submitOrder from '../lib/trading/submitOrder'
import { selectedProduct } from '../stores/main'
import { showToast } from '../stores/toasts'
import { formatBigInt, parseDecimal } from '../lib/utils'
Expand Down
4 changes: 2 additions & 2 deletions src/components/Positions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import { positions } from '../stores/positions'
import { showToast } from '../stores/toasts'
import { formatBigInt, parseDecimal } from '../lib/utils'
import submitOrderUpdate from '../lib/submitOrderUpdate'
import getProductInfo from '../lib/getProductInfo'
import submitOrderUpdate from '../lib/trading/submitOrderUpdate'
import getProductInfo from '../lib/products/getProductInfo'
import getBlockByNumber from '../lib/getBlockByNumber'
import { figiToProduct } from '../lib/products'
Expand Down
16 changes: 0 additions & 16 deletions src/lib/approveDAI.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export const EMPTY_BYTES32 = '0x' + '0'.repeat(64);
export const CONTRACTS = {
'0x8376940b1db0': { // arbitrum testnet 5
DAI: '0xf011dd14767Da5506c46b2bC3D1659dB758e0683'.toLowerCase(),
CAP: '0x26b8c17B21B52B265fC05eC367D621eb02a8726f'.toLowerCase(),
PRODUCTS: '0x76511C9cC16248E6BaF6EC0D514986d41BE888Ee'.toLowerCase(),
TRADING: '0x427ABE67aEBBdC1382d97B656cC67c53D91a03e7'.toLowerCase(),
TREASURY: '0xFb78eeFaE670a001edA2216894eE252b612AF903'.toLowerCase()
TREASURY: '0xFb78eeFaE670a001edA2216894eE252b612AF903'.toLowerCase(),
GOVERNANCE: '0xe9CFBDE58582732c03E191f13A837e23cEdA366C'.toLowerCase()
}
/*
'0x1': {
Expand Down
19 changes: 19 additions & 0 deletions src/lib/governance/castVote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getAddress, encodeUint, encodeBool } from '../utils'
import ethSend from '../ethSend'

export default async function castVote(params) {

console.log('params', params);

const {
proposalId,
support
} = params;

return ethSend({
address: getAddress('GOVERNANCE'),
method: 'castVote(uint256,bool)',
data: encodeUint(proposalId) + encodeBool(support)
});

}
18 changes: 18 additions & 0 deletions src/lib/governance/executeProposal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getAddress, encodeUint } from '../utils'
import ethSend from '../ethSend'

export default async function executeProposal(params) {

console.log('params', params);

const {
proposalId
} = params;

return ethSend({
address: getAddress('GOVERNANCE'),
method: 'executeProposal(uint256)',
data: encodeUint(proposalId)
});

}
29 changes: 29 additions & 0 deletions src/lib/governance/getProposal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import ethCall from '../ethCall'
import { getAddress, encodeUint, abiDecodeOutput } from '../utils'

export default function proposals(params) {

const {
proposal_id
} = params;

return ethCall({
address: getAddress('GOVERNANCE'),
method: 'proposals(uint256)',
data: encodeUint(proposal_id)
}).then((result) => {
return abiDecodeOutput(result, [
{name: 'id', type: 'uint256'},
{name: 'proposer', type: 'address'},
{name: 'startBlock', type: 'uint256'},
{name: 'endBlock', type: 'uint256'},
{name: 'expirationBlock', type: 'uint256'},
{name: 'forVotes', type: 'uint256'},
{name: 'againstVotes', type: 'uint256'},
{name: 'canceled', type: 'bool'},
{name: 'executed', type: 'bool'},
{name: 'expedited', type: 'bool'}
]);
});

}
24 changes: 24 additions & 0 deletions src/lib/governance/getStakedBalance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ethCall from '../ethCall'
import { get } from 'svelte/store'
import { user } from '../../stores/main'
import { getAddress, encodeAddress } from '../utils'

export default function getStakedBalance(params) {

const _user = get(user);

if (!_user) return 0n;

return ethCall({
address: getAddress('GOVERNANCE'),
method: 'balanceOf(address)',
data: encodeAddress(_user)
}).then((balance) => {
// console.log('got balance', balance);
if (balance == '0x') return 0n;
return BigInt(balance);
}).catch((e) => {
console.log('balance err', e);
});

}
12 changes: 12 additions & 0 deletions src/lib/governance/proposalCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getAddress } from '../utils'
import ethCall from '../ethCall'

export default function proposalCount(params) {

return ethCall({
address: getAddress('GOVERNANCE'),
method: 'proposalCount()',
data: ''
}).then(BigInt);

}
18 changes: 18 additions & 0 deletions src/lib/governance/releaseStaked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getAddress, encodeUint } from '../utils'
import ethSend from '../ethSend'

export default async function stakeAmount(params) {

console.log('params', params);

const {
amount
} = params;

return ethSend({
address: getAddress('GOVERNANCE'),
method: 'releaseStaked(uint256)',
data: encodeUint(amount)
});

}
34 changes: 34 additions & 0 deletions src/lib/governance/stakeAmount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getAddress, encodeUint } from '../utils'
import ethSend from '../ethSend'
import getAllowance from '../token/getAllowance'
import approve from '../token/approve'

export default async function stakeAmount(params) {

console.log('params', params);

const {
amount
} = params;

const GOVERNANCE_ADDRESS = getAddress('GOVERNANCE');

const allowance = await getAllowance({
address: getAddress('CAP'),
spender: GOVERNANCE_ADDRESS
})

console.log('allowance', allowance, amount, name, nonce);

// approve if allowance not enough
if (allowance < 100n * amount) {
await approve({symbol: 'CAP', spender: GOVERNANCE_ADDRESS});
}

return ethSend({
address: GOVERNANCE_ADDRESS,
method: 'stakeToVote(uint256)',
data: encodeUint(amount)
});

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ethCall from './ethCall'
import { getAddress, encodeBytes32, encodeUint, decodeUint } from './utils'
import ethCall from '../ethCall'
import { getAddress, encodeBytes32, encodeUint, decodeUint } from '../utils'

let cache = {}

Expand Down
19 changes: 19 additions & 0 deletions src/lib/token/approve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ethSend from '../ethSend'
import { getAddress, encodeAddress, encodeUint } from '../utils'

export default async function approve(params) {

const {
symbol,
spender
} = params

return ethSend({
address: getAddress(symbol),
method: 'approve(address,uint256)',
data: encodeAddress(spender) + BigInt('0x' + 'F'.repeat(64)).toString()
}).then((x,e) => {
console.log('got x', x, e)
});

}
6 changes: 3 additions & 3 deletions src/lib/getAllowance.js → src/lib/token/getAllowance.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get } from 'svelte/store'
import { user } from '../stores/main'
import { getAddress, encodeAddress } from './utils'
import ethCall from './ethCall'
import { user } from '../../stores/main'
import { getAddress, encodeAddress } from '../utils'
import ethCall from '../ethCall'

export default function getAllowance(params) {

Expand Down
6 changes: 3 additions & 3 deletions src/lib/getBalance.js → src/lib/token/getBalance.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ethCall from './ethCall'
import ethCall from '../ethCall'
import { get } from 'svelte/store'
import { user } from '../stores/main'
import { getAddress, encodeAddress } from './utils'
import { user } from '../../stores/main'
import { getAddress, encodeAddress } from '../utils'

export default function getBalance(address) {

Expand Down
4 changes: 2 additions & 2 deletions src/lib/getName.js → src/lib/token/getName.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ethCall from './ethCall'
import { decodeString } from './utils'
import ethCall from '../ethCall'
import { decodeString } from '../utils'

const cache = {};

Expand Down
6 changes: 3 additions & 3 deletions src/lib/getNonce.js → src/lib/token/getNonce.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get } from 'svelte/store'
import { user } from '../stores/main'
import { encodeAddress } from './utils'
import ethCall from './ethCall'
import { user } from '../../stores/main'
import { encodeAddress } from '../utils'
import ethCall from '../ethCall'

export default function getNonce(address) {

Expand Down
4 changes: 2 additions & 2 deletions src/lib/requestFaucet.js → src/lib/token/requestFaucet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ethSend from './ethSend'
import { getAddress } from './utils'
import ethSend from '../ethSend'
import { getAddress } from '../utils'

export default function requestFaucet(address) {

Expand Down
27 changes: 10 additions & 17 deletions src/lib/deposit.js → src/lib/trading/deposit.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { EMPTY_BYTES32, BIGINT_ZERO } from './constants'
import { EMPTY_BYTES32, BIGINT_ZERO } from '../constants'
import { get } from 'svelte/store'
import { user } from '../stores/main'
import {
getAddress,
encodeMethodSignature,
encodeBytes32,
encodeAddress,
encodeUint
} from './utils'

import ethSend from './ethSend'
import sign from './sign'
import getNonce from './getNonce'
import getName from './getName'
import getAllowance from './getAllowance'
import approveDAI from './approveDAI'
import { user } from '../../stores/main'
import { getAddress, encodeMethodSignature, encodeBytes32, encodeAddress, encodeUint } from '../utils'
import ethSend from '../ethSend'
import sign from '../sign'
import getNonce from '../token/getNonce'
import getName from '../token/getName'
import getAllowance from '../token/getAllowance'
import approve from '../token/approve'

export default async function deposit(params) {

Expand Down Expand Up @@ -46,7 +39,7 @@ export default async function deposit(params) {

// approve if allowance not enough
if (allowance < 100n * amount) {
approveDAI(getAddress('TRADING'));
await approve({symbol: 'DAI', spender: getAddress('TRADING')});
}

const { v, r, s } = signature;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/getPositions.js → src/lib/trading/getPositions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ethCall from './ethCall'
import ethCall from '../ethCall'
import { get } from 'svelte/store'
import { user } from '../stores/main'
import { getAddress, encodeAddress, decodeUint, decodeBytes32,decodeAddress } from './utils'
import { user } from '../../stores/main'
import { getAddress, encodeAddress, decodeUint, decodeBytes32,decodeAddress } from '../utils'

export default function getPositions(address) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ethCall from './ethCall'
import ethCall from '../ethCall'
import { get } from 'svelte/store'
import { user } from '../stores/main'
import { getAddress, encodeAddress, decodeUint, decodeBytes32,decodeAddress } from './utils'
import { user } from '../../stores/main'
import { getAddress, encodeAddress, decodeUint, decodeBytes32,decodeAddress } from '../utils'

export default function getUserFreeMargin() {

Expand Down
15 changes: 4 additions & 11 deletions src/lib/submitOrder.js → src/lib/trading/submitOrder.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { EMPTY_BYTES32, BIGINT_ZERO } from './constants'
import { EMPTY_BYTES32, BIGINT_ZERO } from '../constants'
import { get } from 'svelte/store'
import { user } from '../stores/main'
import {
getAddress,
encodeMethodSignature,
encodeBytes32,
encodeAddress,
encodeUint
} from './utils'

import ethSend from './ethSend'
import { user } from '../../stores/main'
import { getAddress, encodeMethodSignature, encodeBytes32, encodeAddress, encodeUint } from '../utils'
import ethSend from '../ethSend'

export default async function submitOrder(params) {

Expand Down
Loading

0 comments on commit 92eebde

Please sign in to comment.