-
Notifications
You must be signed in to change notification settings - Fork 17
Token trade plugin added #521
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
3c4c842
[WIP] Token trade plugin added but not tested yet
namesty 98605c8
Eliminate indexed 'proposal' field on TokenTrade proposal type
namesty 22ac365
[WIP] TokenTrade Proposal tests
namesty f8aee15
Names and types tweaks
namesty 00b7a59
Updated packages with new migration version
namesty 4519907
updated test env version
namesty d736292
Token trade tests (haven't run yet) and type changes
namesty 7a47625
Comparison fix on trade test
namesty 7a893c9
[WIP] Test missing redeem step
namesty f1737f8
tests works
cbrzn 6c34993
fix lint error
cbrzn 9f70fa2
removing logs
cbrzn 87d384b
improving tests
cbrzn bd9a15d
[WIP] Approve tokens method
namesty 323006a
Merge remote-tracking branch 'dOrgTech/token-trade' into token-trade
namesty 9e3ebfe
trying to make approve work
cbrzn 9ae640a
Token approval fix
namesty 3c22550
Merge branch 'client-2-0' into token-trade
namesty 0f8a902
Build fixes
namesty fdc1801
creating token trade on test
cbrzn 51585c0
tests fixed
cbrzn 99a4c16
lint fixed
cbrzn 2a04fab
waiting for plugin to be index
cbrzn c9e03b7
trying to fix test
cbrzn 6c68572
Plugin Address fix
namesty 4b63073
test env updated
cbrzn 8e046cf
Merge branch 'token-trade' of github.com:dOrgTech/client into token-t…
cbrzn 5c5c700
removing old changes
cbrzn b83ae19
hardcoding erc20 token abi based on Oren's feedback
cbrzn 2f59022
tests for approve tokens created
cbrzn fdfce3d
improving approval test
cbrzn 154f1a1
bump version
cbrzn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import BN from 'bn.js' | |
import { Contract, Signer } from 'ethers' | ||
import { providers } from 'ethers' | ||
import 'ethers/dist/shims' | ||
import { JsonRpcProvider } from 'ethers/providers' | ||
import { BigNumber } from 'ethers/utils' | ||
import gql from 'graphql-tag' | ||
import { Observable, Observer, of } from 'rxjs' | ||
|
@@ -627,6 +628,31 @@ export class Arc extends GraphNodeObserver { | |
Logger.debug(`Data saved successfully as ${descriptionHash}`) | ||
return descriptionHash | ||
} | ||
|
||
public approveTokens(tokenAddress: Address, spender: Address, amount: BN) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any test which cover that ? |
||
const signer = (this.web3 as JsonRpcProvider).getSigner(this.defaultAccount as any) | ||
const abi = [ | ||
{ | ||
constant: false, | ||
inputs: [ | ||
{ name: '_spender', type: 'address' }, | ||
{ name: '_value', type: 'uint256' } | ||
], | ||
name: 'approve', | ||
outputs: [{ name: '', type: 'bool' }], | ||
payable: false, | ||
stateMutability: 'nonpayable', | ||
type: 'function' | ||
} | ||
] | ||
const tokenContract = new Contract(tokenAddress, abi, signer) | ||
|
||
return this.sendTransaction({ | ||
contract: tokenContract, | ||
method: 'approve', | ||
args: [spender, amount.toString()] | ||
}) | ||
} | ||
} | ||
|
||
export interface IContractInfo { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './plugin' | ||
export * from './proposal' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import BN from 'bn.js' | ||
import { DocumentNode } from 'graphql' | ||
import gql from 'graphql-tag' | ||
import { | ||
Address, | ||
Arc, | ||
getEventArgs, | ||
IGenesisProtocolParams, | ||
IPluginState, | ||
IProposalBaseCreateOptions, | ||
ITransaction, | ||
ITransactionReceipt, | ||
mapGenesisProtocolParams, | ||
Plugin, | ||
ProposalPlugin, | ||
transactionErrorHandler, | ||
transactionResultHandler | ||
} from '../../index' | ||
import { ITokenTradeProposalState, TokenTradeProposal } from './proposal' | ||
|
||
export interface ITokenTradeState extends IPluginState { | ||
pluginParams: { | ||
votingMachine: Address | ||
voteParams: IGenesisProtocolParams | ||
} | ||
} | ||
|
||
export interface IProposalCreateOptionsTokenTrade extends IProposalBaseCreateOptions { | ||
sendTokenAddress: Address, | ||
sendTokenAmount: number, | ||
receiveTokenAddress: Address, | ||
receiveTokenAmount: number, | ||
descriptionHash: string | ||
} | ||
|
||
export interface IInitParamsTT { | ||
daoId: string | ||
votingMachine: string | ||
votingParams: number[] | ||
voteOnBehalf: string | ||
voteParamsHash: string | ||
} | ||
|
||
export class TokenTrade extends ProposalPlugin< | ||
ITokenTradeState, | ||
ITokenTradeProposalState, | ||
IProposalCreateOptionsTokenTrade> { | ||
|
||
public static get fragment() { | ||
if (!this.fragmentField) { | ||
this.fragmentField = { | ||
name: 'TokenTradeParams', | ||
fragment: gql` fragment TokenTradeParams on ControllerScheme { | ||
tokenTradeParams { | ||
id | ||
votingMachine | ||
voteParams { | ||
id | ||
queuedVoteRequiredPercentage | ||
queuedVotePeriodLimit | ||
boostedVotePeriodLimit | ||
preBoostedVotePeriodLimit | ||
thresholdConst | ||
limitExponentValue | ||
quietEndingPeriod | ||
proposingRepReward | ||
votersReputationLossRatio | ||
minimumDaoBounty | ||
daoBountyConst | ||
activationTime | ||
voteOnBehalf | ||
} | ||
} | ||
}` | ||
} | ||
} | ||
|
||
return this.fragmentField | ||
} | ||
|
||
public static initializeParamsMap(initParams: IInitParamsTT) { | ||
|
||
Object.keys(initParams).forEach((key) => { | ||
if (initParams[key] === undefined) { | ||
throw new Error(`TokenTrade's initialize parameter '${key}' cannot be undefined`) | ||
} | ||
}) | ||
|
||
return [ | ||
initParams.daoId, | ||
initParams.votingMachine, | ||
initParams.votingParams, | ||
initParams.voteOnBehalf, | ||
initParams.voteParamsHash | ||
] | ||
} | ||
|
||
public static itemMap(context: Arc, item: any, queriedId?: string): ITokenTradeState | null { | ||
if (!item) { | ||
return null | ||
} | ||
|
||
if (!item.tokenTradeParams) { | ||
throw new Error(`Plugin ${queriedId ? `with id '${queriedId}'` : ''}wrongly instantiated as TokenTrade Plugin`) | ||
} | ||
|
||
const baseState = Plugin.itemMapToBaseState(context, item) | ||
|
||
const tokenTradeParams = { | ||
voteParams: mapGenesisProtocolParams(item.tokenTradeParams.voteParams), | ||
votingMachine: item.tokenTradeParams.votingMachine | ||
} | ||
|
||
return { | ||
...baseState, | ||
pluginParams: tokenTradeParams | ||
} | ||
} | ||
|
||
private static fragmentField: { name: string, fragment: DocumentNode } | undefined | ||
|
||
public async createProposalTransaction(options: IProposalCreateOptionsTokenTrade): Promise<ITransaction> { | ||
|
||
if (options.plugin === undefined) { | ||
throw new Error(`Missing argument "plugin" for TokenTrade in Proposal.create()`) | ||
} | ||
if (!options.receiveTokenAddress) { | ||
throw new Error(`Missing argument "receiveTokenAddress" for TokenTrade in Proposal.create()`) | ||
} | ||
if (!options.sendTokenAddress) { | ||
throw new Error(`Missing argument "sendTokenAddress" for TokenTrade in Proposal.create()`) | ||
} | ||
if (options.receiveTokenAmount <= 0) { | ||
throw new Error(`Argument "receiveTokenAmount" must be greater than 0 for TokenTrade in Proposal.create()`) | ||
} | ||
if (options.sendTokenAmount <= 0) { | ||
throw new Error(`Argument "sendTokenAmount" must be greater than 0 for TokenTrade in Proposal.create()`) | ||
} | ||
|
||
if (!options.descriptionHash) { | ||
options.descriptionHash = await this.context.saveIPFSData(options) | ||
} | ||
|
||
const { address: pluginAddress } = await this.fetchState() | ||
|
||
await this.context.approveTokens(options.sendTokenAddress, pluginAddress, new BN(options.sendTokenAmount)).send() | ||
|
||
return { | ||
contract: this.context.getContract(pluginAddress), | ||
method: 'proposeTokenTrade', | ||
args: [ | ||
options.sendTokenAddress, | ||
options.sendTokenAmount, | ||
options.receiveTokenAddress, | ||
options.receiveTokenAmount, | ||
options.descriptionHash | ||
] | ||
} | ||
} | ||
|
||
public createProposalTransactionMap(): transactionResultHandler<any> { | ||
return async (receipt: ITransactionReceipt) => { | ||
const args = getEventArgs(receipt, 'TokenTradeProposed', 'TokenTrade.createProposal') | ||
const proposalId = args[1] | ||
return new TokenTradeProposal(this.context, proposalId) | ||
} | ||
} | ||
|
||
public createProposalErrorHandler(options: IProposalCreateOptionsTokenTrade): transactionErrorHandler { | ||
return async (err) => { | ||
throw err | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.