-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Jeff-CCH/feat/integrate-permit2
Add Permit2 Pull Token Logics
- Loading branch information
Showing
10 changed files
with
166 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@protocolink/logics': patch | ||
--- | ||
|
||
add Permit2 pull token logics |
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as core from '@protocolink/core'; | ||
|
||
export const supportedChainIds = Object.keys(core.contractAddressMap).map((chainId) => Number(chainId)); |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './configs'; | ||
export * from './logic.pull-token'; |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { LogicTestCase } from 'test/types'; | ||
import { PullTokenLogic, PullTokenLogicFields } from './logic.pull-token'; | ||
import * as common from '@protocolink/common'; | ||
import { constants, utils } from 'ethers'; | ||
import * as core from '@protocolink/core'; | ||
import { expect } from 'chai'; | ||
import { mainnetTokens } from '@protocolink/test-helpers'; | ||
|
||
describe('Permit2 PullTokenLogic', function () { | ||
const chainId = common.ChainId.mainnet; | ||
const logic = new PullTokenLogic(chainId); | ||
|
||
context('Test build', function () { | ||
const routerKit = new core.RouterKit(chainId); | ||
const account = '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa'; | ||
const iface = routerKit.permit2Iface; | ||
|
||
const testCases: LogicTestCase<PullTokenLogicFields>[] = [ | ||
{ fields: { input: new common.TokenAmount(mainnetTokens.WETH, '1') } }, | ||
]; | ||
|
||
testCases.forEach(({ fields }, i) => { | ||
it(`case ${i + 1}`, async function () { | ||
const routerLogic = await logic.build(fields, { account }); | ||
const sig = routerLogic.data.substring(0, 10); | ||
const permit2Address = await routerKit.getPermit2Address(); | ||
|
||
expect(routerLogic.to).to.eq(permit2Address); | ||
expect(utils.isBytesLike(routerLogic.data)).to.be.true; | ||
expect(sig).to.eq(iface.getSighash('transferFrom(address,address,uint160,address)')); | ||
expect(routerLogic.inputs).to.deep.eq([]); | ||
expect(routerLogic.approveTo).to.eq(constants.AddressZero); | ||
expect(routerLogic.callback).to.eq(constants.AddressZero); | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as core from '@protocolink/core'; | ||
import { supportedChainIds } from './configs'; | ||
|
||
export type PullTokenLogicFields = core.TokenInFields; | ||
|
||
export type PullTokenLogicOptions = Pick<core.GlobalOptions, 'account'>; | ||
|
||
@core.LogicDefinitionDecorator() | ||
export class PullTokenLogic extends core.Logic implements core.LogicBuilderInterface { | ||
static readonly supportedChainIds = supportedChainIds; | ||
|
||
async build(fields: PullTokenLogicFields, options: PullTokenLogicOptions) { | ||
const { input } = fields; | ||
const { account } = options; | ||
const userAgent = await this.calcAgent(account); | ||
const to = await this.getPermit2Address(); | ||
const data = this.permit2Iface.encodeFunctionData('transferFrom(address,address,uint160,address)', [ | ||
account, | ||
userAgent, | ||
input.amountWei, | ||
input.token.address, | ||
]); | ||
|
||
return core.newLogic({ to, data }); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; | ||
import { claimToken, getChainId, mainnetTokens, snapshotAndRevertEach } from '@protocolink/test-helpers'; | ||
import * as common from '@protocolink/common'; | ||
import * as core from '@protocolink/core'; | ||
import { expect } from 'chai'; | ||
import hre from 'hardhat'; | ||
import * as permit2 from 'src/logics/permit2'; | ||
import * as utils from 'test/utils'; | ||
|
||
describe('mainnet: Test Permit2 PullToken Logic', function () { | ||
let chainId: number; | ||
let user: SignerWithAddress; | ||
let routerKit: core.RouterKit; | ||
let agent: string; | ||
|
||
before(async function () { | ||
chainId = await getChainId(); | ||
[, user] = await hre.ethers.getSigners(); | ||
routerKit = new core.RouterKit(chainId); | ||
agent = await routerKit.calcAgent(user.address); | ||
|
||
await claimToken(chainId, user.address, mainnetTokens.USDC, '100'); | ||
await claimToken(chainId, user.address, mainnetTokens.WETH, '100'); | ||
}); | ||
|
||
snapshotAndRevertEach(); | ||
|
||
const testCases = [ | ||
{ | ||
fields: { input: new common.TokenAmount(mainnetTokens.USDC, '1') }, | ||
}, | ||
{ | ||
fields: { input: new common.TokenAmount(mainnetTokens.WETH, '1') }, | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ fields }, i) => { | ||
it(`case ${i + 1}`, async function () { | ||
// 1. build tokensReturn | ||
const input = fields.input; | ||
const permit2PullTokenLogic = new permit2.PullTokenLogic(chainId); | ||
const tokensReturn: string[] = []; | ||
const funds = new common.TokenAmounts(fields.input); | ||
|
||
// 2. build router logics | ||
const routerLogics: core.DataType.LogicStruct[] = []; | ||
routerLogics.push(await permit2PullTokenLogic.build(fields, { account: user.address })); | ||
|
||
// 3. get router permit2 datas | ||
const permit2Datas = await utils.approvePermit2AndGetPermit2Datas(chainId, user, funds.erc20); | ||
|
||
// 4. send router tx | ||
const transactionRequest = routerKit.buildExecuteTransactionRequest({ | ||
permit2Datas, | ||
routerLogics, | ||
tokensReturn, | ||
value: funds.native?.amountWei ?? 0, | ||
}); | ||
|
||
await expect(user.sendTransaction(transactionRequest)).to.not.be.reverted; | ||
await expect(user.address).to.changeBalance(input.token, -input.amount); | ||
await expect(agent).to.changeBalance(input.token, input.amount); | ||
}); | ||
}); | ||
}); |
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