Skip to content

Commit

Permalink
Merge branch 'master' into add-eth-token-whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Oct 30, 2023
2 parents 29b5e0f + 4865a3c commit e779cd4
Show file tree
Hide file tree
Showing 35 changed files with 1,581 additions and 1,342 deletions.
38 changes: 19 additions & 19 deletions alephium/artifacts/.deployments.devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"signature": "241350fac4d7f45c4b69300729cf89758748b4f7dff95bcebd7dedb1c2d1a1e72cc9cbc24b35e4ddefce53840eca6bb984d6c0e46443a24d3afe4cf0e11c7ce3",
"gasPrice": "100000000000",
"gasAmount": 57919,
"blockHash": "e42ba95d71fb40d3fda115ff99a3a78caa823fb4fbfc29c4156a037540807740",
"blockHash": "81926010a748096ec105ad9e218c961c2218d928303fe6ec291562902d259610",
"codeHash": "1fe28b45549ce9d2ec4216af57bab8833bf6d80ee12ebafd9ce26515fabf4494",
"contractInstance": {
"address": "ueqikDZdceZyKqmjy67woT4oP4kyj3siqp4forHdnmro",
Expand All @@ -21,7 +21,7 @@
"signature": "03adbf71697d962738d3ac16068cfe1f470f3fef23e45cb13e9a2a225a653e1e0ef9f5c37a6cdb03654489934d671a1f2c4337373a59d03ce02ca07741aeae40",
"gasPrice": "100000000000",
"gasAmount": 58053,
"blockHash": "e83f214bfa70d3ba13b6a7a3f5a126fe3ea7f7f31d4da3d86374991dd1b37cd0",
"blockHash": "2b2aeef87d1d94a4fb085394481dce9e8577bb0c8d5aea40fce7fffdc4332cb0",
"codeHash": "3f8c4a0d32504a9a51f59c020e53b637cb062870413715c8226bce3251ade221",
"contractInstance": {
"address": "26wUqa2iFTaEyU9x62kiTh63AJYuEFPaxBYdzBANLgeAT",
Expand All @@ -35,7 +35,7 @@
"signature": "45e445b3f9f9121f73217ae15b28dddc2b7dfa58026103f10d99dcad46d0d0fa49d23bb985eb1cec073a9c2deff147138bf004ecb086e4e35fa10a5d152e53f6",
"gasPrice": "100000000000",
"gasAmount": 58479,
"blockHash": "b3b96da3a34a1ce7af6b0d02822228bc04f48dc67b9587a5ee02ae267e2d9150",
"blockHash": "06f80d236a1f5b952e5f2388707a49b852c7312e6f20e57bbfc5141ae8dd9b80",
"codeHash": "aa373a72fc20f97eb84a8c2cf2aae520a9a2bd89a218e3529c99b4cb7ab779c7",
"contractInstance": {
"address": "214vaa7KQb4zrAtBantc6Fj5JSA7z2scYpPB5DyjVJmPm",
Expand All @@ -49,7 +49,7 @@
"signature": "fe08b6b80b04ae94361ca6f3f37b0a30b1222edb3159c64b5c2ee0fedd5ec7f5304e3f45dd0c8ea40f685c9d85f06477762d90de9288e7fb1aeee80e44eb9d88",
"gasPrice": "100000000000",
"gasAmount": 57786,
"blockHash": "012849e4586d2f21e6f0461c01167b4717c0db733cc7c7eeacbe607de7f9fda0",
"blockHash": "37cf268268dea39031890ab87e7ab613cd21c69e48f5eeb4204a494429201120",
"codeHash": "54360922965cf998e03dad9bda9a1860e1e0d52d30ce6f6b251b846dcd6e2bca",
"contractInstance": {
"address": "24R9AWKrWn5tKrDWZwHAGSpypS2fseBnzfRknrZdHwNRm",
Expand All @@ -63,7 +63,7 @@
"signature": "3d1223fff34363836817d777008cf50d804881e55a0fbad790fb344bdf80f42a7534af1351fc1be44c474edf567839bf7495186097f262b794f2bd6e3b2e7f55",
"gasPrice": "100000000000",
"gasAmount": 57257,
"blockHash": "d8548c3a31225e7005076f591280dff65f0e6bc84af90d6b7180d7c8194f8580",
"blockHash": "2f3cd690abfce0d256880f03459d183da3ef10cceb19f99ba19bcafc56d60f90",
"codeHash": "44b1c46e96f5ef7da094c816824b565e3a9ad062a337ed2d4168efc63b6491ef",
"contractInstance": {
"address": "22AGs5YeVAfbKp7EXfc51pD35AGCmprBztgEgGrpv7BQ3",
Expand All @@ -77,7 +77,7 @@
"signature": "c00b85d63ae016c83a3e19dc0b2d44d72cbdae06ee988626b78eb6eccbf27bc85c90371c03843cf338bcb270620466ba1fcbd47cb841c12615db754c4a2d390e",
"gasPrice": "100000000000",
"gasAmount": 57779,
"blockHash": "39f948674a397fdd99f82947756ae98279184812e0d5950c1dfd37857f4c6b30",
"blockHash": "f75137e5d668adb2aad50d5ae979692546e4c9c8f98ae880e8777458858bfd30",
"codeHash": "a616d7546fff278db2fe47b31b0e35dd6cac0939de60bc042c6ceafed678fa4d",
"contractInstance": {
"address": "23bsYXnmkRWgFTK1ks61NuFGbU4urCs7sYb43oAZo2YBR",
Expand All @@ -91,7 +91,7 @@
"signature": "824dfced4a364ea4b89cac99a51d187a835ea230e5b979a18550dd591c27439c1a5f8b2cea4a02b3118c291f17ebf9f6ae542f44286de16b39b046306865381b",
"gasPrice": "100000000000",
"gasAmount": 59127,
"blockHash": "b06cb888ecd84cf9f01c2621fcd6f1f22113e864147077fa64cf3885d343c140",
"blockHash": "8ee3724a3b23c53c29d9871c2ccbfdcd153a2d6cedb87d5a7023079f7f8c1090",
"codeHash": "5593cf9f61d41050503610eda0a0e0189b64db9ac0fcde83aaacb4899751be97",
"contractInstance": {
"address": "2AkTpF1wY7BpbvgAKYAT93pZzW9v4fiHHfg7bePd6WzjZ",
Expand All @@ -105,7 +105,7 @@
"signature": "3cb8c3221e9640dbf56e302d554fdf906c2d68f8ea32a5c5e4451ca99963a36046478e7fe277027c873889722113c29a1b72bb9ad1d282f87e18b3a080851298",
"gasPrice": "100000000000",
"gasAmount": 59598,
"blockHash": "7651ce635b8fadc9f7785d547db84b3fd800eea9db0d737e5e59d988d37b3f90",
"blockHash": "5e640401b139b9249fda728b0cb5f903d475c478031c341aa208b7b3df638010",
"codeHash": "b3d940d8e6dd21c1e5e0bb86e96d317d72f8478218b4342ad22ec1231c139a25",
"contractInstance": {
"address": "xD9GkZCU5FaVizrpVgJHCk8Yn22nCqwYJWqDj9GXaKmR",
Expand All @@ -119,7 +119,7 @@
"signature": "aabc4aa2f743eb656d02fbc588ec4abcd6a963e1ff22f20e60c12f35af1e83d360e1641f91bb6229a1cf4cb15152db209fc3ef7a8044dd6136ace1f3d93cb90a",
"gasPrice": "100000000000",
"gasAmount": 57320,
"blockHash": "b122012e356ca74c499cfb7d0f0d985a458d6617bd0070e3e49c0e073d355af0",
"blockHash": "59f01863bbc7ae8f694ed968a9a2e889d2c6a93f5d234ef912d22c4ce1da3f60",
"codeHash": "e60ab4f6ebdbb2e7526892df65c394002e6f418af8dae0561635199119ad05d6",
"contractInstance": {
"address": "222aegh2SJ4KiRLubqbhui5UHRndPcifkkXDHtPHR7miP",
Expand All @@ -129,17 +129,17 @@
"issueTokenAmount": "115792089237316195423570985008687907853269984665640564039457584007913129639935"
},
"BridgeRewardRouter": {
"txId": "b9381fd3d5c7db03296cab67dcf93562e99ee1ff86760a96c84956660d85825f",
"unsignedTx": "000401010103000000081500bee85f379545a2ed9f6cceb331288842f378cf0f04012ad4ac8824aae7d6f80a13c53790bb855137640000a214408400024073407e01010309004033160213c3038d7ea4c68000a2160116020e0c16000107b2a413c41bc16d674ec80000344c40251601111306626c1703133b16031340422c2a170416041340432a170516050e2a1706160116051606626d170716011606160616072a62631708160813c40de0b6b3a7640000a801010202000316001601a9140100140100ad188000dfb6c1174876e8000137a4444714f22dd7aa65c2f2011746c238a315868ebfe2e69a58da9a63b4cec78409edc9000381818e63bd9e35a5489b52a430accefc608fd60aa2c7c0d1b393b5239aedf6b001c6d389f179c00f2808f00000bee85f379545a2ed9f6cceb331288842f378cf0f04012ad4ac8824aae7d6f80a00000000000000000000",
"signature": "7d1a4deba953fe9fe07c33b39d889725fb757571e77d7a9372b948ce260ee48d67d76107988e73ddcafc7c9084fe677d099259658d8a4a0fdfb595b1e7536951",
"txId": "cc574330a791ef5f20756b3aa65db89bb82b7bc9c76874541e65d51cc0a3eb2f",
"unsignedTx": "000401010103000000081500bee85f379545a2ed9f6cceb331288842f378cf0f04012ad4ac8824aae7d6f80a13c53790bb855137640000a214409f0102408e40990101030b004043160213c3038d7ea4c68000a2160116020e0c16000107b2a413c41bc16d674ec80000344c40351601111306626c1703133b16031340422c2a170416041340412a17051601160516050e2a626d17061606ce00304c1916041340432a170716070e2a1708160116071608626d170916011608160816092a6263170a160a13c40de0b6b3a7640000a801010202000316001601a91404010240ff140100ad188000e00fc1174876e8000137a4444714f22dd7aa65c2f2011746c238a315868ebfe2e69a58da9a63b4cec78409edc9000381818e63bd9e35a5489b52a430accefc608fd60aa2c7c0d1b393b5239aedf6b001c6d389f179b7f6f6b2480000bee85f379545a2ed9f6cceb331288842f378cf0f04012ad4ac8824aae7d6f80a00000000000000000000",
"signature": "8b730c341089c7c2f7098d4fb493ddc7a56ef96032e26244143f64ea603d0809501b9daaedd722afe0629fd0f3ca0a6008086b15287c51c6fff6e4eff34ab4b9",
"gasPrice": "100000000000",
"gasAmount": 57270,
"blockHash": "2b1bf6545ae19dfdba00ef48d7dc3d2edbb714ba4a0cca9799c14beb1cbd7fc0",
"codeHash": "5da6ef654d83d7ddcc0905a989afeccc0fa21a870ed39372933e12ce80552bcc",
"gasAmount": 57359,
"blockHash": "88f76beeb36cc609418ffb37d1164487aa1fae1b324c9ce8eb7f6d00bf562f90",
"codeHash": "b9c2ebbdc7d95b8abf951f37b9ea156e16ab94f81f455dd5cd22b5904c59a399",
"attoAlphAmount": "1025000000000000000000",
"contractInstance": {
"address": "24jny9U6xGy6SKUk1zBYUufiNp953PzxJvjB3BrTSH6HV",
"contractId": "954f45d05e4fb42200e381ef51491b8e05b9fc54a7c25bea96a311fa3af12200",
"address": "vWeKZBJWGbQJCUJquRUouyTN1qbXUAsFmd37za3Efpxo",
"contractId": "1b1372c4e2dc3d09ffd7c5639181ba6151a70b63fe24a94eb813959307083c00",
"groupIndex": 0
}
}
Expand All @@ -154,7 +154,7 @@
"gasPrice": "100000000000",
"txId": "79c4ac15c7ed217a0d547650bed163c198feb2e31c66c48b91cf17bd558a0100",
"groupIndex": 0,
"blockHash": "784b8e5cb193e24f0f9c6b2443407d3def3aef3c684e6359d0d514e585f95860",
"blockHash": "77319f65171e6795a7fb08c20bd8678d1af7a1641152358acc82d10dab14a720",
"codeHash": "dddd9d73e479a06afc139cfedf7d6e0ee131e34743d895cffa9bc2c32d4bb3c1",
"attoAlphAmount": "1000000000000000000"
},
Expand All @@ -167,7 +167,7 @@
"gasPrice": "100000000000",
"txId": "4677943ce8acb1c5ed6c0ace8ee773c95d5b650b50cb9cdf56e1231adf7cec22",
"groupIndex": 0,
"blockHash": "f37c34172bde590301fe4ddaba67ef5c54ba32a1a4d2fdae56c65a491d466a80",
"blockHash": "fbd865a863021a13127090c2fb2bb2ac38447b9c9b6e22edf000308aa0326010",
"codeHash": "4c092cbd9a641e4dcc1089357a5a5ff8d1f6f49aa47549d566d5f7198f887d60",
"attoAlphAmount": "1001000000000000000"
}
Expand Down
16 changes: 11 additions & 5 deletions alephium/artifacts/token_bridge/BridgeRewardRouter.ral.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{
"version": "v2.5.6",
"name": "BridgeRewardRouter",
"bytecode": "00024073407e01010309004033160213c3038d7ea4c68000a2160116020e0c16000107b2a413c41bc16d674ec80000344c40251601111306626c1703133b16031340422c2a170416041340432a170516050e2a1706160116051606626d170716011606160616072a62631708160813c40de0b6b3a7640000a801010202000316001601a9",
"codeHash": "e9ef2833d1b214a533490f2ca81b7d6d09022bcc0d30a874230d5aca6dfbc57a",
"bytecode": "0102408e40990101030b004043160213c3038d7ea4c68000a2160116020e0c16000107b2a413c41bc16d674ec80000344c40351601111306626c1703133b16031340422c2a170416041340412a17051601160516050e2a626d17061606ce00304c1916041340432a170716070e2a1708160116071608626d170916011608160816092a6263170a160a13c40de0b6b3a7640000a801010202000316001601a9",
"codeHash": "213e716a4d9125a71678f579c770a9721e5d4b1c95822f2f623f3ae27cc0f76b",
"fieldsSig": {
"names": [],
"types": [],
"isMutable": []
"names": [
"alphChainId"
],
"types": [
"U256"
],
"isMutable": [
false
]
},
"eventsSig": [],
"functions": [
Expand Down
28 changes: 16 additions & 12 deletions alephium/artifacts/ts/BridgeRewardRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,34 @@ import { getContractByCodeHash } from "./contracts";

// Custom types for the contract
export namespace BridgeRewardRouterTypes {
export type State = Omit<ContractState<any>, "fields">;
export type Fields = {
alphChainId: bigint;
};

export type State = ContractState<Fields>;
}

class Factory extends ContractFactory<BridgeRewardRouterInstance, {}> {
class Factory extends ContractFactory<
BridgeRewardRouterInstance,
BridgeRewardRouterTypes.Fields
> {
at(address: string): BridgeRewardRouterInstance {
return new BridgeRewardRouterInstance(address);
}

tests = {
completeTransfer: async (
params: Omit<
TestContractParams<
never,
{ tokenBridgeForChain: HexString; vaa: HexString; caller: Address }
>,
"initialFields"
params: TestContractParams<
BridgeRewardRouterTypes.Fields,
{ tokenBridgeForChain: HexString; vaa: HexString; caller: Address }
>
): Promise<TestContractResult<null>> => {
return testMethod(this, "completeTransfer", params);
},
addRewards: async (
params: Omit<
TestContractParams<never, { caller: Address; amount: bigint }>,
"initialFields"
params: TestContractParams<
BridgeRewardRouterTypes.Fields,
{ caller: Address; amount: bigint }
>
): Promise<TestContractResult<null>> => {
return testMethod(this, "addRewards", params);
Expand All @@ -65,7 +69,7 @@ export const BridgeRewardRouter = new Factory(
Contract.fromJson(
BridgeRewardRouterContractJson,
"",
"e9ef2833d1b214a533490f2ca81b7d6d09022bcc0d30a874230d5aca6dfbc57a"
"213e716a4d9125a71678f579c770a9721e5d4b1c95822f2f623f3ae27cc0f76b"
)
);

Expand Down
18 changes: 12 additions & 6 deletions alephium/contracts/token_bridge/bridge_reward_router.ral
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
Contract BridgeRewardRouter() {
Contract BridgeRewardRouter(alphChainId: U256) {
@using(preapprovedAssets = true, assetsInContract = true, checkExternalCaller = false)
pub fn completeTransfer(tokenBridgeForChain: TokenBridgeForChain, vaa: ByteVec, caller: Address) -> () {
tokenBridgeForChain.completeTransfer{caller -> ALPH: dustAmount!()}(vaa, caller)
if (tokenRemaining!(selfAddress!(), ALPH) >= 2 alph) { // 1 alph for `minimalAlphInContract`
let signatureSize = u256From1Byte!(byteVecSlice!(vaa, 5, 6))
let payloadOffset = 59 + signatureSize * 66 // 6 + signatureSize * 66 + 53
let targetAddressSizeOffset = payloadOffset + 67 // payloadOffset + 1 + 32 + 32 + 2
let targetAddressOffset = targetAddressSizeOffset + 2
let targetAddressSize = u256From2Byte!(byteVecSlice!(vaa, targetAddressSizeOffset, targetAddressOffset))
let targetAddress = byteVecToAddress!(byteVecSlice!(vaa, targetAddressOffset, targetAddressOffset + targetAddressSize))
transferTokenFromSelf!(targetAddress, ALPH, 1 alph)
let tokenChainIdOffset = payloadOffset + 65 // payloadOffset + 1 + 32 + 32
let tokenChainId = u256From2Byte!(byteVecSlice!(vaa, tokenChainIdOffset, tokenChainIdOffset + 2))

// only reward tokens from other chains
if (tokenChainId != alphChainId) {
let targetAddressSizeOffset = payloadOffset + 67 // payloadOffset + 1 + 32 + 32 + 2
let targetAddressOffset = targetAddressSizeOffset + 2
let targetAddressSize = u256From2Byte!(byteVecSlice!(vaa, targetAddressSizeOffset, targetAddressOffset))
let targetAddress = byteVecToAddress!(byteVecSlice!(vaa, targetAddressOffset, targetAddressOffset + targetAddressSize))
transferTokenFromSelf!(targetAddress, ALPH, 1 alph)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion alephium/scripts/06_deploy_bridge_reward_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const deployBridgeRewardRouter: DeployFunction<Settings> = async (
network: Network<Settings>
): Promise<void> => {
const result = await deployer.deployContract(BridgeRewardRouter, {
initialFields: {},
initialFields: { alphChainId: BigInt(network.settings.chainId) },
initialAttoAlphAmount: ONE_ALPH + network.settings.initRewards
})
console.log(
Expand Down
5 changes: 4 additions & 1 deletion alephium/test/fixtures/token-bridge-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,5 +525,8 @@ export function expectAssetsEqual(expected: Asset[], have: Asset[]) {

export function createBridgeRewardRouter(alphAmount: bigint): ContractFixture<any> {
const address = randomContractAddress()
return new ContractFixture(BridgeRewardRouter.stateForTest({}, { alphAmount }, address), [])
return new ContractFixture(
BridgeRewardRouter.stateForTest({ alphChainId: BigInt(CHAIN_ID_ALEPHIUM) }, { alphAmount }, address),
[]
)
}
36 changes: 36 additions & 0 deletions alephium/test/token-bridge-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ describe('test token bridge', () => {

async function test(fixture: ContractFixture<any>) {
const testResult = await BridgeRewardRouter.tests.completeTransfer({
initialFields: fixture.selfState.fields,
address: fixture.address,
initialAsset: fixture.selfState.asset,
testArgs: {
Expand Down Expand Up @@ -1085,6 +1086,40 @@ describe('test token bridge', () => {
expect(bridgeRewardRouterState1.asset.alphAmount).toEqual(ONE_ALPH)
})

it('should not reward if the token is from alephium', async () => {
await buildProject()
const testToken = createTestToken()
const localTokenPoolFixture = newLocalTokenPoolTestFixture(remoteChainId, remoteTokenBridgeId, testToken.contractId)
const toAddressHex = randomAssetAddressHex()
const toAddress = hexToBase58(toAddressHex)
const transferAmount = oneAlph
const arbiterFee = defaultMessageFee
const transfer = new Transfer(transferAmount, testToken.contractId, CHAIN_ID_ALEPHIUM, toAddressHex, arbiterFee)
const vaaBody = new VAABody(transfer.encode(), remoteChainId, CHAIN_ID_ALEPHIUM, remoteTokenBridgeId, 0)
const vaa = initGuardianSet.sign(initGuardianSet.quorumSize(), vaaBody)

const fixture = createBridgeRewardRouter(alph(3))
const testResult = await BridgeRewardRouter.tests.completeTransfer({
address: fixture.address,
initialFields: fixture.selfState.fields,
testArgs: {
tokenBridgeForChain: localTokenPoolFixture.tokenBridgeForChain.contractId,
vaa: binToHex(vaa.encode()),
caller: payer
},
initialAsset: fixture.selfState.asset,
inputAssets: [defaultInputAsset],
existingContracts: localTokenPoolFixture.localTokenPool.states().concat(testToken.states())
})

const recipientOutputs = testResult.txOutputs.filter((o) => o.address === toAddress)
expect(recipientOutputs.length).toEqual(1)
expect(recipientOutputs[0].alphAmount).toEqual(dustAmount)

const rewardRouterState = getContractState(testResult.contracts, fixture.address)
expect(rewardRouterState.asset.alphAmount).toEqual(alph(3))
})

it('should failed to complete transfer and create unexecuted sequence contracts', async () => {
await buildProject()
const testTokenInfo = createTestToken()
Expand Down Expand Up @@ -1498,6 +1533,7 @@ describe('test token bridge', () => {
await buildProject()
const fixture = createBridgeRewardRouter(ONE_ALPH)
const testResult = await BridgeRewardRouter.tests.addRewards({
initialFields: fixture.selfState.fields,
address: fixture.address,
initialAsset: fixture.selfState.asset,
testArgs: { caller: payer, amount: ONE_ALPH },
Expand Down
Loading

0 comments on commit e779cd4

Please sign in to comment.