-
Notifications
You must be signed in to change notification settings - Fork 133
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
Replace this.token
with methods on TokenContract
#1446
Merged
Merged
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
80a84cc
move token() off of account update, deprecate on smart contract
mitschabaude d975101
additional refactor, rename to TokenContract.internal
mitschabaude b721057
fix tests where immediately possible
mitschabaude 56bd4c4
tweak comment
mitschabaude 8f7546c
refactor dex with actions to be a token contract
mitschabaude 7b24cc0
refactor dex to token contract
mitschabaude da7786e
tweak comment
mitschabaude f64e6cc
adapt example
mitschabaude cafbb52
isNew precondition
mitschabaude e5b2845
changelog
mitschabaude be96191
method to generate random keypair
mitschabaude 1b89d81
fix token contract test
mitschabaude 358aa27
fix dex deploy scripts to respect isNew precondition
mitschabaude 8e9a616
changelog
mitschabaude 71087ac
dump vks
mitschabaude a6a06e4
remove obsolete example
mitschabaude 40ebf95
fixup upgradability test
mitschabaude 6be807b
another fix
mitschabaude 88d9a70
document token contract deploy
mitschabaude d8079fd
minor
mitschabaude 01614a5
link in comment
mitschabaude 41ba6e4
Merge branch 'main' into feature/move-token-helpers
mitschabaude 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 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 was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import { | ||
Account, | ||
AccountUpdate, | ||
AccountUpdateForest, | ||
Field, | ||
InferProvable, | ||
Mina, | ||
|
@@ -16,24 +17,31 @@ import { | |
SmartContract, | ||
State, | ||
Struct, | ||
TokenContract, | ||
TokenId, | ||
UInt64, | ||
method, | ||
state, | ||
} from 'o1js'; | ||
|
||
import { randomAccounts } from './dex.js'; | ||
import { TrivialCoin as TokenContract } from './erc20.js'; | ||
import { TrivialCoin } from './erc20.js'; | ||
|
||
export { Dex, DexTokenHolder, addresses, getTokenBalances, keys, tokenIds }; | ||
|
||
class RedeemAction extends Struct({ address: PublicKey, dl: UInt64 }) {} | ||
|
||
class Dex extends SmartContract { | ||
class Dex extends TokenContract { | ||
// addresses of token contracts are constants | ||
tokenX = addresses.tokenX; | ||
tokenY = addresses.tokenY; | ||
|
||
// Approvable API | ||
|
||
@method approveBase(forest: AccountUpdateForest) { | ||
this.checkZeroBalanceChange(forest); | ||
} | ||
|
||
/** | ||
* state that keeps track of total lqXY supply -- this is needed to calculate what to return when redeeming liquidity | ||
* | ||
|
@@ -71,7 +79,7 @@ class Dex extends SmartContract { | |
} | ||
|
||
@method createAccount() { | ||
this.token.mint({ address: this.sender, amount: UInt64.from(0) }); | ||
this.internal.mint({ address: this.sender, amount: UInt64.from(0) }); | ||
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.
|
||
} | ||
|
||
/** | ||
|
@@ -86,14 +94,14 @@ class Dex extends SmartContract { | |
*/ | ||
@method supplyLiquidityBase(dx: UInt64, dy: UInt64): UInt64 { | ||
let user = this.sender; | ||
let tokenX = new TokenContract(this.tokenX); | ||
let tokenY = new TokenContract(this.tokenY); | ||
let tokenX = new TrivialCoin(this.tokenX); | ||
let tokenY = new TrivialCoin(this.tokenY); | ||
|
||
// get balances of X and Y token | ||
let dexX = AccountUpdate.create(this.address, tokenX.token.id); | ||
let dexX = AccountUpdate.create(this.address, tokenX.deriveTokenId()); | ||
let x = dexX.account.balance.getAndRequireEquals(); | ||
|
||
let dexY = AccountUpdate.create(this.address, tokenY.token.id); | ||
let dexY = AccountUpdate.create(this.address, tokenY.deriveTokenId()); | ||
let y = dexY.account.balance.getAndRequireEquals(); | ||
|
||
// // assert dy === [dx * y/x], or x === 0 | ||
|
@@ -108,7 +116,7 @@ class Dex extends SmartContract { | |
// calculate liquidity token output simply as dl = dx + dx | ||
// => maintains ratio x/l, y/l | ||
let dl = dy.add(dx); | ||
this.token.mint({ address: user, amount: dl }); | ||
this.internal.mint({ address: user, amount: dl }); | ||
|
||
// update l supply | ||
let l = this.totalSupply.get(); | ||
|
@@ -157,7 +165,7 @@ class Dex extends SmartContract { | |
*/ | ||
@method redeemInitialize(dl: UInt64) { | ||
this.reducer.dispatch(new RedeemAction({ address: this.sender, dl })); | ||
this.token.burn({ address: this.sender, amount: dl }); | ||
this.internal.burn({ address: this.sender, amount: dl }); | ||
// TODO: preconditioning on the state here ruins concurrent interactions, | ||
// there should be another `finalize` DEX method which reduces actions & updates state | ||
this.totalSupply.set(this.totalSupply.getAndRequireEquals().sub(dl)); | ||
|
@@ -186,8 +194,8 @@ class Dex extends SmartContract { | |
* the called methods which requires proof authorization. | ||
*/ | ||
swapX(dx: UInt64): UInt64 { | ||
let tokenY = new TokenContract(this.tokenY); | ||
let dexY = new DexTokenHolder(this.address, tokenY.token.id); | ||
let tokenY = new TrivialCoin(this.tokenY); | ||
let dexY = new DexTokenHolder(this.address, tokenY.deriveTokenId()); | ||
let dy = dexY.swap(this.sender, dx, this.tokenX); | ||
tokenY.transfer(dexY.self, this.sender, dy); | ||
return dy; | ||
|
@@ -204,16 +212,12 @@ class Dex extends SmartContract { | |
* the called methods which requires proof authorization. | ||
*/ | ||
swapY(dy: UInt64): UInt64 { | ||
let tokenX = new TokenContract(this.tokenX); | ||
let dexX = new DexTokenHolder(this.address, tokenX.token.id); | ||
let tokenX = new TrivialCoin(this.tokenX); | ||
let dexX = new DexTokenHolder(this.address, tokenX.deriveTokenId()); | ||
let dx = dexX.swap(this.sender, dy, this.tokenY); | ||
tokenX.transfer(dexX.self, this.sender, dx); | ||
return dx; | ||
} | ||
|
||
@method transfer(from: PublicKey, to: PublicKey, amount: UInt64) { | ||
this.token.send({ from, to, amount }); | ||
} | ||
} | ||
|
||
class DexTokenHolder extends SmartContract { | ||
|
@@ -292,10 +296,10 @@ class DexTokenHolder extends SmartContract { | |
): UInt64 { | ||
// we're writing this as if our token === y and other token === x | ||
let dx = otherTokenAmount; | ||
let tokenX = new TokenContract(otherTokenAddress); | ||
let tokenX = new TrivialCoin(otherTokenAddress); | ||
|
||
// get balances of X and Y token | ||
let dexX = AccountUpdate.create(this.address, tokenX.token.id); | ||
let dexX = AccountUpdate.create(this.address, tokenX.deriveTokenId()); | ||
let x = dexX.account.balance.getAndRequireEquals(); | ||
let y = this.account.balance.getAndRequireEquals(); | ||
|
||
|
This file contains 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this example was meant as a reproduction at a time where the missing 'access' permission caused a security hole for token contracts, and is long obsolete